-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathchildrenOf.js
50 lines (43 loc) · 1.6 KB
/
childrenOf.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
/*
* `childrenOf` proptype that can be set for expecting a Component
* with a particular displayName.
*
* This is only necessary where we want to limit the range of
* a propType to a certain Component or set of Components.. ie. a
* string or a `<FormattedMessage>` and the range allowed by `PropTypes.node`
* is too wide.
*/
function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || 'Component';
}
function requirable(predicate) {
const propType = (props, propName, ...rest) => {
// don't do any validation if empty
if (props[propName] === undefined) {
return null;
}
return predicate(props, propName, ...rest);
};
propType.isRequired = (props, propName, componentName, ...rest) => {
// warn if empty
if (props[propName] === undefined) {
return new Error(`Required prop \`${propName}\` was not specified in \`${componentName}\`.`);
}
return predicate(props, propName, componentName, ...rest);
};
return propType;
}
export default function childrenOf(...types) {
return requirable((props, propName, componentName, location = 'prop', propFullName = propName) => {
const component = props[propName];
const check = c => types.some(type => type === c.type);
const valid = Array.isArray(component) ? component.every(check) : check(component);
if (!valid) {
return new Error(
// eslint-disable-next-line
`Invalid ${location} \`${propFullName}\` supplied to \`${componentName}\`. Every element must be a <${types.map(t => getDisplayName(t)).join('|')}>.`
);
}
return null;
});
}