我想在输入焦点时删除箭头,但我无法使用 CSS 文件。我必须在我的组件上执行此操作。
我怎样才能做到这一点? TailwindCSS 可以吗?
const InputCounter = ({ id, label, value }: NumericInputProps) => {
const [count, setCount] = useState(value ?? 0);
return (
<div className="hk-flex hk-flex-col hk-gap-2">
<label htmlFor={id} className="hk-text-sm hk-font-bold hk-text-neutral-700">
{label}
</label>
<div>
<button
type="button"
className="hk-h-10 hk-w-10 hk-bg-transparent hk-rounded hk-rounded-r focus:hk-outline-none"
onClick={() => setCount(count - 1)}
>
<IconLess width={16} height={16} />
</button>
<input
id={id}
type="number"
value={count}
className="hk-h-10 hk-w-16 hk-text-center hk-text-base hk-border-none hk-rounded focus:hk-outline-none hk-appearance-none"
/>
<button
type="button"
className="hk-h-10 hk-w-10 hk-bg-transparent hk-rounded hk-rounded-l focus:hk-outline-none"
onClick={() => setCount(count + 1)}
>
<IconPlus width={16} height={16} />
</button>
</div>
</div>
);
};
最佳答案
将[&::-webkit-inner-spin-button]
属性与appearance-none
实用程序结合使用。
如您所见here ,-webkit-inner-spin-button
定义您要禁用的箭头:
The ::-webkit-inner-spin-button CSS pseudo-element is used to style the inner part of the spinner button of number picker input elements.
以下作品:
<input id="{id}" type="number" value="0" class="[&::-webkit-inner-spin-button]:appearance-none" />
使用您的代码:
<div class="flex items-center border border-neutral-300 w-fit rounded m-8 ">
<button type="button" class="h-10 w-10 rounded border-r border-gray-300 bg-transparent focus:outline-none">-</button>
<input id="{id}" type="number" value="0" class="h-10 w-16 text-center [&::-webkit-inner-spin-button]:appearance-none" />
<button type="button" class="h-10 w-10 rounded border-l border-neutral-300 bg-transparent focus:outline-none">+</button>
</div>
关于html - 如何删除输入中输入类型 "number"中的箭头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75879418/