Skip to content

Commit

Permalink
feat: add aria required prop to slider (#1051)
Browse files Browse the repository at this point in the history
Co-authored-by: afc163 <afc163@gmail.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 28, 2024
1 parent 97e960e commit 29c1850
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ The following APIs are shared by Slider and Range.
| tabIndex | number | `0` | Set the tabIndex of the slider handle. |
| ariaLabelForHandle | string | - | Set the `aria-label` attribute on the slider handle. |
| ariaLabelledByForHandle | string | - | Set the `aria-labelledby` attribute on the slider handle. |
| ariaRequired | boolean | - | Set the `aria-required` attribute on the slider handle. |
| 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. |

### Range
Expand Down
2 changes: 2 additions & 0 deletions src/Handles/Handle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const Handle = React.forwardRef<HTMLDivElement, HandleProps>((props, ref) => {
tabIndex,
ariaLabelForHandle,
ariaLabelledByForHandle,
ariaRequired,
ariaValueTextFormatterForHandle,
styles,
classNames,
Expand Down Expand Up @@ -168,6 +169,7 @@ const Handle = React.forwardRef<HTMLDivElement, HandleProps>((props, ref) => {
'aria-disabled': disabled,
'aria-label': getIndex(ariaLabelForHandle, valueIndex),
'aria-labelledby': getIndex(ariaLabelledByForHandle, valueIndex),
'aria-required': getIndex(ariaRequired, valueIndex),
'aria-valuetext': getIndex(ariaValueTextFormatterForHandle, valueIndex)?.(value),
'aria-orientation': direction === 'ltr' || direction === 'rtl' ? 'horizontal' : 'vertical',
onMouseDown: onInternalStartMove,
Expand Down
4 changes: 4 additions & 0 deletions src/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export interface SliderProps<ValueType = number | number[]> {
tabIndex?: number | number[];
ariaLabelForHandle?: string | string[];
ariaLabelledByForHandle?: string | string[];
ariaRequired?: boolean;
ariaValueTextFormatterForHandle?: AriaValueFormat | AriaValueFormat[];
}

Expand Down Expand Up @@ -180,6 +181,7 @@ const Slider = React.forwardRef<SliderRef, SliderProps<number | number[]>>((prop
tabIndex = 0,
ariaLabelForHandle,
ariaLabelledByForHandle,
ariaRequired,
ariaValueTextFormatterForHandle,
} = props;

Expand Down Expand Up @@ -542,6 +544,7 @@ const Slider = React.forwardRef<SliderRef, SliderProps<number | number[]>>((prop
tabIndex,
ariaLabelForHandle,
ariaLabelledByForHandle,
ariaRequired,
ariaValueTextFormatterForHandle,
styles: styles || {},
classNames: classNames || {},
Expand All @@ -560,6 +563,7 @@ const Slider = React.forwardRef<SliderRef, SliderProps<number | number[]>>((prop
tabIndex,
ariaLabelForHandle,
ariaLabelledByForHandle,
ariaRequired,
ariaValueTextFormatterForHandle,
styles,
classNames,
Expand Down
1 change: 1 addition & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface SliderContextProps {
tabIndex: number | number[];
ariaLabelForHandle?: string | string[];
ariaLabelledByForHandle?: string | string[];
ariaRequired?: boolean;
ariaValueTextFormatterForHandle?: AriaValueFormat | AriaValueFormat[];
classNames: SliderClassNames;
styles: SliderStyles;
Expand Down
8 changes: 8 additions & 0 deletions tests/Slider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,14 @@ describe('Slider', () => {
);
});

it('sets aria-required on the handle', () => {
const { container } = render(<Slider ariaRequired={true} />);
expect(container.getElementsByClassName('rc-slider-handle')[0]).toHaveAttribute(
'aria-required',
'true',
);
});

it('sets aria-valuetext on the handle', () => {
const { container } = render(
<Slider
Expand Down

0 comments on commit 29c1850

Please sign in to comment.