Skip to content

Commit e43d7da

Browse files
authored
feat: support aria-describedby on handles (#1088)
1 parent 9982e48 commit e43d7da

7 files changed

Lines changed: 31 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ Then open `http://localhost:8000`.
7575
| `allowCross` | boolean | true | Allow handles to cross in range mode. |
7676
| `ariaLabelForHandle` | string \| string[] | - | `aria-label` for handle elements. |
7777
| `ariaLabelledByForHandle` | string \| string[] | - | `aria-labelledby` for handle elements. |
78+
| `ariaDescribedByForHandle` | string \| string[] | - | `aria-describedby` for handle elements. |
7879
| `ariaRequired` | boolean | - | `aria-required` for handle elements. |
7980
| `ariaValueTextFormatterForHandle` | `((value: number) => string) \| ((value: number) => string)[]` | - | Formatter for `aria-valuetext`. |
8081
| `autoFocus` | boolean | - | Focus the slider on mount. |

README.zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ npm start
7474
| `allowCross` | boolean | true | 允许手柄在范围模式下交叉。 |
7575
| `ariaLabelForHandle` | string \| string[] | - | `aria-label` for handle elements. |
7676
| `ariaLabelledByForHandle` | string \| string[] | - | `aria-labelledby` for handle elements. |
77+
| `ariaDescribedByForHandle` | string \| string[] | - | 句柄元素的 `aria-describedby`|
7778
| `ariaRequired` | boolean | - | 句柄元素的 `aria-required`|
7879
| `ariaValueTextFormatterForHandle` | `((value: number) => string) \| ((value: number) => string)[]` | - | `aria-valuetext` 格式化函数。 |
7980
| `autoFocus` | boolean | - | 挂载后自动聚焦滑块。 |

src/Handles/Handle.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const Handle = React.forwardRef<HTMLDivElement, HandleProps>((props, ref) => {
6060
tabIndex,
6161
ariaLabelForHandle,
6262
ariaLabelledByForHandle,
63+
ariaDescribedByForHandle,
6364
ariaRequired,
6465
ariaValueTextFormatterForHandle,
6566
styles,
@@ -173,6 +174,7 @@ const Handle = React.forwardRef<HTMLDivElement, HandleProps>((props, ref) => {
173174
'aria-disabled': mergedDisabled,
174175
'aria-label': getIndex(ariaLabelForHandle, valueIndex),
175176
'aria-labelledby': getIndex(ariaLabelledByForHandle, valueIndex),
177+
'aria-describedby': getIndex(ariaDescribedByForHandle, valueIndex),
176178
'aria-required': getIndex(ariaRequired, valueIndex),
177179
'aria-valuetext': getIndex(ariaValueTextFormatterForHandle, valueIndex)?.(value),
178180
'aria-orientation': direction === 'ltr' || direction === 'rtl' ? 'horizontal' : 'vertical',

src/Slider.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export interface SliderProps<ValueType = number | number[]> {
107107
tabIndex?: number | number[];
108108
ariaLabelForHandle?: string | string[];
109109
ariaLabelledByForHandle?: string | string[];
110+
ariaDescribedByForHandle?: string | string[];
110111
ariaRequired?: boolean;
111112
ariaValueTextFormatterForHandle?: AriaValueFormat | AriaValueFormat[];
112113
}
@@ -175,6 +176,7 @@ const Slider = React.forwardRef<SliderRef, SliderProps<number | number[]>>((prop
175176
tabIndex = 0,
176177
ariaLabelForHandle,
177178
ariaLabelledByForHandle,
179+
ariaDescribedByForHandle,
178180
ariaRequired,
179181
ariaValueTextFormatterForHandle,
180182
} = props;
@@ -565,6 +567,7 @@ const Slider = React.forwardRef<SliderRef, SliderProps<number | number[]>>((prop
565567
tabIndex,
566568
ariaLabelForHandle,
567569
ariaLabelledByForHandle,
570+
ariaDescribedByForHandle,
568571
ariaRequired,
569572
ariaValueTextFormatterForHandle,
570573
styles: styles || {},
@@ -585,6 +588,7 @@ const Slider = React.forwardRef<SliderRef, SliderProps<number | number[]>>((prop
585588
tabIndex,
586589
ariaLabelForHandle,
587590
ariaLabelledByForHandle,
591+
ariaDescribedByForHandle,
588592
ariaRequired,
589593
ariaValueTextFormatterForHandle,
590594
styles,

src/context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface SliderContextProps {
1515
tabIndex: number | number[];
1616
ariaLabelForHandle?: string | string[];
1717
ariaLabelledByForHandle?: string | string[];
18+
ariaDescribedByForHandle?: string | string[];
1819
ariaRequired?: boolean;
1920
ariaValueTextFormatterForHandle?: AriaValueFormat | AriaValueFormat[];
2021
classNames: SliderClassNames;

tests/Range.test.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,20 @@ describe('Range', () => {
493493
);
494494
});
495495

496+
it('sets aria-describedby on the handles', () => {
497+
const { container } = render(
498+
<Slider range ariaDescribedByForHandle={['some_description', 'some_other_description']} />,
499+
);
500+
expect(container.getElementsByClassName('rc-slider-handle')[0]).toHaveAttribute(
501+
'aria-describedby',
502+
'some_description',
503+
);
504+
expect(container.getElementsByClassName('rc-slider-handle')[1]).toHaveAttribute(
505+
'aria-describedby',
506+
'some_other_description',
507+
);
508+
});
509+
496510
it('sets aria-valuetext on the handles', () => {
497511
const { container } = render(
498512
<Slider

tests/Slider.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,14 @@ describe('Slider', () => {
440440
);
441441
});
442442

443+
it('sets aria-describedby on the handle', () => {
444+
const { container } = render(<Slider ariaDescribedByForHandle="some_description" />);
445+
expect(container.getElementsByClassName('rc-slider-handle')[0]).toHaveAttribute(
446+
'aria-describedby',
447+
'some_description',
448+
);
449+
});
450+
443451
it('sets aria-required on the handle', () => {
444452
const { container } = render(<Slider ariaRequired={true} />);
445453
expect(container.getElementsByClassName('rc-slider-handle')[0]).toHaveAttribute(

0 commit comments

Comments
 (0)