From 7d25a7a63e7b196ff903c7b9202d975aef1a98e1 Mon Sep 17 00:00:00 2001 From: Juan Di Toro Date: Tue, 5 Mar 2024 17:59:16 +0100 Subject: [PATCH] fix --- app/components/forms.tsx | 30 +++++++++++++++++++++++++++--- app/components/ui/checkbox.tsx | 4 +++- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/app/components/forms.tsx b/app/components/forms.tsx index b8bcfea..55ecaee 100644 --- a/app/components/forms.tsx +++ b/app/components/forms.tsx @@ -1,6 +1,6 @@ import { Label } from "@radix-ui/react-label" import { Input } from "./ui/input" -import { FieldName } from "@conform-to/react" +import { FieldName, useInputControl } from "@conform-to/react" import { useId } from "react" import { cn } from "~/utils" import { Checkbox } from "~/components/ui/checkbox" @@ -44,12 +44,22 @@ export const FieldCheckbox = ({ className }: { inputProps: { - name: FieldName + name: string + form: string + value?: string } & React.ComponentPropsWithoutRef labelProps: React.LabelHTMLAttributes errors?: ListOfErrors className?: string }) => { + const { key, defaultChecked, ...checkboxProps } = inputProps + const checkedValue = inputProps.value ?? "on" + const input = useInputControl({ + key, + name: inputProps.name, + formId: inputProps.form, + initialValue: defaultChecked ? checkedValue : undefined + }) const fallbackId = useId() const id = inputProps.id ?? fallbackId const errorId = errors?.length ? `${id}-error` : undefined @@ -58,10 +68,24 @@ export const FieldCheckbox = ({ className={cn("space-x-2 flex items-center text-primary-2", className)} > { + input.change(state.valueOf() ? checkedValue : "") + inputProps.onCheckedChange?.(state) + }} + onFocus={(event) => { + input.focus() + inputProps.onFocus?.(event) + }} + onBlur={(event) => { + input.blur() + inputProps.onBlur?.(event) + }} + type="button" />