mirror of
https://github.com/vercel/commerce.git
synced 2025-07-24 18:51:23 +00:00
.github
.vscode
app
components
cart
actions.ts
add-to-cart.tsx
close-cart.tsx
delete-item-button.tsx
edit-item-quantity-button.tsx
index.tsx
modal.tsx
open-cart.tsx
grid
icons
layout
product
carousel.tsx
label.tsx
loading-dots.tsx
logo-square.tsx
opengraph-image.tsx
price.tsx
prose.tsx
fonts
lib
.env.example
.eslintrc.js
.gitignore
.nvmrc
.prettierignore
README.md
license.md
next.config.js
package.json
pnpm-lock.yaml
postcss.config.js
prettier.config.js
tailwind.config.js
tsconfig.json
58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
'use client';
|
|
|
|
import { MinusIcon, PlusIcon } from '@heroicons/react/24/outline';
|
|
import clsx from 'clsx';
|
|
import { updateItemQuantity } from 'components/cart/actions';
|
|
import LoadingDots from 'components/loading-dots';
|
|
import type { CartItem } from 'lib/shopify/types';
|
|
import { useFormState, useFormStatus } from 'react-dom';
|
|
|
|
function SubmitButton({ type }: { type: 'plus' | 'minus' }) {
|
|
const { pending } = useFormStatus();
|
|
|
|
return (
|
|
<button
|
|
type="submit"
|
|
onClick={(e: React.FormEvent<HTMLButtonElement>) => {
|
|
if (pending) e.preventDefault();
|
|
}}
|
|
aria-label={type === 'plus' ? 'Increase item quantity' : 'Reduce item quantity'}
|
|
aria-disabled={pending}
|
|
className={clsx(
|
|
'ease flex h-full min-w-[36px] max-w-[36px] flex-none items-center justify-center rounded-full px-2 transition-all duration-200 hover:border-neutral-800 hover:opacity-80',
|
|
{
|
|
'cursor-not-allowed': pending,
|
|
'ml-auto': type === 'minus'
|
|
}
|
|
)}
|
|
>
|
|
{pending ? (
|
|
<LoadingDots className="bg-black dark:bg-white" />
|
|
) : type === 'plus' ? (
|
|
<PlusIcon className="h-4 w-4 dark:text-neutral-500" />
|
|
) : (
|
|
<MinusIcon className="h-4 w-4 dark:text-neutral-500" />
|
|
)}
|
|
</button>
|
|
);
|
|
}
|
|
|
|
export function EditItemQuantityButton({ item, type }: { item: CartItem; type: 'plus' | 'minus' }) {
|
|
const [message, formAction] = useFormState(updateItemQuantity, null);
|
|
const payload = {
|
|
lineId: item.id,
|
|
variantId: item.merchandise.id,
|
|
quantity: type === 'plus' ? item.quantity + 1 : item.quantity - 1
|
|
};
|
|
const actionWithVariant = formAction.bind(null, payload);
|
|
|
|
return (
|
|
<form action={actionWithVariant}>
|
|
<SubmitButton type={type} />
|
|
<p aria-live="polite" className="sr-only" role="status">
|
|
{message}
|
|
</p>
|
|
</form>
|
|
);
|
|
}
|