forked from code-dot-org/code-dot-org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpropTypes.js
More file actions
101 lines (98 loc) · 2.94 KB
/
Copy pathpropTypes.js
File metadata and controls
101 lines (98 loc) · 2.94 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
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
/**
* Code.org custom react proptypes.
*/
import React from 'react';
import {isSubsequence} from './utils';
/**
* A custom React PropType to ensure that the specified
* component types are given in the specified order
* as children of the component that this prop type is used on.
*
* Example:
* propTypes: {
* children: childrenOfType(Heading, Body)
* }
*
* In this example, the prop type does not validate if there are more than
* one <Heading> components in the children or if <Heading> comes after <Body>.
* However, the prop type will be valid if you omit <Heading> and/or <Body>.
*
* @param ...validChildrenTypes array<constructor> - a list of types to allow as children
*/
export function childrenOfType(...validChildrenTypes) {
return function(props, propName, componentName) {
if (propName !== 'children') {
return new Error(
'The childrenOfType prop type should only be used on the children prop.'
);
}
const prop = props[propName];
if (!prop) {
return;
}
const actualChildrenTypes =
React.Children.map(prop, el => el && el.type) || [];
if (!isSubsequence(validChildrenTypes, actualChildrenTypes)) {
return new Error(
componentName +
' was given children of types ' +
actualChildrenTypes.map(t => `<${t.name}>`).join(', ') +
' but only accepts one of each child in the following order: ' +
validChildrenTypes.map(t => `<${t.name}>`).join(', ') +
'.'
);
}
};
}
/**
* A custom React PropType for a prop that can only be specified
* when a child node of the corresponding type has not been specified.
* This is useful for having an API where your component accepts "configuration"
* either in the form of a prop, or as a child node. For example, to allow for:
*
* <IconButton>
* <Icon src="/images/icons/cow.png"/>
* Click the cow!
* </IconButton>
*
* --- or ---
*
* <IconButton icon="/images/icons/cow.png">Click the cow!</IconButton>
*
* Why would you want to do this? It allows callsites to use non-default Icon
* components if they want. Like so:
*
* <IconButton>
* <AnimatedIcon animation="spin" svg="/images/icons/cow.svg"/>
* Click the cow!
* </IconButton>
*
* Example:
*
* propTypes: {
* icon: whenNoChildOfTypes(Icon, AnimatedIcon)
* }
*
*/
export function whenNoChildOfTypes(...unexpectedChildTypes) {
return function(props, propName, componentName) {
if (!props.children || !props[propName]) {
return;
}
let error;
const actualChildrenTypes = React.Children.map(
props['children'],
el => el.type
);
for (const childType of actualChildrenTypes) {
if (unexpectedChildTypes.includes(childType)) {
error = new Error(
`${componentName} was given a ${propName} prop and a ` +
`<${childType.name}> child, but only one of those is allowed.`
);
break;
}
}
return error;
};
}