-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormSelect.tsx
More file actions
83 lines (75 loc) · 1.76 KB
/
FormSelect.tsx
File metadata and controls
83 lines (75 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { FormField } from "@/components/common/FormField";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
interface Option {
value: string;
label: string;
}
interface FormSelectPropsBase {
label: string;
options: Option[];
placeholder?: string;
error?: string;
touched?: boolean;
optional?: boolean;
helperText?: string;
}
type StringFormSelectProps = FormSelectPropsBase & {
valueType?: "string";
value: string | undefined;
onChange: (value: string) => void;
};
type NumberFormSelectProps = FormSelectPropsBase & {
valueType: "number";
value: number | undefined;
onChange: (value: number) => void;
};
type FormSelectProps = StringFormSelectProps | NumberFormSelectProps;
export const FormSelect = ({
label,
options,
placeholder = "Select an option",
error,
touched,
optional,
helperText,
valueType,
value,
onChange,
}: FormSelectProps) => {
const stringValue = value !== undefined ? String(value) : "";
const handleChange = (val: string) => {
if (valueType === "number") {
onChange(Number(val));
} else {
onChange(val);
}
};
return (
<FormField
label={label}
error={error}
touched={touched}
optional={optional}
helperText={helperText}
>
<Select value={stringValue} onValueChange={handleChange}>
<SelectTrigger className="w-full" aria-invalid={!!error}>
<SelectValue placeholder={placeholder} />
</SelectTrigger>
<SelectContent>
{options.map((option) => (
<SelectItem key={option.value} value={option.value}>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
</FormField>
);
};