@carbon/react
Version:
React components for the Carbon Design System
30 lines (28 loc) • 1.09 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
//#region src/prop-types/isRequiredOneOf.ts
/**
* Returns a set of prop-type validators that enforce at least one of the
* specified props must be provided.
*
* @param propTypes - An object of prop-type validators. The keys of the object
* are the names of the props, and the values are the prop-type validators.
* @returns A new object of wrapped prop-type validators.
*/
const isRequiredOneOf = (propTypes) => {
const names = Object.keys(propTypes);
const checker = (propType) => (props, propName, componentName, ...rest) => {
if (names.every((name) => typeof props[name] === "undefined")) return /* @__PURE__ */ new Error(`${componentName} requires one of the following props: ${names.join(", ")}`);
return propType(props, propName, componentName, ...rest);
};
return names.reduce((acc, name) => ({
...acc,
[name]: checker(propTypes[name])
}), {});
};
//#endregion
export { isRequiredOneOf };