From 8a4ebea999f099f4577bf73908f53ed7cbec63ff Mon Sep 17 00:00:00 2001 From: good-jinu Date: Wed, 18 Dec 2024 01:19:09 +0900 Subject: [PATCH] [Slider] Update Slider onChange, onChangeCommitted - Set onChangeCommitted callback in Slider to receive the last argument passed to onChange --- packages/mui-material/src/Slider/useSlider.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/mui-material/src/Slider/useSlider.ts b/packages/mui-material/src/Slider/useSlider.ts index ba951a2b9f3395..4bf503b65b437d 100644 --- a/packages/mui-material/src/Slider/useSlider.ts +++ b/packages/mui-material/src/Slider/useSlider.ts @@ -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(null); const [valueDerived, setValueState] = useControlled({ controlled: valueProp, @@ -567,7 +569,8 @@ export function useSlider(parameters: UseSliderParameters): UseSliderReturnValue } if (handleChange && !areValuesEqual(newValue, valueDerived)) { - handleChange(nativeEvent, newValue, activeIndex); + lastChangedValue.current = newValue; + handleChange(nativeEvent, lastChangedValue.current ?? newValue, activeIndex); } }); @@ -587,7 +590,7 @@ export function useSlider(parameters: UseSliderParameters): UseSliderReturnValue } if (onChangeCommitted) { - onChangeCommitted(nativeEvent, newValue); + onChangeCommitted(nativeEvent, lastChangedValue.current ?? newValue); } touchId.current = undefined;