Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add aria required prop to slider #1051

Merged
merged 6 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading