Skip to content

Commit

Permalink
Update QuantitySelector
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasKn committed Dec 20, 2023
1 parent 98ac991 commit 19feef6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
7 changes: 5 additions & 2 deletions templates/hydrogen-theme/app/components/QuantitySelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function QuantitySelector(props: {children: React.ReactNode}) {
);
}

export function QuantitySelectorButton(
function Button(
props: {
children?: React.ReactNode;
variant: 'decrease' | 'increase';
Expand Down Expand Up @@ -46,8 +46,11 @@ export function QuantitySelectorButton(
);
}

export function QuantitySelectorValue(props: {children: React.ReactNode}) {
function Value(props: {children: React.ReactNode}) {
return (
<div className="min-w-[2.5rem] px-2 text-center">{props.children}</div>
);
}

QuantitySelector.Button = Button;
QuantitySelector.Value = Value;
16 changes: 6 additions & 10 deletions templates/hydrogen-theme/app/components/cart/CartLineItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ import {cx} from 'class-variance-authority';

import {useLocalePath} from '~/hooks/useLocalePath';

import {
QuantitySelector,
QuantitySelectorButton,
QuantitySelectorValue,
} from '../QuantitySelector';
import {QuantitySelector} from '../QuantitySelector';
import {IconRemove} from '../icons/IconRemove';

type OptimisticData = {
Expand Down Expand Up @@ -183,7 +179,7 @@ function CartLineQuantityAdjust({line}: {line: CartLine}) {
</label>
<QuantitySelector>
<UpdateCartForm lines={[{id: lineId, quantity: prevQuantity}]}>
<QuantitySelectorButton
<QuantitySelector.Button
disabled={optimisticQuantity <= 1}
value={prevQuantity}
variant="decrease"
Expand All @@ -192,16 +188,16 @@ function CartLineQuantityAdjust({line}: {line: CartLine}) {
data={{quantity: prevQuantity}}
id={optimisticId}
/>
</QuantitySelectorButton>
</QuantitySelector.Button>
</UpdateCartForm>
<QuantitySelectorValue>{optimisticQuantity}</QuantitySelectorValue>
<QuantitySelector.Value>{optimisticQuantity}</QuantitySelector.Value>
<UpdateCartForm lines={[{id: lineId, quantity: nextQuantity}]}>
<QuantitySelectorButton value={nextQuantity} variant="increase">
<QuantitySelector.Button value={nextQuantity} variant="increase">
<OptimisticInput
data={{quantity: nextQuantity}}
id={optimisticId}
/>
</QuantitySelectorButton>
</QuantitySelector.Button>
</UpdateCartForm>
</QuantitySelector>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ import {useEnvironmentVariables} from '~/hooks/useEnvironmentVariables';
import {useSanityThemeContent} from '~/hooks/useSanityThemeContent';
import {useSelectedVariant} from '~/hooks/useSelectedVariant';

import {
QuantitySelector,
QuantitySelectorButton,
QuantitySelectorValue,
} from '../QuantitySelector';
import {QuantitySelector} from '../QuantitySelector';
import CleanString from '../sanity/CleanString';

export function AddToCartForm(props: {
Expand All @@ -35,13 +31,13 @@ export function AddToCartForm(props: {
{showQuantitySelector && (
<div className="flex">
<QuantitySelector>
<QuantitySelectorButton
<QuantitySelector.Button
disabled={isOutOfStock || quantity === 1}
onClick={() => setQuantity(quantity - 1)}
variant="decrease"
/>
<QuantitySelectorValue>{quantity}</QuantitySelectorValue>
<QuantitySelectorButton
<QuantitySelector.Value>{quantity}</QuantitySelector.Value>
<QuantitySelector.Button
disabled={isOutOfStock}
onClick={() => setQuantity(quantity + 1)}
variant="increase"
Expand Down

0 comments on commit 19feef6

Please sign in to comment.