@mskcc/carbon-react
Version:
Carbon react components for the MSKCC DSM
33 lines (28 loc) • 1.11 kB
JavaScript
/**
* MSKCC 2021, 2024
*/
;
Object.defineProperty(exports, '__esModule', { value: true });
/**
* @param {Object<string, Function>} propTypes The list of type checkers, keyed by prop names.
* @returns {Object<string, Function>}
* The new prop type checkers that checks if one of the given props exist,
* in addition to the original type checkings.
*/
function isRequiredOneOf(propTypes) {
const names = Object.keys(propTypes);
const checker = propType => function (props, propName, componentName) {
if (process.env.NODE_ENV !== "production" && names.every(name => typeof props[name] === 'undefined')) {
return new Error(`${componentName} requires one of the following props: ${names.join(', ')}`);
}
for (var _len = arguments.length, rest = new Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) {
rest[_key - 3] = arguments[_key];
}
return propType(props, propName, componentName, ...rest);
};
return names.reduce((o, name) => ({
...o,
[name]: checker(propTypes[name])
}), {});
}
exports["default"] = isRequiredOneOf;