Skip to content

Commit 074da7c

Browse files
committed
fix: revert form changes but keep track of options in params
1 parent a521247 commit 074da7c

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

.changeset/shy-owls-move.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Update `product-options-transformer.ts` to manually track persisted fields:
1414
case 'DropdownList': {
1515
return {
1616
// before
17-
persist: option.isVariantOption,
17+
// persist: option.isVariantOption,
1818
// after (manually persist)
1919
persist: true,
2020
type: 'select',

core/vibes/soul/sections/product-detail/product-detail-form.tsx

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,26 @@ export function ProductDetailForm<F extends Field>({
119119
[Key in keyof SchemaRawShape]?: z.infer<SchemaRawShape[Key]>;
120120
}>(
121121
(acc, field) => {
122-
// Checkbox fields need to be handled differently since we keep track of the checkedValue and not the boolean value of the default value.
122+
// Checkbox field has to be handled separately because we want to convert checked or unchecked value to true or undefined respectively.
123+
// This is because the form expects a boolean value, but we want to store the checked or unchecked value in the query params.
123124
if (field.type === 'checkbox') {
125+
if (params[field.name] === field.checkedValue) {
126+
return {
127+
...acc,
128+
[field.name]: 'true',
129+
};
130+
}
131+
132+
if (params[field.name] === field.uncheckedValue) {
133+
return {
134+
...acc,
135+
[field.name]: undefined,
136+
};
137+
}
138+
124139
return {
125140
...acc,
126-
[field.name]:
127-
(params[field.name] ?? field.defaultValue === 'true') ? field.checkedValue : '',
141+
[field.name]: field.defaultValue, // Default value is either 'true' or undefined
128142
};
129143
}
130144

@@ -344,7 +358,13 @@ function FormField({
344358

345359
const handleChange = useCallback(
346360
(value: string) => {
347-
void setParams({ [field.name]: value || null }); // Passing `null` to remove the value from the query params if fieldValue is falsey
361+
// Checkbox field has to be handled separately because we want to convert 'true' or '' to the checked or unchecked value respectively.
362+
if (field.type === 'checkbox') {
363+
void setParams({ [field.name]: value ? field.checkedValue : field.uncheckedValue });
364+
} else {
365+
void setParams({ [field.name]: value || null }); // Passing `null` to remove the value from the query params if fieldValue is falsey
366+
}
367+
348368
controls.change(value || ''); // If fieldValue is falsey, we set it to an empty string
349369
},
350370
[setParams, field, controls],
@@ -424,13 +444,13 @@ function FormField({
424444
case 'checkbox':
425445
return (
426446
<Checkbox
427-
checked={controls.value === field.checkedValue}
447+
checked={controls.value === 'true'}
428448
errors={formField.errors}
429449
key={formField.id}
430450
label={field.label}
431451
name={formField.name}
432452
onBlur={controls.blur}
433-
onCheckedChange={(value) => handleChange(value ? field.checkedValue.toString() : '')}
453+
onCheckedChange={(value) => handleChange(value ? 'true' : '')}
434454
onFocus={controls.focus}
435455
required={formField.required}
436456
value={controls.value ?? ''}

0 commit comments

Comments
 (0)