Skip to content

Commit ec42a11

Browse files
authored
Merge pull request #2141 from dxc-technology/jialecl-slider-ariaLabel
ariaLabel prop added to slider component
2 parents d016030 + 211f808 commit ec42a11

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

apps/website/screens/components/slider/code/SliderCodePage.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,16 @@ const sections = [
213213
<td>Reference to the component.</td>
214214
<td>-</td>
215215
</tr>
216+
<tr>
217+
<td>ariaLabel</td>
218+
<td>
219+
<TableCode>string</TableCode>
220+
</td>
221+
<td>
222+
Specifies a string to be used as the name for the slider element when no <Code>label</Code> is provided.
223+
</td>
224+
<td>'Slider'</td>
225+
</tr>
216226
</tbody>
217227
</DxcTable>
218228
),

packages/lib/src/slider/Slider.test.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ describe("Slider component tests", () => {
2121
const sliderId = getByText("label").getAttribute("id");
2222
expect(getByRole("slider").getAttribute("aria-labelledby")).toBe(sliderId);
2323
expect(getByRole("slider").getAttribute("aria-orientation")).toBe("horizontal");
24+
expect(getByRole("slider").getAttribute("aria-label")).toBeNull();
25+
});
26+
27+
test("Renders with correct error aria label", () => {
28+
const { getByRole } = render(
29+
<DxcSlider ariaLabel="Example aria label" minValue={0} maxValue={100} showLimitsValues />
30+
);
31+
const slider = getByRole("slider");
32+
expect(slider.getAttribute("aria-label")).toBe("Example aria label");
2433
});
2534

2635
test("Slider renders with correct initial value when it is uncontrolled", () => {

packages/lib/src/slider/Slider.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ const DxcSlider = forwardRef<RefType, SliderPropsType>(
248248
labelFormatCallback,
249249
margin,
250250
size = "fillParent",
251+
ariaLabel = "Slider",
251252
},
252253
ref
253254
): JSX.Element => {
@@ -319,9 +320,11 @@ const DxcSlider = forwardRef<RefType, SliderPropsType>(
319320
return (
320321
<ThemeProvider theme={colorsTheme.slider}>
321322
<Container margin={margin} size={size} ref={ref}>
322-
<Label id={labelId} disabled={disabled}>
323-
{label}
324-
</Label>
323+
{label && (
324+
<Label id={labelId} disabled={disabled}>
325+
{label}
326+
</Label>
327+
)}
325328
<HelperText disabled={disabled}>{helperText}</HelperText>
326329
<SliderContainer>
327330
{showLimitsValues && <MinLabelContainer disabled={disabled}>{minLabel}</MinLabelContainer>}
@@ -334,11 +337,12 @@ const DxcSlider = forwardRef<RefType, SliderPropsType>(
334337
max={maxValue}
335338
step={step}
336339
disabled={disabled}
337-
aria-labelledby={labelId}
340+
aria-labelledby={label ? labelId : undefined}
338341
aria-orientation="horizontal"
339342
aria-valuemax={maxValue}
340343
aria-valuemin={minValue}
341344
aria-valuenow={value != null && value >= 0 ? value : innerValue}
345+
aria-label={label ? undefined : ariaLabel}
342346
onChange={handleSliderChange}
343347
onMouseUp={handleSliderOnChangeCommitted}
344348
onMouseDown={handleSliderDragging}

packages/lib/src/slider/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ type Props = {
7373
* Size of the component.
7474
*/
7575
size?: "medium" | "large" | "fillParent";
76+
/**
77+
* Specifies a string to be used as the name for the slider element when no `label` is provided.
78+
*/
79+
ariaLabel?: string;
7680
};
7781

7882
/**

0 commit comments

Comments
 (0)