Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variant selector refactor #2099

Merged
merged 6 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions packages/hydrogen/src/product/VariantSelector.example.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ const ProductForm = ({product}) => {
<>
<div>{option.name}</div>
<div>
{option.values.map(({value, isAvailable, to, isActive}) => (
<Link
to={to}
prefetch="intent"
className={
isActive ? 'active' : isAvailable ? '' : 'opacity-80'
}
>
{value}
</Link>
))}
{option.values.map(
({value, isAvailable, to, isActive, variant}) => (
<Link
to={to}
prefetch="intent"
className={
isActive ? 'active' : isAvailable ? '' : 'opacity-80'
}
>
{value}
<br />
{variant && `SKU: ${variant.sku}`}
</Link>
),
)}
</div>
</>
)}
Expand Down
26 changes: 15 additions & 11 deletions packages/hydrogen/src/product/VariantSelector.example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@ const ProductForm = ({product}: {product: Product}) => {
<>
<div>{option.name}</div>
<div>
{option.values.map(({value, isAvailable, to, isActive}) => (
<Link
to={to}
prefetch="intent"
className={
isActive ? 'active' : isAvailable ? '' : 'opacity-80'
}
>
{value}
</Link>
))}
{option.values.map(
({value, isAvailable, to, isActive, variant}) => (
<Link
to={to}
prefetch="intent"
className={
isActive ? 'active' : isAvailable ? '' : 'opacity-80'
}
>
{value}
<br />
{variant && `SKU: ${variant.sku}`}
</Link>
),
)}
</div>
</>
)}
Expand Down
64 changes: 64 additions & 0 deletions packages/hydrogen/src/product/VariantSelector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,4 +488,68 @@ describe('<VariantSelector>', () => {
</DocumentFragment>
`);
});

it('returns variant in option values', () => {
const {asFragment} = render(
createElement(VariantSelector, {
handle: 'snowboard',
options: [{name: 'Size', values: ['S', 'M']}],
variants: {
nodes: [
{
availableForSale: true,
sku: 'ABC-01234',
selectedOptions: [{name: 'Size', value: 'S'}],
} as ProductVariant,
{
availableForSale: true,
sku: 'XYZ-56789',
selectedOptions: [{name: 'Size', value: 'M'}],
} as ProductVariant,
],
},
children: ({option}) =>
createElement(
'div',
null,
option.values.map(({value, to, isAvailable, variant}) =>
createElement(
'a',
{
key: option.name + value,
href: to,
className: isAvailable ? 'available' : 'unavailable',
},
value,
createElement('br', null),
variant && `SKU: ${variant?.sku}`,
),
),
),
}),
);

expect(asFragment()).toMatchInlineSnapshot(`
<DocumentFragment>
<div>
<a
class="available"
href="/products/snowboard?Size=S"
>
S
<br />
SKU: ABC-01234
</a>
<a
class="available"
href="/products/snowboard?Size=M"
>
M
<br />
SKU: XYZ-56789
</a>
</div>
</DocumentFragment>
`);
});
});
2 changes: 2 additions & 0 deletions packages/hydrogen/src/product/VariantSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type VariantOptionValue = {
to: string;
search: string;
isActive: boolean;
variant?: PartialDeep<ProductVariant>;
};

type VariantSelectorProps = {
Expand Down Expand Up @@ -115,6 +116,7 @@ export function VariantSelector({
to: path + searchString,
search: searchString,
isActive: calculatedActiveValue,
variant,
});
}

Expand Down
Loading