Improve quantity input behavior in cart item

This commit is contained in:
Luis Alvarez 2021-04-01 18:55:15 -06:00
parent bef569619f
commit 601ec01b96

View File

@ -35,7 +35,7 @@ const CartItem = ({
const updateItem = useUpdateItem({ item })
const removeItem = useRemoveItem()
const [quantity, setQuantity] = useState(item.quantity)
const [quantity, setQuantity] = useState<number | ''>(item.quantity)
const [removing, setRemoving] = useState(false)
const updateQuantity = async (val: number) => {
@ -43,10 +43,10 @@ const CartItem = ({
}
const handleQuantity = (e: ChangeEvent<HTMLInputElement>) => {
const val = Number(e.target.value)
const val = !e.target.value ? '' : Number(e.target.value)
if (Number.isInteger(val) && val >= 0) {
setQuantity(Number(e.target.value))
if (!val || (Number.isInteger(val) && val >= 0)) {
setQuantity(val)
}
}
const handleBlur = () => {