-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
PropTypes.js
84 lines (68 loc) · 2.11 KB
/
PropTypes.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
import PropTypes from 'prop-types';
import createChainableTypeChecker from 'prop-types-extra/lib/utils/createChainableTypeChecker';
import ValidComponentChildren from './ValidComponentChildren';
const idPropType = PropTypes.oneOfType([PropTypes.string, PropTypes.number]);
export function generatedId(name) {
return (props, ...args) => {
let error = null;
if (!props.generateChildId) {
error = idPropType(props, ...args);
if (!error && !props.id) {
error = new Error(
`In order to properly initialize the ${name} in a way that is accessible to assistive technologies ` +
`(such as screen readers) an \`id\` or a \`generateChildId\` prop to ${name} is required`,
);
}
}
return error;
};
}
export function requiredRoles(...roles) {
return createChainableTypeChecker((props, propName, component) => {
let missing;
roles.every(role => {
if (
!ValidComponentChildren.some(
props.children,
child => child.props.bsRole === role,
)
) {
missing = role;
return false;
}
return true;
});
if (missing) {
return new Error(
`(children) ${component} - Missing a required child with bsRole: ` +
`${missing}. ${component} must have at least one child of each of ` +
`the following bsRoles: ${roles.join(', ')}`,
);
}
return null;
});
}
export function exclusiveRoles(...roles) {
return createChainableTypeChecker((props, propName, component) => {
let duplicate;
roles.every(role => {
const childrenWithRole = ValidComponentChildren.filter(
props.children,
child => child.props.bsRole === role,
);
if (childrenWithRole.length > 1) {
duplicate = role;
return false;
}
return true;
});
if (duplicate) {
return new Error(
`(children) ${component} - Duplicate children detected of bsRole: ` +
`${duplicate}. Only one child each allowed with the following ` +
`bsRoles: ${roles.join(', ')}`,
);
}
return null;
});
}