Skip to content

Commit

Permalink
fix: a11y: search component submit accessible naming (#2737)
Browse files Browse the repository at this point in the history
Co-authored-by: Brandon Lenz <[email protected]>
  • Loading branch information
shkeating and brandonlenz authored Feb 22, 2024
1 parent d4b30d8 commit b5fb85d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/components/Search/Search/Search.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ describe('Search component', () => {

it('renders a label', () => {
const mockSubmit = jest.fn()
const { queryByLabelText } = render(
const { queryAllByLabelText } = render(
<Search
onSubmit={mockSubmit}
label="Buscar"
i18n={sampleLocalization}></Search>
)

expect(queryByLabelText('Buscar')).toBeInTheDocument()
expect(queryAllByLabelText('Buscar')).toHaveLength(2)
})

it('does not render button text when small', () => {
Expand Down
4 changes: 3 additions & 1 deletion src/components/Search/Search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type SearchInputProps = {
placeholder?: string
label?: React.ReactNode
i18n?: SearchLocalization
buttonAriaLabel?: string
inputProps?: OptionalTextInputProps
}

Expand All @@ -31,6 +32,7 @@ export const Search = ({
label = 'Search',
inputId = 'search-field',
i18n,
buttonAriaLabel,
inputProps,
...formProps
}: SearchInputProps & OptionalFormProps): React.ReactElement => {
Expand All @@ -52,7 +54,7 @@ export const Search = ({
label={label}
defaultValue={formProps.defaultValue}
/>
<SearchButton size={size} i18n={i18n} />
<SearchButton size={size} i18n={i18n} buttonAriaLabel={buttonAriaLabel} />
</Form>
)
}
Expand Down
4 changes: 3 additions & 1 deletion src/components/Search/SearchButton/SearchButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ type SearchButtonProps = {
size?: 'big' | 'small'
className?: string
i18n?: SearchLocalization
buttonAriaLabel?: string
}

export const SearchButton = ({
size,
className,
i18n,
buttonAriaLabel,
}: SearchButtonProps): React.ReactElement => {
const buttonText = i18n?.buttonText || 'Search'
const isSmall = size === 'small'
Expand All @@ -33,7 +35,7 @@ export const SearchButton = ({
)
return (
<div className={classes}>
<Button type="submit">
<Button aria-label={buttonAriaLabel || buttonText} type="submit">
{!isSmall && (
<span className="usa-search__submit-text">{buttonText}</span>
)}
Expand Down

0 comments on commit b5fb85d

Please sign in to comment.