aria-label-prop-type
Advanced tools
Comparing version
83
index.js
@@ -1,84 +0,5 @@ | ||
var React = require('react'); | ||
var accessibilityLabel; | ||
if (process.env.NODE_ENV !== 'production') { | ||
// development: | ||
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.' | ||
); | ||
}; | ||
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 = require('./aria-label-prop-type.dev.js'); | ||
} else { | ||
// production: | ||
accessibilityLabel = function() {}; | ||
accessibilityLabel.isRequired = accessibilityLabel; | ||
accessibilityLabel.renderPropDefault = function() { | ||
return accessibilityLabel; | ||
}; | ||
module.exports = require('./aria-label-prop-type.prod.js'); | ||
} | ||
module.exports = accessibilityLabel; |
{ | ||
"name": "aria-label-prop-type", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "A prop type for aria-label and accessibilityLabel that accepts either a string, or a string child somewhere in children", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
6950
-23.51%5
-93.06%1
Infinity%