"use client" import { useState } from "react" import { useFormContext, useController, type FieldValues, type FieldPath } from "react-hook-form" import { useQueryClient } from "@tanstack/react-query" import { PlusIcon } from "lucide-react" import { EMPLOYEE_ROUTES } from "@garage/api" import { Field, FieldLabel, FieldError, FieldDescription } from "@/shared/components/ui/field" import { FieldShell } from "@/shared/components/form/field-shell" import { Button } from "@/shared/components/ui/button" import { Dialog, DialogContent, DialogHeader, DialogTitle, } from "@/shared/components/ui/dialog" import { ScrollArea } from "@/shared/components/ui/scroll-area" import { EmployeeCombobox } from "./employee-combobox" import { EmployeeForm } from "./employee-form" // ── Props ── export type RhfEmployeeSelectFieldProps< TValues extends FieldValues, TName extends FieldPath, > = { name: TName label?: string description?: string required?: boolean disabled?: boolean placeholder?: string /** Show a "+" button to create a new employee inline */ showCreate?: boolean } // ── Component ── export function RhfEmployeeSelectField< TValues extends FieldValues, TName extends FieldPath, >({ name, label = "Employee", description, required, disabled, placeholder = "Search by name or email...", showCreate, }: RhfEmployeeSelectFieldProps) { const { control } = useFormContext() const { field, fieldState: { error }, } = useController({ name, control, disabled }) const queryClient = useQueryClient() const [isCreateOpen, setIsCreateOpen] = useState(false) const handleCreateSuccess = () => { queryClient.invalidateQueries({ queryKey: [EMPLOYEE_ROUTES.INDEX] }) setIsCreateOpen(false) } const combobox = ( { field.onChange(emp ? { value: emp.value, label: emp.label } : null) }} disabled={field.disabled} placeholder={placeholder} showClear={!!field.value} onBlur={field.onBlur} aria-invalid={!!error || undefined} /> ) if (showCreate) { return ( <> {label && (
{label} {required && *}
)} {combobox} {description && {description}} {error && {error.message}}
{ if (!v) setIsCreateOpen(false) }}> Add Employee ) } return ( {combobox} ) }