Uecko_ERP/packages/rdx-ui/src/components/form/select-field.tsx

130 lines
3.4 KiB
TypeScript
Raw Normal View History

2025-08-23 11:57:48 +00:00
import {
2026-03-18 16:38:40 +00:00
Field,
FieldDescription,
FieldError,
2025-08-23 11:57:48 +00:00
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@repo/shadcn-ui/components";
import { cn } from "@repo/shadcn-ui/lib/utils";
2026-04-03 16:15:25 +00:00
import React from "react";
import { Controller, type FieldPath, type FieldValues, useFormContext } from "react-hook-form";
import { FormFieldLabel } from "./form-field-label.tsx";
2026-03-18 16:38:40 +00:00
2026-04-03 16:15:25 +00:00
interface SelectFieldItem {
value: string;
label: string;
}
2025-08-23 11:57:48 +00:00
type SelectFieldProps<TFormValues extends FieldValues> = {
name: FieldPath<TFormValues>;
2026-04-03 16:15:25 +00:00
2025-08-23 11:57:48 +00:00
label?: string;
description?: string;
2026-04-03 16:15:25 +00:00
2025-08-23 11:57:48 +00:00
disabled?: boolean;
required?: boolean;
readOnly?: boolean;
2026-03-18 16:38:40 +00:00
placeholder?: string;
2026-04-10 08:05:45 +00:00
items?: SelectFieldItem[];
2026-03-18 16:38:40 +00:00
orientation?: "vertical" | "horizontal" | "responsive";
2025-08-23 11:57:48 +00:00
className?: string;
2026-03-18 16:38:40 +00:00
inputClassName?: string;
2025-08-23 11:57:48 +00:00
};
export function SelectField<TFormValues extends FieldValues>({
name,
2026-04-03 16:15:25 +00:00
2025-08-23 11:57:48 +00:00
label,
description,
2026-04-03 16:15:25 +00:00
2025-08-23 11:57:48 +00:00
disabled = false,
required = false,
readOnly = false,
2026-03-18 16:38:40 +00:00
2026-04-03 16:15:25 +00:00
placeholder,
2026-04-10 08:05:45 +00:00
items = [],
2026-04-03 16:15:25 +00:00
2026-03-18 16:38:40 +00:00
orientation = "vertical",
2025-08-23 11:57:48 +00:00
className,
2026-03-18 16:38:40 +00:00
inputClassName,
2025-08-23 11:57:48 +00:00
}: SelectFieldProps<TFormValues>) {
2026-04-03 16:15:25 +00:00
const triggerId = React.useId();
const { control, formState } = useFormContext<TFormValues>();
const isDisabled = Boolean(disabled || readOnly || formState.isSubmitting);
2025-08-23 11:57:48 +00:00
return (
2026-03-18 16:38:40 +00:00
<Controller
2025-08-23 11:57:48 +00:00
control={control}
name={name}
2026-03-18 16:38:40 +00:00
render={({ field, fieldState }) => {
2026-04-10 08:05:45 +00:00
const fieldValue = typeof field.value === "string" ? field.value.trim() : "";
const normalizedItems =
fieldValue && !items.some((item) => item.value === fieldValue)
? [{ value: fieldValue, label: fieldValue }, ...items]
: items;
2026-03-18 16:38:40 +00:00
return (
<Field
className={cn("gap-1", className)}
data-invalid={fieldState.invalid}
orientation={orientation}
>
2026-04-03 16:15:25 +00:00
{label ? (
<FormFieldLabel htmlFor={triggerId} required={required}>
{label}
</FormFieldLabel>
) : null}
2026-03-18 16:38:40 +00:00
2026-04-03 16:15:25 +00:00
<Select
disabled={isDisabled}
onValueChange={field.onChange}
value={field.value ?? undefined}
>
2026-04-12 17:28:26 +00:00
<SelectTrigger
aria-invalid={fieldState.invalid}
aria-required={required}
className={cn(
"bg-muted/50 font-medium",
"hover:border-ring hover:ring-ring/20 hover:ring-[3px]",
"focus-visible:border-ring focus-visible:ring-ring/60 focus-visible:ring-[3px]",
"placeholder:text-muted-foreground/50",
inputClassName
)}
id={triggerId}
>
<SelectValue
className={"placeholder:font-normal placeholder:italic"}
placeholder={placeholder}
/>
</SelectTrigger>
2026-03-18 16:38:40 +00:00
<SelectContent>
2026-04-10 08:05:45 +00:00
{normalizedItems.map((item) => (
2026-03-18 16:38:40 +00:00
<SelectItem key={`key-${item.value}`} value={item.value}>
{item.label}
</SelectItem>
))}
</SelectContent>
</Select>
2025-08-23 11:57:48 +00:00
2026-04-03 16:15:25 +00:00
{description ? (
<FieldDescription>{description}</FieldDescription>
) : (
<div aria-hidden="true" className="min-h-5" />
)}
2026-03-18 16:38:40 +00:00
<FieldError errors={[fieldState.error]} />
</Field>
);
}}
2025-08-23 11:57:48 +00:00
/>
);
}