Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Slider] Set onChangeCommitted to receive the last argument passed to onChange #44795

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on my findings, the root cause of this issue is that the last argument passed to onChange is different from the argument passed to onChangeCommitted. This happens when the slider slides fast quickly.

The event flow is as follows:

  1. MouseDown to 10 => onChange(event, 10)
  2. MouseMove to 15 => onChange(event, 15)
  3. MouseUp to 20 => onChangeCommit(event, 20)

I couldn't find any tests for this. So I propose this test case.

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
Loading