Skip to content

Commit

Permalink
fix: controlled should lock correctly (#1012)
Browse files Browse the repository at this point in the history
* fix: should not draggable if controlled

* test: update test case
  • Loading branch information
zombieJ committed Jul 23, 2024
1 parent a988760 commit 574fae1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/examples/multiple.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default () => {
value={value}
onChange={(nextValue) => {
// console.log('>>>', nextValue);
setValue(nextValue as any);
// setValue(nextValue as any);
}}
activeHandleRender={(node) => <NodeWrapper>{node}</NodeWrapper>}
styles={{
Expand Down
5 changes: 4 additions & 1 deletion src/hooks/useDrag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,10 @@ function useDrag(
});

const maxDiffCount = editable ? 1 : 0;
const diffCount: number = Object.values(counts).reduce((prev, next) => prev + next, 0);
const diffCount: number = Object.values(counts).reduce(
(prev, next) => prev + Math.abs(next),
0,
);

return diffCount <= maxDiffCount ? cacheValues : rawValues;
}, [rawValues, cacheValues, editable]);
Expand Down
12 changes: 12 additions & 0 deletions tests/Range.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,18 @@ describe('Range', () => {
expect(container.getElementsByClassName('rc-slider-handle')).toHaveLength(2);
});

it('not moved if controlled', () => {
const onChange = jest.fn();
const { container } = render(<Slider range value={[2, 4, 6]} onChange={onChange} />);
doMouseMove(container, 0, 9999999);

expect(onChange).toHaveBeenCalled();

expect(container.querySelector('.rc-slider-handle-dragging')).toHaveStyle({
left: '2%',
});
});

// Not trigger onChange anymore
// it('should only update bounds that are out of range', () => {
// const props = { min: 0, max: 10000, value: [0.01, 10000], onChange: jest.fn() };
Expand Down

0 comments on commit 574fae1

Please sign in to comment.