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

126 lines
3.2 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
FormControl,
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-03 16:15:25 +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,
items,
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 }) => {
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-03-18 16:38:40 +00:00
<FormControl>
<SelectTrigger
2026-04-03 16:15:25 +00:00
aria-invalid={fieldState.invalid}
aria-required={required}
2026-03-18 16:38:40 +00:00
className={cn(
2026-04-03 16:15:25 +00:00
"bg-muted/50 font-medium",
2026-04-09 18:39:54 +00:00
"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",
2026-03-18 16:38:40 +00:00
inputClassName
)}
2026-04-03 16:15:25 +00:00
id={triggerId}
2026-03-18 16:38:40 +00:00
>
<SelectValue
className={"placeholder:font-normal placeholder:italic"}
placeholder={placeholder}
/>
</SelectTrigger>
</FormControl>
<SelectContent>
{items.map((item) => (
<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
/>
);
}