-
-
Notifications
You must be signed in to change notification settings - Fork 871
Expand file tree
/
Copy pathSelect.quanta.tsx
More file actions
181 lines (170 loc) · 4.86 KB
/
Copy pathSelect.quanta.tsx
File metadata and controls
181 lines (170 loc) · 4.86 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
171
172
173
174
175
176
177
178
179
180
181
import React from 'react';
import { Description, FieldError, Label } from '../Field/Field.quanta';
import { ChevrondownIcon } from '../../components/icons/ChevrondownIcon';
import { CheckboxIcon } from '../../components/icons/CheckboxIcon';
import { twMerge } from 'tailwind-merge';
import type {
ListBoxProps,
PopoverProps,
SelectProps as AriaSelectProps,
ValidationResult,
Key,
} from 'react-aria-components';
import {
Button,
Select as AriaSelect,
SelectValue,
Popover,
Dialog,
ListBoxItem,
ListBox,
} from 'react-aria-components';
export type Option = {
id: Key;
name: string;
};
interface SelectTriggerProps {
className?: string;
placeholder?: string;
errorMessage?: string | ((validation: ValidationResult) => string);
}
const SelectTrigger = ({
className,
placeholder,
errorMessage,
}: SelectTriggerProps) => {
return (
<Button
className={twMerge(
'flex h-10 w-full cursor-default items-center justify-between gap-2 rounded-md border border-gray-300 bg-white px-3 py-2 text-sm shadow-sm focus:border-gray-500 focus:ring-2 focus:ring-gray-400 disabled:opacity-50',
errorMessage
? 'border-red-500 focus:border-red-500 focus:ring-red-400'
: 'border-gray-300 focus:border-gray-500 focus:ring-gray-400',
className,
)}
>
<SelectValue className="flex-1 truncate text-left data-[placeholder]:text-gray-500" />
<ChevrondownIcon className="h-4 w-4 shrink-0 text-gray-400 transition-transform duration-200 group-data-[state=open]:rotate-180" />
</Button>
);
};
interface SelectListProps<T extends Option>
extends Omit<ListBoxProps<T>, 'layout' | 'orientation'>,
Pick<PopoverProps, 'placement'> {
items?: Iterable<T>;
className?: string;
}
const SelectList = <T extends Option>({
items,
className,
placement = 'bottom',
onBlur,
onFocus,
...props
}: SelectListProps<T>) => {
return (
<Popover
className="z-50 w-[var(--trigger-width)] overflow-hidden rounded-md border border-gray-200 bg-white shadow-lg"
placement={placement}
>
<Dialog role="dialog" className="w-[var(--trigger-width)] outline-none">
<ListBox
className={twMerge('max-h-60 overflow-auto outline-none', className)}
items={items}
onBlur={onBlur}
onFocus={onFocus}
>
{(item) => (
<ListBoxItem
key={item.id}
id={item.id}
textValue={item.name}
className="cursor-pointer px-3 py-2 text-sm text-gray-700 hover:bg-gray-100 focus:bg-gray-100 focus:outline-none data-[focused]:bg-gray-100"
>
{({ isSelected }) => (
<div className="flex w-full items-center">
<span className="flex-1">{item.name}</span>
{isSelected && (
<CheckboxIcon className="ml-2 h-4 w-4 text-gray-600" />
)}
</div>
)}
</ListBoxItem>
)}
</ListBox>
</Dialog>
</Popover>
);
};
export interface SelectProps<T extends Option>
extends Omit<AriaSelectProps<T>, 'children'> {
label?: string;
description?: string;
errorMessage?: string | ((validation: ValidationResult) => string);
placeholder?: string;
items?: Iterable<T>;
className?: string;
listBoxClassName?: string;
placement?: PopoverProps['placement'];
choices?: string[]; // alternative to items
onChange?: (value: Key | null) => void;
onBlur?: () => void;
onFocus?: () => void;
}
function Select<T extends Option>({
label,
description,
errorMessage,
placeholder = 'Select an option...',
className,
items,
listBoxClassName,
placement = 'bottom',
onChange,
onBlur,
onFocus,
...props
}: SelectProps<T>) {
return (
<AriaSelect
onSelectionChange={onChange}
className={twMerge('group flex w-full flex-col gap-1.5', className)}
>
{label && <Label>{label}</Label>}
<SelectTrigger placeholder={placeholder} errorMessage={errorMessage} />
<SelectList
items={items}
className={listBoxClassName}
placement={placement}
onBlur={onBlur}
onFocus={onFocus}
/>
{description && <Description>{description}</Description>}
{errorMessage && (
<div className="text-sm text-red-600">
{typeof errorMessage === 'function'
? errorMessage({} as ValidationResult)
: errorMessage}
</div>
)}
</AriaSelect>
);
}
export function SelectWidget<T extends Option>({
choices,
items,
...props
}: SelectProps<T>) {
let selectItems = items;
// If choices are provided, map them to the items format.
if (choices && choices.length > 0) {
selectItems = choices.map(
(item) =>
({
id: item,
name: item,
}) as T,
);
}
return <Select {...props} items={selectItems} />;
}