forked from godaddy/react-validation-context
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.js
144 lines (125 loc) · 4.05 KB
/
validate.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { func } from 'prop-types';
import Validates from './validates';
const undef = void 0;
/**
* This library revolves around the idea of "validity". A component can have one of the following validities:
*
* - `undefined` - No validation state defined. This is the default.
* - `null` - Validation is disabled.
* - `true` - Validation passed.
* - `false` - Validation failed.
*
* @typedef {(undefined|null|Boolean)} Validity
*/
/**
* The `Validate` component is used to wrap a component which has descendants that may be validated, and provides an interface for
* validating all of those descendants. It extends `Validates` to provide the same interface for listening for validation changes
* on the component itself.
*
* **NOTE**: This component is able to keep track of all conforming descendant components (not just direct children) via the React
* `context` api.
*/
export default class Validate extends Validates {
/**
* Creates a new instance of the component.
*
* @param {Object} props - The component's props.
*/
constructor(props) {
super(props);
this.state = {
validates: undef, // validity that results from calling the validate() function from props
valids: {} // set of validities for descendent components; key is component name, value is validity
};
}
/**
* Whether or not the component currently validates.
*
* @type {Validity}
* @private
*/
get validates() {
// Prefer props over state.
const { validates = this.state.validates } = this.props;
return validates;
}
/**
* Get the child context.
*
* @returns {Object} The child context.
*/
getChildContext() {
return {
/**
* Child validity change handler.
*
* @param {String} name Identifier for the field whose validity changed.
* @param {Validity} isValid The field's current validity.
*/
onValidChange: (name, isValid) => {
const { valids } = this.state;
const { validate } = this.props;
if (isValid === undef) {
delete valids[name];
} else {
valids[name] = isValid;
}
this.setState({ valids, validates: validate(valids) });
}
};
}
/**
* React lifecycle handler called immediately before the component's initial render.
*/
componentWillMount() {
// Update the handlers with the initial state
this.onValidChange(this.validates);
}
/**
* React lifecycle handler called when a component is receiving new props.
*
* @param {Object} nextProps Component's new props.
*/
componentWillReceiveProps(nextProps) {
const { validate } = nextProps;
if (validate === this.props.validate) {
return;
}
const { valids } = this.state;
// Compute new validity, update state
this.setState({ validates: validate(valids) });
}
/**
* React lifecycle handler called when a component finished updating.
*
* @param {Object} prevProps Component's previous props.
* @param {Object} prevState Component's previous state.
*/
componentDidUpdate(prevProps, prevState) {
const isValid = this.validates;
// Prefer props over state.
const { validates: wasValid = prevState.validates, name: prevName } = prevProps;
this.onValidChange(isValid, wasValid, prevName);
}
/**
* React lifecycle handler called when component is about to be unmounted.
*/
componentWillUnmount() {
// Update the handlers with `isValid=undefined` to notify them that the component no longer is being validated
this.onValidChange(undef, this.validates);
}
}
Validate.defaultProps = {
validate: () => {} // by default, no validation defined.
};
Validate.propTypes = {
validate: func.isRequired // validation function
};
// Inherit all propTypes from Validate. In production propTypes are stripped
// so be sure to check for their existence before copying them over.
if (Validates.propTypes) {
Object.keys(Validates.propTypes).forEach(k => (Validate.propTypes[k] = Validates.propTypes[k]));
}
Validate.childContextTypes = {
onValidChange: func
};