Skip to content

Commit 29c1850

Browse files
DSolizafc163coderabbitai[bot]
authored
feat: add aria required prop to slider (#1051)
Co-authored-by: afc163 <afc163@gmail.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 97e960e commit 29c1850

File tree

5 files changed

+16
-0
lines changed

5 files changed

+16
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ The following APIs are shared by Slider and Range.
132132
| tabIndex | number | `0` | Set the tabIndex of the slider handle. |
133133
| ariaLabelForHandle | string | - | Set the `aria-label` attribute on the slider handle. |
134134
| ariaLabelledByForHandle | string | - | Set the `aria-labelledby` attribute on the slider handle. |
135+
| ariaRequired | boolean | - | Set the `aria-required` attribute on the slider handle. |
135136
| ariaValueTextFormatterForHandle | (value) => string | - | A function to set the `aria-valuetext` attribute on the slider handle. It receives the current value of the slider and returns a formatted string describing the value. See [WAI-ARIA Authoring Practices 1.1](https://www.w3.org/TR/wai-aria-practices-1.1/#slider) for more information. |
136137

137138
### Range

src/Handles/Handle.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const Handle = React.forwardRef<HTMLDivElement, HandleProps>((props, ref) => {
6161
tabIndex,
6262
ariaLabelForHandle,
6363
ariaLabelledByForHandle,
64+
ariaRequired,
6465
ariaValueTextFormatterForHandle,
6566
styles,
6667
classNames,
@@ -168,6 +169,7 @@ const Handle = React.forwardRef<HTMLDivElement, HandleProps>((props, ref) => {
168169
'aria-disabled': disabled,
169170
'aria-label': getIndex(ariaLabelForHandle, valueIndex),
170171
'aria-labelledby': getIndex(ariaLabelledByForHandle, valueIndex),
172+
'aria-required': getIndex(ariaRequired, valueIndex),
171173
'aria-valuetext': getIndex(ariaValueTextFormatterForHandle, valueIndex)?.(value),
172174
'aria-orientation': direction === 'ltr' || direction === 'rtl' ? 'horizontal' : 'vertical',
173175
onMouseDown: onInternalStartMove,

src/Slider.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export interface SliderProps<ValueType = number | number[]> {
112112
tabIndex?: number | number[];
113113
ariaLabelForHandle?: string | string[];
114114
ariaLabelledByForHandle?: string | string[];
115+
ariaRequired?: boolean;
115116
ariaValueTextFormatterForHandle?: AriaValueFormat | AriaValueFormat[];
116117
}
117118

@@ -180,6 +181,7 @@ const Slider = React.forwardRef<SliderRef, SliderProps<number | number[]>>((prop
180181
tabIndex = 0,
181182
ariaLabelForHandle,
182183
ariaLabelledByForHandle,
184+
ariaRequired,
183185
ariaValueTextFormatterForHandle,
184186
} = props;
185187

@@ -542,6 +544,7 @@ const Slider = React.forwardRef<SliderRef, SliderProps<number | number[]>>((prop
542544
tabIndex,
543545
ariaLabelForHandle,
544546
ariaLabelledByForHandle,
547+
ariaRequired,
545548
ariaValueTextFormatterForHandle,
546549
styles: styles || {},
547550
classNames: classNames || {},
@@ -560,6 +563,7 @@ const Slider = React.forwardRef<SliderRef, SliderProps<number | number[]>>((prop
560563
tabIndex,
561564
ariaLabelForHandle,
562565
ariaLabelledByForHandle,
566+
ariaRequired,
563567
ariaValueTextFormatterForHandle,
564568
styles,
565569
classNames,

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+
ariaRequired?: boolean;
1819
ariaValueTextFormatterForHandle?: AriaValueFormat | AriaValueFormat[];
1920
classNames: SliderClassNames;
2021
styles: SliderStyles;

tests/Slider.test.js

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

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

0 commit comments

Comments
 (0)