Skip to content

Instantly share code, notes, and snippets.

@stinoga
Created December 6, 2016 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stinoga/8b7aee1f2680b7cad4cfabd5f5851103 to your computer and use it in GitHub Desktop.
Save stinoga/8b7aee1f2680b7cad4cfabd5f5851103 to your computer and use it in GitHub Desktop.
React custom Proptype function to check if another prop is defined
function requiredIfNotHidden(props, propName, componentName) {
if (!props['hiddenLabel']) {
let value = props[propName];
// If no hidden label is set, children are required for accessibility
if (typeof value === undefined) {
return new Error(`Required prop '${propName}' was not specified in '${componentName}'. It is required if the 'hiddenLabel' prop is not set for accessibility
}
}
// We're ok if hiddenLabel is set
return null;
}
Checkbox.propTypes = {
/** Class string. */
className: PropTypes.string,
children: requiredIfNotHidden,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment