react-mui-form-validator
Version:
Validator for forms designed with material-ui components.
173 lines (163 loc) • 5.55 kB
TypeScript
import * as React$1 from 'react';
import React__default from 'react';
import { FilledTextFieldProps, OutlinedTextFieldProps, StandardTextFieldProps, BoxProps } from '@mui/material';
import { MuiTelInputInfo } from 'mui-tel-input';
import { TextFieldProps } from '@mui/material/TextField';
interface required {
validator: "required";
}
interface matchRegexp {
validator: "matchRegexp";
regexp: RegExp | string;
}
interface isEmail {
validator: "isEmail";
}
interface isEmpty {
validator: "isEmpty";
}
interface trim {
validator: "trim";
}
interface isNumber {
validator: "isNumber";
}
interface isFloat {
validator: "isFloat";
}
interface isPositive {
validator: "isPositive";
}
interface maxNumber {
validator: "maxNumber";
max: number;
}
interface minNumber {
validator: "minNumber";
min: number;
}
interface maxFloat {
validator: "maxFloat";
max: number;
}
interface minFloat {
validator: "minFloat";
min: number;
}
interface isString {
validator: "isString";
}
interface minStringLength {
validator: "minStringLength";
min: number;
}
interface maxStringLength {
validator: "maxStringLength";
max: number;
}
interface isFile {
validator: "isFile";
}
interface maxFileSize {
validator: "maxFileSize";
max: number;
}
interface allowedExtensions {
validator: "allowedExtensions";
fileTypes: string;
}
type Validator = required | matchRegexp | isEmail | isEmpty | trim | isNumber | isFloat | isPositive | maxNumber | minNumber | maxFloat | minFloat | isString | minStringLength | maxStringLength | isFile | maxFileSize | allowedExtensions;
type ComponentProps = {
errorMessages?: string | string[];
validators?: Validator[];
value: any;
validatorListener?: (value: boolean) => void;
withRequiredValidator?: boolean;
containerProps?: object;
onChangeTel?: (value: string, info: MuiTelInputInfo) => void;
};
type ValidatorComponentProps = (FilledTextFieldProps | OutlinedTextFieldProps | StandardTextFieldProps) & ComponentProps;
interface ValidatorComponentState {
isValid?: boolean;
value: any;
errorMessages?: string | string[];
validators?: Validator[];
}
interface ValidatorFormProps {
children: React.ReactNode;
onSubmit: () => void;
instantValidate?: boolean;
onError?: (errors: any) => void;
debounceTime?: number;
}
type FormContextValue = {
form: {
attachToForm: (component: any) => void;
detachFromForm: (component: any) => void;
instantValidate: boolean;
debounceTime: number;
} | null;
};
declare const FormContext: React$1.Context<FormContextValue>;
declare class ValidatorForm extends React$1.Component<ValidatorFormProps & BoxProps> {
static getValidator: (validator: Validator, value: any, includeRequired: boolean) => boolean;
childs: any[];
errors: any[];
get instantValidate(): boolean;
get debounceTime(): number;
getFormHelpers: () => FormContextValue;
attachToForm: (component: any) => void;
detachFromForm: (component: any) => void;
submit: (event: React$1.FormEvent<HTMLFormElement>) => void;
walk: (children: any[], dryRun?: boolean) => Promise<boolean>;
checkInput: (input: any, dryRun?: boolean) => Promise<boolean>;
validate: (input: any, includeRequired: boolean, dryRun?: boolean) => Promise<boolean>;
resetValidations: () => void;
isFormValid: (dryRun?: boolean) => Promise<boolean>;
render(): React$1.JSX.Element;
}
type DebouncedValidate = {
(value: any, includeRequired?: boolean, dryRun?: boolean): void;
cancel?: () => void;
};
declare class ValidatorComponent<P extends ValidatorComponentProps = ValidatorComponentProps> extends React$1.Component<P, ValidatorComponentState> {
static contextType: React$1.Context<FormContextValue>;
context: React$1.ContextType<typeof FormContext>;
validateDebounced: DebouncedValidate | null;
form: NonNullable<FormContextValue["form"]> | null;
debounceTime: number;
instantValidate: boolean;
invalid: number[];
state: ValidatorComponentState;
renderValidatorComponent(): React$1.ReactNode;
componentDidMount(): void;
componentDidUpdate(prevProps: P): void;
componentWillUnmount(): void;
shouldComponentUpdate(nextProps: P, nextState: ValidatorComponentState): boolean;
configure: () => void;
getErrorMessage: () => string | boolean;
validate: (value: any, includeRequired?: boolean, dryRun?: boolean) => Promise<boolean>;
isValid: () => boolean;
makeInvalid: () => void;
makeValid: () => void;
renderComponent: () => React$1.ReactNode;
render(): React$1.JSX.Element;
}
declare class MuiSelect extends ValidatorComponent {
renderValidatorComponent(): React$1.JSX.Element;
}
type MuiTextFieldProps = Omit<TextFieldProps, "value"> & {
value: unknown;
validators?: any[];
errorMessages?: string | string[];
validatorListener?: (isValid: boolean) => void;
withRequiredValidator?: boolean;
containerProps?: React__default.HTMLAttributes<HTMLDivElement>;
};
declare class MuiTextField extends ValidatorComponent<MuiTextFieldProps> {
renderValidatorComponent(): React__default.JSX.Element;
}
declare class MuiTelInputDefault extends ValidatorComponent {
renderValidatorComponent(): React__default.JSX.Element;
}
export { ValidatorComponent as MuiComponent, ValidatorForm as MuiForm, MuiTelInputDefault as MuiPhoneNumber, MuiSelect, MuiTextField, type Validator as MuiValidator };