forked from plemarquand/react-bootstrap-validation
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathValidatedInput.js
More file actions
48 lines (41 loc) · 1.49 KB
/
ValidatedInput.js
File metadata and controls
48 lines (41 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import React from 'react';
import { FormControl } from 'react-bootstrap';
export default class ValidatedInput extends React.Component {
constructor(props) {
super(props);
const {validationEvent, validate, errorHelp, _registerInput, _unregisterInput, ...rest} = props;
this._registerInput = _registerInput;
this._unregisterInput = _unregisterInput;
if (!this._registerInput || !this._unregisterInput) {
throw new Error('Input must be placed inside the Form component');
}
}
componentWillMount() {
this._registerInput(this);
}
componentWillUnmount() {
this._unregisterInput(this);
}
render() {
// Capture props at time of render and strip local component data.
const {validationEvent, validate, errorHelp, _registerInput, _unregisterInput, ...inputProps} = this.props;
return <FormControl ref='control' {...inputProps}>{this.props.children}</FormControl>;
}
}
ValidatedInput.propTypes = {
name : React.PropTypes.string.isRequired,
validationEvent: React.PropTypes.oneOf([
'', 'onChange', 'onBlur', 'onFocus'
]),
validate : React.PropTypes.oneOfType([
React.PropTypes.func,
React.PropTypes.string
]),
errorHelp : React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.object
])
};
ValidatedInput.defaultProps = {
validationEvent: ''
};