Created
December 6, 2016 20:04
-
-
Save stinoga/8b7aee1f2680b7cad4cfabd5f5851103 to your computer and use it in GitHub Desktop.
React custom Proptype function to check if another prop is defined
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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