@alepha/ui 0.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -0
- package/components.json +20 -0
- package/package.json +85 -0
- package/src/components/action-error-toaster/action-error-toaster.tsx +70 -0
- package/src/components/admin/admin-audits.tsx +247 -0
- package/src/components/admin/admin-files.tsx +346 -0
- package/src/components/admin/admin-jobs-executions-panel.tsx +235 -0
- package/src/components/admin/admin-jobs-status-badge.tsx +33 -0
- package/src/components/admin/admin-jobs.tsx +294 -0
- package/src/components/admin/admin-keys.tsx +162 -0
- package/src/components/admin/admin-notifications.tsx +130 -0
- package/src/components/admin/admin-page.tsx +24 -0
- package/src/components/admin/admin-parameters.tsx +864 -0
- package/src/components/admin/admin-payments.tsx +93 -0
- package/src/components/admin/admin-sessions.tsx +174 -0
- package/src/components/admin/admin-user-detail-audits-tab.tsx +82 -0
- package/src/components/admin/admin-user-detail-identity-aside.tsx +218 -0
- package/src/components/admin/admin-user-detail-overview-tab.tsx +87 -0
- package/src/components/admin/admin-user-detail-password-dialog.tsx +85 -0
- package/src/components/admin/admin-user-detail-password-schema.ts +11 -0
- package/src/components/admin/admin-user-detail-profile-schema.ts +19 -0
- package/src/components/admin/admin-user-detail-provider-labels.ts +15 -0
- package/src/components/admin/admin-user-detail-security-tab.tsx +146 -0
- package/src/components/admin/admin-user-detail-sessions-tab.tsx +93 -0
- package/src/components/admin/admin-user-detail.tsx +606 -0
- package/src/components/admin/admin-users-roles-picker.tsx +126 -0
- package/src/components/admin/admin-users-status-filter.tsx +51 -0
- package/src/components/admin/admin-users.tsx +518 -0
- package/src/components/admin/diff-lines.spec.ts +58 -0
- package/src/components/admin/diff-lines.ts +58 -0
- package/src/components/admin/parameter-diff-dialog.tsx +119 -0
- package/src/components/admin/parameter-history-item.tsx +252 -0
- package/src/components/admin/parameter-json-dialog.tsx +93 -0
- package/src/components/admin/parameter-save-dialog.tsx +126 -0
- package/src/components/admin/use-confirmed-action.tsx +93 -0
- package/src/components/alepha-table/__tests__/use-table-selection.browser.spec.tsx +92 -0
- package/src/components/alepha-table/alepha-table.tsx +1158 -0
- package/src/components/alepha-table/use-table-selection.ts +95 -0
- package/src/components/app-shell/app-shell.tsx +568 -0
- package/src/components/auth/__tests__/auth-router.spec.ts +52 -0
- package/src/components/auth/auth-login.tsx +382 -0
- package/src/components/auth/auth-register.tsx +786 -0
- package/src/components/auth/auth-reset-password.tsx +349 -0
- package/src/components/auth/auth-router.tsx +126 -0
- package/src/components/auth/auth-verify-email.tsx +179 -0
- package/src/components/auto-form/auto-form.tsx +867 -0
- package/src/components/brand-icon/brand-icon.tsx +104 -0
- package/src/components/button-dark/button-dark.tsx +78 -0
- package/src/components/button-language/button-language.tsx +98 -0
- package/src/components/button-theme/button-theme.tsx +148 -0
- package/src/components/button-user/button-user.tsx +214 -0
- package/src/components/captcha/turnstile-widget.tsx +157 -0
- package/src/components/control/control.tsx +735 -0
- package/src/components/control-array/control-array.tsx +700 -0
- package/src/components/control-base/__tests__/form-field-a11y.browser.spec.tsx +93 -0
- package/src/components/control-base/field-i18n.ts +60 -0
- package/src/components/control-base/form-field.tsx +275 -0
- package/src/components/control-base/grid.tsx +59 -0
- package/src/components/control-base/icon-hint.tsx +64 -0
- package/src/components/control-date/control-date.tsx +190 -0
- package/src/components/control-number/control-number.tsx +136 -0
- package/src/components/control-object/control-object.tsx +200 -0
- package/src/components/control-select/control-select.tsx +774 -0
- package/src/components/control-upload/control-upload.tsx +475 -0
- package/src/components/file-image/file-image.tsx +52 -0
- package/src/components/language-toggle/language-toggle.tsx +66 -0
- package/src/components/markdown-view/markdown-view.css +114 -0
- package/src/components/markdown-view/markdown-view.tsx +92 -0
- package/src/components/nav-shell/nav-page.tsx +56 -0
- package/src/components/nav-shell/nav-shell.tsx +72 -0
- package/src/components/nav-shell/nav-tree-util.ts +62 -0
- package/src/components/nav-shell/spotlight.tsx +163 -0
- package/src/components/nav-shell/use-nav-breadcrumbs.ts +52 -0
- package/src/components/nav-shell/use-nav-entries.ts +101 -0
- package/src/components/nav-shell/use-nav-tree.ts +61 -0
- package/src/components/ui/accordion.tsx +77 -0
- package/src/components/ui/alert-dialog.tsx +186 -0
- package/src/components/ui/alert.tsx +75 -0
- package/src/components/ui/aspect-ratio.tsx +22 -0
- package/src/components/ui/avatar.tsx +106 -0
- package/src/components/ui/badge.tsx +51 -0
- package/src/components/ui/breadcrumb.tsx +121 -0
- package/src/components/ui/button-group.tsx +86 -0
- package/src/components/ui/button.tsx +83 -0
- package/src/components/ui/calendar.tsx +235 -0
- package/src/components/ui/card.tsx +102 -0
- package/src/components/ui/carousel.tsx +241 -0
- package/src/components/ui/chart.tsx +370 -0
- package/src/components/ui/checkbox.tsx +27 -0
- package/src/components/ui/collapsible.tsx +19 -0
- package/src/components/ui/combobox.tsx +298 -0
- package/src/components/ui/command.tsx +196 -0
- package/src/components/ui/context-menu.tsx +271 -0
- package/src/components/ui/dialog.tsx +153 -0
- package/src/components/ui/direction.tsx +4 -0
- package/src/components/ui/drawer.tsx +130 -0
- package/src/components/ui/dropdown-menu.tsx +269 -0
- package/src/components/ui/empty.tsx +100 -0
- package/src/components/ui/hover-card.tsx +50 -0
- package/src/components/ui/input-group.tsx +157 -0
- package/src/components/ui/input-otp.tsx +83 -0
- package/src/components/ui/input.tsx +19 -0
- package/src/components/ui/item.tsx +200 -0
- package/src/components/ui/kbd.tsx +26 -0
- package/src/components/ui/label.tsx +19 -0
- package/src/components/ui/menubar.tsx +283 -0
- package/src/components/ui/navigation-menu.tsx +170 -0
- package/src/components/ui/pagination.tsx +133 -0
- package/src/components/ui/popover.tsx +87 -0
- package/src/components/ui/progress.tsx +82 -0
- package/src/components/ui/radio-group.tsx +35 -0
- package/src/components/ui/resizable.tsx +49 -0
- package/src/components/ui/scroll-area.tsx +51 -0
- package/src/components/ui/segmented.tsx +232 -0
- package/src/components/ui/select.tsx +201 -0
- package/src/components/ui/separator.tsx +22 -0
- package/src/components/ui/sheet.tsx +133 -0
- package/src/components/ui/sidebar.tsx +723 -0
- package/src/components/ui/skeleton.tsx +13 -0
- package/src/components/ui/slider.tsx +51 -0
- package/src/components/ui/sonner.tsx +46 -0
- package/src/components/ui/spinner.tsx +15 -0
- package/src/components/ui/switch.tsx +29 -0
- package/src/components/ui/table.tsx +115 -0
- package/src/components/ui/tabs.tsx +79 -0
- package/src/components/ui/textarea.tsx +17 -0
- package/src/components/ui/toggle-group.tsx +86 -0
- package/src/components/ui/toggle.tsx +44 -0
- package/src/components/ui/tooltip.tsx +63 -0
- package/src/components/use-dialog/use-dialog.tsx +244 -0
- package/src/components/use-toast/use-toast.tsx +89 -0
- package/src/hooks/use-mobile.ts +21 -0
- package/src/lib/__tests__/i18n-fr.spec.ts +72 -0
- package/src/lib/i18n-fr.ts +500 -0
- package/src/lib/utils.ts +6 -0
- package/src/styles.css +129 -0
|
@@ -0,0 +1,774 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
void React;
|
|
4
|
+
|
|
5
|
+
import { FormField } from "@alepha/ui/components/control-base/form-field";
|
|
6
|
+
import type { IconComponent } from "@alepha/ui/components/control-base/icon-hint";
|
|
7
|
+
import {
|
|
8
|
+
ComboboxChip,
|
|
9
|
+
ComboboxChips,
|
|
10
|
+
ComboboxChipsInput,
|
|
11
|
+
ComboboxContent,
|
|
12
|
+
ComboboxEmpty,
|
|
13
|
+
ComboboxInput,
|
|
14
|
+
ComboboxItem,
|
|
15
|
+
ComboboxList,
|
|
16
|
+
Combobox as ComboboxRoot,
|
|
17
|
+
ComboboxTrigger,
|
|
18
|
+
useComboboxAnchor,
|
|
19
|
+
} from "@alepha/ui/components/ui/combobox";
|
|
20
|
+
import { Segmented } from "@alepha/ui/components/ui/segmented";
|
|
21
|
+
import {
|
|
22
|
+
Select,
|
|
23
|
+
SelectContent,
|
|
24
|
+
SelectItem,
|
|
25
|
+
SelectSeparator,
|
|
26
|
+
SelectTrigger,
|
|
27
|
+
SelectValue,
|
|
28
|
+
} from "@alepha/ui/components/ui/select";
|
|
29
|
+
import { cn } from "@alepha/ui/lib/utils";
|
|
30
|
+
import type { Async } from "alepha";
|
|
31
|
+
import { useAction } from "alepha/react";
|
|
32
|
+
import {
|
|
33
|
+
type BaseInputField,
|
|
34
|
+
parseField,
|
|
35
|
+
useFieldValue,
|
|
36
|
+
useFormState,
|
|
37
|
+
} from "alepha/react/form";
|
|
38
|
+
import { useI18n } from "alepha/react/i18n";
|
|
39
|
+
import { Loader2 } from "lucide-react";
|
|
40
|
+
import { type ReactNode, useEffect, useRef, useState } from "react";
|
|
41
|
+
|
|
42
|
+
export type SelectOption =
|
|
43
|
+
| string
|
|
44
|
+
| {
|
|
45
|
+
value: string;
|
|
46
|
+
label: string;
|
|
47
|
+
/**
|
|
48
|
+
* Optional secondary line shown under the label in the dropdown.
|
|
49
|
+
*/
|
|
50
|
+
description?: string;
|
|
51
|
+
/**
|
|
52
|
+
* Optional small badge rendered next to the label.
|
|
53
|
+
*/
|
|
54
|
+
tag?: string;
|
|
55
|
+
/**
|
|
56
|
+
* Optional icon/element rendered before the label, in both the
|
|
57
|
+
* dropdown row and (for single-select) the trigger when selected.
|
|
58
|
+
*/
|
|
59
|
+
icon?: ReactNode;
|
|
60
|
+
/**
|
|
61
|
+
* When true, the row is non-interactive — can't be selected if
|
|
62
|
+
* not selected, can't be deselected if selected. Useful for
|
|
63
|
+
* default/mandatory entries (e.g. the base "user" role).
|
|
64
|
+
*/
|
|
65
|
+
disabled?: boolean;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
type LoaderMode = "static" | "short" | "long";
|
|
69
|
+
|
|
70
|
+
export interface ControlSelectProps {
|
|
71
|
+
/**
|
|
72
|
+
* Bound `InputField` from `useForm`. Single or multi value depending on schema.
|
|
73
|
+
*/
|
|
74
|
+
input: BaseInputField;
|
|
75
|
+
/**
|
|
76
|
+
* Field label. Falls back to schema `title`.
|
|
77
|
+
*/
|
|
78
|
+
label?: string;
|
|
79
|
+
/**
|
|
80
|
+
* Helper text shown below the input.
|
|
81
|
+
*/
|
|
82
|
+
description?: string;
|
|
83
|
+
/**
|
|
84
|
+
* Render as a `<Segmented>` control (best for 2–4 options).
|
|
85
|
+
*/
|
|
86
|
+
segmented?: boolean;
|
|
87
|
+
/**
|
|
88
|
+
* Render as a searchable combobox instead of a native select.
|
|
89
|
+
*/
|
|
90
|
+
combobox?: boolean;
|
|
91
|
+
/**
|
|
92
|
+
* Async option loader. Triggers long-mode (server-side search) above `loaderThreshold` options.
|
|
93
|
+
*/
|
|
94
|
+
loader?: (search: string, resolve?: string[]) => Async<SelectOption[]>;
|
|
95
|
+
/**
|
|
96
|
+
* Option count above which `loader` is invoked on every search instead of once. Defaults to ~50.
|
|
97
|
+
*/
|
|
98
|
+
loaderThreshold?: number;
|
|
99
|
+
/**
|
|
100
|
+
* Debounce in ms applied to search queries when calling `loader` in long mode.
|
|
101
|
+
*/
|
|
102
|
+
loaderDebounce?: number;
|
|
103
|
+
/**
|
|
104
|
+
* Disable the control.
|
|
105
|
+
*/
|
|
106
|
+
disabled?: boolean;
|
|
107
|
+
/**
|
|
108
|
+
* Inline option list (overrides schema `enum`). Accepts either a static
|
|
109
|
+
* array or an async function `(query) => SelectOption[]`. The async form
|
|
110
|
+
* is mapped to a long-mode loader.
|
|
111
|
+
*/
|
|
112
|
+
items?:
|
|
113
|
+
| SelectOption[]
|
|
114
|
+
| ((query: string) => SelectOption[] | Promise<SelectOption[]>);
|
|
115
|
+
/**
|
|
116
|
+
* Allow the user to add a new option by typing. When `true`, the typed
|
|
117
|
+
* query becomes the value (and label) of a freshly created entry. When a
|
|
118
|
+
* function, the function builds the option from the query.
|
|
119
|
+
*
|
|
120
|
+
* - For multi-select fields: each entry is appended to the value array.
|
|
121
|
+
* - For single fields: behaves like a regular text input with a dropdown
|
|
122
|
+
* suggesting existing options.
|
|
123
|
+
*/
|
|
124
|
+
createNewEntry?: boolean | ((query: string) => Exclude<SelectOption, string>);
|
|
125
|
+
/**
|
|
126
|
+
* When true, the dropdown gets a synthetic "none" row at the top that
|
|
127
|
+
* resets the field to `undefined`. Useful for optional filter chips
|
|
128
|
+
* (e.g. an admin-table status filter) where the user needs an explicit
|
|
129
|
+
* "no filter" option — Base UI's `Select` reserves empty-string values
|
|
130
|
+
* as its internal no-selection sentinel, so a regular `<SelectItem
|
|
131
|
+
* value="">` can't be picked. With `clearable`, ControlSelect uses an
|
|
132
|
+
* internal sentinel and translates it to `undefined` in `onChange`.
|
|
133
|
+
*
|
|
134
|
+
* Currently honored by the static-short `Select` and the searchable
|
|
135
|
+
* Combobox render paths. Ignored by `segmented` (use a real segment
|
|
136
|
+
* for "all" there).
|
|
137
|
+
*/
|
|
138
|
+
clearable?: boolean;
|
|
139
|
+
/**
|
|
140
|
+
* Label rendered for the "clear" row injected by `clearable`. Also
|
|
141
|
+
* used as the trigger placeholder when the field is empty. Defaults to
|
|
142
|
+
* `"None"`. Localize at the call site (e.g. `"All types"`,
|
|
143
|
+
* `"All status"`).
|
|
144
|
+
*/
|
|
145
|
+
clearLabel?: string;
|
|
146
|
+
/**
|
|
147
|
+
* Extra className applied to the visible trigger (the `<SelectTrigger>`
|
|
148
|
+
* or combobox `<Button>`). Useful for sizing filter chips
|
|
149
|
+
* (`"w-40"`, `"w-72"`, etc.) without wrapping the whole `FormField` in
|
|
150
|
+
* an extra div.
|
|
151
|
+
*/
|
|
152
|
+
triggerClassName?: string;
|
|
153
|
+
/**
|
|
154
|
+
* Leading icon rendered on the left of the trigger, matching the
|
|
155
|
+
* text-input controls. Resolved by the parent `Control` from its `icon`
|
|
156
|
+
* prop — unlike text inputs there is no schema-hint fallback, so a select
|
|
157
|
+
* only shows an icon when one is explicitly set.
|
|
158
|
+
*/
|
|
159
|
+
icon?: IconComponent;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Internal sentinel for the `clearable` row. Picked to be unlikely to
|
|
164
|
+
* collide with a real option value; we translate it to `undefined` at
|
|
165
|
+
* the boundary so callers never see it.
|
|
166
|
+
*/
|
|
167
|
+
const CLEAR_VALUE = "__alepha_clear__";
|
|
168
|
+
|
|
169
|
+
const optValue = (o: SelectOption) => (typeof o === "string" ? o : o.value);
|
|
170
|
+
const optLabel = (o: SelectOption) => (typeof o === "string" ? o : o.label);
|
|
171
|
+
const optDesc = (o: SelectOption) =>
|
|
172
|
+
typeof o === "string" ? undefined : o.description;
|
|
173
|
+
const optTag = (o: SelectOption) => (typeof o === "string" ? undefined : o.tag);
|
|
174
|
+
const optDisabled = (o: SelectOption) =>
|
|
175
|
+
typeof o === "string" ? false : Boolean(o.disabled);
|
|
176
|
+
const optIcon = (o: SelectOption): ReactNode =>
|
|
177
|
+
typeof o === "string" ? undefined : o.icon;
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Friendly label for plain string options: "optional" → "Optional",
|
|
181
|
+
* "in_progress" → "In Progress". Custom `{ value, label }` items pass through
|
|
182
|
+
* untouched.
|
|
183
|
+
*/
|
|
184
|
+
const titlecase = (s: string) =>
|
|
185
|
+
s.replace(/[_-]+/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
|
|
186
|
+
const segmentedLabel = (o: SelectOption) =>
|
|
187
|
+
typeof o === "string" ? titlecase(o) : o.label;
|
|
188
|
+
|
|
189
|
+
export function ControlSelect(props: ControlSelectProps) {
|
|
190
|
+
const { tr } = useI18n();
|
|
191
|
+
const form = useFormState(props.input, ["error"]);
|
|
192
|
+
const [value, setValue] = useFieldValue(props.input);
|
|
193
|
+
|
|
194
|
+
const meta = parseField(props.input, {
|
|
195
|
+
label: props.label,
|
|
196
|
+
description: props.description,
|
|
197
|
+
error: form.error,
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
const isArray = meta.isArray;
|
|
201
|
+
const isNumeric = meta.type === "number" || meta.type === "integer";
|
|
202
|
+
const isBoolean = meta.type === "boolean";
|
|
203
|
+
|
|
204
|
+
// Normalize items prop: array → static; function → loader
|
|
205
|
+
const itemsArray = Array.isArray(props.items)
|
|
206
|
+
? (props.items as SelectOption[])
|
|
207
|
+
: undefined;
|
|
208
|
+
const itemsLoader =
|
|
209
|
+
typeof props.items === "function"
|
|
210
|
+
? (props.items as (q: string) => Async<SelectOption[]>)
|
|
211
|
+
: undefined;
|
|
212
|
+
|
|
213
|
+
const enumValues =
|
|
214
|
+
itemsArray ?? (meta.enum as SelectOption[] | undefined) ?? [];
|
|
215
|
+
|
|
216
|
+
const effectiveLoader = props.loader ?? itemsLoader;
|
|
217
|
+
|
|
218
|
+
const {
|
|
219
|
+
data: asyncData,
|
|
220
|
+
loading,
|
|
221
|
+
mode,
|
|
222
|
+
search,
|
|
223
|
+
} = useAsyncLoader(
|
|
224
|
+
effectiveLoader,
|
|
225
|
+
props.loaderThreshold ?? 100,
|
|
226
|
+
props.loaderDebounce ?? 300,
|
|
227
|
+
props.input.initialValue,
|
|
228
|
+
);
|
|
229
|
+
|
|
230
|
+
const [staticData, setStaticData] = useState<SelectOption[]>([]);
|
|
231
|
+
const enumKey = enumValues.map(optValue).join("");
|
|
232
|
+
const min = meta.constraints.minimum;
|
|
233
|
+
const max = meta.constraints.maximum;
|
|
234
|
+
useEffect(() => {
|
|
235
|
+
if (effectiveLoader) return;
|
|
236
|
+
if (isBoolean && enumValues.length === 0) {
|
|
237
|
+
setStaticData([
|
|
238
|
+
{ value: "true", label: tr("controlSelect.yes", { default: "Yes" }) },
|
|
239
|
+
{ value: "false", label: tr("controlSelect.no", { default: "No" }) },
|
|
240
|
+
]);
|
|
241
|
+
} else if (
|
|
242
|
+
isNumeric &&
|
|
243
|
+
enumValues.length === 0 &&
|
|
244
|
+
typeof min === "number" &&
|
|
245
|
+
typeof max === "number" &&
|
|
246
|
+
max - min <= 20
|
|
247
|
+
) {
|
|
248
|
+
const range: SelectOption[] = [];
|
|
249
|
+
for (let i = min; i <= max; i++) range.push(String(i));
|
|
250
|
+
setStaticData(range);
|
|
251
|
+
} else {
|
|
252
|
+
setStaticData(enumValues);
|
|
253
|
+
}
|
|
254
|
+
}, [effectiveLoader, enumKey, isBoolean, isNumeric, min, max, tr]);
|
|
255
|
+
|
|
256
|
+
const data = effectiveLoader ? asyncData : staticData;
|
|
257
|
+
|
|
258
|
+
if (!props.input?.props) return null;
|
|
259
|
+
|
|
260
|
+
const coerce = (raw: string): unknown => {
|
|
261
|
+
if (isNumeric) return Number(raw);
|
|
262
|
+
if (isBoolean) return raw === "true";
|
|
263
|
+
return raw;
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
if (props.segmented) {
|
|
267
|
+
return (
|
|
268
|
+
<FormField
|
|
269
|
+
id={meta.id}
|
|
270
|
+
label={meta.label}
|
|
271
|
+
description={meta.description}
|
|
272
|
+
error={meta.error}
|
|
273
|
+
required={meta.required}
|
|
274
|
+
>
|
|
275
|
+
<Segmented
|
|
276
|
+
value={value != null ? String(value) : undefined}
|
|
277
|
+
onChange={(v) => setValue(coerce(v))}
|
|
278
|
+
disabled={props.disabled}
|
|
279
|
+
options={data.slice(0, 10).map((o) => ({
|
|
280
|
+
value: optValue(o),
|
|
281
|
+
label: segmentedLabel(o),
|
|
282
|
+
}))}
|
|
283
|
+
fullWidth
|
|
284
|
+
/>
|
|
285
|
+
</FormField>
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// Async loader, multi-select, explicit combobox, or many items → Combobox
|
|
290
|
+
if (isArray || props.combobox || mode === "long" || data.length > 20) {
|
|
291
|
+
return (
|
|
292
|
+
<FormField
|
|
293
|
+
id={meta.id}
|
|
294
|
+
label={meta.label}
|
|
295
|
+
description={meta.description}
|
|
296
|
+
error={meta.error}
|
|
297
|
+
required={meta.required}
|
|
298
|
+
>
|
|
299
|
+
<Combobox
|
|
300
|
+
id={meta.id}
|
|
301
|
+
data={data}
|
|
302
|
+
loading={loading}
|
|
303
|
+
multi={isArray}
|
|
304
|
+
disabled={props.disabled}
|
|
305
|
+
value={value}
|
|
306
|
+
onChange={(v) => setValue(v)}
|
|
307
|
+
coerce={coerce}
|
|
308
|
+
onSearch={mode === "long" ? search.run : undefined}
|
|
309
|
+
createNewEntry={props.createNewEntry}
|
|
310
|
+
icon={props.icon}
|
|
311
|
+
placeholder={
|
|
312
|
+
props.clearable
|
|
313
|
+
? (props.clearLabel ??
|
|
314
|
+
tr("controlSelect.none", { default: "None" }))
|
|
315
|
+
: undefined
|
|
316
|
+
}
|
|
317
|
+
/>
|
|
318
|
+
</FormField>
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// Infer / short — native Select
|
|
323
|
+
const clearLabel =
|
|
324
|
+
props.clearLabel ?? tr("controlSelect.none", { default: "None" });
|
|
325
|
+
const labelFor = (raw: string) => {
|
|
326
|
+
const found = data.find((o) => optValue(o) === raw);
|
|
327
|
+
return found ? optLabel(found) : raw;
|
|
328
|
+
};
|
|
329
|
+
const selectedValue =
|
|
330
|
+
value != null ? String(value) : props.clearable ? CLEAR_VALUE : undefined;
|
|
331
|
+
return (
|
|
332
|
+
<FormField
|
|
333
|
+
id={meta.id}
|
|
334
|
+
label={meta.label}
|
|
335
|
+
description={meta.description}
|
|
336
|
+
error={meta.error}
|
|
337
|
+
required={meta.required}
|
|
338
|
+
>
|
|
339
|
+
<Select
|
|
340
|
+
value={selectedValue}
|
|
341
|
+
onValueChange={(v) => {
|
|
342
|
+
if (v === CLEAR_VALUE) {
|
|
343
|
+
setValue(undefined);
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
setValue(coerce(v ?? ""));
|
|
347
|
+
}}
|
|
348
|
+
disabled={props.disabled}
|
|
349
|
+
>
|
|
350
|
+
<SelectTrigger
|
|
351
|
+
id={meta.id}
|
|
352
|
+
className={cn("w-full", props.triggerClassName)}
|
|
353
|
+
>
|
|
354
|
+
{props.icon && (
|
|
355
|
+
<props.icon className="text-muted-foreground size-4 shrink-0" />
|
|
356
|
+
)}
|
|
357
|
+
<SelectValue
|
|
358
|
+
placeholder={
|
|
359
|
+
props.clearable
|
|
360
|
+
? clearLabel
|
|
361
|
+
: tr("controlSelect.select", { default: "Select…" })
|
|
362
|
+
}
|
|
363
|
+
>
|
|
364
|
+
{selectedValue === CLEAR_VALUE
|
|
365
|
+
? clearLabel
|
|
366
|
+
: selectedValue != null
|
|
367
|
+
? labelFor(selectedValue)
|
|
368
|
+
: undefined}
|
|
369
|
+
</SelectValue>
|
|
370
|
+
</SelectTrigger>
|
|
371
|
+
<SelectContent>
|
|
372
|
+
{props.clearable && (
|
|
373
|
+
<>
|
|
374
|
+
<SelectItem value={CLEAR_VALUE}>{clearLabel}</SelectItem>
|
|
375
|
+
<SelectSeparator />
|
|
376
|
+
</>
|
|
377
|
+
)}
|
|
378
|
+
{data.map((o) => {
|
|
379
|
+
const icon = optIcon(o);
|
|
380
|
+
return (
|
|
381
|
+
<SelectItem key={optValue(o)} value={optValue(o)}>
|
|
382
|
+
{icon ? (
|
|
383
|
+
<span className="flex items-center gap-2">
|
|
384
|
+
<span className="flex shrink-0 items-center">{icon}</span>
|
|
385
|
+
<span>{optLabel(o)}</span>
|
|
386
|
+
</span>
|
|
387
|
+
) : (
|
|
388
|
+
optLabel(o)
|
|
389
|
+
)}
|
|
390
|
+
</SelectItem>
|
|
391
|
+
);
|
|
392
|
+
})}
|
|
393
|
+
</SelectContent>
|
|
394
|
+
</Select>
|
|
395
|
+
</FormField>
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
interface ComboboxProps {
|
|
400
|
+
id?: string;
|
|
401
|
+
data: SelectOption[];
|
|
402
|
+
loading: boolean;
|
|
403
|
+
multi: boolean;
|
|
404
|
+
disabled?: boolean;
|
|
405
|
+
value: unknown;
|
|
406
|
+
onChange: (v: unknown) => void;
|
|
407
|
+
coerce: (v: string) => unknown;
|
|
408
|
+
onSearch?: (q: string) => void;
|
|
409
|
+
createNewEntry?: ControlSelectProps["createNewEntry"];
|
|
410
|
+
icon?: IconComponent;
|
|
411
|
+
/**
|
|
412
|
+
* Trigger text when nothing is selected. Mirrors the native-Select path,
|
|
413
|
+
* where a `clearable` field shows its `clearLabel` (e.g. "All zones") as the
|
|
414
|
+
* empty placeholder. Defaults to "Select…".
|
|
415
|
+
*/
|
|
416
|
+
placeholder?: string;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Normalized option used as the Base UI `Combobox.Item` value. Because the
|
|
421
|
+
* shape is `{ value, label }`, Base UI uses `label` for display/search and
|
|
422
|
+
* `value` for selection automatically — which is what makes the popup search
|
|
423
|
+
* match the visible label rather than the raw id.
|
|
424
|
+
*/
|
|
425
|
+
interface ComboOption {
|
|
426
|
+
value: string;
|
|
427
|
+
label: string;
|
|
428
|
+
description?: string;
|
|
429
|
+
tag?: string;
|
|
430
|
+
icon?: ReactNode;
|
|
431
|
+
disabled?: boolean;
|
|
432
|
+
/**
|
|
433
|
+
* Marks the synthetic "create new" row injected by `createNewEntry`.
|
|
434
|
+
*/
|
|
435
|
+
create?: boolean;
|
|
436
|
+
/**
|
|
437
|
+
* The raw query that produced a `create` row (used as the new entry's label).
|
|
438
|
+
*/
|
|
439
|
+
query?: string;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Searchable single/multi select built on the Base UI `Combobox` primitive.
|
|
444
|
+
*
|
|
445
|
+
* Composed in the "select-like" shape: a button trigger that shows the current
|
|
446
|
+
* value, with the search `<input>` living inside the popup. We disable Base
|
|
447
|
+
* UI's internal filtering (`filter={null}`) and filter in JS so the same code
|
|
448
|
+
* path serves static lists, server-driven (`onSearch`) lists, and the
|
|
449
|
+
* `createNewEntry` affordance.
|
|
450
|
+
*/
|
|
451
|
+
function Combobox(props: ComboboxProps) {
|
|
452
|
+
const { tr } = useI18n();
|
|
453
|
+
const [query, setQuery] = useState("");
|
|
454
|
+
// Remembers labels for values the user has picked, so the trigger/chips keep
|
|
455
|
+
// a human label even after the source option drops out of a server-filtered
|
|
456
|
+
// `data` set — or for freshly created entries that never existed in `data`.
|
|
457
|
+
const labelCache = useRef(new Map<string, string>());
|
|
458
|
+
// Positioning anchor for the multi-select chips box (the popup is anchored to
|
|
459
|
+
// the chips container rather than the inline input).
|
|
460
|
+
const anchor = useComboboxAnchor();
|
|
461
|
+
|
|
462
|
+
const options: ComboOption[] = props.data.map((o) => ({
|
|
463
|
+
value: optValue(o),
|
|
464
|
+
label: optLabel(o),
|
|
465
|
+
description: optDesc(o),
|
|
466
|
+
tag: optTag(o),
|
|
467
|
+
icon: optIcon(o),
|
|
468
|
+
disabled: optDisabled(o),
|
|
469
|
+
}));
|
|
470
|
+
|
|
471
|
+
const selected: string[] = props.multi
|
|
472
|
+
? Array.isArray(props.value)
|
|
473
|
+
? (props.value as unknown[]).map(String)
|
|
474
|
+
: []
|
|
475
|
+
: props.value != null
|
|
476
|
+
? [String(props.value)]
|
|
477
|
+
: [];
|
|
478
|
+
|
|
479
|
+
const labelFor = (val: string) =>
|
|
480
|
+
options.find((o) => o.value === val)?.label ??
|
|
481
|
+
labelCache.current.get(val) ??
|
|
482
|
+
val;
|
|
483
|
+
|
|
484
|
+
const disabledFor = (val: string) =>
|
|
485
|
+
options.find((o) => o.value === val)?.disabled ?? false;
|
|
486
|
+
|
|
487
|
+
// Server mode (`onSearch`) filters upstream; for static lists we filter on
|
|
488
|
+
// the visible label — never the value/id (that was the cmdk bug).
|
|
489
|
+
const serverMode = Boolean(props.onSearch);
|
|
490
|
+
const q = query.trim().toLowerCase();
|
|
491
|
+
const filtered =
|
|
492
|
+
serverMode || !q
|
|
493
|
+
? options
|
|
494
|
+
: options.filter((o) => o.label.toLowerCase().includes(q));
|
|
495
|
+
|
|
496
|
+
const showCreate =
|
|
497
|
+
Boolean(props.createNewEntry) &&
|
|
498
|
+
q.length > 0 &&
|
|
499
|
+
!options.some((o) => o.value === query || o.label.toLowerCase() === q) &&
|
|
500
|
+
!selected.includes(query);
|
|
501
|
+
|
|
502
|
+
const createOption: ComboOption | undefined = showCreate
|
|
503
|
+
? (() => {
|
|
504
|
+
const built =
|
|
505
|
+
typeof props.createNewEntry === "function"
|
|
506
|
+
? props.createNewEntry(query)
|
|
507
|
+
: { value: query, label: query };
|
|
508
|
+
return {
|
|
509
|
+
value: built.value,
|
|
510
|
+
label: built.label,
|
|
511
|
+
create: true,
|
|
512
|
+
query,
|
|
513
|
+
};
|
|
514
|
+
})()
|
|
515
|
+
: undefined;
|
|
516
|
+
|
|
517
|
+
const items: ComboOption[] = createOption
|
|
518
|
+
? [...filtered, createOption]
|
|
519
|
+
: filtered;
|
|
520
|
+
|
|
521
|
+
// Reconstruct option objects for the controlled value. Base UI matches them
|
|
522
|
+
// back to list items via `isItemEqualToValue` (by `value`), so identity
|
|
523
|
+
// across renders doesn't matter.
|
|
524
|
+
const toValueObject = (val: string): ComboOption =>
|
|
525
|
+
options.find((o) => o.value === val) ?? {
|
|
526
|
+
value: val,
|
|
527
|
+
label: labelFor(val),
|
|
528
|
+
};
|
|
529
|
+
|
|
530
|
+
const cbValue = props.multi
|
|
531
|
+
? selected.map(toValueObject)
|
|
532
|
+
: selected[0]
|
|
533
|
+
? toValueObject(selected[0])
|
|
534
|
+
: null;
|
|
535
|
+
|
|
536
|
+
const remember = (o: ComboOption) => {
|
|
537
|
+
if (o.create) labelCache.current.set(o.value, o.query ?? o.value);
|
|
538
|
+
else labelCache.current.set(o.value, o.label);
|
|
539
|
+
};
|
|
540
|
+
|
|
541
|
+
const handleSingle = (o: ComboOption | null) => {
|
|
542
|
+
if (!o) {
|
|
543
|
+
props.onChange(undefined);
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
remember(o);
|
|
547
|
+
props.onChange(props.coerce(o.value));
|
|
548
|
+
setQuery("");
|
|
549
|
+
};
|
|
550
|
+
|
|
551
|
+
const handleMulti = (list: ComboOption[]) => {
|
|
552
|
+
for (const o of list) remember(o);
|
|
553
|
+
props.onChange(list.map((o) => props.coerce(o.value)));
|
|
554
|
+
setQuery("");
|
|
555
|
+
};
|
|
556
|
+
|
|
557
|
+
const emptyLabel =
|
|
558
|
+
props.placeholder ?? tr("controlSelect.select", { default: "Select…" });
|
|
559
|
+
const triggerLabel = selected[0] ? labelFor(selected[0]) : emptyLabel;
|
|
560
|
+
|
|
561
|
+
// The list (loading / empty / items) is identical for single and multi — only
|
|
562
|
+
// the trigger differs (button vs. chips box), so render it once.
|
|
563
|
+
const popupBody = props.loading ? (
|
|
564
|
+
<div className="text-muted-foreground flex items-center justify-center gap-2 p-4 text-sm">
|
|
565
|
+
<Loader2 className="size-4 animate-spin" />{" "}
|
|
566
|
+
{tr("controlSelect.loading", { default: "Loading…" })}
|
|
567
|
+
</div>
|
|
568
|
+
) : (
|
|
569
|
+
<>
|
|
570
|
+
<ComboboxEmpty>
|
|
571
|
+
{props.createNewEntry
|
|
572
|
+
? ""
|
|
573
|
+
: tr("controlSelect.noResults", { default: "No results." })}
|
|
574
|
+
</ComboboxEmpty>
|
|
575
|
+
<ComboboxList>
|
|
576
|
+
{(opt: ComboOption) =>
|
|
577
|
+
opt.create ? (
|
|
578
|
+
<ComboboxItem key={`__create__${opt.value}`} value={opt}>
|
|
579
|
+
<span className="mr-2">+</span>
|
|
580
|
+
{tr("controlSelect.create", {
|
|
581
|
+
default: `Create "${opt.query}"`,
|
|
582
|
+
args: [String(opt.query ?? "")],
|
|
583
|
+
})}
|
|
584
|
+
</ComboboxItem>
|
|
585
|
+
) : (
|
|
586
|
+
<ComboboxItem key={opt.value} value={opt} disabled={opt.disabled}>
|
|
587
|
+
{opt.icon && (
|
|
588
|
+
<span className="mr-2 flex shrink-0 items-center">
|
|
589
|
+
{opt.icon}
|
|
590
|
+
</span>
|
|
591
|
+
)}
|
|
592
|
+
<div className="flex min-w-0 flex-1 flex-col">
|
|
593
|
+
<div className="flex items-center gap-1.5">
|
|
594
|
+
{opt.tag && (
|
|
595
|
+
<span className="bg-muted text-muted-foreground rounded px-1 text-[10px] uppercase tracking-wide">
|
|
596
|
+
{opt.tag}
|
|
597
|
+
</span>
|
|
598
|
+
)}
|
|
599
|
+
<span className="truncate">{opt.label}</span>
|
|
600
|
+
</div>
|
|
601
|
+
{opt.description && (
|
|
602
|
+
<span className="text-muted-foreground text-xs truncate">
|
|
603
|
+
{opt.description}
|
|
604
|
+
</span>
|
|
605
|
+
)}
|
|
606
|
+
</div>
|
|
607
|
+
</ComboboxItem>
|
|
608
|
+
)
|
|
609
|
+
}
|
|
610
|
+
</ComboboxList>
|
|
611
|
+
</>
|
|
612
|
+
);
|
|
613
|
+
|
|
614
|
+
return (
|
|
615
|
+
<ComboboxRoot
|
|
616
|
+
items={items as never}
|
|
617
|
+
multiple={props.multi}
|
|
618
|
+
disabled={props.disabled}
|
|
619
|
+
value={cbValue as never}
|
|
620
|
+
onValueChange={
|
|
621
|
+
(props.multi
|
|
622
|
+
? (v: ComboOption[]) => handleMulti(v)
|
|
623
|
+
: (v: ComboOption | null) => handleSingle(v)) as never
|
|
624
|
+
}
|
|
625
|
+
isItemEqualToValue={
|
|
626
|
+
((a: ComboOption, b: ComboOption) => a.value === b.value) as never
|
|
627
|
+
}
|
|
628
|
+
filter={null}
|
|
629
|
+
// Base UI leaves `autoHighlight` off by default, so nothing is
|
|
630
|
+
// highlighted while typing and Enter has no target — the user has to
|
|
631
|
+
// click a row (including the `createNewEntry` one). Highlighting the
|
|
632
|
+
// first match makes Enter pick it, which is what a search field is
|
|
633
|
+
// expected to do. It only engages while the query is non-empty, so
|
|
634
|
+
// opening the popup still starts with no preselection.
|
|
635
|
+
autoHighlight
|
|
636
|
+
onInputValueChange={(v) => {
|
|
637
|
+
setQuery(v);
|
|
638
|
+
props.onSearch?.(v);
|
|
639
|
+
}}
|
|
640
|
+
>
|
|
641
|
+
{props.multi ? (
|
|
642
|
+
// Multi-select: removable chips for each value with an inline search
|
|
643
|
+
// input, all inside the bordered chips box (Base UI's native shape).
|
|
644
|
+
<>
|
|
645
|
+
<ComboboxChips
|
|
646
|
+
ref={anchor}
|
|
647
|
+
id={props.id}
|
|
648
|
+
className={cn(
|
|
649
|
+
"w-full",
|
|
650
|
+
props.disabled && "pointer-events-none opacity-50",
|
|
651
|
+
)}
|
|
652
|
+
>
|
|
653
|
+
{props.icon && (
|
|
654
|
+
<props.icon className="text-muted-foreground ml-1 size-4 shrink-0" />
|
|
655
|
+
)}
|
|
656
|
+
{selected.map((val) => (
|
|
657
|
+
<ComboboxChip key={val} showRemove={!disabledFor(val)}>
|
|
658
|
+
{labelFor(val)}
|
|
659
|
+
</ComboboxChip>
|
|
660
|
+
))}
|
|
661
|
+
<ComboboxChipsInput
|
|
662
|
+
placeholder={
|
|
663
|
+
selected.length === 0
|
|
664
|
+
? (props.placeholder ?? "Search…")
|
|
665
|
+
: "Search…"
|
|
666
|
+
}
|
|
667
|
+
/>
|
|
668
|
+
</ComboboxChips>
|
|
669
|
+
<ComboboxContent anchor={anchor}>{popupBody}</ComboboxContent>
|
|
670
|
+
</>
|
|
671
|
+
) : (
|
|
672
|
+
// Single-select: a button trigger styled to mirror <SelectTrigger>,
|
|
673
|
+
// with the search input inside the popup.
|
|
674
|
+
<>
|
|
675
|
+
<ComboboxTrigger
|
|
676
|
+
id={props.id}
|
|
677
|
+
disabled={props.disabled}
|
|
678
|
+
className={cn(
|
|
679
|
+
"flex h-8 w-full items-center justify-between gap-1.5 rounded-lg border border-input bg-transparent py-2 pr-2 pl-2.5 text-sm whitespace-nowrap transition-colors outline-none select-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50 dark:bg-input/30 dark:hover:bg-input/50",
|
|
680
|
+
selected.length === 0 && "text-muted-foreground",
|
|
681
|
+
)}
|
|
682
|
+
>
|
|
683
|
+
<span className="flex min-w-0 items-center gap-2">
|
|
684
|
+
{props.icon && (
|
|
685
|
+
<props.icon className="text-muted-foreground size-4 shrink-0" />
|
|
686
|
+
)}
|
|
687
|
+
<span className="truncate">{triggerLabel}</span>
|
|
688
|
+
</span>
|
|
689
|
+
</ComboboxTrigger>
|
|
690
|
+
<ComboboxContent>
|
|
691
|
+
<ComboboxInput showTrigger={false} placeholder="Search…" />
|
|
692
|
+
{popupBody}
|
|
693
|
+
</ComboboxContent>
|
|
694
|
+
</>
|
|
695
|
+
)}
|
|
696
|
+
</ComboboxRoot>
|
|
697
|
+
);
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
const useAsyncLoader = (
|
|
701
|
+
loader: ControlSelectProps["loader"],
|
|
702
|
+
threshold: number,
|
|
703
|
+
debounceMs: number,
|
|
704
|
+
defaultValue: unknown,
|
|
705
|
+
) => {
|
|
706
|
+
const [data, setData] = useState<SelectOption[]>([]);
|
|
707
|
+
const [loading, setLoading] = useState(false);
|
|
708
|
+
const [mode, setMode] = useState<LoaderMode>("static");
|
|
709
|
+
const cache = useRef(new Map<string, SelectOption[]>());
|
|
710
|
+
|
|
711
|
+
useAction(
|
|
712
|
+
{
|
|
713
|
+
// `id`, not `name`: `id` is what useAction threads into its
|
|
714
|
+
// react:action:* events. `name` was accepted and never read, so this
|
|
715
|
+
// identifier reached nothing.
|
|
716
|
+
id: "select:loader:init",
|
|
717
|
+
runOnInit: true,
|
|
718
|
+
handler: async () => {
|
|
719
|
+
if (!loader) {
|
|
720
|
+
setMode("static");
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
setLoading(true);
|
|
724
|
+
try {
|
|
725
|
+
const result = await loader("");
|
|
726
|
+
const isShort = result.length <= threshold;
|
|
727
|
+
setMode(isShort ? "short" : "long");
|
|
728
|
+
cache.current.set("", result);
|
|
729
|
+
setData(result);
|
|
730
|
+
|
|
731
|
+
if (!isShort && defaultValue != null && String(defaultValue) !== "") {
|
|
732
|
+
const resolved = await loader("", [String(defaultValue)]);
|
|
733
|
+
if (resolved.length > 0) {
|
|
734
|
+
setData((prev) => {
|
|
735
|
+
const existing = new Set(prev.map(optValue));
|
|
736
|
+
const fresh = resolved.filter(
|
|
737
|
+
(r) => !existing.has(optValue(r)),
|
|
738
|
+
);
|
|
739
|
+
return [...prev, ...fresh];
|
|
740
|
+
});
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
} finally {
|
|
744
|
+
setLoading(false);
|
|
745
|
+
}
|
|
746
|
+
},
|
|
747
|
+
},
|
|
748
|
+
[loader, threshold],
|
|
749
|
+
);
|
|
750
|
+
|
|
751
|
+
const search = useAction<[string]>(
|
|
752
|
+
{
|
|
753
|
+
debounce: debounceMs,
|
|
754
|
+
handler: async (text) => {
|
|
755
|
+
if (!loader || mode !== "long") return;
|
|
756
|
+
if (cache.current.has(text)) {
|
|
757
|
+
setData(cache.current.get(text)!);
|
|
758
|
+
return;
|
|
759
|
+
}
|
|
760
|
+
setLoading(true);
|
|
761
|
+
try {
|
|
762
|
+
const result = await loader(text);
|
|
763
|
+
cache.current.set(text, result);
|
|
764
|
+
setData(result);
|
|
765
|
+
} finally {
|
|
766
|
+
setLoading(false);
|
|
767
|
+
}
|
|
768
|
+
},
|
|
769
|
+
},
|
|
770
|
+
[loader, mode, debounceMs],
|
|
771
|
+
);
|
|
772
|
+
|
|
773
|
+
return { data, loading, mode, search };
|
|
774
|
+
};
|