Skip to content

Commit cef7f91

Browse files
committed
Use generic for SelectOption value
1 parent 0eae0ea commit cef7f91

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

src/components/select/index.tsx

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,47 @@ import { Select as BaseSelect, SelectContent, SelectItem, SelectTrigger, SelectV
22
import { Label } from '../ui/label'
33
import { cn } from '../../lib/utils'
44

5-
export type SelectOption = {
5+
export type SelectOption<T = string> = {
66
label: string
7-
value: string
7+
value: T
88
}
99

10-
export type SelectProps = {
10+
export type SelectProps<T = string> = {
1111
className?: string
12-
defaultValue?: string
12+
defaultValue?: T
1313
disabled?: boolean
14-
handleChange?: (value: string) => void
14+
handleChange?: (value: T) => void
1515
label?: string
16-
options: SelectOption[]
16+
options?: SelectOption<T>[]
1717
placeholder?: string
18-
value?: string
18+
value?: T
1919
}
2020

21-
export const Select = ({
21+
export const Select = <T extends string = string>({
2222
className,
2323
defaultValue,
2424
disabled = false,
2525
handleChange,
2626
label,
27-
options,
27+
options = [],
2828
placeholder = 'Select an option',
2929
value,
30-
}: SelectProps) => {
30+
}: SelectProps<T>) => {
3131
return (
3232
<div className={cn('space-y-2', className)}>
3333
{label && <Label>{label}</Label>}
34-
<BaseSelect value={value} onValueChange={handleChange} defaultValue={defaultValue} disabled={disabled}>
34+
<BaseSelect
35+
value={value as string}
36+
onValueChange={handleChange as ((value: string) => void) | undefined}
37+
defaultValue={defaultValue as string}
38+
disabled={disabled}
39+
>
3540
<SelectTrigger className="text-foreground font-medium data-[size=default]:h-10 w-full bg-background focus-visible:ring-ring/20">
3641
<SelectValue placeholder={placeholder} />
3742
</SelectTrigger>
3843
<SelectContent>
3944
{options.map(option => (
40-
<SelectItem key={option.value} value={option.value}>
45+
<SelectItem key={String(option.value)} value={String(option.value)}>
4146
{option.label}
4247
</SelectItem>
4348
))}

0 commit comments

Comments
 (0)