-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSelect.tsx
More file actions
170 lines (164 loc) · 5.22 KB
/
Select.tsx
File metadata and controls
170 lines (164 loc) · 5.22 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import type { ReactNode } from 'react'
import { Fragment, useEffect, useId, useState } from 'react'
import { Combobox, Transition } from '@headlessui/react'
import { cx } from 'class-variance-authority'
import { Check, Chevron } from './icons.js'
import { Label, FieldError } from './index.js'
export type SelectOption = {
label: string
value: string
}
type SelectProps = {
options: SelectOption[]
placeholder: string
name?: string
label?: string
disabled?: boolean
required?: boolean
error?: string | string[]
value?: SelectOption
defaultValue?: SelectOption
description?: ReactNode
withBorder?: boolean
searchable?: boolean
onChange?: (value: string) => void
}
export const Select = ({
options,
name,
placeholder,
label,
error,
disabled = false,
required = false,
defaultValue = {
label: '',
value: ''
},
value,
withBorder,
searchable = false,
onChange
}: SelectProps) => {
const id = useId()
const [internalValue, setInternalValue] = useState<SelectOption>(defaultValue)
const [searchTerm, setSearchTerm] = useState('')
const filteredOptions =
searchTerm === ''
? options
: options.filter((option) =>
option.label
.toLowerCase()
.replace(/\s+/g, '')
.includes(searchTerm.toLowerCase().replace(/\s+/g, ''))
)
useEffect(() => {
if (value) {
setInternalValue(value)
}
}, [value])
return (
<Combobox
value={internalValue}
onChange={(e) => {
setInternalValue(e)
if (onChange !== undefined) {
onChange(e.value)
}
}}
disabled={disabled}
>
{name ? (
<input type="hidden" name={name} value={internalValue.value} />
) : null}
<div className={cx('flex flex-col relative w-full', label && 'mt-1')}>
{label && <Label className="w-full mb-px">{label}</Label>}
<div
className={cx(
'relative w-full cursor-default overflow-hidden bg-white text-left outline-0 focus:outline-none sm:text-sm h-9',
withBorder && 'border rounded-lg'
)}
>
{searchable ? (
<Combobox.Input
className="w-full border-none py-2 pl-3 pr-10 text-sm text-gray-900 outline-none"
id={id}
required={required}
displayValue={(option: SelectOption) => option.label}
onChange={(event) => setSearchTerm(event.target.value)}
placeholder={placeholder}
/>
) : (
<Combobox.Button className="w-full max-h-8 h-8 border-none py-2 pl-3 pr-10 text-sm text-left text-gray-900 outline-none overflow-y-hidden">
<>{internalValue ? internalValue.label : placeholder}</>
</Combobox.Button>
)}
<Combobox.Button className="absolute inset-y-0 right-0 flex items-center pr-2">
{({ open }) => (
<Chevron
className="h-5 w-5 text-gray-400"
aria-hidden="true"
strokeWidth={3}
direction={open ? 'down' : 'left'}
/>
)}
</Combobox.Button>
</div>
{error ? <FieldError error={error} /> : null}
<Transition
as={Fragment}
leave="transition ease-in duration-100"
leaveFrom="opacity-100"
leaveTo="opacity-0"
afterLeave={() => setSearchTerm('')}
>
<Combobox.Options
className={cx(
'absolute max-h-60 w-auto overflow-auto rounded-b-md bg-white py-1 z-10 text-base shadow-lg outline-0 focus:outline-none sm:text-sm',
label ? 'mt-14' : 'mt-8'
)}
>
{filteredOptions.length === 0 && searchTerm !== '' ? (
<div className="relative cursor-default select-none px-4 py-2 text-gray-700">
Nothing found.
</div>
) : (
filteredOptions.map((option) => (
<Combobox.Option
key={option.value}
className={({ active }) =>
`relative cursor-default select-none py-2 pl-10 pr-4 ${
active ? 'bg-teal-600 text-white' : 'text-gray-900'
}`
}
value={option}
>
{({ selected, active }) => (
<>
<span
className={`block truncate ${
selected ? 'font-medium' : 'font-normal'
}`}
>
{option.label}
</span>
{selected ? (
<span
className={`absolute inset-y-0 left-0 flex items-center pl-3 ${
active ? 'text-white' : 'text-teal-600'
}`}
>
<Check className="h-5 w-5" aria-hidden="true" />
</span>
) : null}
</>
)}
</Combobox.Option>
))
)}
</Combobox.Options>
</Transition>
</div>
</Combobox>
)
}