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(store-ui): Fix search-input a11y #1000

Merged
merged 5 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { render } from '@testing-library/react'
import { axe } from 'jest-axe'
import React from 'react'

import type { SearchInputProps } from './SearchInput'
Expand All @@ -17,3 +18,11 @@ describe('SearchInput', () => {
)
})
})

describe('Accessibility', () => {
it('should have no violations', async () => {
const { getByTestId } = render(<Wrapper />)

expect(await axe(getByTestId('search-input'))).toHaveNoViolations()
})
})
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you put this test suite inside the Search Input test suite?

16 changes: 13 additions & 3 deletions packages/store-ui/src/molecules/SearchInput/SearchInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export interface SearchInputProps extends InputProps {
* Custom icon inside the submit button.
*/
icon?: ReactNode
/**
* Custom aria-label for input and button.
*/
ariaLabel?: string
Copy link
Contributor

Choose a reason for hiding this comment

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

IMHO, i prefer to use aria-label as the HTML does.

Suggested change
ariaLabel?: string
aria-label?: ArriaAttributes['aria-label']

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, could receive aria-labelledby.

/**
* ID to find this component in testing tools (e.g.: cypress, testing library, and jest).
*/
Expand All @@ -41,7 +45,13 @@ export interface SearchInputProps extends InputProps {

const SearchInput = forwardRef<HTMLFormElement, SearchInputProps>(
function SearchInput(
{ onSubmit, icon, testId = 'store-search-input', ...props },
{
onSubmit,
icon,
ariaLabel = 'search',
testId = 'store-search-input',
...props
},
ref
) {
const valueRef = useRef<HTMLInputElement>(null)
Expand All @@ -61,8 +71,8 @@ const SearchInput = forwardRef<HTMLFormElement, SearchInputProps>(
data-testid={testId}
onSubmit={handleSubmit}
>
<Input ref={valueRef} {...props} />
<Button type="submit">
<Input ref={valueRef} aria-label={ariaLabel} {...props} />
<Button type="submit" aria-label={`button-${ariaLabel}`}>
<Icon component={icon ?? <SearchIcon />} />
</Button>
</form>
Expand Down