Skip to content

Commit 3e2a008

Browse files
committed
fix(core): checkbox issue with default value and required validation
1 parent b19ee74 commit 3e2a008

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

.changeset/cold-meals-sin.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
"@bigcommerce/catalyst-core": patch
3+
---
4+
5+
- Fixes an issue with the checkbox not properly triggering the required validation.
6+
- Fixes an issue with the checkbox not setting the default value from the API.
7+
- Fixes an issue with the field value being incorrectly set as `undefined`
8+
9+
## Migration
10+
11+
Update the props to set a `checked` value and pasa an empty string when checked box is unselected.
12+
13+
```
14+
case 'checkbox':
15+
return (
16+
<Checkbox
17+
checked={controls.value === 'true'}
18+
errors={formField.errors}
19+
key={formField.id}
20+
label={field.label}
21+
name={formField.name}
22+
onBlur={controls.blur}
23+
onCheckedChange={(value) => handleChange(value ? 'true' : '')}
24+
onFocus={controls.focus}
25+
required={formField.required}
26+
value={controls.value ?? ''}
27+
/>
28+
);
29+
```

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,11 @@ function FormField({
224224
const handleChange = useCallback(
225225
(value: string) => {
226226
// Ensure that if page is reached without a full reload, we are still setting the selection properly based on query params.
227-
const fieldValue = value || String(params[field.name]);
227+
const fieldValue = value || params[field.name];
228228

229229
void setParams({ [field.name]: fieldValue || null }); // Passing `null` to remove the value from the query params if fieldValue is falsey
230-
controls.change(fieldValue);
230+
231+
controls.change(fieldValue ?? ''); // If fieldValue is falsey, we set it to an empty string
231232
},
232233
[setParams, field, controls, params],
233234
);
@@ -306,15 +307,16 @@ function FormField({
306307
case 'checkbox':
307308
return (
308309
<Checkbox
310+
checked={controls.value === 'true'}
309311
errors={formField.errors}
310312
key={formField.id}
311313
label={field.label}
312314
name={formField.name}
313315
onBlur={controls.blur}
314-
onCheckedChange={(value) => handleChange(String(value))}
316+
onCheckedChange={(value) => handleChange(value ? 'true' : '')}
315317
onFocus={controls.focus}
316318
required={formField.required}
317-
value={controls.value ?? 'false'}
319+
value={controls.value ?? ''}
318320
/>
319321
);
320322

0 commit comments

Comments
 (0)