fix: can not limit Chinese input lenth correctly (#947)

This commit is contained in:
Qi 2023-02-10 20:35:12 +08:00 committed by GitHub
parent 7890219e29
commit cb118149f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,7 +19,6 @@ type inputProps = {
onBlur?: FocusEventHandler<HTMLInputElement>; onBlur?: FocusEventHandler<HTMLInputElement>;
onKeyDown?: KeyboardEventHandler<HTMLInputElement>; onKeyDown?: KeyboardEventHandler<HTMLInputElement>;
}; };
export const Input = (props: inputProps) => { export const Input = (props: inputProps) => {
const { const {
disabled, disabled,
@ -35,15 +34,11 @@ export const Input = (props: inputProps) => {
} = props; } = props;
const [value, setValue] = useState<string>(valueProp || ''); const [value, setValue] = useState<string>(valueProp || '');
const handleChange: InputHTMLAttributes<HTMLInputElement>['onChange'] = e => { const handleChange: InputHTMLAttributes<HTMLInputElement>['onChange'] = e => {
if ( const { value } = e.target;
(maxLength && e.target.value.length > maxLength) || setValue(value);
(minLength && e.target.value.length < minLength) onChange && onChange(value);
) {
return;
}
setValue(e.target.value);
onChange && onChange(e.target.value);
}; };
const handleBlur: InputHTMLAttributes<HTMLInputElement>['onBlur'] = e => { const handleBlur: InputHTMLAttributes<HTMLInputElement>['onBlur'] = e => {
onBlur && onBlur(e); onBlur && onBlur(e);
}; };