diff --git a/components/cart/CartItem/CartItem.module.css b/components/cart/CartItem/CartItem.module.css index feffa0f3a..af094ba99 100644 --- a/components/cart/CartItem/CartItem.module.css +++ b/components/cart/CartItem/CartItem.module.css @@ -6,23 +6,6 @@ padding-top: 0; } -.actions { - @apply flex p-1 border-accent-2 border items-center justify-center w-12 text-accent-7; - transition-property: border-color, background, color, transform, box-shadow; - transition-duration: 0.15s; - transition-timing-function: ease; -} - -.actions:hover { - @apply bg-accent-1 border-accent-3 text-accent-9; - transition: border-color; - z-index: 10; -} - -.actions:focus { - @apply bg-accent-2 outline-none; -} - .quantity { appearance: textfield; @apply w-8 border-accent-2 border mx-3 rounded text-center text-sm text-black; diff --git a/components/cart/CartItem/CartItem.tsx b/components/cart/CartItem/CartItem.tsx index 4f66da868..e51274e0f 100644 --- a/components/cart/CartItem/CartItem.tsx +++ b/components/cart/CartItem/CartItem.tsx @@ -1,4 +1,4 @@ -import { ChangeEvent, useEffect, useState } from 'react' +import { ChangeEvent, FocusEventHandler, useEffect, useState } from 'react' import cn from 'classnames' import Image from 'next/image' import Link from 'next/link' @@ -9,6 +9,7 @@ import type { LineItem } from '@commerce/types/cart' import usePrice from '@framework/product/use-price' import useUpdateItem from '@framework/cart/use-update-item' import useRemoveItem from '@framework/cart/use-remove-item' +import Quantity from '@components/ui/Quantity' type ItemOption = { name: string @@ -28,6 +29,10 @@ const CartItem = ({ currencyCode: string }) => { const { closeSidebarIfPresent } = useUI() + const [removing, setRemoving] = useState(false) + const [quantity, setQuantity] = useState(item.quantity) + const removeItem = useRemoveItem() + const updateItem = useUpdateItem({ item }) const { price } = usePrice({ amount: item.variant.price * item.quantity, @@ -35,43 +40,22 @@ const CartItem = ({ currencyCode, }) - const updateItem = useUpdateItem({ item }) - const removeItem = useRemoveItem() - const [quantity, setQuantity] = useState(item.quantity) - const [removing, setRemoving] = useState(false) - - const updateQuantity = async (val: number) => { - await updateItem({ quantity: val }) + const handleChange = async ({ + target: { value }, + }: ChangeEvent) => { + setQuantity(Number(value)) + await updateItem({ quantity: Number(value) }) } - const handleQuantity = (e: ChangeEvent) => { - const val = !e.target.value ? '' : Number(e.target.value) - - if (!val || (Number.isInteger(val) && val >= 0)) { - setQuantity(val) - } - } - - const handleBlur = () => { - const val = Number(quantity) - if (val !== item.quantity) { - updateQuantity(val) - } - } - - const increaseQuantity = (n = 1) => { + const increaseQuantity = async (n = 1) => { const val = Number(quantity) + n - if (Number.isInteger(val) && val >= 0) { - setQuantity(val) - updateQuantity(val) - } + setQuantity(val) + await updateItem({ quantity: val }) } const handleRemove = async () => { setRemoving(true) try { - // If this action succeeds then there's no need to do `setRemoving(true)` - // because the component will be removed from the view await removeItem(item) } catch (error) { setRemoving(false) @@ -152,38 +136,13 @@ const CartItem = ({ {variant === 'default' && ( -
- - - - -
+ increaseQuantity(1)} + decrease={() => increaseQuantity(-1)} + /> )} ) diff --git a/components/ui/Quantifier/Quantifier.module.css b/components/ui/Quantifier/Quantifier.module.css deleted file mode 100644 index c3a2af639..000000000 --- a/components/ui/Quantifier/Quantifier.module.css +++ /dev/null @@ -1,2 +0,0 @@ -.root { -} diff --git a/components/ui/Quantifier/Quantifier.tsx b/components/ui/Quantifier/Quantifier.tsx deleted file mode 100644 index 5f1bcb62c..000000000 --- a/components/ui/Quantifier/Quantifier.tsx +++ /dev/null @@ -1,10 +0,0 @@ -import React, { FC } from 'react' -import s from './Quantifier.module.css' - -export interface QuantifierProps {} - -const Quantifier: FC = ({}) => { - return
-} - -export default Quantifier diff --git a/components/ui/Quantifier/index.ts b/components/ui/Quantifier/index.ts deleted file mode 100644 index c9a2fdb29..000000000 --- a/components/ui/Quantifier/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export { default } from './Quantifier' -export * from './Quantifier' diff --git a/components/ui/Quantity/Quantity.module.css b/components/ui/Quantity/Quantity.module.css new file mode 100644 index 000000000..2ca2c5fcf --- /dev/null +++ b/components/ui/Quantity/Quantity.module.css @@ -0,0 +1,22 @@ +.actions { + @apply flex p-1 border-accent-2 border items-center justify-center w-12 text-accent-7; + transition-property: border-color, background, color, transform, box-shadow; + transition-duration: 0.15s; + transition-timing-function: ease; + user-select: none; +} + +.actions:hover { + @apply border bg-accent-1 border-accent-3 text-accent-9; + transition: border-color; + z-index: 10; +} + +.actions:focus { + @apply outline-none; +} + +.input { + @apply bg-transparent px-4 w-full h-full focus:outline-none; + user-select: none; +} diff --git a/components/ui/Quantity/Quantity.tsx b/components/ui/Quantity/Quantity.tsx new file mode 100644 index 000000000..abde145aa --- /dev/null +++ b/components/ui/Quantity/Quantity.tsx @@ -0,0 +1,62 @@ +import React, { FC } from 'react' +import s from './Quantity.module.css' +import { Cross, Plus, Minus } from '@components/icons' +import cn from 'classnames' +export interface QuantityProps { + value: number + increase: () => any + decrease: () => any + handleRemove: React.MouseEventHandler + handleChange: React.ChangeEventHandler + max?: number +} + +const Quantity: FC = ({ + value, + increase, + decrease, + handleChange, + handleRemove, + max = 6, +}) => { + return ( +
+ + + + +
+ ) +} + +export default Quantity diff --git a/components/ui/Quantity/index.ts b/components/ui/Quantity/index.ts new file mode 100644 index 000000000..5ee880cc9 --- /dev/null +++ b/components/ui/Quantity/index.ts @@ -0,0 +1,2 @@ +export { default } from './Quantity' +export * from './Quantity' diff --git a/components/ui/index.ts b/components/ui/index.ts index 37ddd0dbc..ae5ebac25 100644 --- a/components/ui/index.ts +++ b/components/ui/index.ts @@ -11,4 +11,5 @@ export { default as Modal } from './Modal' export { default as Text } from './Text' export { default as Input } from './Input' export { default as Collapse } from './Collapse' +export { default as Quantity } from './Quantity' export { useUI } from './context'