Skip to content

Commit 1173457

Browse files
authored
Merge pull request #2142 from dxc-technology/jialecl-select-ariaLabel
ariaLabel prop added to select component
2 parents ec42a11 + fa7ef2b commit 1173457

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

apps/website/screens/components/select/code/SelectCodePage.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,16 @@ const sections = [
268268
<td>Reference to the component.</td>
269269
<td>-</td>
270270
</tr>
271+
<tr>
272+
<td>ariaLabel</td>
273+
<td>
274+
<TableCode>string</TableCode>
275+
</td>
276+
<td>
277+
Specifies a string to be used as the name for the select element when no <Code>label</Code> is provided.
278+
</td>
279+
<td>'Select'</td>
280+
</tr>
271281
</tbody>
272282
</DxcTable>
273283
),

packages/lib/src/select/Select.test.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,21 @@ describe("Select component tests", () => {
114114
expect(select.getAttribute("aria-labelledby")).toBe(label.id);
115115
expect(select.getAttribute("aria-activedescendant")).toBeNull();
116116
expect(select.getAttribute("aria-invalid")).toBe("false");
117+
expect(select.getAttribute("aria-label")).toBeNull();
117118
await userEvent.click(select);
118119
const list = getByRole("listbox");
119120
expect(select.getAttribute("aria-controls")).toBe(list.id);
120121
expect(list.getAttribute("aria-multiselectable")).toBe("false");
121122
});
122123

124+
test("Renders with correct error aria label", () => {
125+
const { getByRole } = render(
126+
<DxcSelect ariaLabel="Example aria label" placeholder="Example" options={singleOptions} />
127+
);
128+
const select = getByRole("combobox");
129+
expect(select.getAttribute("aria-label")).toBe("Example aria label");
130+
});
131+
123132
test("Single selection: Renders with correct default value", async () => {
124133
const { getByText, getByRole, getAllByRole, queryByRole, container } = render(
125134
<DxcSelect label="test-select-label" name="test" defaultValue="4" options={singleOptions} />

packages/lib/src/select/Select.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const DxcSelect = forwardRef<RefType, SelectPropsType>(
5252
margin,
5353
size = "medium",
5454
tabIndex = 0,
55+
ariaLabel = "Select",
5556
},
5657
ref
5758
): JSX.Element => {
@@ -327,6 +328,7 @@ const DxcSelect = forwardRef<RefType, SelectPropsType>(
327328
aria-invalid={!!error}
328329
aria-errormessage={error ? errorId : undefined}
329330
aria-required={!disabled && !optional}
331+
aria-label={label ? undefined : ariaLabel}
330332
>
331333
{multiple && Array.isArray(selectedOption) && selectedOption.length > 0 && (
332334
<SelectionIndicator>

packages/lib/src/select/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ type CommonProps = {
9494
* Value of the tabindex attribute.
9595
*/
9696
tabIndex?: number;
97+
/**
98+
* Specifies a string to be used as the name for the select element when no `label` is provided.
99+
*/
100+
ariaLabel?: string;
97101
};
98102

99103
type SingleSelect = CommonProps & {

0 commit comments

Comments
 (0)