Wiktor Kisielewski
Wiktor Kisielewski

Reputation: 437

Input [number] arrows out of input area

I have a fairly simple react component:

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

styled with StyledComponents like this:

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

so it looks like this now:

enter image description here

and I would like to make it look like this:

enter image description here

Thanks!

Upvotes: 1

Views: 2689

Answers (1)

Ovidiu G
Ovidiu G

Reputation: 1273

What you can do is hide the default incrementer button and make your own buttons to increment and decrement the value from state.

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)} />

Upvotes: 1

Related Questions