Skip to content

Commit

Permalink
[Slider] Set onChangeCommitted to receive the last argument passed to…
Browse files Browse the repository at this point in the history
… onChange (#44795)

Co-authored-by: Albert Yu <[email protected]>
  • Loading branch information
good-jinu and mj12albert authored Jan 3, 2025
1 parent afd551a commit d0eed6b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
34 changes: 34 additions & 0 deletions packages/mui-material/src/Slider/Slider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1665,4 +1665,38 @@ describe('<Slider />', () => {
width: '10px',
});
});

describe('When the onMouseUp event occurs at a different location than the last onChange event', () => {
it('should pass onChangeCommitted the same value that was passed to the last onChange event', () => {
const handleChange = spy();
const handleChangeCommitted = spy();

const { container } = render(
<Slider onChange={handleChange} onChangeCommitted={handleChangeCommitted} value={0} />,
);
stub(container.firstChild, 'getBoundingClientRect').callsFake(() => ({
width: 100,
left: 0,
}));

fireEvent.mouseDown(container.firstChild, {
buttons: 1,
clientX: 10,
});
fireEvent.mouseMove(container.firstChild, {
buttons: 1,
clientX: 15,
});
fireEvent.mouseUp(container.firstChild, {
buttons: 1,
clientX: 20,
});

expect(handleChange.callCount).to.equal(2);
expect(handleChange.args[0][1]).to.equal(10);
expect(handleChange.args[1][1]).to.equal(15);
expect(handleChangeCommitted.callCount).to.equal(1);
expect(handleChangeCommitted.args[0][1]).to.equal(15);
});
});
});
7 changes: 5 additions & 2 deletions packages/mui-material/src/Slider/useSlider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ export function useSlider(parameters: UseSliderParameters): UseSliderReturnValue
const [open, setOpen] = React.useState(-1);
const [dragging, setDragging] = React.useState(false);
const moveCount = React.useRef(0);
// lastChangedValue is updated whenever onChange is triggered.
const lastChangedValue = React.useRef<number | number[] | null>(null);

const [valueDerived, setValueState] = useControlled({
controlled: valueProp,
Expand All @@ -261,6 +263,7 @@ export function useSlider(parameters: UseSliderParameters): UseSliderReturnValue
value: { value, name },
});

lastChangedValue.current = value;
onChange(clonedEvent, value, thumbIndex);
});

Expand Down Expand Up @@ -350,7 +353,7 @@ export function useSlider(parameters: UseSliderParameters): UseSliderReturnValue
}

if (onChangeCommitted) {
onChangeCommitted(event, newValue);
onChangeCommitted(event, lastChangedValue.current ?? newValue);
}
};

Expand Down Expand Up @@ -587,7 +590,7 @@ export function useSlider(parameters: UseSliderParameters): UseSliderReturnValue
}

if (onChangeCommitted) {
onChangeCommitted(nativeEvent, newValue);
onChangeCommitted(nativeEvent, lastChangedValue.current ?? newValue);
}

touchId.current = undefined;
Expand Down

0 comments on commit d0eed6b

Please sign in to comment.