From a168846954bb28e234534207c30313d9fc2b43e3 Mon Sep 17 00:00:00 2001 From: anlyyao Date: Thu, 17 Oct 2024 19:04:32 +0800 Subject: [PATCH] fix(Stepper): fix the problem of not supporting direct input --- src/stepper/Stepper.tsx | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/stepper/Stepper.tsx b/src/stepper/Stepper.tsx index 8c3bad5f..75b91004 100644 --- a/src/stepper/Stepper.tsx +++ b/src/stepper/Stepper.tsx @@ -1,4 +1,4 @@ -import React, { FormEvent, forwardRef, memo } from 'react'; +import React, { forwardRef, memo } from 'react'; import classNames from 'classnames'; import { AddIcon, RemoveIcon } from 'tdesign-icons-react'; import { usePrefixClass } from '../hooks/useClass'; @@ -89,22 +89,22 @@ const Stepper = forwardRef((originProps, ref) => { updateValue(formatValue(add(Number(currentValue), -step))); }; - const handleInput = (e: FormEvent) => { - const value = formatNumber((e.target as HTMLInputElement).value, !integer); - setCurrentValue(value); - }; - - const handleChange = () => { - const formattedValue = formatValue(Number(currentValue)); - updateValue(formattedValue); + const handleChange = (e: React.FocusEvent) => { + const { value } = e.currentTarget; + if (isNaN(Number(value))) return; + setCurrentValue(formatNumber(value, !integer)); }; const handleFocus = () => { - onFocus(Number(currentValue)); + onFocus?.(Number(currentValue)); }; - const handleBlur = () => { - onBlur(Number(currentValue)); + const handleBlur = (e: React.FocusEvent) => { + const { value } = e.currentTarget; + if (isNaN(Number(value))) return; + const formattedValue = formatValue(Number(value)); + setCurrentValue(formattedValue); + onBlur?.(formattedValue); }; return ( @@ -139,7 +139,6 @@ const Stepper = forwardRef((originProps, ref) => { readOnly={disableInput} onFocus={handleFocus} onBlur={handleBlur} - onInput={handleInput} onChange={handleChange} />