英文:
Input type "number" doesn't allow numbers to be entered
问题
我正在创建一个可重复使用的组件,应该像这样:
问题:增加/减少按钮正常工作,但当我点击输入字段并键入数字时,什么都不发生。为什么?
以下是一个最小化的重现代码示例:沙盒
以下是我的代码:
const InputCounter = ({ id, label, value }: NumericInputProps) => {
const [count, setCount] = useState(value ?? 0);
return (
<div className="flex flex-col gap-2">
<label htmlFor={id} className="text-sm font-bold text-neutral-700">
{label}
</label>
<div className="flex w-fit items-center rounded border border-solid border-neutral-300">
<button
type="button"
className="h-10 w-10 rounded border -m-px mr-0 border-solid border-neutral-300 bg-transparent focus:outline-none hover:cursor-pointer"
onClick={() => setCount(count > 0 ? count - 1 : 0)}
>
<IconLess width={16} height={16} />
</button>
<input
id={id}
type="number"
value={count}
className="h-10 w-16 rounded border-none text-center text-base font-semibold focus:outline-none"
/>
<button
type="button"
className="h-10 w-10 rounded border -m-px ml-0 border-solid border-neutral-300 bg-transparent focus:outline-none hover:cursor-pointer"
onClick={() => setCount(count + 1)}
>
<IconPlus width={16} height={16} />
</button>
</div>
</div>
);
}
英文:
I'm creating a reusable component that should be like this :
Problem: The increment/decrement buttons work fine, but when I click in the input field and type a number, nothing happens. Why?
Here is a minimal reproduced code example: Sandbox
Here is my code:
const InputCounter = ({ id, label, value }: NumericInputProps) => {
const [count, setCount] = useState(value ?? 0);
return (
<div className="flex flex-col gap-2">
<label htmlFor={id} className="text-sm font-bold text-neutral-700">
{label}
</label>
<div className="flex w-fit items-center rounded border border-solid border-neutral-300">
<button
type="button"
className="h-10 w-10 rounded border -m-px mr-0 border-solid border-neutral-300 bg-transparent focus:outline-none hover:cursor-pointer"
onClick={() => setCount(count > 0 ? count - 1 : 0)}
>
<IconLess width={16} height={16} />
</button>
<input
id={id}
type="number"
value={count}
className="h-10 w-16 rounded border-none text-center text-base font-semibold focus:outline-none"
/>
<button
type="button"
className="h-10 w-10 rounded border -m-px ml-0 border-solid border-neutral-300 bg-transparent focus:outline-none hover:cursor-pointer"
onClick={() => setCount(count + 1)}
>
<IconPlus width={16} height={16} />
</button>
</div>
</div>
);
答案1
得分: 2
你需要在 onChange 中设置 setCount 的值,就像下面的代码所示。同时,我已经在 sandbox 上更新了代码。
<input
id={id}
type="number"
value={String(count)}
onChange={(e) => setCount(Number(e.target.value))}
className="h-10 w-16 rounded border-none text-center text-base font-semibold focus:outline-none"
/>
英文:
you have to set the value in setCount on onChange, as you can see the below code, also I have update the code on sandbox
<input
id={id}
type="number"
value={String(count)}
onChange={(e) => setCount(Number(e.target.value))}
className="h-10 w-16 rounded border-none text-center text-base font-semibold
focus:outline-none"
/>
答案2
得分: 1
这是您要翻译的内容的翻译部分:
"It is because you are setting the value of number, but you are not hooking the change to the value of number, you need to consider using onChange on the number and try updating the value of number.
Updated code with few optimizations would be :
const InputCounter = ({ id, label, value }: NumericInputProps) => {
const [count, setCount] = useState(value ?? 0);
const handleInputChange = (event) => {
const value = parseInt(event.target.value);
setCount(isNaN(value) ? 0 : value);
};
const handleDecrement = () => {
setCount((count) => Math.max(0, count - 1));
};
const handleIncrement = () => {
setCount((count) => count + 1);
};
return (
<div className="flex flex-col gap-2">
<label htmlFor={id} className="text-sm font-bold text-neutral-700">
{label}
</label>
<div className="flex w-fit items-center rounded border border-solid border-neutral-300">
<button
type="button"
className="h-10 w-10 rounded border -m-px mr-0 border-solid border-neutral-300 bg-transparent focus:outline-none hover:cursor-pointer"
onClick={handleDecrement}
>
<IconLess width={16} height={16} />
</button>
<input
id={id}
type="number"
value={count}
onChange={handleInputChange}
className="h-10 w-16 rounded border-none text-center text-base font-semibold focus:outline-none"
/>
<button
type="button"
className="h-10 w-10 rounded border -m-px ml-0 border-solid border-neutral-300 bg-transparent focus:outline-none hover:cursor-pointer"
onClick={handleIncrement}
>
<IconPlus width={16} height={16} />
</button>
</div>
</div>
);
};
注意:我只翻译了您提供的代码部分,不包括代码中的HTML和CSS。
英文:
It is because you are setting the value of number, but you are not hooking the change to the value of number, you need to consider using onChange on the number and try updating the value of number.
Updated code with few optimizations would be :
const InputCounter = ({ id, label, value }: NumericInputProps) => {
const [count, setCount] = useState(value ?? 0);
const handleInputChange = (event) => {
const value = parseInt(event.target.value);
setCount(isNaN(value) ? 0 : value);
};
const handleDecrement = () => {
setCount((count) => Math.max(0, count - 1));
};
const handleIncrement = () => {
setCount((count) => count + 1);
};
return (
<div className="flex flex-col gap-2">
<label htmlFor={id} className="text-sm font-bold text-neutral-700">
{label}
</label>
<div className="flex w-fit items-center rounded border border-solid border-neutral-300">
<button
type="button"
className="h-10 w-10 rounded border -m-px mr-0 border-solid border-neutral-300 bg-transparent focus:outline-none hover:cursor-pointer"
onClick={handleDecrement}
>
<IconLess width={16} height={16} />
</button>
<input
id={id}
type="number"
value={count}
onChange={handleInputChange}
className="h-10 w-16 rounded border-none text-center text-base font-semibold focus:outline-none"
/>
<button
type="button"
className="h-10 w-10 rounded border -m-px ml-0 border-solid border-neutral-300 bg-transparent focus:outline-none hover:cursor-pointer"
onClick={handleIncrement}
>
<IconPlus width={16} height={16} />
</button>
</div>
</div>
);
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论