Skip to content

Commit e35ad14

Browse files
authored
refactor: port deprecateValuesWithin to typescript (#20394)
* refactor: port deprecateValuesWithin to typescript * refactor: update deprecateValuesWithin generics
1 parent ccb653e commit e35ad14

File tree

10 files changed

+29
-25
lines changed

10 files changed

+29
-25
lines changed

packages/react/src/components/CodeSnippet/CodeSnippet.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { useId } from '../../internal/useId';
2323
import copy from 'copy-to-clipboard';
2424
import { deprecate } from '../../prop-types/deprecate';
2525
import { usePrefix } from '../../internal/usePrefix';
26-
import deprecateValuesWithin from '../../prop-types/deprecateValuesWithin';
26+
import { deprecateValuesWithin } from '../../prop-types/deprecateValuesWithin';
2727
import type {
2828
DeprecatedPopoverAlignment,
2929
NewPopoverAlignment,

packages/react/src/components/ComboButton/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { useFeatureFlag } from '../FeatureFlags';
2626
import mergeRefs from '../../tools/mergeRefs';
2727
import { MenuAlignment } from '../MenuButton';
2828
import { TranslateWithId } from '../../types/common';
29-
import deprecateValuesWithin from '../../prop-types/deprecateValuesWithin';
29+
import { deprecateValuesWithin } from '../../prop-types/deprecateValuesWithin';
3030
import { mapPopoverAlign } from '../../tools/mapPopoverAlign';
3131

3232
const defaultTranslations = {

packages/react/src/components/Copy/Copy.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { composeEventHandlers } from '../../tools/events';
2020
import { usePrefix } from '../../internal/usePrefix';
2121
import { IconButton } from '../IconButton';
2222
import { noopFn } from '../../internal/noopFn';
23-
import deprecateValuesWithin from '../../prop-types/deprecateValuesWithin';
23+
import { deprecateValuesWithin } from '../../prop-types/deprecateValuesWithin';
2424
import { mapPopoverAlign } from '../../tools/mapPopoverAlign';
2525
import type {
2626
DeprecatedPopoverAlignment,

packages/react/src/components/CopyButton/CopyButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Copy from '../Copy';
1414
import { LayoutConstraint } from '../Layout';
1515
import { usePrefix } from '../../internal/usePrefix';
1616
import { noopFn } from '../../internal/noopFn';
17-
import deprecateValuesWithin from '../../prop-types/deprecateValuesWithin';
17+
import { deprecateValuesWithin } from '../../prop-types/deprecateValuesWithin';
1818
import { mapPopoverAlign } from '../../tools/mapPopoverAlign';
1919
import type {
2020
DeprecatedPopoverAlignment,

packages/react/src/components/IconButton/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Tooltip } from '../Tooltip';
1313
import { useId } from '../../internal/useId';
1414
import { usePrefix } from '../../internal/usePrefix';
1515
import ButtonBase from '../Button/ButtonBase';
16-
import deprecateValuesWithin from '../../prop-types/deprecateValuesWithin';
16+
import { deprecateValuesWithin } from '../../prop-types/deprecateValuesWithin';
1717
import BadgeIndicator from '../BadgeIndicator';
1818
import type {
1919
DeprecatedPopoverAlignment,

packages/react/src/components/Notification/Notification.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import { noopFn } from '../../internal/noopFn';
4141
import { wrapFocus, wrapFocusWithoutSentinels } from '../../internal/wrapFocus';
4242
import { useFeatureFlag } from '../FeatureFlags';
4343
import { warning } from '../../internal/warning';
44-
import deprecateValuesWithin from '../../prop-types/deprecateValuesWithin';
44+
import { deprecateValuesWithin } from '../../prop-types/deprecateValuesWithin';
4545

4646
/**
4747
* Conditionally call a callback when the escape key is pressed
@@ -1249,11 +1249,12 @@ export type NewKindProps = 'warning' | 'info';
12491249

12501250
export type KindProps = DeprecatedKindProps | NewKindProps;
12511251

1252-
const propMappingFunction = (deprecatedValue) => {
1253-
const mapping = {
1254-
error: 'warning', // only redirect error -> warning
1255-
success: 'info', // only redirect success -> info
1256-
};
1252+
const mapping = {
1253+
error: 'warning', // only redirect error -> warning
1254+
success: 'info', // only redirect success -> info
1255+
};
1256+
1257+
const propMappingFunction = (deprecatedValue: string) => {
12571258
return mapping[deprecatedValue];
12581259
};
12591260

packages/react/src/components/OverflowMenu/OverflowMenu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import { setupGetInstanceId } from '../../tools/setupGetInstanceId';
4343
import { IconButton, IconButtonProps } from '../IconButton';
4444
import { OverflowMenuItemProps } from '../OverflowMenuItem/OverflowMenuItem';
4545
import { useOutsideClick } from '../../internal/useOutsideClick';
46-
import deprecateValuesWithin from '../../prop-types/deprecateValuesWithin';
46+
import { deprecateValuesWithin } from '../../prop-types/deprecateValuesWithin';
4747
import { mapPopoverAlign } from '../../tools/mapPopoverAlign';
4848

4949
const getInstanceId = setupGetInstanceId();

packages/react/src/components/OverflowMenu/next/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import mergeRefs from '../../../tools/mergeRefs';
2020
import { useId } from '../../../internal/useId';
2121
import { usePrefix } from '../../../internal/usePrefix';
2222
import { useAttachedMenu } from '../../../internal/useAttachedMenu';
23-
import deprecateValuesWithin from '../../../prop-types/deprecateValuesWithin';
23+
import { deprecateValuesWithin } from '../../../prop-types/deprecateValuesWithin';
2424
import { mapPopoverAlign } from '../../../tools/mapPopoverAlign';
2525

2626
const defaultSize = 'md';

packages/react/src/components/Popover/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import cx from 'classnames';
99
import PropTypes, { WeakValidationMap } from 'prop-types';
10-
import deprecateValuesWithin from '../../prop-types/deprecateValuesWithin';
10+
import { deprecateValuesWithin } from '../../prop-types/deprecateValuesWithin';
1111
import React, { useEffect, useMemo, useRef, type ElementType } from 'react';
1212
import useIsomorphicEffect from '../../internal/useIsomorphicEffect';
1313
import { useMergedRefs } from '../../internal/useMergedRefs';

packages/react/src/prop-types/deprecateValuesWithin.js renamed to packages/react/src/prop-types/deprecateValuesWithin.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
/**
2-
* Copyright IBM Corp. 2016, 2023
2+
* Copyright IBM Corp. 2016, 2025
33
*
44
* This source code is licensed under the Apache-2.0 license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import type { Requireable, Validator } from 'prop-types';
89
import { warning } from '../internal/warning';
910

10-
const didWarnAboutDeprecation = {};
11+
type DeprecationTracker = Record<string, Record<string, boolean>>;
1112

12-
export default function deprecateValuesWithin(
13-
propType,
14-
allowedValues,
15-
propMappingFunction
16-
) {
17-
return function checker(props, propName, componentName, ...rest) {
18-
if (props[propName] === undefined) {
19-
return;
13+
const didWarnAboutDeprecation: DeprecationTracker = {};
14+
15+
export const deprecateValuesWithin = <T>(
16+
propType: Requireable<T>,
17+
allowedValues?: readonly unknown[],
18+
propMappingFunction?: (deprecatedValue: T) => T
19+
): Validator<T> | Requireable<T> => {
20+
return (props, propName, componentName, ...rest) => {
21+
if (typeof props[propName] === 'undefined') {
22+
return null;
2023
}
2124

2225
if (
@@ -45,4 +48,4 @@ export default function deprecateValuesWithin(
4548
}
4649
return propType(props, propName, componentName, ...rest);
4750
};
48-
}
51+
};

0 commit comments

Comments
 (0)