@wix/headless-stores 0.0.52 → 0.0.53
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/dist/astro/actions/custom-checkout.d.ts +1 -1
- package/cjs/dist/astro/actions/custom-checkout.js +2 -2
- package/cjs/dist/astro/actions/index.d.ts +1 -1
- package/cjs/dist/astro/actions/index.js +1 -1
- package/cjs/dist/enums/index.d.ts +2 -2
- package/cjs/dist/enums/index.js +2 -2
- package/cjs/dist/react/Category.d.ts +168 -67
- package/cjs/dist/react/Category.js +166 -50
- package/cjs/dist/react/CategoryList.d.ts +56 -138
- package/cjs/dist/react/CategoryList.js +44 -129
- package/cjs/dist/react/Choice.d.ts +193 -0
- package/cjs/dist/react/Choice.js +259 -0
- package/cjs/dist/react/Option.d.ts +224 -0
- package/cjs/dist/react/Option.js +388 -0
- package/cjs/dist/react/Product.d.ts +339 -96
- package/cjs/dist/react/Product.js +507 -94
- package/cjs/dist/react/{BuyNow.js → core/BuyNow.js} +2 -2
- package/cjs/dist/react/core/Category.d.ts +98 -0
- package/cjs/dist/react/core/Category.js +81 -0
- package/cjs/dist/react/core/CategoryList.d.ts +185 -0
- package/cjs/dist/react/core/CategoryList.js +174 -0
- package/{dist/react → cjs/dist/react/core}/PayNow.js +2 -2
- package/cjs/dist/react/core/Product.d.ts +148 -0
- package/cjs/dist/react/core/Product.js +126 -0
- package/cjs/dist/react/{ProductList.d.ts → core/ProductList.d.ts} +3 -3
- package/cjs/dist/react/{ProductList.js → core/ProductList.js} +10 -10
- package/{dist/react → cjs/dist/react/core}/ProductListFilters.d.ts +3 -3
- package/{dist/react → cjs/dist/react/core}/ProductListFilters.js +7 -7
- package/cjs/dist/react/{ProductListPagination.js → core/ProductListPagination.js} +8 -8
- package/{dist/react → cjs/dist/react/core}/ProductListSort.d.ts +1 -1
- package/cjs/dist/react/{ProductListSort.js → core/ProductListSort.js} +3 -3
- package/{dist/react → cjs/dist/react/core}/ProductModifiers.d.ts +1 -1
- package/{dist/react → cjs/dist/react/core}/ProductModifiers.js +13 -13
- package/{dist/react → cjs/dist/react/core}/ProductVariantSelector.d.ts +2 -2
- package/{dist/react → cjs/dist/react/core}/ProductVariantSelector.js +7 -10
- package/{dist/react → cjs/dist/react/core}/SelectedVariant.d.ts +2 -2
- package/{dist/react → cjs/dist/react/core}/SelectedVariant.js +43 -15
- package/cjs/dist/react/index.d.ts +15 -10
- package/cjs/dist/react/index.js +15 -10
- package/cjs/dist/react/types.d.ts +8 -0
- package/cjs/dist/react/types.js +9 -0
- package/cjs/dist/server-actions/custom-checkout-action.js +14 -10
- package/cjs/dist/server-actions/index.d.ts +1 -1
- package/cjs/dist/server-actions/index.js +1 -1
- package/cjs/dist/services/buy-now-service.d.ts +1 -1
- package/cjs/dist/services/buy-now-service.js +6 -6
- package/cjs/dist/services/categories-list-service.d.ts +4 -4
- package/cjs/dist/services/categories-list-service.js +10 -10
- package/cjs/dist/services/category-service.d.ts +18 -22
- package/cjs/dist/services/category-service.js +12 -10
- package/cjs/dist/services/index.d.ts +7 -7
- package/cjs/dist/services/index.js +7 -7
- package/cjs/dist/services/pay-now-service.d.ts +1 -1
- package/cjs/dist/services/pay-now-service.js +4 -4
- package/cjs/dist/services/product-modifiers-service.d.ts +3 -3
- package/cjs/dist/services/product-modifiers-service.js +7 -7
- package/cjs/dist/services/product-service.d.ts +4 -4
- package/cjs/dist/services/product-service.js +18 -18
- package/cjs/dist/services/products-list-search-service.d.ts +5 -5
- package/cjs/dist/services/products-list-search-service.js +117 -117
- package/cjs/dist/services/products-list-service.d.ts +4 -4
- package/cjs/dist/services/products-list-service.js +12 -12
- package/cjs/dist/services/selected-variant-service.d.ts +6 -2
- package/cjs/dist/services/selected-variant-service.js +86 -83
- package/cjs/dist/utils/index.d.ts +1 -0
- package/cjs/dist/utils/index.js +5 -4
- package/cjs/dist/utils/renderAsChild.d.ts +96 -0
- package/cjs/dist/utils/renderAsChild.js +66 -0
- package/cjs/dist/utils/renderChildren.d.ts +41 -0
- package/cjs/dist/utils/renderChildren.js +44 -0
- package/cjs/dist/utils/url-params.js +3 -3
- package/dist/astro/actions/custom-checkout.d.ts +1 -1
- package/dist/astro/actions/custom-checkout.js +2 -2
- package/dist/astro/actions/index.d.ts +1 -1
- package/dist/astro/actions/index.js +1 -1
- package/dist/enums/index.d.ts +2 -2
- package/dist/enums/index.js +2 -2
- package/dist/react/Category.d.ts +168 -67
- package/dist/react/Category.js +166 -50
- package/dist/react/CategoryList.d.ts +56 -138
- package/dist/react/CategoryList.js +44 -129
- package/dist/react/Choice.d.ts +193 -0
- package/dist/react/Choice.js +259 -0
- package/dist/react/Option.d.ts +224 -0
- package/dist/react/Option.js +388 -0
- package/dist/react/Product.d.ts +339 -96
- package/dist/react/Product.js +507 -94
- package/dist/react/{BuyNow.js → core/BuyNow.js} +2 -2
- package/dist/react/core/Category.d.ts +98 -0
- package/dist/react/core/Category.js +81 -0
- package/dist/react/core/CategoryList.d.ts +185 -0
- package/dist/react/core/CategoryList.js +174 -0
- package/{cjs/dist/react → dist/react/core}/PayNow.js +2 -2
- package/dist/react/core/Product.d.ts +148 -0
- package/dist/react/core/Product.js +126 -0
- package/dist/react/{ProductList.d.ts → core/ProductList.d.ts} +3 -3
- package/dist/react/{ProductList.js → core/ProductList.js} +10 -10
- package/{cjs/dist/react → dist/react/core}/ProductListFilters.d.ts +3 -3
- package/{cjs/dist/react → dist/react/core}/ProductListFilters.js +7 -7
- package/dist/react/{ProductListPagination.js → core/ProductListPagination.js} +8 -8
- package/{cjs/dist/react → dist/react/core}/ProductListSort.d.ts +1 -1
- package/dist/react/{ProductListSort.js → core/ProductListSort.js} +3 -3
- package/{cjs/dist/react → dist/react/core}/ProductModifiers.d.ts +1 -1
- package/{cjs/dist/react → dist/react/core}/ProductModifiers.js +13 -13
- package/{cjs/dist/react → dist/react/core}/ProductVariantSelector.d.ts +2 -2
- package/{cjs/dist/react → dist/react/core}/ProductVariantSelector.js +7 -10
- package/{cjs/dist/react → dist/react/core}/SelectedVariant.d.ts +2 -2
- package/{cjs/dist/react → dist/react/core}/SelectedVariant.js +43 -15
- package/dist/react/index.d.ts +15 -10
- package/dist/react/index.js +15 -10
- package/dist/react/types.d.ts +8 -0
- package/dist/react/types.js +9 -0
- package/dist/server-actions/custom-checkout-action.js +14 -10
- package/dist/server-actions/index.d.ts +1 -1
- package/dist/server-actions/index.js +1 -1
- package/dist/services/buy-now-service.d.ts +1 -1
- package/dist/services/buy-now-service.js +6 -6
- package/dist/services/categories-list-service.d.ts +4 -4
- package/dist/services/categories-list-service.js +10 -10
- package/dist/services/category-service.d.ts +18 -22
- package/dist/services/category-service.js +12 -10
- package/dist/services/index.d.ts +7 -7
- package/dist/services/index.js +7 -7
- package/dist/services/pay-now-service.d.ts +1 -1
- package/dist/services/pay-now-service.js +4 -4
- package/dist/services/product-modifiers-service.d.ts +3 -3
- package/dist/services/product-modifiers-service.js +7 -7
- package/dist/services/product-service.d.ts +4 -4
- package/dist/services/product-service.js +18 -18
- package/dist/services/products-list-search-service.d.ts +5 -5
- package/dist/services/products-list-search-service.js +117 -117
- package/dist/services/products-list-service.d.ts +4 -4
- package/dist/services/products-list-service.js +12 -12
- package/dist/services/selected-variant-service.d.ts +6 -2
- package/dist/services/selected-variant-service.js +86 -83
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +5 -4
- package/dist/utils/renderAsChild.d.ts +96 -0
- package/dist/utils/renderAsChild.js +66 -0
- package/dist/utils/renderChildren.d.ts +41 -0
- package/dist/utils/renderChildren.js +44 -0
- package/dist/utils/url-params.js +3 -3
- package/package.json +7 -3
- /package/cjs/dist/react/{BuyNow.d.ts → core/BuyNow.d.ts} +0 -0
- /package/cjs/dist/react/{PayNow.d.ts → core/PayNow.d.ts} +0 -0
- /package/cjs/dist/react/{ProductListPagination.d.ts → core/ProductListPagination.d.ts} +0 -0
- /package/dist/react/{BuyNow.d.ts → core/BuyNow.d.ts} +0 -0
- /package/dist/react/{PayNow.d.ts → core/PayNow.d.ts} +0 -0
- /package/dist/react/{ProductListPagination.d.ts → core/ProductListPagination.d.ts} +0 -0
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { renderAsChild } from '../utils/index.js';
|
|
4
|
+
import * as Choice from './Choice.js';
|
|
5
|
+
import * as ProductModifiersPrimitive from './core/ProductModifiers.js';
|
|
6
|
+
import * as ProductVariantSelectorPrimitive from './core/ProductVariantSelector.js';
|
|
7
|
+
var TestIds;
|
|
8
|
+
(function (TestIds) {
|
|
9
|
+
TestIds["optionRoot"] = "option-root";
|
|
10
|
+
TestIds["optionName"] = "option-name";
|
|
11
|
+
TestIds["optionMandatoryIndicator"] = "option-mandatory-indicator";
|
|
12
|
+
TestIds["optionChoices"] = "option-choices";
|
|
13
|
+
})(TestIds || (TestIds = {}));
|
|
14
|
+
/**
|
|
15
|
+
* Root component that provides context for variant options.
|
|
16
|
+
* Automatically detects and sets the option type based on the option name and available choices.
|
|
17
|
+
*
|
|
18
|
+
* @component
|
|
19
|
+
*
|
|
20
|
+
* **Data Attributes**
|
|
21
|
+
*
|
|
22
|
+
* - `data-testid="option-root"` - Applied to option container (TestIds.optionRoot)
|
|
23
|
+
* - `data-type` - The type of the option: 'color' | 'text' | 'free-text'
|
|
24
|
+
* @example
|
|
25
|
+
* ```tsx
|
|
26
|
+
* // Color option (data-type="color")
|
|
27
|
+
* <Option.Root
|
|
28
|
+
* option={{ name: "Color" }}
|
|
29
|
+
* onValueChange={(value) => console.log('Selected:', value)}
|
|
30
|
+
* >
|
|
31
|
+
* <Option.Name />
|
|
32
|
+
* <Option.Choices>
|
|
33
|
+
* <Option.ChoiceRepeater>
|
|
34
|
+
* <Choice.Color />
|
|
35
|
+
* </Option.ChoiceRepeater>
|
|
36
|
+
* </Option.Choices>
|
|
37
|
+
* </Option.Root>
|
|
38
|
+
*
|
|
39
|
+
* // Text option (data-type="text")
|
|
40
|
+
* <Option.Root
|
|
41
|
+
* option={{ name: "Size" }}
|
|
42
|
+
* onValueChange={(value) => console.log('Selected:', value)}
|
|
43
|
+
* >
|
|
44
|
+
* <Option.Name />
|
|
45
|
+
* <Option.Choices>
|
|
46
|
+
* <Option.ChoiceRepeater>
|
|
47
|
+
* <Choice.Text />
|
|
48
|
+
* </Option.ChoiceRepeater>
|
|
49
|
+
* </Option.Choices>
|
|
50
|
+
* </Option.Root>
|
|
51
|
+
*
|
|
52
|
+
* // Free text option (data-type="free-text")
|
|
53
|
+
* <Option.Root
|
|
54
|
+
* option={{ name: "Custom Text" }}
|
|
55
|
+
* onValueChange={(value) => console.log('Entered:', value)}
|
|
56
|
+
* >
|
|
57
|
+
* <Option.Name />
|
|
58
|
+
* <Option.Choices>
|
|
59
|
+
* <Option.ChoiceRepeater>
|
|
60
|
+
* <Choice.FreeText />
|
|
61
|
+
* </Option.ChoiceRepeater>
|
|
62
|
+
* </Option.Choices>
|
|
63
|
+
* </Option.Root>
|
|
64
|
+
*
|
|
65
|
+
* // With allowed types filter
|
|
66
|
+
* <Option.Root
|
|
67
|
+
* option={{ name: "Size" }}
|
|
68
|
+
* allowedTypes={['text']}
|
|
69
|
+
* onValueChange={(value) => console.log('Selected size:', value)}
|
|
70
|
+
* >
|
|
71
|
+
* <Option.Name />
|
|
72
|
+
* <Option.Choices>
|
|
73
|
+
* <Option.ChoiceRepeater>
|
|
74
|
+
* <Choice.Text />
|
|
75
|
+
* </Option.ChoiceRepeater>
|
|
76
|
+
* </Option.Choices>
|
|
77
|
+
* </Option.Root>
|
|
78
|
+
*
|
|
79
|
+
* // asChild with react component
|
|
80
|
+
* <Option.Root
|
|
81
|
+
* asChild
|
|
82
|
+
* option={{ name: "Color" }}
|
|
83
|
+
* onValueChange={(value) => console.log('Selected:', value)}
|
|
84
|
+
* >
|
|
85
|
+
* {React.forwardRef(({option, onValueChange, allowedTypes, ...props}, ref) => (
|
|
86
|
+
* <section ref={ref} {...props} className="option-section">
|
|
87
|
+
* <h3>Option: {option.name}</h3>
|
|
88
|
+
* <p>Allowed types: {allowedTypes.join(', ')}</p>
|
|
89
|
+
* {props.children}
|
|
90
|
+
* </section>
|
|
91
|
+
* ))}
|
|
92
|
+
* </Option.Root>
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
export const Root = React.forwardRef((props, ref) => {
|
|
96
|
+
const { asChild, children, option, onValueChange, allowedTypes } = props;
|
|
97
|
+
// Determine the option type based on the option name and available choices
|
|
98
|
+
const getOptionType = () => {
|
|
99
|
+
if (option.type === 'FREE_TEXT') {
|
|
100
|
+
return 'free-text';
|
|
101
|
+
}
|
|
102
|
+
if (option.hasChoices &&
|
|
103
|
+
option.choices?.some((choice) => choice.colorCode)) {
|
|
104
|
+
return 'color';
|
|
105
|
+
}
|
|
106
|
+
return 'text';
|
|
107
|
+
};
|
|
108
|
+
const optionType = getOptionType();
|
|
109
|
+
const contextValue = {
|
|
110
|
+
...option,
|
|
111
|
+
optionType,
|
|
112
|
+
onValueChange,
|
|
113
|
+
allowedTypes,
|
|
114
|
+
mandatory: option?.mandatory || false,
|
|
115
|
+
};
|
|
116
|
+
const attributes = {
|
|
117
|
+
'data-testid': TestIds.optionRoot,
|
|
118
|
+
'data-type': optionType,
|
|
119
|
+
};
|
|
120
|
+
const content = (_jsx(OptionContext.Provider, { value: contextValue, children: typeof children === 'function' ? null : children }));
|
|
121
|
+
if (asChild) {
|
|
122
|
+
const rendered = renderAsChild({
|
|
123
|
+
children,
|
|
124
|
+
props: { option, onValueChange, allowedTypes },
|
|
125
|
+
ref,
|
|
126
|
+
content,
|
|
127
|
+
attributes,
|
|
128
|
+
});
|
|
129
|
+
if (rendered)
|
|
130
|
+
return rendered;
|
|
131
|
+
}
|
|
132
|
+
return (_jsx("div", { ...attributes, ref: ref, children: content }));
|
|
133
|
+
});
|
|
134
|
+
// Create a context to pass option data down
|
|
135
|
+
export const OptionContext = React.createContext(null);
|
|
136
|
+
/**
|
|
137
|
+
* Displays the option name.
|
|
138
|
+
* Follows true Radix pattern - use separate components for each styleable element.
|
|
139
|
+
*
|
|
140
|
+
* @component
|
|
141
|
+
* @example
|
|
142
|
+
* ```tsx
|
|
143
|
+
* // True Radix pattern - separate components
|
|
144
|
+
* <div className="flex items-center gap-1">
|
|
145
|
+
* <Option.Name className="text-lg font-medium" />
|
|
146
|
+
* <Option.MandatoryIndicator className="text-red-500" />
|
|
147
|
+
* </div>
|
|
148
|
+
*
|
|
149
|
+
* // Custom rendering
|
|
150
|
+
* <Option.Name asChild>
|
|
151
|
+
* {React.forwardRef<HTMLElement, { name: string }>(({ name, ...props }, ref) => (
|
|
152
|
+
* <h4 ref={ref} {...props} className="text-lg font-medium text-content-primary">
|
|
153
|
+
* {name}
|
|
154
|
+
* </h4>
|
|
155
|
+
* ))}
|
|
156
|
+
* </Option.Name>
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
export const Name = React.forwardRef((props, ref) => {
|
|
160
|
+
const { asChild, children, className } = props;
|
|
161
|
+
const optionData = React.useContext(OptionContext);
|
|
162
|
+
if (!optionData)
|
|
163
|
+
return null;
|
|
164
|
+
const name = optionData.name || '';
|
|
165
|
+
const attributes = {
|
|
166
|
+
'data-testid': TestIds.optionName,
|
|
167
|
+
};
|
|
168
|
+
if (asChild) {
|
|
169
|
+
const rendered = renderAsChild({
|
|
170
|
+
children,
|
|
171
|
+
props: { name },
|
|
172
|
+
ref,
|
|
173
|
+
content: name,
|
|
174
|
+
attributes,
|
|
175
|
+
});
|
|
176
|
+
if (rendered)
|
|
177
|
+
return rendered;
|
|
178
|
+
}
|
|
179
|
+
return (_jsx("div", { className: className, ...attributes, ref: ref, children: name }));
|
|
180
|
+
});
|
|
181
|
+
/**
|
|
182
|
+
* Displays the mandatory indicator (asterisk) when the option is required.
|
|
183
|
+
* Follows true Radix pattern - separate component for each styleable element.
|
|
184
|
+
*
|
|
185
|
+
* @component
|
|
186
|
+
* @example
|
|
187
|
+
* ```tsx
|
|
188
|
+
* // Default usage - only renders if mandatory
|
|
189
|
+
* <Option.MandatoryIndicator className="text-red-500 ml-1" />
|
|
190
|
+
*
|
|
191
|
+
* // Custom rendering
|
|
192
|
+
* <Option.MandatoryIndicator asChild>
|
|
193
|
+
* {React.forwardRef<HTMLElement, { mandatory: boolean }>(({ mandatory, ...props }, ref) => (
|
|
194
|
+
* mandatory ? <span ref={ref} {...props} className="text-red-500">*</span> : null
|
|
195
|
+
* ))}
|
|
196
|
+
* </Option.MandatoryIndicator>
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
export const MandatoryIndicator = React.forwardRef((props, ref) => {
|
|
200
|
+
const { asChild, children, className } = props;
|
|
201
|
+
const optionData = React.useContext(OptionContext);
|
|
202
|
+
if (!optionData)
|
|
203
|
+
return null;
|
|
204
|
+
const mandatory = optionData.mandatory || false;
|
|
205
|
+
// Don't render anything if not mandatory
|
|
206
|
+
if (!mandatory)
|
|
207
|
+
return null;
|
|
208
|
+
const attributes = {
|
|
209
|
+
'data-testid': TestIds.optionMandatoryIndicator,
|
|
210
|
+
};
|
|
211
|
+
if (asChild) {
|
|
212
|
+
const rendered = renderAsChild({
|
|
213
|
+
children,
|
|
214
|
+
props: { mandatory },
|
|
215
|
+
ref,
|
|
216
|
+
content: '*',
|
|
217
|
+
attributes,
|
|
218
|
+
});
|
|
219
|
+
if (rendered)
|
|
220
|
+
return rendered;
|
|
221
|
+
}
|
|
222
|
+
return (_jsx("span", { className: className, ...attributes, ref: ref, children: "*" }));
|
|
223
|
+
});
|
|
224
|
+
/**
|
|
225
|
+
* Container for the list items with empty state support.
|
|
226
|
+
* Follows List Container Level pattern (inspired by Product.VariantOptions).
|
|
227
|
+
*
|
|
228
|
+
* @component
|
|
229
|
+
*
|
|
230
|
+
* **Data Attributes**
|
|
231
|
+
*
|
|
232
|
+
* - `data-testid="option-choices"` - Applied to choices container (TestIds.optionChoices)
|
|
233
|
+
* - `data-type` - The type of the option 'color' | 'text' | 'free-text'
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* ```tsx
|
|
237
|
+
* // Default usage with empty state
|
|
238
|
+
* <Option.Choices emptyState={<div>No choices available</div>}>
|
|
239
|
+
* <Option.ChoiceRepeater>
|
|
240
|
+
* <Choice.Text />
|
|
241
|
+
* <Choice.Color />
|
|
242
|
+
* </Option.ChoiceRepeater>
|
|
243
|
+
* </Option.Choices>
|
|
244
|
+
*
|
|
245
|
+
* // Simple container usage
|
|
246
|
+
* <Option.Choices emptyState={<div>No choices</div>}>
|
|
247
|
+
* <div className="choices-grid">
|
|
248
|
+
* <Option.ChoiceRepeater>
|
|
249
|
+
* <Choice.Text />
|
|
250
|
+
* </Option.ChoiceRepeater>
|
|
251
|
+
* </div>
|
|
252
|
+
* </Option.Choices>
|
|
253
|
+
* ```
|
|
254
|
+
*/
|
|
255
|
+
export const Choices = React.forwardRef((props, ref) => {
|
|
256
|
+
const { children, emptyState } = props;
|
|
257
|
+
const optionData = React.useContext(OptionContext);
|
|
258
|
+
if (!optionData)
|
|
259
|
+
return null;
|
|
260
|
+
// Check if we have choices to render (List Container Level pattern)
|
|
261
|
+
const hasChoicesOrFreeText = (optionData.choices && optionData.choices.length > 0) ||
|
|
262
|
+
optionData.optionType === 'free-text';
|
|
263
|
+
if (!hasChoicesOrFreeText) {
|
|
264
|
+
return emptyState || null;
|
|
265
|
+
}
|
|
266
|
+
const attributes = {
|
|
267
|
+
'data-testid': TestIds.optionChoices,
|
|
268
|
+
'data-type': optionData.optionType || 'text',
|
|
269
|
+
};
|
|
270
|
+
return (_jsx("div", { ...attributes, ref: ref, children: children }));
|
|
271
|
+
});
|
|
272
|
+
/**
|
|
273
|
+
* Repeater component that renders Choice.Root for each choice.
|
|
274
|
+
* Inspired by Product.VariantOptionRepeater pattern.
|
|
275
|
+
*
|
|
276
|
+
* Now handles ProductVariantSelector.Choice logic and provides ChoiceContext.
|
|
277
|
+
*
|
|
278
|
+
* @component
|
|
279
|
+
*
|
|
280
|
+
* @example
|
|
281
|
+
* ```tsx
|
|
282
|
+
* <Option.ChoiceRepeater>
|
|
283
|
+
* <Choice.Text />
|
|
284
|
+
* <Choice.Color />
|
|
285
|
+
* <Choice.FreeText />
|
|
286
|
+
* </Option.ChoiceRepeater>
|
|
287
|
+
* ```
|
|
288
|
+
*/
|
|
289
|
+
export const ChoiceRepeater = React.forwardRef((props, _ref) => {
|
|
290
|
+
const { children } = props;
|
|
291
|
+
const optionData = React.useContext(OptionContext);
|
|
292
|
+
if (!optionData ||
|
|
293
|
+
(!optionData.choices?.length && optionData.optionType !== 'free-text'))
|
|
294
|
+
return null;
|
|
295
|
+
const onValueChange = optionData.onValueChange || (() => { });
|
|
296
|
+
const isModifier = optionData.type;
|
|
297
|
+
const allowedTypes = optionData.allowedTypes || [
|
|
298
|
+
'color',
|
|
299
|
+
'text',
|
|
300
|
+
'free-text',
|
|
301
|
+
];
|
|
302
|
+
if (optionData.optionType === 'free-text') {
|
|
303
|
+
// Check if free-text is allowed
|
|
304
|
+
if (!allowedTypes.includes('free-text')) {
|
|
305
|
+
return null;
|
|
306
|
+
}
|
|
307
|
+
const choice = {
|
|
308
|
+
...optionData,
|
|
309
|
+
type: 'free-text',
|
|
310
|
+
};
|
|
311
|
+
return (_jsx(ProductModifiersPrimitive.Choice, { modifier: optionData, choice: choice, children: (renderProps) => {
|
|
312
|
+
const { value, isSelected, select } = renderProps;
|
|
313
|
+
const contextValue = {
|
|
314
|
+
choice,
|
|
315
|
+
onValueChange,
|
|
316
|
+
isSelected,
|
|
317
|
+
isVisible: true,
|
|
318
|
+
isInStock: true,
|
|
319
|
+
isPreOrderEnabled: true,
|
|
320
|
+
shouldRenderAsColor: false,
|
|
321
|
+
shouldRenderAsText: false,
|
|
322
|
+
shouldRenderAsFreeText: true,
|
|
323
|
+
select,
|
|
324
|
+
value,
|
|
325
|
+
};
|
|
326
|
+
return (_jsx(Choice.ChoiceContext.Provider, { value: contextValue, children: _jsx(Choice.Root, { children: children }) }));
|
|
327
|
+
} }));
|
|
328
|
+
}
|
|
329
|
+
return (_jsx(_Fragment, { children: optionData.choices.map((choice) => {
|
|
330
|
+
const choiceKey = choice.choiceId;
|
|
331
|
+
const getChoiceType = () => {
|
|
332
|
+
if (choice?.choiceType === 'ONE_COLOR')
|
|
333
|
+
return 'color';
|
|
334
|
+
if (choice?.choiceType === 'CHOICE_TEXT')
|
|
335
|
+
return 'text';
|
|
336
|
+
return 'text';
|
|
337
|
+
};
|
|
338
|
+
const choiceType = getChoiceType();
|
|
339
|
+
if (!allowedTypes.includes(choiceType)) {
|
|
340
|
+
return null;
|
|
341
|
+
}
|
|
342
|
+
const choiceData = {
|
|
343
|
+
...choice,
|
|
344
|
+
type: choiceType,
|
|
345
|
+
};
|
|
346
|
+
if (isModifier) {
|
|
347
|
+
return (_jsx(ProductModifiersPrimitive.Choice, { modifier: optionData, choice: choiceData, children: (renderProps) => {
|
|
348
|
+
const { value, isSelected, select } = renderProps;
|
|
349
|
+
const contextValue = {
|
|
350
|
+
choice: choiceData,
|
|
351
|
+
onValueChange,
|
|
352
|
+
shouldRenderAsColor: choiceType === 'color',
|
|
353
|
+
shouldRenderAsText: choiceType === 'text',
|
|
354
|
+
shouldRenderAsFreeText: choiceType === 'free-text',
|
|
355
|
+
isSelected,
|
|
356
|
+
isVisible: true, // ProductModifiers doesn't provide visibility
|
|
357
|
+
isInStock: true, // ProductModifiers doesn't provide stock info
|
|
358
|
+
isPreOrderEnabled: true, // ProductModifiers doesn't provide pre-order info
|
|
359
|
+
select,
|
|
360
|
+
value,
|
|
361
|
+
};
|
|
362
|
+
return (_jsx(Choice.ChoiceContext.Provider, { value: contextValue, children: _jsx(Choice.Root, { children: children }) }));
|
|
363
|
+
} }, choiceKey));
|
|
364
|
+
}
|
|
365
|
+
else {
|
|
366
|
+
return (_jsx(ProductVariantSelectorPrimitive.Choice, { option: optionData, choice: choiceData, children: (renderProps) => {
|
|
367
|
+
const { value, isSelected, select, isVisible, isInStock, isPreOrderEnabled, } = renderProps;
|
|
368
|
+
// Don't render if not visible
|
|
369
|
+
if (!isVisible)
|
|
370
|
+
return null;
|
|
371
|
+
const contextValue = {
|
|
372
|
+
choice: choiceData,
|
|
373
|
+
onValueChange,
|
|
374
|
+
shouldRenderAsColor: choiceType === 'color',
|
|
375
|
+
shouldRenderAsText: choiceType === 'text',
|
|
376
|
+
shouldRenderAsFreeText: choiceType === 'free-text',
|
|
377
|
+
isSelected,
|
|
378
|
+
isVisible,
|
|
379
|
+
isInStock,
|
|
380
|
+
isPreOrderEnabled,
|
|
381
|
+
select,
|
|
382
|
+
value,
|
|
383
|
+
};
|
|
384
|
+
return (_jsx(Choice.ChoiceContext.Provider, { value: contextValue, children: _jsx(Choice.Root, { children: children }) }));
|
|
385
|
+
} }, choiceKey));
|
|
386
|
+
}
|
|
387
|
+
}) }));
|
|
388
|
+
});
|