I'm using for my project the Range component from here: https://www.npmjs.com/package/rc-slider
There are 4 values marked on the range:
- the
minvalue, - a (let's say) random value in the
middle, - the
maxvalue - the
currentlyChosenValue.
The requirement was to introduce steps for this slider, so that
- the user only sees values rounded to the nearest 10 as
currentlyChosenValue, when he is dragging the slider, - the user only sees values rounded up to the nearest 10, when he pushes the left-right arrow keys.
I could not use the step property defined for the , as my min, max and middle values are pretty random (they are not rounded).
Therefore, I defined a function which rounds the values onChange - it works properly for point #1 (e.g. rounding to min, when selected value is close to min).
However, when the user presses the left/right arrow key, the slider tries to move one step left/right - which gets rounded back to the original value ==> slider won't move eventually. I could not find a way to define a separate onKeyDown event for my Range, I tried to add a Handle with an onKeyDown - but my function there was never triggered.
import { Handle, Range } from "rc-slider";
const handle = props => {
const { value, ...restProps } = props;
const change = event => {
// was never triggered
console.log("a key was pressed", event);
};
return <Handle value={value} onKeyPress={change} {...restProps} />;
};
<Range
handle={handle}
value={[min, middle, currentlyChosenValue]}
min={min}
max={max}
onChange={memoizedOnChange}
/>