-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharia-label-prop-type.dev.js
70 lines (61 loc) · 1.74 KB
/
aria-label-prop-type.dev.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
var React = require('react');
var deepTraverseChildren = function (children, callback) {
React.Children.forEach(children, function (child) {
if (!child) {
return;
}
callback(child);
if (child.props && child.props.children) {
deepTraverseChildren(child.props.children, callback);
}
});
};
var _accessibilityLabel = function (
props,
propName,
componentName,
renderPropDefault
) {
var label = props[propName];
if (typeof label === 'string' && label.trim() !== '') {
return;
}
var hasStringChild = false;
var children =
typeof props.children === 'function'
? props.children(renderPropDefault)
: props.children;
deepTraverseChildren(children, function (child) {
if (typeof child === 'string') {
hasStringChild = true;
}
});
if (hasStringChild) {
return;
}
return new Error(
'`' +
componentName +
'` must have either an `' +
propName +
'` prop, or one or more text (string) children ' +
'for screen-reader accessibility.'
);
};
var accessibilityLabel = function (props, propName, componentName) {
return _accessibilityLabel(props, propName, componentName);
};
accessibilityLabel.isRequired = accessibilityLabel;
accessibilityLabel.renderPropDefault = function (renderPropDefault) {
var propType = function (props, propName, componentName) {
return _accessibilityLabel(
props,
propName,
componentName,
renderPropDefault
);
};
propType.isRequired = propType;
return propType;
};
module.exports = accessibilityLabel;