@capgeminiuk/dcx-react-library
Version:
[](https://circleci.com/gh/Capgemini/dcx-react-library)
62 lines (61 loc) • 1.81 kB
TypeScript
import React from 'react';
interface Validation {
rule: any;
message: string;
}
/**
* This custom hooks accept in input the element to validate and the validation rule
* to pass the input in case of HTMLInputElement you can use useRef hooks.
* For a list of Validation rules have a look on: https://github.com/oussamahamdaoui/forgJs
* @Example
* const validation = {
* rule: {
* type: 'float',
* min: 100,
* },
* message: 'the value have to be float and more then 100',
* }
* const inputEl = React.useRef<HTMLInputElement | null>(null)
* const {validity} = useValidation(
* inputEl.current ? inputEl.current.value : '',
* validation,
* )
* @param value
* @param validation
*/
declare const useValidation: (value: string, validation: Validation) => {
validity: {
valid: boolean;
message: string;
};
setValid: React.Dispatch<React.SetStateAction<{
valid: boolean;
message: string;
}>>;
};
/**
* This custom hooks accept in input a validation rule and provide in
* output the validity or not of the element.
* For a list of Validation rules have a look on: https://github.com/oussamahamdaoui/forgJs
* @Example
* const {validity, onValueChange} = useValidationOnChange({
* rule: {
* type: 'float',
* min: 100,
* },
* message: 'the value have to be float and more then 100',
* })
* return (<input onChange={onValueChange}/>
* @param validation
*/
declare const useValidationOnChange: (validation: Validation | null, value?: string) => {
validity: null;
onValueChange: null;
} | {
validity: {
valid: boolean;
message: string;
};
onValueChange: (evt: React.FormEvent<HTMLInputElement>) => void;
};
export { useValidation, useValidationOnChange };