Skip to content

Commit 2c641e3

Browse files
committed
fix: preserve pushable gaps on track clicks
1 parent 02260ea commit 2c641e3

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

src/Slider.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,15 +395,34 @@ const Slider = React.forwardRef<SliderRef, SliderProps<number | number[]>>((prop
395395
});
396396

397397
let focusIndex: number;
398+
let valueOffset = 0;
398399

399-
if (effectiveRangeEditable && valueDist !== 0 && (!maxCount || rawValues.length < maxCount)) {
400+
if (!rawValues.length) {
401+
cloneNextValues.push(newValue);
402+
focusIndex = 0;
403+
} else if (
404+
effectiveRangeEditable &&
405+
valueDist !== 0 &&
406+
(!maxCount || rawValues.length < maxCount)
407+
) {
400408
cloneNextValues.splice(valueBeforeIndex + 1, 0, newValue);
401409
focusIndex = valueBeforeIndex + 1;
402410
} else {
403-
cloneNextValues[valueIndex] = newValue;
411+
valueOffset = newValue - rawValues[valueIndex];
404412
focusIndex = valueIndex;
405413
}
406414

415+
if (rawValues.length) {
416+
// Keep track clicks consistent with drag and keyboard constraints.
417+
const { values: nextValues } = offsetValues(
418+
cloneNextValues,
419+
valueOffset,
420+
focusIndex,
421+
'dist',
422+
);
423+
cloneNextValues.splice(0, cloneNextValues.length, ...nextValues);
424+
}
425+
407426
// Fill value to match default 2 (only when `rawValues` is empty)
408427
if (rangeEnabled && !rawValues.length && count === undefined) {
409428
cloneNextValues.push(newValue);

tests/Range.test.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,35 @@ describe('Range', () => {
856856
});
857857
});
858858

859+
it('keeps pushable when clicking the track', () => {
860+
const onChange = jest.fn();
861+
const { container } = render(
862+
<Slider range defaultValue={[20, 40]} pushable={20} onChange={onChange} />,
863+
);
864+
865+
doMouseDown(container, 30, 'rc-slider', true);
866+
fireEvent.mouseUp(document);
867+
868+
expect(onChange).toHaveBeenLastCalledWith([10, 30]);
869+
});
870+
871+
it('keeps pushable when inserting an editable handle', () => {
872+
const onChange = jest.fn();
873+
const { container } = render(
874+
<Slider
875+
range={{ editable: true }}
876+
defaultValue={[20, 40]}
877+
pushable={20}
878+
onChange={onChange}
879+
/>,
880+
);
881+
882+
doMouseDown(container, 30, 'rc-slider', true);
883+
fireEvent.mouseUp(document);
884+
885+
expect(onChange).toHaveBeenLastCalledWith([10, 30, 50]);
886+
});
887+
859888
describe('disabled as array', () => {
860889
const getHandle = (container: HTMLElement, index = 0) =>
861890
container.getElementsByClassName('rc-slider-handle')[index] as HTMLElement;

0 commit comments

Comments
 (0)