Skip to content

Commit 64c39c1

Browse files
committed
FormValidation
1 parent 9bc4bf3 commit 64c39c1

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

components/FormValidation.tsx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import ValidationContext from '@react-form-fields/core/components/ValidationContext';
2+
import * as React from 'react';
3+
4+
interface IProps {
5+
onSubmit: (valid: boolean) => void;
6+
}
7+
8+
export class FormValidation extends React.PureComponent<IProps> {
9+
private validationContext: ValidationContext;
10+
11+
private bindValidationContext = (validationContext: ValidationContext): void => {
12+
this.validationContext = validationContext;
13+
}
14+
15+
private onSubmit = (e?: React.SyntheticEvent) => {
16+
e && e.preventDefault && e.preventDefault();
17+
18+
const isValid = this.validationContext.isValid(true);
19+
this.props.onSubmit(isValid);
20+
}
21+
22+
reset = () => {
23+
this.validationContext.reset();
24+
}
25+
26+
render() {
27+
return (
28+
<form noValidate onSubmit={this.onSubmit}>
29+
<ValidationContext ref={this.bindValidationContext}>
30+
{this.props.children}
31+
</ValidationContext>
32+
</form>
33+
);
34+
}
35+
}

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"validation",
1111
"material"
1212
],
13-
"version": "1.8.9",
13+
"version": "1.9.0",
1414
"main": "./dist/index.js",
1515
"types": "./dist/index.d.ts",
1616
"license": "MIT",
@@ -32,13 +32,10 @@
3232
"prepare": "yarn tsc"
3333
},
3434
"dependencies": {
35-
"@material-ui/core": ">=3.0.0",
36-
"@material-ui/icons": ">=3.0.0",
3735
"@react-form-fields/core": ">=1.1.0",
3836
"autosuggest-highlight": ">=3.0.0",
3937
"date-fns": ">=2.0.0-alpha.16",
4038
"material-ui-pickers": ">=1.0.0-rc.13",
41-
"react": ">=16.3.0",
4239
"react-autosuggest": ">=9.0.0",
4340
"react-color": ">=2.14.1",
4441
"tslib": ">=1.9.2",
@@ -50,6 +47,9 @@
5047
"react": ">=16.0.0"
5148
},
5249
"devDependencies": {
50+
"react": ">=16.3.0",
51+
"@material-ui/core": ">=3.0.0",
52+
"@material-ui/icons": ">=3.0.0",
5353
"@types/react-autosuggest": "9.3.5",
5454
"@types/react-color": "2.13.6",
5555
"@types/validatorjs": "3.7.1",

0 commit comments

Comments
 (0)