Skip to content

Commit

Permalink
feat: add disableKeyboardEvents (#852)
Browse files Browse the repository at this point in the history
* feat: add disableKeyboardEvents

* feat: rename api
  • Loading branch information
hullis committed Nov 11, 2022
1 parent 5924005 commit d99ad5c
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -88,6 +88,7 @@ The following APIs are shared by Slider and Range.
| included | boolean | `true` | If the value is `true`, it means a continuous value interval, otherwise, it is a independent value. |
| reverse | boolean | `false` | If the value is `true`, it means the component is rendered reverse. |
| disabled | boolean | `false` | If `true`, handles can't be moved. |
| keyboard | boolean | `true` | Support using keyboard to move handlers. |
| dots | boolean | `false` | When the `step` value is greater than 1, you can set the `dots` to `true` if you want to render the slider with dots. |
| onBeforeChange | Function | NOOP | `onBeforeChange` will be triggered when `ontouchstart` or `onmousedown` is triggered. |
| onChange | Function | NOOP | `onChange` will be triggered while the value of Slider changing. |
Expand Down
4 changes: 4 additions & 0 deletions docs/examples/handle.tsx
Expand Up @@ -29,5 +29,9 @@ export default () => (
tipFormatter={(value) => `${value}!`}
/>
</div>
<div style={wrapperStyle}>
<p>Keyboard events disabled</p>
<Slider defaultValue={3} keyboard={false} />
</div>
</div>
);
3 changes: 2 additions & 1 deletion src/Handles/Handle.tsx
Expand Up @@ -42,6 +42,7 @@ const Handle = React.forwardRef((props: HandleProps, ref: React.Ref<HTMLDivEleme
max,
direction,
disabled,
keyboard,
range,
tabIndex,
ariaLabelForHandle,
Expand All @@ -59,7 +60,7 @@ const Handle = React.forwardRef((props: HandleProps, ref: React.Ref<HTMLDivEleme

// =========================== Keyboard ===========================
const onKeyDown: React.KeyboardEventHandler<HTMLDivElement> = (e) => {
if (!disabled) {
if (!disabled && keyboard) {
let offset: number | 'min' | 'max' = null;

// Change the value
Expand Down
4 changes: 4 additions & 0 deletions src/Slider.tsx
Expand Up @@ -38,6 +38,7 @@ export interface SliderProps<ValueType = number | number[]> {

// Status
disabled?: boolean;
keyboard?: boolean;
autoFocus?: boolean;
onFocus?: (e: React.FocusEvent<HTMLDivElement>) => void;
onBlur?: (e: React.FocusEvent<HTMLDivElement>) => void;
Expand Down Expand Up @@ -102,6 +103,7 @@ const Slider = React.forwardRef((props: SliderProps, ref: React.Ref<SliderRef>)

// Status
disabled = false,
keyboard = true,
autoFocus,
onFocus,
onBlur,
Expand Down Expand Up @@ -445,6 +447,7 @@ const Slider = React.forwardRef((props: SliderProps, ref: React.Ref<SliderRef>)
max: mergedMax,
direction,
disabled,
keyboard,
step: mergedStep,
included,
includedStart,
Expand All @@ -460,6 +463,7 @@ const Slider = React.forwardRef((props: SliderProps, ref: React.Ref<SliderRef>)
mergedMax,
direction,
disabled,
keyboard,
mergedStep,
included,
includedStart,
Expand Down
2 changes: 2 additions & 0 deletions src/context.ts
Expand Up @@ -8,6 +8,7 @@ export interface SliderContextProps {
includedEnd: number;
direction: Direction;
disabled?: boolean;
keyboard?: boolean;
included?: boolean;
step: number | null;
range?: boolean;
Expand All @@ -25,6 +26,7 @@ const SliderContext = React.createContext<SliderContextProps>({
includedStart: 0,
includedEnd: 0,
tabIndex: 0,
keyboard: true,
});

export default SliderContext;
13 changes: 13 additions & 0 deletions tests/Range.test.js
Expand Up @@ -120,6 +120,19 @@ describe('Range', () => {
expect(onAfterChange).toBeCalled();
});

it('should not change value from keyboard events when disabled', () => {
const onAfterChange = jest.fn();
const { container } = render(
<Slider range keyboard={false} defaultValue={[20, 50]} onAfterChange={onAfterChange} />,
);

fireEvent.keyDown(container.getElementsByClassName('rc-slider-handle')[1], {
keyCode: keyCode.RIGHT,
});

expect(onAfterChange).not.toBeCalled();
});

it('should render Multi-Range with value correctly', () => {
const { container } = render(<Slider range count={3} value={[0, 25, 50, 75]} />);

Expand Down

1 comment on commit d99ad5c

@vercel
Copy link

@vercel vercel bot commented on d99ad5c Nov 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.