Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update TextInput ref forwarding #3011

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit weird, but the existing pattern for making sure the components have a display name is done like so:

I don't remember if there was a reason we did it that way, or if your approach is better, so I won't block on it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am happy to change but I find it a bit strange - this approach makes it so that the display name of the component (shows up in dev tools, error messages) will be DayForwardRef not Day which I find somewhat unideal.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you. Let's leave it as is. Can always update easily if we find any problems or want to unify one way or the other

Loading