@carbon/react
Version:
React components for the Carbon Design System
28 lines (26 loc) • 1.52 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.
*/
import { warning } from "../internal/warning.js";
//#region src/prop-types/deprecateValuesWithin.ts
const didWarnAboutDeprecation = {};
const deprecateValuesWithin = (propType, allowedValues, propMappingFunction) => {
return (props, propName, componentName, ...rest) => {
if (typeof props[propName] === "undefined") return null;
if (!didWarnAboutDeprecation[componentName] || !didWarnAboutDeprecation[componentName][propName]) {
didWarnAboutDeprecation[componentName] = {
...didWarnAboutDeprecation[componentName],
[propName]: true
};
const deprecatedValue = props[propName];
const newValue = propMappingFunction ? propMappingFunction(deprecatedValue) : null;
if (allowedValues && !allowedValues.includes(deprecatedValue)) warning(false, propMappingFunction ? `"${deprecatedValue}" is a deprecated value for the "${propName}" prop on the "${componentName}" component. Use "${newValue}" instead. "${deprecatedValue}" will be removed in the next major release.` : `"${deprecatedValue}" is a deprecated value for the "${propName}" prop on the "${componentName}" component. Allowed values is/are: ${allowedValues.join(", ")}. "${deprecatedValue}" will be removed in the next major release. `);
}
return propType(props, propName, componentName, ...rest);
};
};
//#endregion
export { deprecateValuesWithin };