Skip to content

Commit

Permalink
chore: transform into bool before passing as switch value
Browse files Browse the repository at this point in the history
  • Loading branch information
luizakp committed Sep 24, 2024
1 parent a62f743 commit 215b60e
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/components/FormBuilder/fields/SwitchField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ export interface SwitchFieldProps extends BaseField {
type: "switch";
}

function transformIntoBoolean(value: any): boolean {
if (typeof value === "boolean") {
return value;
}

if (typeof value === "string") {
return value === "true";
}

if (typeof value === "number") {
return value === 1;
}

return false;
}

export const SwitchField = withConditional<SwitchFieldProps>(
({ form, field }) => {
const disabled =
Expand All @@ -40,7 +56,7 @@ export const SwitchField = withConditional<SwitchFieldProps>(
<FormControl>
<Switch
disabled={disabled || false}
checked={formField.value}
checked={transformIntoBoolean(formField.value)}
onCheckedChange={formField.onChange}
/>
</FormControl>
Expand Down

0 comments on commit 215b60e

Please sign in to comment.