create-brainerce-store 1.15.0 → 1.15.1
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/package.json
CHANGED
|
@@ -189,147 +189,159 @@ export function CheckoutForm({
|
|
|
189
189
|
|
|
190
190
|
{!emailOnly && (
|
|
191
191
|
<>
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
{
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
192
|
+
{/* Country + Region row */}
|
|
193
|
+
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
|
194
|
+
<div>
|
|
195
|
+
<label htmlFor="country" className="text-foreground mb-1 block text-sm font-medium">
|
|
196
|
+
{t('country')} <span className="text-destructive">*</span>
|
|
197
|
+
</label>
|
|
198
|
+
{hasCountryOptions ? (
|
|
199
|
+
<select
|
|
200
|
+
id="country"
|
|
201
|
+
value={formData.country}
|
|
202
|
+
onChange={(e) => updateField('country', e.target.value)}
|
|
203
|
+
className={cn(
|
|
204
|
+
selectClass,
|
|
205
|
+
errors.country ? 'border-destructive' : 'border-border'
|
|
206
|
+
)}
|
|
207
|
+
>
|
|
208
|
+
<option value="">{t('selectCountry')}</option>
|
|
209
|
+
{destinations.countries.map((c) => (
|
|
210
|
+
<option key={c.code} value={c.code}>
|
|
211
|
+
{c.name}
|
|
212
|
+
</option>
|
|
213
|
+
))}
|
|
214
|
+
</select>
|
|
215
|
+
) : (
|
|
216
|
+
<input
|
|
217
|
+
id="country"
|
|
218
|
+
type="text"
|
|
219
|
+
value={formData.country}
|
|
220
|
+
onChange={(e) => updateField('country', e.target.value)}
|
|
221
|
+
className={cn(
|
|
222
|
+
inputClass,
|
|
223
|
+
errors.country ? 'border-destructive' : 'border-border'
|
|
224
|
+
)}
|
|
225
|
+
placeholder={t('countryPlaceholder')}
|
|
226
|
+
/>
|
|
227
|
+
)}
|
|
228
|
+
{errors.country && <p className="text-destructive mt-1 text-xs">{errors.country}</p>}
|
|
229
|
+
</div>
|
|
230
|
+
|
|
231
|
+
<div>
|
|
232
|
+
<label htmlFor="region" className="text-foreground mb-1 block text-sm font-medium">
|
|
233
|
+
{t('stateRegion')}
|
|
234
|
+
</label>
|
|
235
|
+
{hasRegionOptions ? (
|
|
236
|
+
<select
|
|
237
|
+
id="region"
|
|
238
|
+
value={formData.region || ''}
|
|
239
|
+
onChange={(e) => updateField('region', e.target.value)}
|
|
240
|
+
className={cn(selectClass, 'border-border')}
|
|
241
|
+
>
|
|
242
|
+
<option value="">{t('selectRegion')}</option>
|
|
243
|
+
{countryRegions.map((r) => (
|
|
244
|
+
<option key={r.code} value={r.code}>
|
|
245
|
+
{r.name}
|
|
246
|
+
</option>
|
|
247
|
+
))}
|
|
248
|
+
</select>
|
|
249
|
+
) : (
|
|
250
|
+
<input
|
|
251
|
+
id="region"
|
|
252
|
+
type="text"
|
|
253
|
+
value={formData.region || ''}
|
|
254
|
+
onChange={(e) => updateField('region', e.target.value)}
|
|
255
|
+
className={cn(inputClass, 'border-border')}
|
|
256
|
+
/>
|
|
257
|
+
)}
|
|
258
|
+
</div>
|
|
259
|
+
</div>
|
|
260
|
+
|
|
261
|
+
{/* Address line 1 */}
|
|
262
|
+
<div>
|
|
263
|
+
<label htmlFor="line1" className="text-foreground mb-1 block text-sm font-medium">
|
|
264
|
+
{t('address')} <span className="text-destructive">*</span>
|
|
265
|
+
</label>
|
|
213
266
|
<input
|
|
214
|
-
id="
|
|
267
|
+
id="line1"
|
|
215
268
|
type="text"
|
|
216
|
-
value={formData.
|
|
217
|
-
onChange={(e) => updateField('
|
|
218
|
-
className={cn(inputClass, errors.
|
|
219
|
-
placeholder={t('
|
|
269
|
+
value={formData.line1}
|
|
270
|
+
onChange={(e) => updateField('line1', e.target.value)}
|
|
271
|
+
className={cn(inputClass, errors.line1 ? 'border-destructive' : 'border-border')}
|
|
272
|
+
placeholder={t('streetAddress')}
|
|
220
273
|
/>
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
</div>
|
|
274
|
+
{errors.line1 && <p className="text-destructive mt-1 text-xs">{errors.line1}</p>}
|
|
275
|
+
</div>
|
|
224
276
|
|
|
225
|
-
|
|
226
|
-
<
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
<select
|
|
231
|
-
id="region"
|
|
232
|
-
value={formData.region || ''}
|
|
233
|
-
onChange={(e) => updateField('region', e.target.value)}
|
|
234
|
-
className={cn(selectClass, 'border-border')}
|
|
235
|
-
>
|
|
236
|
-
<option value="">{t('selectRegion')}</option>
|
|
237
|
-
{countryRegions.map((r) => (
|
|
238
|
-
<option key={r.code} value={r.code}>
|
|
239
|
-
{r.name}
|
|
240
|
-
</option>
|
|
241
|
-
))}
|
|
242
|
-
</select>
|
|
243
|
-
) : (
|
|
277
|
+
{/* Address line 2 */}
|
|
278
|
+
<div>
|
|
279
|
+
<label htmlFor="line2" className="text-foreground mb-1 block text-sm font-medium">
|
|
280
|
+
{t('apartmentSuite')}
|
|
281
|
+
</label>
|
|
244
282
|
<input
|
|
245
|
-
id="
|
|
283
|
+
id="line2"
|
|
246
284
|
type="text"
|
|
247
|
-
value={formData.
|
|
248
|
-
onChange={(e) => updateField('
|
|
285
|
+
value={formData.line2 || ''}
|
|
286
|
+
onChange={(e) => updateField('line2', e.target.value)}
|
|
249
287
|
className={cn(inputClass, 'border-border')}
|
|
288
|
+
placeholder={t('aptPlaceholder')}
|
|
250
289
|
/>
|
|
251
|
-
|
|
252
|
-
</div>
|
|
253
|
-
</div>
|
|
254
|
-
|
|
255
|
-
{/* Address line 1 */}
|
|
256
|
-
<div>
|
|
257
|
-
<label htmlFor="line1" className="text-foreground mb-1 block text-sm font-medium">
|
|
258
|
-
{t('address')} <span className="text-destructive">*</span>
|
|
259
|
-
</label>
|
|
260
|
-
<input
|
|
261
|
-
id="line1"
|
|
262
|
-
type="text"
|
|
263
|
-
value={formData.line1}
|
|
264
|
-
onChange={(e) => updateField('line1', e.target.value)}
|
|
265
|
-
className={cn(inputClass, errors.line1 ? 'border-destructive' : 'border-border')}
|
|
266
|
-
placeholder={t('streetAddress')}
|
|
267
|
-
/>
|
|
268
|
-
{errors.line1 && <p className="text-destructive mt-1 text-xs">{errors.line1}</p>}
|
|
269
|
-
</div>
|
|
270
|
-
|
|
271
|
-
{/* Address line 2 */}
|
|
272
|
-
<div>
|
|
273
|
-
<label htmlFor="line2" className="text-foreground mb-1 block text-sm font-medium">
|
|
274
|
-
{t('apartmentSuite')}
|
|
275
|
-
</label>
|
|
276
|
-
<input
|
|
277
|
-
id="line2"
|
|
278
|
-
type="text"
|
|
279
|
-
value={formData.line2 || ''}
|
|
280
|
-
onChange={(e) => updateField('line2', e.target.value)}
|
|
281
|
-
className={cn(inputClass, 'border-border')}
|
|
282
|
-
placeholder={t('aptPlaceholder')}
|
|
283
|
-
/>
|
|
284
|
-
</div>
|
|
290
|
+
</div>
|
|
285
291
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
292
|
+
{/* City + Postal code row */}
|
|
293
|
+
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
|
294
|
+
<div>
|
|
295
|
+
<label htmlFor="city" className="text-foreground mb-1 block text-sm font-medium">
|
|
296
|
+
{t('city')} <span className="text-destructive">*</span>
|
|
297
|
+
</label>
|
|
298
|
+
<input
|
|
299
|
+
id="city"
|
|
300
|
+
type="text"
|
|
301
|
+
value={formData.city}
|
|
302
|
+
onChange={(e) => updateField('city', e.target.value)}
|
|
303
|
+
className={cn(inputClass, errors.city ? 'border-destructive' : 'border-border')}
|
|
304
|
+
/>
|
|
305
|
+
{errors.city && <p className="text-destructive mt-1 text-xs">{errors.city}</p>}
|
|
306
|
+
</div>
|
|
301
307
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
308
|
+
<div>
|
|
309
|
+
<label
|
|
310
|
+
htmlFor="postalCode"
|
|
311
|
+
className="text-foreground mb-1 block text-sm font-medium"
|
|
312
|
+
>
|
|
313
|
+
{t('postalCode')} <span className="text-destructive">*</span>
|
|
314
|
+
</label>
|
|
315
|
+
<input
|
|
316
|
+
id="postalCode"
|
|
317
|
+
type="text"
|
|
318
|
+
value={formData.postalCode}
|
|
319
|
+
onChange={(e) => updateField('postalCode', e.target.value)}
|
|
320
|
+
className={cn(
|
|
321
|
+
inputClass,
|
|
322
|
+
errors.postalCode ? 'border-destructive' : 'border-border'
|
|
323
|
+
)}
|
|
324
|
+
/>
|
|
325
|
+
{errors.postalCode && (
|
|
326
|
+
<p className="text-destructive mt-1 text-xs">{errors.postalCode}</p>
|
|
327
|
+
)}
|
|
328
|
+
</div>
|
|
329
|
+
</div>
|
|
318
330
|
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
331
|
+
{/* Phone */}
|
|
332
|
+
<div>
|
|
333
|
+
<label htmlFor="phone" className="text-foreground mb-1 block text-sm font-medium">
|
|
334
|
+
{t('phone')}
|
|
335
|
+
</label>
|
|
336
|
+
<input
|
|
337
|
+
id="phone"
|
|
338
|
+
type="tel"
|
|
339
|
+
value={formData.phone || ''}
|
|
340
|
+
onChange={(e) => updateField('phone', e.target.value)}
|
|
341
|
+
className={cn(inputClass, 'border-border')}
|
|
342
|
+
placeholder={t('phonePlaceholder')}
|
|
343
|
+
/>
|
|
344
|
+
</div>
|
|
333
345
|
</>
|
|
334
346
|
)}
|
|
335
347
|
|
|
@@ -1,147 +1,277 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
|
|
3
|
-
import { useMemo } from 'react';
|
|
4
|
-
import type { Product, ProductVariant } from 'brainerce';
|
|
5
|
-
import { getVariantOptions, getStockStatus, formatPrice } from 'brainerce';
|
|
6
|
-
import { useStoreInfo } from '@/providers/store-provider';
|
|
7
|
-
import { cn } from '@/lib/utils';
|
|
8
|
-
|
|
9
|
-
interface VariantSelectorProps {
|
|
10
|
-
product: Product;
|
|
11
|
-
selectedVariant: ProductVariant | null;
|
|
12
|
-
onVariantChange: (variant: ProductVariant) => void;
|
|
13
|
-
className?: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
interface AttributeGroup {
|
|
17
|
-
name: string;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
<
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useMemo } from 'react';
|
|
4
|
+
import type { Product, ProductVariant } from 'brainerce';
|
|
5
|
+
import { getVariantOptions, getProductSwatches, getStockStatus, formatPrice } from 'brainerce';
|
|
6
|
+
import { useStoreInfo } from '@/providers/store-provider';
|
|
7
|
+
import { cn } from '@/lib/utils';
|
|
8
|
+
|
|
9
|
+
interface VariantSelectorProps {
|
|
10
|
+
product: Product;
|
|
11
|
+
selectedVariant: ProductVariant | null;
|
|
12
|
+
onVariantChange: (variant: ProductVariant) => void;
|
|
13
|
+
className?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface AttributeGroup {
|
|
17
|
+
name: string;
|
|
18
|
+
displayType: string;
|
|
19
|
+
values: Array<{
|
|
20
|
+
value: string;
|
|
21
|
+
swatchColor?: string | null;
|
|
22
|
+
swatchColor2?: string | null;
|
|
23
|
+
swatchImageUrl?: string | null;
|
|
24
|
+
variants: ProductVariant[];
|
|
25
|
+
}>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function VariantSelector({
|
|
29
|
+
product,
|
|
30
|
+
selectedVariant,
|
|
31
|
+
onVariantChange,
|
|
32
|
+
className,
|
|
33
|
+
}: VariantSelectorProps) {
|
|
34
|
+
const { storeInfo } = useStoreInfo();
|
|
35
|
+
const currency = storeInfo?.currency || 'USD';
|
|
36
|
+
const variants = useMemo(() => product.variants || [], [product.variants]);
|
|
37
|
+
|
|
38
|
+
// Get swatch metadata from product attribute options
|
|
39
|
+
const swatchData = useMemo(() => getProductSwatches(product), [product]);
|
|
40
|
+
const swatchMap = useMemo(() => {
|
|
41
|
+
const map = new Map<
|
|
42
|
+
string,
|
|
43
|
+
{
|
|
44
|
+
displayType: string;
|
|
45
|
+
options: Map<
|
|
46
|
+
string,
|
|
47
|
+
{
|
|
48
|
+
swatchColor?: string | null;
|
|
49
|
+
swatchColor2?: string | null;
|
|
50
|
+
swatchImageUrl?: string | null;
|
|
51
|
+
}
|
|
52
|
+
>;
|
|
53
|
+
}
|
|
54
|
+
>();
|
|
55
|
+
for (const attr of swatchData) {
|
|
56
|
+
const optMap = new Map<
|
|
57
|
+
string,
|
|
58
|
+
{
|
|
59
|
+
swatchColor?: string | null;
|
|
60
|
+
swatchColor2?: string | null;
|
|
61
|
+
swatchImageUrl?: string | null;
|
|
62
|
+
}
|
|
63
|
+
>();
|
|
64
|
+
for (const opt of attr.options) {
|
|
65
|
+
optMap.set(opt.name, {
|
|
66
|
+
swatchColor: opt.swatchColor,
|
|
67
|
+
swatchColor2: opt.swatchColor2,
|
|
68
|
+
swatchImageUrl: opt.swatchImageUrl,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
map.set(attr.attributeName, { displayType: attr.displayType, options: optMap });
|
|
72
|
+
}
|
|
73
|
+
return map;
|
|
74
|
+
}, [swatchData]);
|
|
75
|
+
|
|
76
|
+
// Build attribute groups from variant data, enriched with swatch info
|
|
77
|
+
const attributeGroups = useMemo<AttributeGroup[]>(() => {
|
|
78
|
+
const groups = new Map<string, Map<string, ProductVariant[]>>();
|
|
79
|
+
|
|
80
|
+
for (const variant of variants) {
|
|
81
|
+
const options = getVariantOptions(variant);
|
|
82
|
+
for (const { name, value } of options) {
|
|
83
|
+
if (!groups.has(name)) {
|
|
84
|
+
groups.set(name, new Map());
|
|
85
|
+
}
|
|
86
|
+
const valuesMap = groups.get(name)!;
|
|
87
|
+
if (!valuesMap.has(value)) {
|
|
88
|
+
valuesMap.set(value, []);
|
|
89
|
+
}
|
|
90
|
+
valuesMap.get(value)!.push(variant);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return Array.from(groups.entries()).map(([name, valuesMap]) => {
|
|
95
|
+
const attrSwatch = swatchMap.get(name);
|
|
96
|
+
return {
|
|
97
|
+
name,
|
|
98
|
+
displayType: attrSwatch?.displayType || 'DROPDOWN',
|
|
99
|
+
values: Array.from(valuesMap.entries()).map(([value, variantList]) => {
|
|
100
|
+
const optSwatch = attrSwatch?.options.get(value);
|
|
101
|
+
return {
|
|
102
|
+
value,
|
|
103
|
+
swatchColor: optSwatch?.swatchColor,
|
|
104
|
+
swatchColor2: optSwatch?.swatchColor2,
|
|
105
|
+
swatchImageUrl: optSwatch?.swatchImageUrl,
|
|
106
|
+
variants: variantList,
|
|
107
|
+
};
|
|
108
|
+
}),
|
|
109
|
+
};
|
|
110
|
+
});
|
|
111
|
+
}, [variants, swatchMap]);
|
|
112
|
+
|
|
113
|
+
// Get currently selected attribute values
|
|
114
|
+
const selectedOptions = useMemo(() => {
|
|
115
|
+
if (!selectedVariant) return new Map<string, string>();
|
|
116
|
+
const opts = getVariantOptions(selectedVariant);
|
|
117
|
+
return new Map(opts.map(({ name, value }) => [name, value]));
|
|
118
|
+
}, [selectedVariant]);
|
|
119
|
+
|
|
120
|
+
// Find the variant that matches all selected attributes
|
|
121
|
+
function findMatchingVariant(
|
|
122
|
+
attributeName: string,
|
|
123
|
+
newValue: string
|
|
124
|
+
): ProductVariant | undefined {
|
|
125
|
+
const nextSelection = new Map(selectedOptions);
|
|
126
|
+
nextSelection.set(attributeName, newValue);
|
|
127
|
+
|
|
128
|
+
return variants.find((v) => {
|
|
129
|
+
const opts = getVariantOptions(v);
|
|
130
|
+
return Array.from(nextSelection.entries()).every(([name, value]) =>
|
|
131
|
+
opts.some((o) => o.name === name && o.value === value)
|
|
132
|
+
);
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (attributeGroups.length === 0) return null;
|
|
137
|
+
|
|
138
|
+
return (
|
|
139
|
+
<div className={cn('space-y-4', className)}>
|
|
140
|
+
{attributeGroups.map((group) => (
|
|
141
|
+
<div key={group.name}>
|
|
142
|
+
<label className="text-foreground mb-2 block text-sm font-medium">
|
|
143
|
+
{group.name}
|
|
144
|
+
{selectedOptions.get(group.name) && (
|
|
145
|
+
<span className="text-muted-foreground ms-1 font-normal">
|
|
146
|
+
: {selectedOptions.get(group.name)}
|
|
147
|
+
</span>
|
|
148
|
+
)}
|
|
149
|
+
</label>
|
|
150
|
+
<div className="flex flex-wrap gap-2">
|
|
151
|
+
{group.values.map(
|
|
152
|
+
({
|
|
153
|
+
value,
|
|
154
|
+
swatchColor,
|
|
155
|
+
swatchColor2,
|
|
156
|
+
swatchImageUrl,
|
|
157
|
+
variants: matchingVariants,
|
|
158
|
+
}) => {
|
|
159
|
+
const isSelected = selectedOptions.get(group.name) === value;
|
|
160
|
+
const matchedVariant = findMatchingVariant(group.name, value);
|
|
161
|
+
const isAvailable = matchedVariant?.inventory?.canPurchase !== false;
|
|
162
|
+
|
|
163
|
+
// Color swatch rendering
|
|
164
|
+
if (group.displayType === 'COLOR_SWATCH' && swatchColor) {
|
|
165
|
+
return (
|
|
166
|
+
<button
|
|
167
|
+
key={value}
|
|
168
|
+
type="button"
|
|
169
|
+
disabled={!isAvailable}
|
|
170
|
+
title={value}
|
|
171
|
+
onClick={() => {
|
|
172
|
+
const variant = matchedVariant || matchingVariants[0];
|
|
173
|
+
if (variant) onVariantChange(variant);
|
|
174
|
+
}}
|
|
175
|
+
className={cn(
|
|
176
|
+
'h-9 w-9 rounded-full border-2 transition-all',
|
|
177
|
+
isSelected
|
|
178
|
+
? 'border-primary ring-primary/30 ring-2'
|
|
179
|
+
: isAvailable
|
|
180
|
+
? 'border-border hover:border-primary'
|
|
181
|
+
: 'cursor-not-allowed opacity-40'
|
|
182
|
+
)}
|
|
183
|
+
style={{
|
|
184
|
+
background: swatchColor2
|
|
185
|
+
? `linear-gradient(135deg, ${swatchColor} 50%, ${swatchColor2} 50%)`
|
|
186
|
+
: swatchColor,
|
|
187
|
+
}}
|
|
188
|
+
>
|
|
189
|
+
{!isAvailable && (
|
|
190
|
+
<span
|
|
191
|
+
className="bg-muted-foreground block h-full w-full rounded-full opacity-50"
|
|
192
|
+
style={{
|
|
193
|
+
backgroundImage:
|
|
194
|
+
'linear-gradient(135deg, transparent 45%, currentColor 45%, currentColor 55%, transparent 55%)',
|
|
195
|
+
}}
|
|
196
|
+
/>
|
|
197
|
+
)}
|
|
198
|
+
</button>
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Image swatch rendering
|
|
203
|
+
if (group.displayType === 'IMAGE_SWATCH' && swatchImageUrl) {
|
|
204
|
+
return (
|
|
205
|
+
<button
|
|
206
|
+
key={value}
|
|
207
|
+
type="button"
|
|
208
|
+
disabled={!isAvailable}
|
|
209
|
+
title={value}
|
|
210
|
+
onClick={() => {
|
|
211
|
+
const variant = matchedVariant || matchingVariants[0];
|
|
212
|
+
if (variant) onVariantChange(variant);
|
|
213
|
+
}}
|
|
214
|
+
className={cn(
|
|
215
|
+
'h-10 w-10 overflow-hidden rounded-lg border-2 transition-all',
|
|
216
|
+
isSelected
|
|
217
|
+
? 'border-primary ring-primary/30 ring-2'
|
|
218
|
+
: isAvailable
|
|
219
|
+
? 'border-border hover:border-primary'
|
|
220
|
+
: 'cursor-not-allowed opacity-40'
|
|
221
|
+
)}
|
|
222
|
+
>
|
|
223
|
+
<img
|
|
224
|
+
src={swatchImageUrl}
|
|
225
|
+
alt={value}
|
|
226
|
+
className="h-full w-full object-cover"
|
|
227
|
+
/>
|
|
228
|
+
</button>
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Default button rendering (BUTTON, DROPDOWN, or fallback)
|
|
233
|
+
return (
|
|
234
|
+
<button
|
|
235
|
+
key={value}
|
|
236
|
+
type="button"
|
|
237
|
+
disabled={!isAvailable}
|
|
238
|
+
onClick={() => {
|
|
239
|
+
const variant = matchedVariant || matchingVariants[0];
|
|
240
|
+
if (variant) onVariantChange(variant);
|
|
241
|
+
}}
|
|
242
|
+
className={cn(
|
|
243
|
+
'rounded border px-4 py-2 text-sm transition-colors',
|
|
244
|
+
isSelected
|
|
245
|
+
? 'border-primary bg-primary text-primary-foreground'
|
|
246
|
+
: isAvailable
|
|
247
|
+
? 'border-border bg-background text-foreground hover:border-primary'
|
|
248
|
+
: 'border-border bg-muted text-muted-foreground cursor-not-allowed line-through opacity-50'
|
|
249
|
+
)}
|
|
250
|
+
>
|
|
251
|
+
{value}
|
|
252
|
+
</button>
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
)}
|
|
256
|
+
</div>
|
|
257
|
+
</div>
|
|
258
|
+
))}
|
|
259
|
+
|
|
260
|
+
{/* Variant-specific info */}
|
|
261
|
+
{selectedVariant && (
|
|
262
|
+
<div className="text-muted-foreground flex items-center gap-3 pt-1 text-sm">
|
|
263
|
+
{selectedVariant.price && (
|
|
264
|
+
<span>
|
|
265
|
+
{
|
|
266
|
+
formatPrice(selectedVariant.salePrice || selectedVariant.price, {
|
|
267
|
+
currency,
|
|
268
|
+
}) as string
|
|
269
|
+
}
|
|
270
|
+
</span>
|
|
271
|
+
)}
|
|
272
|
+
<span>{getStockStatus(selectedVariant.inventory)}</span>
|
|
273
|
+
</div>
|
|
274
|
+
)}
|
|
275
|
+
</div>
|
|
276
|
+
);
|
|
277
|
+
}
|