Skip to content

Commit 62d2579

Browse files
authored
refactor(TextInput): port utils to typescript (#20325)
1 parent 355a64d commit 62d2579

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

packages/react/src/components/TextInput/ControlledPasswordInput.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import React, { type HTMLAttributes } from 'react';
99
import classNames from 'classnames';
1010
import PropTypes from 'prop-types';
1111
import { View, ViewOff, WarningFilled } from '@carbon/icons-react';
12-
import { textInputProps } from './util';
12+
import { getTextInputProps } from './util';
1313
import { deprecate } from '../../prop-types/deprecate';
1414
import { usePrefix } from '../../internal/usePrefix';
1515
import { useId } from '../../internal/useId';
@@ -223,7 +223,7 @@ const ControlledPasswordInput = React.forwardRef(
223223
const input = (
224224
<>
225225
<input
226-
{...textInputProps({
226+
{...getTextInputProps({
227227
invalid,
228228
sharedTextInputProps,
229229
invalidId: errorId,

packages/react/src/components/TextInput/PasswordInput.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import classNames from 'classnames';
1616
import PropTypes from 'prop-types';
1717
import { View, ViewOff } from '@carbon/icons-react';
1818
import { useNormalizedInputProps } from '../../internal/useNormalizedInputProps';
19-
import { textInputProps } from './util';
19+
import { getTextInputProps } from './util';
2020
import { FormContext } from '../FluidForm';
2121
import { Tooltip } from '../Tooltip';
2222
import { PopoverAlignment } from '../Popover';
@@ -350,7 +350,7 @@ const PasswordInput = React.forwardRef(
350350
const input = (
351351
<>
352352
<input
353-
{...textInputProps({
353+
{...getTextInputProps({
354354
sharedTextInputProps,
355355
invalid: normalizedProps.invalid,
356356
invalidId: normalizedProps.invalidId,

packages/react/src/components/TextInput/TextInput.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import React, {
1717
import classNames from 'classnames';
1818
import { useNormalizedInputProps } from '../../internal/useNormalizedInputProps';
1919
import { deprecate } from '../../prop-types/deprecate';
20-
import { textInputProps } from './util';
20+
import { getTextInputProps } from './util';
2121
import { FormContext } from '../FluidForm';
2222
import { useMergedRefs } from '../../internal/useMergedRefs';
2323
import { usePrefix } from '../../internal/usePrefix';
@@ -332,7 +332,7 @@ const TextInput = React.forwardRef(
332332

333333
const input = (
334334
<input
335-
{...textInputProps({
335+
{...getTextInputProps({
336336
sharedTextInputProps,
337337
invalid: normalizedProps.invalid,
338338
invalidId: normalizedProps.invalidId,

packages/react/src/components/TextInput/util.js renamed to packages/react/src/components/TextInput/util.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,39 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
const invalidProps = (invalidId) => ({
8+
const invalidProps = (invalidId: string) => ({
99
'data-invalid': true,
1010
'aria-invalid': true,
1111
'aria-describedby': invalidId,
1212
});
1313

14-
const warnProps = (warnId) => ({
14+
const warnProps = (warnId: string | undefined) => ({
1515
'aria-describedby': warnId,
1616
});
1717

18-
const helperProps = (helperId) => ({
18+
const helperProps = (helperId: string | undefined) => ({
1919
'aria-describedby': helperId,
2020
});
2121

22-
/**
23-
* @param {{sharedTextInputProps: object, invalid?: boolean, invalidId?: string, warn?: boolean, warnId?: string, hasHelper?: boolean, helperId?: string}} config
24-
* @returns {object}
25-
*/
26-
export const textInputProps = ({
22+
interface TextInputPropsConfig {
23+
sharedTextInputProps: Record<string, unknown>;
24+
invalid: boolean;
25+
invalidId: string;
26+
warn?: boolean;
27+
warnId?: string;
28+
hasHelper?: boolean;
29+
helperId?: string;
30+
}
31+
32+
export const getTextInputProps = ({
2733
sharedTextInputProps,
2834
invalid,
2935
invalidId,
3036
warn,
3137
warnId,
3238
hasHelper,
3339
helperId,
34-
}) => ({
40+
}: TextInputPropsConfig) => ({
3541
...sharedTextInputProps,
3642
...(invalid ? invalidProps(invalidId) : {}),
3743
...(warn ? warnProps(warnId) : {}),

0 commit comments

Comments
 (0)