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

fix(Stepper): fix the problem of not supporting direct input #537

Merged
merged 1 commit into from
Oct 17, 2024
Merged
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
25 changes: 12 additions & 13 deletions src/stepper/Stepper.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -89,22 +89,22 @@ const Stepper = forwardRef<HTMLDivElement, StepperProps>((originProps, ref) => {
updateValue(formatValue(add(Number(currentValue), -step)));
};

const handleInput = (e: FormEvent<HTMLInputElement>) => {
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<HTMLInputElement, Element>) => {
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<HTMLInputElement, Element>) => {
const { value } = e.currentTarget;
if (isNaN(Number(value))) return;
const formattedValue = formatValue(Number(value));
setCurrentValue(formattedValue);
onBlur?.(formattedValue);
};

return (
Expand Down Expand Up @@ -139,7 +139,6 @@ const Stepper = forwardRef<HTMLDivElement, StepperProps>((originProps, ref) => {
readOnly={disableInput}
onFocus={handleFocus}
onBlur={handleBlur}
onInput={handleInput}
onChange={handleChange}
/>
<div
Expand Down
Loading