Skip to content

Commit 574fae1

Browse files
authored
fix: controlled should lock correctly (#1012)
* fix: should not draggable if controlled * test: update test case
1 parent a988760 commit 574fae1

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

docs/examples/multiple.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default () => {
3131
value={value}
3232
onChange={(nextValue) => {
3333
// console.log('>>>', nextValue);
34-
setValue(nextValue as any);
34+
// setValue(nextValue as any);
3535
}}
3636
activeHandleRender={(node) => <NodeWrapper>{node}</NodeWrapper>}
3737
styles={{

src/hooks/useDrag.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,10 @@ function useDrag(
207207
});
208208

209209
const maxDiffCount = editable ? 1 : 0;
210-
const diffCount: number = Object.values(counts).reduce((prev, next) => prev + next, 0);
210+
const diffCount: number = Object.values(counts).reduce(
211+
(prev, next) => prev + Math.abs(next),
212+
0,
213+
);
211214

212215
return diffCount <= maxDiffCount ? cacheValues : rawValues;
213216
}, [rawValues, cacheValues, editable]);

tests/Range.test.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,18 @@ describe('Range', () => {
199199
expect(container.getElementsByClassName('rc-slider-handle')).toHaveLength(2);
200200
});
201201

202+
it('not moved if controlled', () => {
203+
const onChange = jest.fn();
204+
const { container } = render(<Slider range value={[2, 4, 6]} onChange={onChange} />);
205+
doMouseMove(container, 0, 9999999);
206+
207+
expect(onChange).toHaveBeenCalled();
208+
209+
expect(container.querySelector('.rc-slider-handle-dragging')).toHaveStyle({
210+
left: '2%',
211+
});
212+
});
213+
202214
// Not trigger onChange anymore
203215
// it('should only update bounds that are out of range', () => {
204216
// const props = { min: 0, max: 10000, value: [0.01, 10000], onChange: jest.fn() };

0 commit comments

Comments
 (0)