Skip to content

Commit ddc6d11

Browse files
authored
Merge pull request #1 from ModusCreateOrg/form-validation-2
Fix validation class and example
2 parents 24aea2c + bae8133 commit ddc6d11

File tree

1 file changed

+64
-61
lines changed
  • 14-form-validation/a-validation/src

1 file changed

+64
-61
lines changed

14-form-validation/a-validation/src/App.js

Lines changed: 64 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,80 +2,83 @@ import React, { Component } from 'react';
22
import './App.css';
33
import '../node_modules/purecss/build/base.css';
44
import '../node_modules/purecss/build/forms.css';
5+
56
class Field extends Component {
6-
static defaultProps = {
7-
value: '',
8-
type: 'text'
9-
}
10-
state = {
11-
invalidmsg: this.props.invalidmsg,
12-
value: this.props.value
13-
}
14-
render () {
15-
const {
16-
name,
17-
id = name,
18-
label,
19-
onChange
20-
} = this.props;
21-
const { value, invalidmsg } = this.state;
22-
const inputProps = {
23-
...this.props,
24-
id,
25-
value,
26-
onChange: (e) => this.handleChange_onChange(e, onChange)
27-
};
28-
return (
29-
<div className="pure-control-group">
30-
<label htmlFor={id}>{label}</label>
31-
<input {...inputProps} style={{minWidth: '200px'}} />
32-
{
33-
invalidmsg ?
34-
<span className="pure-form-message-inline">{invalidmsg}</span> :
35-
null
36-
}
37-
</div>
38-
);
39-
}
40-
handleChange_onChange (e, onChange) {
41-
this.setState({
42-
value: e.target.value
43-
});
44-
this.validateField(e);
45-
if (onChange && typeof onChange === 'function') {
46-
onChange(e);
47-
}
48-
}
49-
validateField (e) {
50-
const { target } = e,
51-
{ validity } = target,
52-
{ valueMissing, valid } = validity;
53-
let invalidmsg;
54-
if (valueMissing) {
55-
invalidmsg = 'This is a required field';
56-
} else if (!valid) {
57-
invalidmsg = target.validationMessage;
58-
}
59-
this.setState({ invalidmsg });
60-
}
7+
static defaultProps = {
8+
value: '',
9+
type: 'text'
10+
}
11+
state = {
12+
invalidmsg: null,
13+
value: this.props.value
14+
}
15+
render () {
16+
const {
17+
name,
18+
id = name,
19+
label,
20+
onChange
21+
} = this.props;
22+
const { value, invalidmsg } = this.state;
23+
const inputProps = {
24+
...this.props,
25+
id,
26+
value,
27+
onChange: (e) => this.handleChange(e, onChange)
28+
};
29+
return (
30+
<div className="pure-control-group">
31+
<label htmlFor={id}>{label}</label>
32+
<input {...inputProps} style={{minWidth: '200px'}} />
33+
{
34+
invalidmsg ?
35+
<span className="pure-form-message-inline">{invalidmsg}</span> :
36+
null
37+
}
38+
</div>
39+
);
40+
}
41+
handleChange (e, onChange) {
42+
this.setState({
43+
value: e.target.value
44+
});
45+
this.validateField(e);
46+
if (onChange && typeof onChange === 'function') {
47+
onChange(e);
48+
}
49+
}
50+
validateField (e) {
51+
const { target } = e,
52+
{ validity } = target,
53+
{ valueMissing, valid } = validity;
54+
let invalidmsg;
55+
56+
if (valueMissing) {
57+
invalidmsg = 'This is a required field';
58+
} else if (!valid) {
59+
invalidmsg = this.props.invalidmsg || target.validationMessage;
60+
}
61+
this.setState({ invalidmsg });
62+
}
6163
}
62-
64+
6365
class App extends Component {
6466
render() {
6567
return (
6668
<div>
6769
<Field
68-
invalidmsg="Field is required"
70+
required
6971
name="test"
70-
label="test field"
72+
label="required text field "
7173
onChange={(e) => console.log('change', e)}
7274
/><br/>
7375
<Field
74-
invalidmsg="Field is required"
76+
type="number"
77+
max={20}
7578
name="test"
76-
label="test field"
79+
label="number field (20 max) "
7780
onChange={(e) => console.log('change', e)}
78-
/><br/>
81+
/>
7982
</div>
8083
)
8184
}

0 commit comments

Comments
 (0)