-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathprops.js
316 lines (288 loc) · 10.4 KB
/
props.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import PropTypes from 'prop-types';
import {
eq,
identity,
includes,
intersection,
noop,
reduce,
} from 'lodash';
export const CommonPropTypes = {
children: PropTypes.node,
className: PropTypes.oneOfType([
PropTypes.string,
PropTypes.array,
PropTypes.object,
]),
style: PropTypes.oneOfType([
PropTypes.object, // e.g., inline styles
PropTypes.func, // function to be passed some datum, required to return an object
]),
width: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
]),
height: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
]),
dataAccessor: PropTypes.oneOfType([
PropTypes.string,
PropTypes.func,
]),
};
export const CommonDefaultProps = {
noop,
identity,
};
/**
* Event functions that happen during animated transition.
* @type {AnimateEventsType}
*/
export const AnimateEvents = PropTypes.shape({
start: PropTypes.func,
interrupt: PropTypes.func,
end: PropTypes.func,
});
/**
* Timing instructions for animated transition.
* @type {AnimateTimingType}
*/
export const AnimateTiming = PropTypes.shape({
delay: PropTypes.number,
duration: PropTypes.number,
ease: PropTypes.func,
});
/**
* A value that an animatable attribute can evaluate to animate to.
* @type {AnimatableValueType}
*/
export const AnimatableValue = PropTypes.oneOfType([
PropTypes.number,
PropTypes.arrayOf(PropTypes.number),
PropTypes.string,
PropTypes.arrayOf(PropTypes.string),
]);
/**
* A function that returns an object describing how the state should initially start.
* @type {AnimateStartCallback}
*/
export const AnimateStart = PropTypes.func;
/**
* A function that returns an object describing how the state should transform.
* @type {AnimateMethodCallback}
*/
export const AnimateMethod = PropTypes.func;
/**
* An object that gives instructions for handling animation
* @type {AnimatePropType}
*/
export const AnimateProp = PropTypes.shape({
start: AnimateStart,
enter: AnimateMethod,
leave: AnimateMethod,
update: AnimateMethod,
events: AnimateEvents,
timing: AnimateTiming,
});
export function exactlyOneOfProp(propTypes) {
return (props, propName, componentName, ...rest) => {
let error = null;
const validProps = intersection(Object.keys(props), Object.keys(propTypes));
if (validProps.length === 1) {
if (validProps[0] === propName) {
error = propTypes[propName](props, propName, componentName, ...rest);
}
} else {
/* eslint-disable max-len */
error = `Exactly one of prop [\`${Object.keys(propTypes).join('`,`')}\`] must be specified in \`${componentName}\`. Found: [${validProps.length ? `\`${validProps.join('`,`')}\`` : ''}].`;
/* eslint-enable max-len */
}
return error && new Error(error);
};
}
export function atLeastOneOfProp(propTypes) {
return (props, propName, componentName, ...rest) => {
let error = null;
const validProps = intersection(Object.keys(props), Object.keys(propTypes));
if (validProps.length > 0) {
if (includes(validProps, propName)) {
error = propTypes[propName](props, propName, componentName, ...rest);
}
} else {
/* eslint-disable max-len */
error = `At least one of prop [\`${Object.keys(propTypes).join('`,`')}\`] must be specified in \`${componentName}\`.`;
/* eslint-enable max-len */
}
return error && new Error(error);
};
}
export function propsChanged(prevProps, nextProps, propsToCompare, propsToOmit, comparator = eq) {
return !reduce(propsToCompare || Object.keys(nextProps),
(acc, prop) => acc && (
includes(propsToOmit, prop) || comparator(prevProps[prop], nextProps[prop])
),
true
);
}
/**
* Helper function to build a state object from updated props.
*
* The `propUpdates` object will define the prop name and the comparison action. The comparison
* action is a callback with signature (accumulator, propName, prevProps, nextProps), that
* evaluates the prop changes, and determines what to add to the state. The return value will be
* the new accumulated state. It is important to return the accumulator even if no relevant prop
* changes are found.
*
* @param {{ propName: comparisonActionCallback }} propUpdates - prop name and comparison action function.
* @param {Object} prevProps
* @param {Object} nextProps
* @param {Object} state
* @param {Object} [context] - optional `this` context with which to call propUpdate functions
* @returns {Object} accumulated state.
*/
export function stateFromPropUpdates(propUpdates, prevProps, nextProps, state, context) {
return reduce(propUpdates,
(acc, value, key) => value(acc, key, prevProps, nextProps, context),
state
);
}
/**
* Helper function to plug into `stateFromPropUpdates` to provide simplified comparison and update
* functionality.
*
* The `func` callback will be called if the immediate prop has changed. Its signature is
* (nextProp, propName, nextProps), and it determines what to add to the state. The return value
* will be the new accumulated state.
*
* @param {updateFuncCallback} func
* @returns {comparisonActionCallback} comparison action callback.
*/
export function updateFunc(func) {
return (acc, key, prevProps = {}, nextProps, context) => (
prevProps[key] !== nextProps[key]
? { ...acc, ...func(nextProps[key], key, nextProps, acc, context) }
: acc
);
}
/**
* Apply a series of function to the properties of two objects.
* @param prevProps
* @param nextProps
* @param propNames
* @param {firstFuncCallback} firstFunc
* @param {restFuncsCallback} restFuncs
* @returns {Object}
*/
export function applyFuncToProps(prevProps, nextProps, propNames, firstFunc, ...restFuncs) {
return reduce(propNames || Object.keys(nextProps), (acc, prop) => ({
...acc,
[prop]: reduce(restFuncs, (funcAcc, func) =>
func(funcAcc), (firstFunc || identity)(prevProps[prop], nextProps[prop])
),
}), {});
}
/**
* @callback firstFuncCallback
* @param {*} prevProp
* @param {*} nextProp
* @returns {*} result
*/
/**
* @callback restFuncsCallback
* @param {*} acc
* @returns {*} result
*/
/**
* @callback comparisonActionCallback
* @param {Object} state accumulator
* @param {string} propName
* @param {Object} prevProps
* @param {Object} nextProps
* @param {Object} [context] - optional `this` context with which to call updateFuncCallback
* @returns {Object} state property
*/
/**
* @callback updateFuncCallback
* @param {any} nextProp
* @param {string} propName
* @param {Object} nextProps
* @param {Object} state accumulator
* @param {Object} [context] - optional `this` context with which to call updateFuncCallback.
* Usually the react component instance.
* @returns {Object} accumulated state.
*/
/**
* @typedef {(string | string[] | number | number[] )} AnimatableValueType
* @description Value for animatable attribute. Values not wrapped in an array will not be animated.
* ie, a `fill` attribute can animate to the color, '#ccc', by the value `['#ccc']`.
* [detailed in react-move](https://react-move.js.org/#/documentation/node-group).
*/
/**
* @typedef {Object} AnimationStartStateType
* @description Object representing animatable attribute's initial state.
* @property {(string | number)} <keyof AnimationInstruction> - value of
* AnimationStartState. The key would be the name of the attribute that is to be
* affected, ie `fill`.
*/
/**
* @callback AnimateStartCallback
* @param {(number | string)} value - computed value of animatable attribute that can be overridden.
* @param {Object} rawDatum - input data object from which `value` was computed.
* @param {number} index - array index of `rawDatum` within greater dataset.
* @returns {AnimationStartStateType}
*/
/**
* @callback AnimateEventsCallback
* @description Function to execute during an animated transition event.
* ie, function to execute on transition `end`.
* No formal parameters the function has access to the context in which it was created.
* @returns {Void}
*/
/**
* @typedef {Object} AnimateEventsType
* @description The given functions are passed. no arguments but
* have access to the context in which they were created.
* [detailed in react-move](https://react-move.js.org/#/documentation/node-group).
* @property {AnimateEventsCallback} start - function to run on `start`.
* @property {AnimateEventsCallback} interrupt - function to run on `interrupt`.
* @property {AnimateEventsCallback} end - function to run on `end`.
*/
/**
* @callback EasingFunction
* @description Easing function [detailed in react-move](https://react-move.js.org/#/documentation/node-group).
* @param {number} t - input value
* @returns {number}
*/
/**
* @typedef {Object} AnimateTimingType
* @param {number} [delay] - time in ms before transition occurs.
* @param {number} [duration] - time in ms of how long the transition should last.
* @param {EasingFunction} [ease] - easing function like `d3-easeLinear`.
*/
/**
* @typedef {Object} AnimationInstructionType
* @description Instructions for how an animatable attribute should animate.
* @property {AnimatableValueType} [<keyof AnimationInstruction>] - optional value of
* AnimationInstruction. The key would be the name of the animatable attribute that is to be
* affected, ie `fill`. Note, the value must be wrapped in an array if animation is desired.
* @property {AnimateEventsType} [events] - optional object containing instructions for events.
* @property {AnimateTimingType} [timing] - optional object containing instructions for timing.
*/
/**
* @callback AnimateMethodCallback
* @param {number|string} value - computed value of animatable attribute that can be overridden.
* @param {Object} rawDatum - input data object from which `value` was computed.
* @param {number} index - array index of `rawDatum` within greater dataset.
* @returns {AnimationInstructionType}
*/
/**
* @typedef {Object} AnimatePropType
* @description An object that gives instructions for handling animation:
* @property {AnimateStart} start - establish state at `start`
* @property {AnimateMethodCallback} enter - establish state for animations at `enter`
* @property {AnimateMethodCallback} update - establish state for animations at `update`
* @property {AnimateMethodCallback} leave - establish state for animations at `leave`
* @property {AnimateEvents} events - `events` used if not overridden by `AnimateMethod`
* @property {AnimateTiming} timing - `timing` used if not overridden by `AnimateMethod`
*/