import * as React from "react" import { Controller, type Control, type FieldPath, type FieldValues } from "react-hook-form" import { FormFieldShell, type FormFieldShellControlProps, resolveFormFieldIds, } from "@/components/form/form-field-shell" import { Switch } from "@/components/ui/switch" import { cn } from "@/lib/utils" export type FormSwitchLabelPlacement = "start" | "end" export type FormSwitchProps< TFieldValues extends FieldValues = FieldValues, TName extends FieldPath = FieldPath, > = Omit, "name" | "checked" | "defaultChecked" | "disabled"> & FormFieldShellControlProps & { control: Control name: TName fieldClassName?: string labelPlacement?: FormSwitchLabelPlacement onCheckedChange?: (checked: boolean) => void } function FormSwitch< TFieldValues extends FieldValues = FieldValues, TName extends FieldPath = FieldPath, >({ control, name, label, description, success, loading, required, className, layout, descriptionPosition, labelAction, requiredIndicator, errorIcon, showErrorIcon, disabled, readOnly, labelClassName, labelRowClassName, descriptionClassName, errorClassName, contentClassName, fieldClassName, labelPlacement = "end", id, onCheckedChange, ...props }: FormSwitchProps) { const switchId = id ?? name return ( { const error = fieldState.error?.message const resolvedIds = resolveFormFieldIds(switchId, { description, error }) const labelContent = label ? ( ) : null const switchControl = ( { field.onChange(checked) onCheckedChange?.(checked) }} {...props} /> ) return labelPlacement === "start" ? ( {switchControl} ) : (
{switchControl} {(label || description || labelAction) && (
{(label || labelAction) && (
{labelContent} {labelAction &&
{labelAction}
}
)} {description && (

{description}

)}
)}
) }} /> ) } export { FormSwitch }