2025-08-23 11:57:48 +00:00
|
|
|
import {
|
|
|
|
|
FormControl,
|
|
|
|
|
FormDescription,
|
|
|
|
|
FormField,
|
|
|
|
|
FormItem,
|
|
|
|
|
FormLabel,
|
|
|
|
|
FormMessage,
|
|
|
|
|
Select,
|
|
|
|
|
SelectContent,
|
|
|
|
|
SelectItem,
|
|
|
|
|
SelectTrigger,
|
|
|
|
|
SelectValue,
|
|
|
|
|
} from "@repo/shadcn-ui/components";
|
|
|
|
|
|
|
|
|
|
import { cn } from "@repo/shadcn-ui/lib/utils";
|
2025-09-21 19:10:05 +00:00
|
|
|
import { Control, FieldPath, FieldValues, useController, useFormState } from "react-hook-form";
|
2025-08-23 11:57:48 +00:00
|
|
|
import { useTranslation } from "../../locales/i18n.ts";
|
|
|
|
|
|
|
|
|
|
type SelectFieldProps<TFormValues extends FieldValues> = {
|
|
|
|
|
control: Control<TFormValues>;
|
|
|
|
|
name: FieldPath<TFormValues>;
|
|
|
|
|
items: Array<{ value: string; label: string }>;
|
|
|
|
|
label?: string;
|
|
|
|
|
placeholder?: string;
|
|
|
|
|
description?: string;
|
|
|
|
|
disabled?: boolean;
|
|
|
|
|
required?: boolean;
|
|
|
|
|
readOnly?: boolean;
|
|
|
|
|
className?: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function SelectField<TFormValues extends FieldValues>({
|
|
|
|
|
control,
|
|
|
|
|
name,
|
|
|
|
|
items,
|
|
|
|
|
label,
|
|
|
|
|
placeholder,
|
|
|
|
|
description,
|
|
|
|
|
disabled = false,
|
|
|
|
|
required = false,
|
|
|
|
|
readOnly = false,
|
|
|
|
|
className,
|
|
|
|
|
}: SelectFieldProps<TFormValues>) {
|
|
|
|
|
const { t } = useTranslation();
|
2025-09-21 19:10:05 +00:00
|
|
|
|
|
|
|
|
const { isSubmitting, isValidating } = useFormState({ control, name });
|
|
|
|
|
const { field, fieldState } = useController({ control, name });
|
|
|
|
|
|
2025-08-23 11:57:48 +00:00
|
|
|
const isDisabled = disabled || readOnly;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<FormField
|
|
|
|
|
control={control}
|
|
|
|
|
name={name}
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem className={cn("space-y-0", className)}>
|
|
|
|
|
{label && (
|
2025-09-24 11:49:17 +00:00
|
|
|
<div className='mb-1 flex justify-between'>
|
2025-09-21 19:10:05 +00:00
|
|
|
<div className='flex items-center gap-2'>
|
|
|
|
|
<FormLabel htmlFor={name} className='m-0'>
|
|
|
|
|
{label}
|
|
|
|
|
</FormLabel>
|
|
|
|
|
{required && (
|
|
|
|
|
<span className='text-xs text-destructive'>{t("common.required")}</span>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
{/* Punto “unsaved” */}
|
|
|
|
|
{fieldState.isDirty && (
|
|
|
|
|
<span className='text-[10px] text-muted-foreground'>{t("common.modified")}</span>
|
|
|
|
|
)}
|
2025-08-23 11:57:48 +00:00
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
<Select onValueChange={field.onChange} defaultValue={field.value} disabled={isDisabled}>
|
|
|
|
|
<FormControl>
|
2025-09-24 11:49:17 +00:00
|
|
|
<SelectTrigger className='w-full bg-background h-8'>
|
2025-09-30 15:58:04 +00:00
|
|
|
<SelectValue
|
|
|
|
|
placeholder={placeholder}
|
|
|
|
|
className=' placeholder:font-normal placeholder:italic '
|
|
|
|
|
/>
|
2025-08-23 11:57:48 +00:00
|
|
|
</SelectTrigger>
|
|
|
|
|
</FormControl>
|
|
|
|
|
<SelectContent>
|
|
|
|
|
{items.map((item) => (
|
|
|
|
|
<SelectItem key={`key-${item.value}`} value={item.value}>
|
|
|
|
|
{item.label}
|
|
|
|
|
</SelectItem>
|
|
|
|
|
))}
|
|
|
|
|
</SelectContent>
|
|
|
|
|
</Select>
|
|
|
|
|
|
2025-09-24 11:49:17 +00:00
|
|
|
<FormDescription className={cn("text-xs truncate", !description && "invisible")}>
|
2025-08-23 11:57:48 +00:00
|
|
|
{description || "\u00A0"}
|
|
|
|
|
</FormDescription>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|