Skip to content

Commit

Permalink
chore(react): fix text field prefix, suffix visible condition
Browse files Browse the repository at this point in the history
  • Loading branch information
naporin0624 committed Feb 13, 2025
1 parent 45c9323 commit b632c05
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
6 changes: 2 additions & 4 deletions packages/react/src/components/TextField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ const TextField = React.forwardRef<HTMLInputElement, TextFieldProps>(
data-invalid={invalid === true}
ref={containerRef}
>
{prefix !== null && (
<div className="charcoal-text-field-prefix">{prefix}</div>
)}
{prefix && <div className="charcoal-text-field-prefix">{prefix}</div>}
<input
className="charcoal-text-field-input"
aria-describedby={showAssistiveText ? describedbyId : undefined}
Expand All @@ -126,7 +124,7 @@ const TextField = React.forwardRef<HTMLInputElement, TextFieldProps>(
value={value}
{...props}
/>
{(suffix !== null || showCount) && (
{(suffix || showCount) && (
<div className="charcoal-text-field-suffix">
{suffix}
{showCount && (
Expand Down
72 changes: 72 additions & 0 deletions packages/react/src/components/TextField/text-field.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { render } from '@testing-library/react'
import TextField from '.'

import '@testing-library/jest-dom'

describe('TextField component', () => {
it('should not render prefix and suffix when not provided', () => {
const { container } = render(<TextField />)

// prefix and suffix elements should not be rendered
const prefixElement = container.querySelector('.charcoal-text-field-prefix')
const suffixElement = container.querySelector('.charcoal-text-field-suffix')

expect(prefixElement).toBeNull()
expect(suffixElement).toBeNull()
})

test.each([
[null, 'null'],
[undefined, 'undefined'],
['', 'empty string'],
[false, 'boolean false'],
[0, 'zero'],
])(
'should not render prefix when value is falsy (%s: %s)',
(prefixValue, _desc) => {
const { container } = render(<TextField prefix={prefixValue} />)
const prefixElement = container.querySelector('.charcoal-text-prefix')
expect(prefixElement).toBeNull()
}
)

test.each([
[null, 'null'],
[undefined, 'undefined'],
['', 'empty string'],
[false, 'boolean false'],
[0, 'zero'],
])(
'should not render suffix when value is falsy (%s: %s) and showCount is false',
(suffixValue, _desc) => {
const { container } = render(
<TextField suffix={suffixValue} showCount={false} />
)
const suffixElement = container.querySelector(
'.charcoal-text-field-suffix'
)
expect(suffixElement).toBeNull()
}
)

it('should render prefix and suffix when provided as truthy values', () => {
const prefixContent = 'Test Prefix'
const suffixContent = 'Test Suffix'
const { container, getByText } = render(
<TextField
prefix={<span>{prefixContent}</span>}
suffix={<span>{suffixContent}</span>}
/>
)

const prefixElement = container.querySelector('.charcoal-text-field-prefix')
const suffixElement = container.querySelector('.charcoal-text-field-suffix')

expect(prefixElement).not.toBeNull()
expect(suffixElement).not.toBeNull()

// Verify text content
expect(getByText(prefixContent)).toBeInTheDocument()
expect(getByText(suffixContent)).toBeInTheDocument()
})
})

0 comments on commit b632c05

Please sign in to comment.