From 1fe7e9dc8b0fc28f8ebafe4b477d8093210d352b Mon Sep 17 00:00:00 2001 From: 3lang <675483520@qq.com> Date: Wed, 26 Jan 2022 09:45:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20Stepper=E6=89=8B=E5=8A=A8=E8=BE=93?= =?UTF-8?q?=E5=85=A5value=E5=8F=98=E5=8C=96=20#314=20(#315)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix #314 --- packages/react-vant/src/stepper/Stepper.tsx | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/react-vant/src/stepper/Stepper.tsx b/packages/react-vant/src/stepper/Stepper.tsx index 4a7c12285..b6dd4ffd2 100644 --- a/packages/react-vant/src/stepper/Stepper.tsx +++ b/packages/react-vant/src/stepper/Stepper.tsx @@ -28,13 +28,13 @@ const Stepper: React.FC = (props) => { const format = useCallback( (value: string | number) => { - const { min, max, allowEmpty, decimalLength } = props; + const { min, max, allowEmpty, decimalLength, integer } = props; if (allowEmpty && value === '') { return value; } - value = formatNumber(String(value), !props.integer); + value = formatNumber(String(value), !integer); value = value === '' ? 0 : +value; value = isNaN(value) ? +min : value; value = Math.max(Math.min(+max, value), +min); @@ -46,7 +46,7 @@ const Stepper: React.FC = (props) => { return value; }, - [props], + [props.min, props.max, props.allowEmpty, props.decimalLength, props.integer], ); const getInitialValue = () => { @@ -89,11 +89,11 @@ const Stepper: React.FC = (props) => { [props, setCurrent], ); const check = useCallback(() => { - const value = format(current); - if (!equal(value, current)) { + const value = format(currentRef.current); + if (!equal(value, currentRef.current)) { innerChange(value); } - }, [current, format, innerChange]); + }, [format, currentRef.current]); const setValue = (value: string | number) => { if (props.beforeChange) { @@ -156,6 +156,11 @@ const Stepper: React.FC = (props) => { ); const onBlur = (event: FormEvent) => { + const input = event.target as HTMLInputElement; + const value = format(input.value); + if (!equal(value, currentRef.current)) { + innerChange(value); + } props.onBlur?.(event); resetScroll(); }; @@ -222,7 +227,7 @@ const Stepper: React.FC = (props) => { setCurrent(props.value); }, [props.value, setCurrent]); - useEffect(() => check, [check, props.max, props.min, props.integer, props.decimalLength]); + useEffect(() => check, [props.max, props.min, props.integer, props.decimalLength]); return (