Skip to content

Commit

Permalink
fix: Update TextInput ref forwarding (#3011)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocoutts-usds authored Aug 26, 2024
1 parent 4513e51 commit f8ac06d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 38 deletions.
30 changes: 29 additions & 1 deletion src/components/forms/TextInput/TextInput.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { MutableRefObject, useRef } from 'react'
import { render } from '@testing-library/react'
import { TextInput } from './TextInput'
import { ValidationStatus } from '../../../types/validationStatus'
Expand Down Expand Up @@ -59,4 +59,32 @@ describe('TextInput component', () => {
}
)
})

describe('forwarding refs', () => {
beforeEach(() => {
vi.clearAllMocks()
})

it('appropriateley renders a ref', () => {
let ref
const Parent = () => {
ref = useRef(null)
return (
<TextInput
id="input-type-text"
name="input-type-text"
type="text"
ref={ref}
/>
)
}

render(<Parent />)

const parentRef = ref as unknown as MutableRefObject<HTMLElement>

expect(parentRef.current).toBeInTheDocument()
expect(parentRef.current.tagName).toBe('INPUT')
})
})
})
82 changes: 45 additions & 37 deletions src/components/forms/TextInput/TextInput.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { forwardRef } from 'react'
import classnames from 'classnames'
import { ValidationStatus } from '../../../types/validationStatus'

Expand Down Expand Up @@ -28,43 +28,51 @@ export type OptionalTextInputProps = CustomTextInputProps &

export type TextInputProps = RequiredTextInputProps & OptionalTextInputProps

export const TextInput = ({
id,
name,
type,
className,
validationStatus,
inputSize,
inputRef,
...inputProps
}: TextInputProps): React.ReactElement => {
const isError = validationStatus === 'error'
const isSuccess = validationStatus === 'success'
const isSmall = inputSize === 'small'
const isMedium = inputSize === 'medium'
export const TextInput = forwardRef(
(
props: TextInputProps,
ref: React.ForwardedRef<HTMLInputElement> | undefined
): React.ReactElement => {
const {
id,
name,
type,
className,
validationStatus,
inputSize,
inputRef,
...inputProps
} = props

const classes = classnames(
'usa-input',
{
'usa-input--error': isError,
'usa-input--success': isSuccess,
'usa-input--small': isSmall,
'usa-input--medium': isMedium,
},
className
)
const isError = validationStatus === 'error'
const isSuccess = validationStatus === 'success'
const isSmall = inputSize === 'small'
const isMedium = inputSize === 'medium'

return (
<input
data-testid="textInput"
className={classes}
id={id}
name={name}
type={type}
ref={inputRef}
{...inputProps}
/>
)
}
const classes = classnames(
'usa-input',
{
'usa-input--error': isError,
'usa-input--success': isSuccess,
'usa-input--small': isSmall,
'usa-input--medium': isMedium,
},
className
)

return (
<input
data-testid="textInput"
className={classes}
id={id}
name={name}
type={type}
ref={inputRef || ref}
{...inputProps}
/>
)
}
)

TextInput.displayName = 'TextInput'
export default TextInput

0 comments on commit f8ac06d

Please sign in to comment.