Skip to content

Commit

Permalink
fix(search): a11y warning on search not expanded (#16873)
Browse files Browse the repository at this point in the history
* fix(search): a11y warning on search not expanded

* fix(tests): update snapshots and search tests

* fix(search): use undefined

* fix(search): aria-expanded if/else
  • Loading branch information
ariellalgilmore authored Jul 1, 2024
1 parent a06f763 commit 5c37cc7
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -501,10 +501,7 @@ exports[`DataTable behaves as expected selection should render and match snapsho
role="search"
>
<div
aria-expanded="false"
aria-label="button"
class="cds--search-magnifier"
role="button"
tabindex="-1"
>
<svg
Expand Down Expand Up @@ -935,10 +932,7 @@ exports[`DataTable renders as expected - Component API should render and match s
role="search"
>
<div
aria-expanded="false"
aria-label="button"
class="cds--search-magnifier"
role="button"
tabindex="-1"
>
<svg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ exports[`TableToolbarSearch renders as expected - Component API should render 1`
role="search"
>
<div
aria-expanded="false"
aria-label="button"
class="cds--search-magnifier"
role="button"
tabindex="-1"
>
<svg
Expand Down
5 changes: 3 additions & 2 deletions packages/react/src/components/Search/Search-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ describe('Search', () => {
it('should have tabbable input and untabbable button if not expandable', async () => {
render(<Search labelText="test-search" />);

// will have 2 buttons which is the close button and div with a role button
expect(screen.getAllByRole('button').length).toBe(2);
// will have 1 button which is the close button
expect(screen.getAllByRole('button').length).toBe(1);
// search icon not tabbable if not exandable
expect(screen.getByRole('searchbox')).not.toHaveAttribute(
'tabIndex',
'-1'
Expand Down
12 changes: 9 additions & 3 deletions packages/react/src/components/Search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,21 @@ const Search = React.forwardRef<HTMLInputElement, SearchProps>(function Search(
however, it does not need a keyboard event bc the input element gets focus on keyboard nav and expands that way*/}
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events */}
<div
aria-label="button"
aria-label={onExpand ? 'button' : undefined}
aria-labelledby={onExpand ? uniqueId : undefined}
role="button"
role={onExpand ? 'button' : undefined}
className={`${prefix}--search-magnifier`}
onClick={onExpand}
onKeyDown={handleExpandButtonKeyDown}
tabIndex={onExpand && !isExpanded ? 0 : -1}
ref={expandButtonRef}
aria-expanded={onExpand && isExpanded ? true : false}
aria-expanded={
onExpand && isExpanded
? true
: onExpand && !isExpanded
? false
: undefined
}
aria-controls={onExpand ? uniqueId : undefined}>
<CustomSearchIcon icon={renderIcon} />
</div>
Expand Down

0 comments on commit 5c37cc7

Please sign in to comment.