Skip to content

Commit 37dff96

Browse files
authored
fix(search): update search landmark labeling to use aria-labelledby (#22347)
* fix(search): update search to use aria-labelledby * chore(story): update story search label * chore(jest): update snapshots
1 parent b23ca74 commit 37dff96

5 files changed

Lines changed: 29 additions & 5 deletions

File tree

packages/react/src/components/DataTable/__tests__/__snapshots__/DataTable-test.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ exports[`DataTable behaves as expected selection should render and match snapsho
537537
class="cds--toolbar-content"
538538
>
539539
<div
540-
aria-label="Filter table"
540+
aria-labelledby="custom-id-search"
541541
class="cds--search cds--toolbar-search-container-persistent"
542542
role="search"
543543
>
@@ -987,7 +987,7 @@ exports[`DataTable renders as expected - Component API should render and match s
987987
class="cds--toolbar-content"
988988
>
989989
<div
990-
aria-label="Filter table"
990+
aria-labelledby="custom-id-search"
991991
class="cds--search cds--toolbar-search-container-persistent"
992992
role="search"
993993
>

packages/react/src/components/DataTable/__tests__/__snapshots__/TableToolbarSearch-test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exports[`TableToolbarSearch renders as expected - Component API should render 1`] = `
44
<div>
55
<div
6-
aria-label="Filter table"
6+
aria-labelledby="custom-id-search"
77
class="cds--search custom-class cds--toolbar-search-container-expandable"
88
role="search"
99
>

packages/react/src/components/Search/Search-test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,30 @@ const contentScenarios = [
2121

2222
describe('Search', () => {
2323
describe('renders as expected - Component API', () => {
24+
it('should label the search landmark using labelText via aria-labelledby', () => {
25+
render(<Search labelText="Search A" />);
26+
27+
expect(
28+
screen.getByRole('search', { name: 'Search A' })
29+
).toBeInTheDocument();
30+
});
31+
32+
it('should give each search landmark a unique accessible name when labelText differs', () => {
33+
render(
34+
<>
35+
<Search labelText="Search A" />
36+
<Search labelText="Search B" />
37+
</>
38+
);
39+
40+
expect(
41+
screen.getByRole('search', { name: 'Search A' })
42+
).toBeInTheDocument();
43+
expect(
44+
screen.getByRole('search', { name: 'Search B' })
45+
).toBeInTheDocument();
46+
});
47+
2448
it('should spread extra props onto the input element', () => {
2549
render(<Search labelText="test-search" data-testid="test-id" />);
2650

packages/react/src/components/Search/Search.stories.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default {
2121
closeButtonLabelText: 'Clear search input',
2222
disabled: false,
2323
defaultWidth: 800,
24-
labelText: 'Search',
24+
labelText: 'Site search',
2525
placeholder: 'Placeholder text',
2626
size: 'md',
2727
type: 'search',

packages/react/src/components/Search/Search.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ const Search = React.forwardRef<HTMLInputElement, SearchProps>(
282282
);
283283

284284
return (
285-
<div role="search" aria-label={placeholder} className={searchClasses}>
285+
<div role="search" aria-labelledby={searchId} className={searchClasses}>
286286
{magnifierWithTooltip}
287287
{/* the magnifier is used in ExpandableSearch as a click target to expand,
288288
however, it does not need a keyboard event bc the input element gets focus on keyboard nav and expands that way*/}

0 commit comments

Comments
 (0)