Input type “number” doesn’t allow numbers to be entered.

huangapple go评论68阅读模式
英文:

Input type "number" doesn't allow numbers to be entered

问题

我正在创建一个可重复使用的组件,应该像这样:

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 :

Input type “number” doesn’t allow numbers to be entered.

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) =&gt; {
  const [count, setCount] = useState(value ?? 0);

  return (
    &lt;div className=&quot;flex flex-col gap-2&quot;&gt;
      &lt;label htmlFor={id} className=&quot;text-sm font-bold text-neutral-700&quot;&gt;
        {label}
      &lt;/label&gt;

      &lt;div className=&quot;flex w-fit items-center rounded border border-solid border-neutral-300&quot;&gt;
        &lt;button
          type=&quot;button&quot;
          className=&quot;h-10 w-10 rounded border -m-px mr-0 border-solid border-neutral-300 bg-transparent focus:outline-none hover:cursor-pointer&quot;
          onClick={() =&gt; setCount(count &gt; 0 ? count - 1 : 0)}
        &gt;
          &lt;IconLess width={16} height={16} /&gt;
        &lt;/button&gt;

        &lt;input
          id={id}
          type=&quot;number&quot;
          value={count}
          className=&quot;h-10 w-16 rounded border-none text-center text-base font-semibold focus:outline-none&quot;
        /&gt;

        &lt;button
          type=&quot;button&quot;
          className=&quot;h-10 w-10 rounded border -m-px ml-0 border-solid border-neutral-300 bg-transparent focus:outline-none hover:cursor-pointer&quot;
          onClick={() =&gt; setCount(count + 1)}
        &gt;
          &lt;IconPlus width={16} height={16} /&gt;
        &lt;/button&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  );

答案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

&lt;input
id={id}
type=&quot;number&quot;
value={String(count)}
onChange={(e) =&gt; setCount(Number(e.target.value))}
className=&quot;h-10 w-16 rounded border-none text-center text-base font-semibold 
focus:outline-none&quot;
/&gt;

答案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) =&gt; {
const [count, setCount] = useState(value ?? 0);
const handleInputChange = (event) =&gt; {
const value = parseInt(event.target.value);
setCount(isNaN(value) ? 0 : value);
};
const handleDecrement = () =&gt; {
setCount((count) =&gt; Math.max(0, count - 1));
};
const handleIncrement = () =&gt; {
setCount((count) =&gt; count + 1);
};
return (
&lt;div className=&quot;flex flex-col gap-2&quot;&gt;
&lt;label htmlFor={id} className=&quot;text-sm font-bold text-neutral-700&quot;&gt;
{label}
&lt;/label&gt;
&lt;div className=&quot;flex w-fit items-center rounded border border-solid border-neutral-300&quot;&gt;
&lt;button
type=&quot;button&quot;
className=&quot;h-10 w-10 rounded border -m-px mr-0 border-solid border-neutral-300 bg-transparent focus:outline-none hover:cursor-pointer&quot;
onClick={handleDecrement}
&gt;
&lt;IconLess width={16} height={16} /&gt;
&lt;/button&gt;
&lt;input
id={id}
type=&quot;number&quot;
value={count}
onChange={handleInputChange}
className=&quot;h-10 w-16 rounded border-none text-center text-base font-semibold focus:outline-none&quot;
/&gt;
&lt;button
type=&quot;button&quot;
className=&quot;h-10 w-10 rounded border -m-px ml-0 border-solid border-neutral-300 bg-transparent focus:outline-none hover:cursor-pointer&quot;
onClick={handleIncrement}
&gt;
&lt;IconPlus width={16} height={16} /&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
);
};

huangapple
  • 本文由 发表于 2023年3月31日 17:15:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定