-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathvalidate.js
163 lines (145 loc) · 4.17 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import Validates from './validates';
import { Context } from './context';
import PropTypes from 'prop-types';
import React from 'react';
/**
* 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.
*
* @public
*/
class Validate extends Validates {
constructor() {
super(...arguments);
this.state = {
//
// Validity that results from calling the validate() function from props
//
validates: undefined,
//
// Set of validities for descendent components; key is component name,
// value is validity.
//
valids: {}
};
this.processValidChange = this.processValidChange.bind(this);
}
/**
* Whether or not the component currently validates.
*
* @type {Validity}
* @private
*/
get validates() {
return this.props.validates || this.state.validates;
}
/**
* Child validity change handler.
*
* @param {String} name Identifier for the field whose validity changed.
* @param {Validity} isValid The field's current validity.
* @private
*/
processValidChange(name, isValid) {
const { valids } = this.state;
const { validate } = this.props;
if (isValid === undefined) {
delete valids[name];
} else {
valids[name] = isValid;
}
this.setState({ valids, validates: validate(valids) });
}
/**
* React lifecycle handler called immediately after the component's initial
* render.
*
* @private
*/
componentDidMount() {
// Update the handlers with the initial state
this.onValidChange(this.validates);
}
/**
* React lifecycle handler called when a component finished updating.
*
* @param {Object} prevProps Component's previous props.
* @param {Object} prevState Component's previous state.
* @private
*/
componentDidUpdate(prevProps, prevState) {
const executeOnValidChange = () => {
const isValid = this.validates;
// Prefer props over state.
const { validates: wasValid = prevState.validates, name: prevName } = prevProps;
this.onValidChange(isValid, wasValid, prevName);
};
if (this.props.validate !== prevProps.validate) {
this.setState({ validates: this.props.validate(this.state.valids) }, executeOnValidChange);
} else {
executeOnValidChange();
}
}
/**
* React lifecycle handler called when component is about to be unmounted.
*
* @public
*/
componentWillUnmount() {
//
// Update the handlers with `isValid=undefined` to notify them that the
// component no longer is being validated
//
this.onValidChange(undefined, this.validates);
}
/**
* Renders the component.
*
* @returns {Context.Provider} Rendered component.
* @public
*/
render() {
return (
<Context.Provider value={{ onValidChange: this.processValidChange }}>
{ this.props.children }
</Context.Provider>
);
}
}
Validate.defaultProps = {
validate: () => {} // by default, no validation defined.
};
/**
* Specify the PropTypes for validation purposes.
*
* @type {Object}
*/
Validate.propTypes = {
validate: PropTypes.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(function each(key) {
Validate.propTypes[key] = Validates.propTypes[key];
});
}
//
// Expose the interface.
//
export default Validate;