Skip to content

Commit 47fd44b

Browse files
committed
Make it accessible
1 parent d41d307 commit 47fd44b

File tree

2 files changed

+62
-45
lines changed

2 files changed

+62
-45
lines changed

site/src/components/NewFilter/NewFilter.stories.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ import { userEvent, within, expect } from "@storybook/test";
33
import { useState } from "react";
44
import { NewFilter } from "./NewFilter";
55

6+
const searchLabel = "Search for something";
7+
68
const meta: Meta<typeof NewFilter> = {
79
title: "components/NewFilter",
810
component: NewFilter,
11+
args: {
12+
id: "search",
13+
label: searchLabel,
14+
},
915
render: function NewFilterWithState(args) {
1016
const [value, setValue] = useState<string>(args.value);
1117
return <NewFilter {...args} value={value} onChange={setValue} />;
@@ -40,8 +46,9 @@ export const Focused: Story = {
4046
},
4147
play: async ({ canvasElement }) => {
4248
const canvas = within(canvasElement);
43-
await userEvent.click(canvas.getByRole("textbox"));
44-
await expect(canvas.getByRole("textbox")).toHaveFocus();
49+
const input = canvas.getByLabelText(searchLabel);
50+
await userEvent.click(input);
51+
await expect(input).toHaveFocus();
4552
},
4653
};
4754

@@ -52,8 +59,9 @@ export const Typing: Story = {
5259
play: async ({ canvasElement }) => {
5360
const text = "owner:SomeSearchString";
5461
const canvas = within(canvasElement);
55-
await userEvent.type(canvas.getByRole("textbox"), text);
56-
await expect(canvas.getByRole("textbox")).toHaveValue(text);
62+
const input = canvas.getByLabelText(searchLabel);
63+
await userEvent.type(input, text);
64+
await expect(input).toHaveValue(text);
5765
},
5866
};
5967

@@ -63,7 +71,8 @@ export const ClearInput: Story = {
6371
},
6472
play: async ({ canvasElement }) => {
6573
const canvas = within(canvasElement);
74+
const input = canvas.getByLabelText(searchLabel);
6675
await userEvent.click(canvas.getByRole("button", { name: "Clear filter" }));
67-
await expect(canvas.getByRole("textbox")).toHaveValue("");
76+
await expect(input).toHaveValue("");
6877
},
6978
};

site/src/components/NewFilter/NewFilter.tsx

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,60 +9,68 @@ import { visuallyHidden } from "@mui/utils";
99
import type { FC } from "react";
1010

1111
type NewFilterProps = {
12+
id: string;
13+
label: string;
1214
value: string;
1315
error?: string;
1416
onChange: (value: string) => void;
1517
};
1618

1719
export const NewFilter: FC<NewFilterProps> = (props) => {
1820
const theme = useTheme();
19-
const { value, error, onChange } = props;
21+
const { value, label, id, error, onChange } = props;
2022
const isEmpty = value.length === 0;
2123

2224
return (
23-
<TextField
24-
error={Boolean(error)}
25-
helperText={error}
26-
type="text"
27-
InputProps={{
28-
size: "small",
29-
startAdornment: (
30-
<InputAdornment position="start">
31-
<SearchOutlined
32-
role="presentation"
33-
css={{
34-
fontSize: 14,
35-
color: theme.palette.text.secondary,
36-
}}
37-
/>
38-
</InputAdornment>
39-
),
40-
endAdornment: !isEmpty && (
41-
<Tooltip title="Clear filter">
42-
<IconButton
43-
size="small"
44-
onClick={() => {
45-
onChange("");
46-
}}
47-
>
48-
<CloseOutlined
25+
<>
26+
<label htmlFor={id} css={{ ...visuallyHidden }}>
27+
{label}
28+
</label>
29+
<TextField
30+
error={Boolean(error)}
31+
helperText={error}
32+
type="text"
33+
InputProps={{
34+
id,
35+
size: "small",
36+
startAdornment: (
37+
<InputAdornment position="start">
38+
<SearchOutlined
39+
role="presentation"
4940
css={{
5041
fontSize: 14,
5142
color: theme.palette.text.secondary,
5243
}}
5344
/>
54-
<span css={{ ...visuallyHidden }}>Clear filter</span>
55-
</IconButton>
56-
</Tooltip>
57-
),
58-
}}
59-
fullWidth
60-
placeholder="Search..."
61-
css={{ fontSize: 14, height: "100%" }}
62-
value={value}
63-
onChange={(e) => {
64-
onChange(e.currentTarget.value);
65-
}}
66-
/>
45+
</InputAdornment>
46+
),
47+
endAdornment: !isEmpty && (
48+
<Tooltip title="Clear filter">
49+
<IconButton
50+
size="small"
51+
onClick={() => {
52+
onChange("");
53+
}}
54+
>
55+
<CloseOutlined
56+
css={{
57+
fontSize: 14,
58+
color: theme.palette.text.secondary,
59+
}}
60+
/>
61+
<span css={{ ...visuallyHidden }}>Clear filter</span>
62+
</IconButton>
63+
</Tooltip>
64+
),
65+
}}
66+
fullWidth
67+
placeholder="Search..."
68+
css={{ fontSize: 14, height: "100%" }}
69+
value={value}
70+
onChange={(e) => {
71+
onChange(e.currentTarget.value);
72+
}}
73+
/>
74+
</>
6775
);
6876
};

0 commit comments

Comments
 (0)