javascript - 输入[数字]箭头超出输入区域

标签 javascript html reactjs styled-components

我有一个相当简单的 react 组件:

<Quantity type="number" value="1"></Quantity>

使用 StyledComponents 进行样式化,如下所示:

const Quantity = styled.input`
border: 1px solid #000;
border-radius: 2px;
width: 48px;
height: 28px;
font-size: 18px;
text-align: center;
margin-right: 10px
`;

现在看起来像这样:

enter image description here

我想让它看起来像这样:

enter image description here

谢谢!

最佳答案

您可以做的是隐藏默认的增量器按钮并创建自己的按钮来增加和减少状态值。

const Quantity = styled.input`
   border: 1px solid #000;
   border-radius: 2px;
   width: 48px;
   height: 28px;
   font-size: 18px;
   text-align: center;
   margin-right: 10px

   //hide the default button
   &::-webkit-appearance: textfield;
   &::-moz-appearance: textfield;
   appearance: textfield;

   &::-webkit-inner-spin-button, 
   &::-webkit-outer-spin-button 
   &::-webkit-appearance: none;

`;

const Incrementer = styled.button`
   ...
`;
const Decrementer = styled.button`
   ...
`;
...

const [inputValue, setInputValue] = useState(0);

...

<Incrementer onClick={() => setInputValue(inputValue + 1)} />
<Quantity value={inputValue}/>
<Decrementer onClick={() => setInputValue(inputValue - 1)} />

关于javascript - 输入[数字]箭头超出输入区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56473503/

相关文章:

jquery - 导航出现在第二行

javascript - 3 行 ul,屏幕宽度可变

html - 如何使用css从顶部和底部对齐div内中心的文本

javascript - Angular 形式 : Set all fields untouched, 不脏且有效

javascript - 从对象属性定义调用对象方法

javascript - 如何过滤和映射复杂的json数据?

javascript - React - Axios 正在将未定义的文件数据发送到 Node 服务器

javascript - 当鼠标悬停在元素上时显示隐藏的 div 并保持显示直到鼠标离开任一元素

reactjs - Material 表内部 onClick 仅显示最后一行值

javascript - 如何从下拉 react /redux更新产品列表