Skip to content

Commit f8f87d1

Browse files
authored
refactor: port requiredIfGivenPropIsTruthy to typescript (#20392)
1 parent 88980c7 commit f8f87d1

File tree

5 files changed

+36
-31
lines changed

5 files changed

+36
-31
lines changed

packages/react/src/components/ComposedModal/ComposedModal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import useIsomorphicEffect from '../../internal/useIsomorphicEffect';
2929
import mergeRefs from '../../tools/mergeRefs';
3030
import cx from 'classnames';
3131
import { toggleClass } from '../../tools/toggleClass';
32-
import requiredIfGivenPropIsTruthy from '../../prop-types/requiredIfGivenPropIsTruthy';
32+
import { requiredIfGivenPropIsTruthy } from '../../prop-types/requiredIfGivenPropIsTruthy';
3333
import {
3434
elementOrParentIsFloatingMenu,
3535
wrapFocus,

packages/react/src/components/DataTable/TableExpandHeader.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import PropTypes from 'prop-types';
1111
import React, { type HTMLAttributes } from 'react';
1212
import { usePrefix } from '../../internal/usePrefix';
1313
import { deprecate } from '../../prop-types/deprecate';
14-
import requiredIfGivenPropIsTruthy from '../../prop-types/requiredIfGivenPropIsTruthy';
14+
import { requiredIfGivenPropIsTruthy } from '../../prop-types/requiredIfGivenPropIsTruthy';
1515

1616
export type TableExpandHeaderPropsBase = {
1717
/**

packages/react/src/components/Modal/Modal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import Button from '../Button';
2222
import ButtonSet from '../ButtonSet';
2323
import InlineLoading from '../InlineLoading';
2424
import { Layer } from '../Layer';
25-
import requiredIfGivenPropIsTruthy from '../../prop-types/requiredIfGivenPropIsTruthy';
25+
import { requiredIfGivenPropIsTruthy } from '../../prop-types/requiredIfGivenPropIsTruthy';
2626
import {
2727
elementOrParentIsFloatingMenu,
2828
wrapFocus,

packages/react/src/prop-types/requiredIfGivenPropIsTruthy.js

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright IBM Corp. 2016, 2025
3+
*
4+
* This source code is licensed under the Apache-2.0 license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import type { Validator } from 'prop-types';
9+
10+
/**
11+
* @param name The name of the prop that must exist to validate the current
12+
* prop.
13+
* @param propType The original prop type checker.
14+
* @returns The new prop type checker for the current prop that becomes required
15+
* if the prop corresponding to the provided prop name exists.
16+
*/
17+
export const requiredIfGivenPropIsTruthy = <T>(
18+
name: string,
19+
propType: Validator<T>
20+
): Validator<T> => {
21+
return (props, propName, componentName, ...rest) => {
22+
if (
23+
process.env.NODE_ENV !== 'production' &&
24+
props[name] === true &&
25+
props[propName] === null
26+
) {
27+
return new Error(
28+
`You must provide a value for \`${propName}\` in \`${componentName}\` if \`${name}\` exists.`
29+
);
30+
}
31+
return propType(props, propName, componentName, ...rest);
32+
};
33+
};

0 commit comments

Comments
 (0)