react-mui-form-validator 1.2.1 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Readme.md CHANGED
@@ -1,3 +1,4 @@
1
+
1
2
  ## Validation component for material-ui forms
2
3
 
3
4
  [![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://opensource.org/licenses/MIT)
@@ -14,10 +15,16 @@ npm install react-mui-form-validator
14
15
  Some rules can accept extra parameter, example:
15
16
 
16
17
  TextField
18
+
17
19
  ```javascript
18
20
  <MuiTextField
19
21
  {...someProps}
20
- validators={["minNumber:0", "maxNumber:255", "matchRegexp:^[0-9]$"]}
22
+ validators={[
23
+ {
24
+ validator: "maxNumber",
25
+ max: 10,
26
+ },
27
+ ]}
21
28
  />
22
29
  ```
23
30
 
@@ -27,193 +34,44 @@ You can pass any props of field components, but note that `errorText` prop will
27
34
  Your component must [provide a theme](http://www.material-ui.com/#/get-started/usage).
28
35
 
29
36
  ```javascript
30
-
31
37
  import { useState } from "react";
32
- import "./App.css";
33
38
  import { MuiForm, MuiTextField } from "react-mui-form-validator";
34
- import { Button, IconButton, InputAdornment, Paper } from "@mui/material";
35
- import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
36
- import {
37
- faEnvelope,
38
- faEye,
39
- faEyeSlash,
40
- } from "@fortawesome/free-solid-svg-icons";
39
+ import { Button } from "@mui/material";
41
40
 
42
41
  export default function App(props: any) {
43
- const [name, setName] = useState();
44
42
  const [email, setEmail] = useState();
45
- const [password, setPassword] = useState();
46
- const [showPassword, setShowPassword] = useState(false);
47
-
48
- const changeName = (event: any) => {
49
- const name = event.target.value;
50
- setName(name);
51
- };
52
-
53
- const changeEmail = (event: any) => {
54
- const email = event.target.value;
55
- setEmail(email);
56
- };
57
-
58
- const changePassword = (event: any) => {
59
- const password = event.target.value;
60
- setPassword(password);
61
- };
62
43
 
63
44
  const handleSubmit = () => {
64
45
  // your submit logic
65
46
  };
66
47
 
67
48
  return (
68
- <div className="App">
69
- <center>
70
- <Paper className="container">
71
- <h3>Example Sign In</h3>
72
- <MuiForm
73
- onSubmit={handleSubmit}
74
- onError={(errors: any) => console.log(errors)}
75
- >
76
- <MuiTextField
77
- name="name"
78
- //className="css-style-name" add your css style
79
- //classes={classes.emailInput} //add your js or ts style
80
- label="Name"
81
- placeholder="Name"
82
- onChange={changeName}
83
- value={name}
84
- validators={["required"]}
85
- errorMessages={["this field is required"]}
86
- fullWidth
87
- />
88
- <br />
89
- <MuiTextField
90
- name="email"
91
- //className="css-style-name" add your css style
92
- //classes={classes.emailInput} //add your js or ts style
93
- label="Email"
94
- placeholder="Email"
95
- onChange={changeEmail}
96
- value={email}
97
- validators={["required", "isEmail"]}
98
- errorMessages={["this field is required", "email is not valid"]}
99
- InputProps={{
100
- startAdornment: (
101
- <InputAdornment position="start">
102
- <FontAwesomeIcon icon={faEnvelope} />
103
- </InputAdornment>
104
- ),
105
- }}
106
- fullWidth
107
- />
108
- <br />
109
- <MuiTextField
110
- type={showPassword ? "text" : "password"}
111
- style={{ marginBottom: 0 }}
112
- name="password"
113
- label="Password"
114
- //InputLabelProps={{ shrink: true }}
115
- placeholder="Password"
116
- value={password}
117
- onChange={changePassword}
118
- InputProps={{
119
- startAdornment: (
120
- <InputAdornment position="start">
121
- <FontAwesomeIcon icon={faEnvelope} />
122
- </InputAdornment>
123
- ),
124
- endAdornment: (
125
- <InputAdornment position="end">
126
- <IconButton
127
- aria-label="toggle password visibility"
128
- onClick={() => setShowPassword(!showPassword)}
129
- onMouseDown={(e) => e.preventDefault()}
130
- edge="end"
131
- >
132
- <FontAwesomeIcon
133
- icon={showPassword ? faEyeSlash : faEye}
134
- />
135
- </IconButton>
136
- </InputAdornment>
137
- ),
138
- }}
139
- validators={["required"]}
140
- errorMessages={["Password is required"]}
141
- variant="outlined"
142
- fullWidth
143
- />
144
- <br />
145
- <Button type="submit" variant="outlined">
146
- Sign In
147
- </Button>
148
- </MuiForm>
149
- </Paper>
150
- </center>
151
- </div>
152
- );
153
- }
154
-
155
- ```
156
- css style example:
157
-
158
- ```
159
- .App {
160
- text-align: center;
161
- width: 100%;
162
- }
163
-
164
- .container {
165
- width: 500px;
166
- height: auto;
167
- background: #FFFFFF;
168
- box-shadow: 0px 20px 56px rgba(0, 0, 0, 0.1);
169
- border-radius: 16px;
170
- padding: 15px;
171
- margin: 20px;
172
- }
173
-
174
- ```
175
-
176
- Class component example:
177
-
178
- ```javascript
179
-
180
- import React from "react";
181
- import Button from "@mui/material/Button";
182
- import { MuiForm, MuiTextField } from "react-mui-form-validator";
183
-
184
- class MyForm extends React.Component {
185
- state = {
186
- email: "",
187
- };
188
-
189
- handleChange = (event) => {
190
- const email = event.target.value;
191
- this.setState({ email });
192
- };
193
-
194
- handleSubmit = () => {
195
- // your submit logic
196
- };
197
-
198
- render() {
199
- const { email } = this.state;
200
- return (
49
+ <div>
50
+ <h3>Example Sign In</h3>
201
51
  <MuiForm
202
- onSubmit={this.handleSubmit}
203
- onError={(errors) => console.log(errors)}
52
+ onSubmit={handleSubmit}
53
+ onError={(errors: any) => console.log(errors)}
204
54
  >
205
55
  <MuiTextField
206
- label="Email"
207
- onChange={this.handleChange}
208
56
  name="email"
57
+ label="Email"
58
+ placeholder="example@domain.com"
59
+ onChange={(v) => setEmail(v.target.value)}
209
60
  value={email}
210
- validators={["required", "isEmail"]}
211
- errorMessages={["this field is required", "email is not valid"]}
61
+ validators={[
62
+ {
63
+ validator: "isEmail",
64
+ },
65
+ ]}
66
+ errorMessages={["Email is required"]}
67
+ fullWidth
212
68
  />
213
- <Button type="submit">Submit</Button>
69
+ <br />
70
+ <Button type="submit" variant="outlined">
71
+ Sign In
72
+ </Button>
214
73
  </MuiForm>
215
- );
216
- }
74
+ </div>
75
+ );
217
76
  }
218
-
219
77
  ```
package/lib/index.d.ts CHANGED
@@ -1,125 +1,167 @@
1
- import * as React from 'react';
1
+ import * as React$1 from 'react';
2
2
  import React__default from 'react';
3
- import * as mui_tel_input from 'mui-tel-input';
4
- import PropTypes from 'prop-types';
3
+ import { TextFieldVariants, FilledTextFieldProps, StandardTextFieldProps, OutlinedTextFieldProps } from '@mui/material';
4
+ import { MuiTelInputInfo } from 'mui-tel-input';
5
5
 
6
- declare class ValidatorComponent extends React__default.Component<any, any, any> {
7
- static getDerivedStateFromProps(nextProps: any, prevState: any): {
6
+ interface required {
7
+ validator: "required";
8
+ }
9
+ interface matchRegexp {
10
+ validator: "matchRegexp";
11
+ regexp: RegExp | string;
12
+ }
13
+ interface isEmail {
14
+ validator: "isEmail";
15
+ }
16
+ interface isEmpty {
17
+ validator: "isEmpty";
18
+ }
19
+ interface trim {
20
+ validator: "trim";
21
+ }
22
+ interface isNumber {
23
+ validator: "isNumber";
24
+ }
25
+ interface isFloat {
26
+ validator: "isFloat";
27
+ }
28
+ interface isPositive {
29
+ validator: "isPositive";
30
+ }
31
+ interface maxNumber {
32
+ validator: "maxNumber";
33
+ max: number;
34
+ }
35
+ interface minNumber {
36
+ validator: "minNumber";
37
+ min: number;
38
+ }
39
+ interface maxFloat {
40
+ validator: "maxFloat";
41
+ max: number;
42
+ }
43
+ interface minFloat {
44
+ validator: "minFloat";
45
+ min: number;
46
+ }
47
+ interface isString {
48
+ validator: "isString";
49
+ }
50
+ interface minStringLength {
51
+ validator: "minStringLength";
52
+ min: number;
53
+ }
54
+ interface maxStringLength {
55
+ validator: "maxStringLength";
56
+ max: number;
57
+ }
58
+ interface isFile {
59
+ validator: "isFile";
60
+ }
61
+ interface maxFileSize {
62
+ validator: "maxFileSize";
63
+ max: number;
64
+ }
65
+ interface allowedExtensions {
66
+ validator: "allowedExtensions";
67
+ fileTypes: string;
68
+ }
69
+ type Validator = required | matchRegexp | isEmail | isEmpty | trim | isNumber | isFloat | isPositive | maxNumber | minNumber | maxFloat | minFloat | isString | minStringLength | maxStringLength | isFile | maxFileSize | allowedExtensions;
70
+
71
+ type MuiTextFieldProps<Variant extends TextFieldVariants = TextFieldVariants> = Variant extends "filled" ? FilledTextFieldProps : Variant extends "standard" ? StandardTextFieldProps : OutlinedTextFieldProps & ValidatorComponentProps;
72
+ interface ValidatorComponentProps {
73
+ errorMessages?: string | string[];
74
+ validators?: Validator[];
75
+ value: any;
76
+ validatorListener?: (value: boolean) => void;
77
+ withRequiredValidator?: boolean;
78
+ containerProps?: object;
79
+ onChangeTel?: (value: string, info: MuiTelInputInfo) => void;
80
+ }
81
+ interface ValidatorComponentState {
82
+ isValid?: boolean;
83
+ value: any;
84
+ errorMessages?: string | string[];
85
+ validators?: Validator[];
86
+ }
87
+ interface ValidatorFormProps {
88
+ onSubmit: () => void;
89
+ instantValidate?: boolean;
90
+ children?: React.ReactNode;
91
+ onError?: (errors: any[]) => void;
92
+ debounceTime?: number;
93
+ }
94
+
95
+ declare class ValidatorForm extends React$1.Component<ValidatorFormProps> {
96
+ static getValidator: (value: any, validator: Validator) => boolean;
97
+ childs: any[];
98
+ errors: any[];
99
+ instantValidate: boolean;
100
+ debounceTime: number | undefined;
101
+ getFormHelpers: () => {
102
+ form: {
103
+ attachToForm: (component: any) => void;
104
+ debounceTime: number | undefined;
105
+ detachFromForm: (component: any) => void;
106
+ instantValidate: boolean | undefined;
107
+ };
108
+ };
109
+ attachToForm: (component: any) => void;
110
+ detachFromForm: (component: any) => void;
111
+ submit: (event: React$1.FormEvent<HTMLFormElement>) => void;
112
+ walk: (children: any[], dryRun?: boolean) => Promise<boolean>;
113
+ checkInput: (input: any, dryRun?: boolean) => Promise<boolean>;
114
+ validate: (input: any, includeRequired: boolean, dryRun?: boolean) => Promise<boolean>;
115
+ find: (collection: any[], fn: (item: any) => boolean) => any;
116
+ resetValidations: () => void;
117
+ isFormValid: (dryRun?: boolean) => Promise<boolean>;
118
+ render(): React$1.JSX.Element;
119
+ }
120
+
121
+ declare class ValidatorComponent extends React$1.Component<MuiTextFieldProps & ValidatorComponentProps, ValidatorComponentState> {
122
+ renderValidatorComponent(): React$1.ReactNode;
123
+ form: any;
124
+ debounceTime: any;
125
+ validateDebounced: {
126
+ (value: any, includeRequired?: boolean | undefined, dryRun?: boolean | undefined): void;
127
+ cancel: () => void;
128
+ } | any;
129
+ constructor(props: MuiTextFieldProps & ValidatorComponentProps);
130
+ getSnapshotBeforeUpdate(nextProps: Readonly<MuiTextFieldProps & ValidatorComponentProps>, prevState: Readonly<ValidatorComponentState>): {
8
131
  value: any;
9
- validators: any;
10
- errorMessages: any;
132
+ validators: Validator[];
133
+ errorMessages: string | string[];
11
134
  } | {
12
135
  value: any;
13
136
  validators?: undefined;
14
137
  errorMessages?: undefined;
15
138
  };
16
- constructor(props: any);
17
- constructor(props: any, context: any);
18
- state: {
19
- isValid: boolean;
20
- value: any;
21
- errorMessages: any;
22
- validators: any;
23
- };
24
- componentDidMount(): void;
25
- shouldComponentUpdate(nextProps: any, nextState: any): boolean;
26
- componentDidUpdate(prevProps: any, prevState: any): void;
27
- componentWillUnmount(): void;
28
- getErrorMessage: () => any;
29
139
  instantValidate: boolean;
30
- invalid: any[];
140
+ invalid: number[];
141
+ componentDidMount(): void;
31
142
  configure: () => void;
32
- debounceTime: any;
33
- validateDebounced: {
34
- (value?: any, includeRequired?: boolean | undefined, dryRun?: boolean | undefined): void;
35
- cancel: () => void;
36
- } | undefined;
37
- validate: (value: any, includeRequired?: boolean, dryRun?: boolean) => Promise<boolean>;
143
+ shouldComponentUpdate(nextProps: MuiTextFieldProps, nextState: ValidatorComponentState): boolean;
144
+ componentDidUpdate(prevProps: MuiTextFieldProps, prevState: ValidatorComponentState): void;
145
+ componentWillUnmount(): void;
146
+ getErrorMessage: () => string | boolean | string[];
147
+ validate: (value: any, dryRun?: boolean) => Promise<boolean>;
38
148
  isValid: () => boolean;
39
149
  makeInvalid: () => void;
40
150
  makeValid: () => void;
41
- renderComponent: (form: any) => any;
42
- form: any;
43
- render(): React__default.JSX.Element;
44
- }
45
- declare namespace ValidatorComponent {
46
- namespace propTypes {
47
- let errorMessages: PropTypes.Requireable<NonNullable<string | any[] | null | undefined>>;
48
- let validators: PropTypes.Requireable<any[]>;
49
- let value: PropTypes.Requireable<any>;
50
- let validatorListener: PropTypes.Requireable<(...args: any[]) => any>;
51
- let withRequiredValidator: PropTypes.Requireable<boolean>;
52
- let containerProps: PropTypes.Requireable<object>;
53
- }
54
- namespace defaultProps {
55
- let errorMessages_1: string;
56
- export { errorMessages_1 as errorMessages };
57
- let validators_1: never[];
58
- export { validators_1 as validators };
59
- export function validatorListener_1(): void;
60
- export { validatorListener_1 as validatorListener };
61
- }
151
+ renderComponent: (form: ValidatorForm) => React$1.ReactNode;
152
+ render(): React$1.JSX.Element;
62
153
  }
63
154
 
64
- declare class TextValidator extends ValidatorComponent {
65
- renderValidatorComponent(): React__default.JSX.Element;
155
+ declare class MuiSelect extends ValidatorComponent {
156
+ renderValidatorComponent(): React$1.JSX.Element;
66
157
  }
67
158
 
68
- declare class MuiSelect$1 extends ValidatorComponent {
69
- renderValidatorComponent(): React.JSX.Element;
159
+ declare class MuiTextField extends ValidatorComponent {
160
+ renderValidatorComponent(): React__default.JSX.Element;
70
161
  }
71
162
 
72
- declare class ValidatorForm extends React__default.Component<any, any, any> {
73
- static getValidator: (validator: any, value: any, includeRequired: any) => boolean;
74
- constructor(props: any);
75
- constructor(props: any, context: any);
76
- getFormHelpers: () => {
77
- form: {
78
- attachToForm: (component: any) => void;
79
- detachFromForm: (component: any) => void;
80
- instantValidate: any;
81
- debounceTime: any;
82
- };
83
- };
84
- instantValidate: any;
85
- debounceTime: any;
86
- childs: any[];
87
- errors: any[];
88
- attachToForm: (component: any) => void;
89
- detachFromForm: (component: any) => void;
90
- submit: (event: any) => void;
91
- walk: (children: any, dryRun: any) => Promise<any>;
92
- checkInput: (input: any, dryRun: any) => Promise<any>;
93
- validate: (input: any, includeRequired: any, dryRun: any) => Promise<any>;
94
- find: (collection: any, fn: any) => any;
95
- resetValidations: () => void;
96
- isFormValid: (dryRun?: boolean) => Promise<any>;
97
- render(): React__default.JSX.Element;
98
- }
99
- declare namespace ValidatorForm {
100
- function addValidationRule(name: any, callback: any): void;
101
- function getValidationRule(name: any): any;
102
- function hasValidationRule(name: any): any;
103
- function removeValidationRule(name: any): void;
104
- namespace propTypes {
105
- let onSubmit: PropTypes.Validator<(...args: any[]) => any>;
106
- let instantValidate: PropTypes.Requireable<boolean>;
107
- let children: PropTypes.Requireable<PropTypes.ReactNodeLike>;
108
- let onError: PropTypes.Requireable<(...args: any[]) => any>;
109
- let debounceTime: PropTypes.Requireable<number>;
110
- }
111
- namespace defaultProps {
112
- export function onError_1(): void;
113
- export { onError_1 as onError };
114
- let debounceTime_1: number;
115
- export { debounceTime_1 as debounceTime };
116
- }
163
+ declare class MuiTelInputDefault extends ValidatorComponent {
164
+ renderValidatorComponent(): React__default.JSX.Element;
117
165
  }
118
166
 
119
- declare const MuiTextField: typeof TextValidator;
120
- declare const MuiPhoneNumber: React.ForwardRefExoticComponent<Omit<mui_tel_input.MuiTelInputProps, "ref"> & React.RefAttributes<HTMLDivElement>>;
121
- declare const MuiSelect: typeof MuiSelect$1;
122
- declare const MuiComponent: typeof ValidatorComponent;
123
- declare const MuiForm: typeof ValidatorForm;
124
-
125
- export { MuiComponent, MuiForm, MuiPhoneNumber, MuiSelect, MuiTextField };
167
+ export { ValidatorComponent as MuiComponent, ValidatorForm as MuiForm, MuiTelInputDefault as MuiPhoneNumber, MuiSelect, MuiTextField, Validator as MuiValidator };