@robert-stackflow/oneui 0.4.2 → 0.5.1

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.
@@ -4,7 +4,7 @@ import { fieldTriggerProps, useFieldControl } from "./field.js";
4
4
  import { MenuItem } from "./MenuItem.js";
5
5
  import { SelectTrigger } from "./SelectTrigger.js";
6
6
  import { useEffect, useId, useRef, useState } from "react";
7
- import { Search } from "lucide-react";
7
+ import { Plus, Search } from "lucide-react";
8
8
  import { Popover } from "./Popover.js";
9
9
  import { usePopover } from "./usePopover.js";
10
10
  import { Button } from "./Button.js";
@@ -28,6 +28,7 @@ function MultiSelect({
28
28
  clearable = true,
29
29
  placeholder = "\u8BF7\u9009\u62E9",
30
30
  display = "summary",
31
+ onCreate,
31
32
  ...aria
32
33
  }) {
33
34
  const field = useFieldControl({
@@ -43,31 +44,78 @@ function MultiSelect({
43
44
  });
44
45
  const pop = usePopover(
45
46
  field.disabled || field.readOnly
46
- ), [query, setQuery] = useState(""), list = useRef(null), search = useRef(null), id = useId();
47
+ ), [query, setQuery] = useState(""), [created, setCreated] = useState([]), [creating, setCreating] = useState(false), [createError, setCreateError] = useState(""), generation = useRef(0), open = useRef(pop.open), selected = useRef(value), list = useRef(null), search = useRef(null), id = useId();
47
48
  const { disabled: triggerDisabled, ...tagField } = fieldTriggerProps(field);
48
- const reserveIcon = options.some((option) => !!option.icon);
49
- const filtered = options.filter(
49
+ open.current = pop.open;
50
+ selected.current = value;
51
+ const available = [
52
+ ...options,
53
+ ...created.filter(
54
+ (item) => !options.some((option) => option.value === item.value)
55
+ )
56
+ ];
57
+ const reserveIcon = available.some((option) => !!option.icon);
58
+ const filtered = available.filter(
50
59
  (item) => item.label.toLowerCase().includes(query.toLowerCase())
51
60
  );
52
61
  const toggle = (option) => {
53
- if (!field.disabled && !field.readOnly && !option.disabled && (value.includes(option.value) || value.length < maxSelected))
62
+ if (!creating && !field.disabled && !field.readOnly && !option.disabled && (value.includes(option.value) || value.length < maxSelected))
54
63
  onChange(
55
64
  value.includes(option.value) ? value.filter((v) => v !== option.value) : [...value, option.value]
56
65
  );
57
66
  };
67
+ const candidate = query.trim();
68
+ const canCreate = !!onCreate && !!candidate && !available.some(
69
+ (option) => option.label.toLowerCase() === candidate.toLowerCase()
70
+ );
71
+ const create = async () => {
72
+ if (!onCreate || !canCreate || creating || field.disabled || field.readOnly || value.length >= maxSelected)
73
+ return;
74
+ const revision = generation.current;
75
+ setCreating(true);
76
+ setCreateError("");
77
+ try {
78
+ const id2 = await onCreate(candidate) || candidate;
79
+ if (!open.current || revision !== generation.current) return;
80
+ setCreated(
81
+ (previous) => previous.some((option) => option.value === id2) ? previous : [...previous, { value: id2, label: candidate }]
82
+ );
83
+ if (!selected.current.includes(id2)) onChange([...selected.current, id2]);
84
+ setQuery("");
85
+ requestAnimationFrame(() => {
86
+ if (open.current) search.current?.focus();
87
+ });
88
+ } catch (failure) {
89
+ if (open.current && revision === generation.current)
90
+ setCreateError(
91
+ failure instanceof Error ? failure.message : "\u521B\u5EFA\u5931\u8D25\uFF0C\u8BF7\u91CD\u8BD5\u3002"
92
+ );
93
+ } finally {
94
+ if (revision === generation.current) setCreating(false);
95
+ }
96
+ };
97
+ useEffect(
98
+ () => () => {
99
+ generation.current++;
100
+ },
101
+ []
102
+ );
58
103
  useEffect(() => {
104
+ generation.current++;
105
+ setCreating(false);
106
+ setCreateError("");
59
107
  if (pop.open) {
60
108
  setQuery("");
61
109
  search.current?.focus();
62
110
  }
63
111
  }, [pop.open]);
64
112
  const remove = (id2) => {
65
- if (field.disabled || field.readOnly || options.find((option) => option.value === id2)?.disabled)
113
+ if (creating || field.disabled || field.readOnly || available.find((option) => option.value === id2)?.disabled)
66
114
  return;
67
115
  onChange(value.filter((v) => v !== id2));
68
116
  pop.anchor.current?.focus();
69
117
  };
70
- const summary = value.length ? options.filter((item) => value.includes(item.value)).map((item) => item.label).slice(0, 2).join("\u3001") + (value.length > 2 ? ` +${value.length - 2}` : "") : placeholder;
118
+ const summary = value.length ? available.filter((item) => value.includes(item.value)).map((item) => item.label).slice(0, 2).join("\u3001") + (value.length > 2 ? ` +${value.length - 2}` : "") : placeholder;
71
119
  return /* @__PURE__ */ jsxs(Fragment, { children: [
72
120
  display === "tags" ? /* @__PURE__ */ jsxs(Fragment, { children: [
73
121
  /* @__PURE__ */ jsxs(
@@ -100,13 +148,13 @@ function MultiSelect({
100
148
  },
101
149
  children: [
102
150
  value.map((id2) => {
103
- const option = options.find((option2) => option2.value === id2);
151
+ const option = available.find((option2) => option2.value === id2);
104
152
  return /* @__PURE__ */ jsx(
105
153
  SelectionTag,
106
154
  {
107
155
  label: option?.label || id2,
108
156
  icon: option?.icon,
109
- disabled: field.disabled || option?.disabled,
157
+ disabled: field.disabled || creating || option?.disabled,
110
158
  onRemove: field.readOnly ? void 0 : () => remove(id2),
111
159
  removeLabel: "\u79FB\u9664 " + (option?.label || id2)
112
160
  },
@@ -163,7 +211,18 @@ function MultiSelect({
163
211
  "aria-label": `\u641C\u7D22${label}`,
164
212
  placeholder: "\u641C\u7D22\u9009\u9879\u2026",
165
213
  value: query,
166
- onChange: (e) => setQuery(e.target.value)
214
+ disabled: creating,
215
+ onChange: (e) => {
216
+ setQuery(e.target.value);
217
+ setCreateError("");
218
+ },
219
+ onKeyDown: (event) => {
220
+ if (event.key === "Enter" && canCreate) {
221
+ event.preventDefault();
222
+ event.stopPropagation();
223
+ void create();
224
+ }
225
+ }
167
226
  }
168
227
  )
169
228
  ] }),
@@ -188,12 +247,22 @@ function MultiSelect({
188
247
  selection: "checkbox",
189
248
  role: "option",
190
249
  "aria-selected": value.includes(option.value),
191
- disabled: option.disabled || !value.includes(option.value) && value.length >= maxSelected,
250
+ disabled: creating || option.disabled || !value.includes(option.value) && value.length >= maxSelected,
192
251
  onClick: () => toggle(option)
193
252
  },
194
253
  option.value
195
254
  )),
196
- !filtered.length && /* @__PURE__ */ jsx("p", { className: s.noResults, children: "\u6CA1\u6709\u5339\u914D\u7684\u9009\u9879" })
255
+ canCreate && /* @__PURE__ */ jsx(
256
+ MenuItem,
257
+ {
258
+ label: creating ? "\u6B63\u5728\u521B\u5EFA\u2026" : "\u521B\u5EFA\u201C" + candidate + "\u201D",
259
+ icon: /* @__PURE__ */ jsx(Plus, { size: 16 }),
260
+ disabled: creating || value.length >= maxSelected,
261
+ onClick: () => void create()
262
+ }
263
+ ),
264
+ !filtered.length && !canCreate && /* @__PURE__ */ jsx("p", { className: s.noResults, children: "\u6CA1\u6709\u5339\u914D\u7684\u9009\u9879" }),
265
+ createError && /* @__PURE__ */ jsx("p", { className: s.createError, role: "alert", children: createError })
197
266
  ]
198
267
  }
199
268
  ),
@@ -203,7 +272,7 @@ function MultiSelect({
203
272
  {
204
273
  variant: "ghost",
205
274
  size: "small",
206
- disabled: !value.length,
275
+ disabled: creating || !value.length,
207
276
  onClick: () => onChange([]),
208
277
  children: "\u6E05\u7A7A"
209
278
  }
@@ -97,3 +97,9 @@
97
97
  padding: 15px;
98
98
  text-align: center;
99
99
  }
100
+
101
+ .createError {
102
+ color: var(--danger);
103
+ padding: 8px 10px;
104
+ font-size: 12px;
105
+ }
@@ -1,6 +1,6 @@
1
1
  import type { ReactNode } from "react";
2
2
  export declare function Page({ title, description, actions, children, }: {
3
- title: string;
3
+ title?: ReactNode;
4
4
  description?: string;
5
5
  actions?: ReactNode;
6
6
  children: ReactNode;
@@ -7,9 +7,9 @@ function Page({
7
7
  children
8
8
  }) {
9
9
  return /* @__PURE__ */ jsxs("div", { className: s.page, children: [
10
- /* @__PURE__ */ jsxs("header", { className: s.heading, children: [
10
+ (title || description || actions) && /* @__PURE__ */ jsxs("header", { className: s.heading, children: [
11
11
  /* @__PURE__ */ jsxs("div", { children: [
12
- /* @__PURE__ */ jsx("h1", { children: title }),
12
+ title && /* @__PURE__ */ jsx("h1", { children: title }),
13
13
  description && /* @__PURE__ */ jsx("p", { children: description })
14
14
  ] }),
15
15
  actions && /* @__PURE__ */ jsx("div", { className: s.actions, children: actions })
@@ -1,4 +1,5 @@
1
1
  export { Badge } from "./Badge";
2
+ export { ActionSheet, type ActionSheetItem } from "./ActionSheet";
2
3
  export { Button } from "./Button";
3
4
  export { Checkbox } from "./Checkbox";
4
5
  export { ColorField } from "./ColorField";
@@ -150,7 +151,7 @@ export { ThemeProvider } from "./ThemeProvider";
150
151
  export type { UITheme, ColorMode } from "./ThemeProvider";
151
152
  export { ThemeStylePicker } from "./ThemeStylePicker";
152
153
  export { AppShell, ShellHeader } from "./AppShell";
153
- export type { NavigationItem, AppShellProps } from "./AppShell";
154
+ export type { NavigationItem, NavigationGroup, AppShellProps, } from "./AppShell";
154
155
  export { SettingsLayout, SettingsCard } from "./SettingsLayout";
155
156
  export { AppearanceSettings } from "./AppearanceSettings";
156
157
  export { appearanceDefaults, normalizeAppearance } from "./appearance";
@@ -1,4 +1,5 @@
1
1
  import { Badge } from "./Badge.js";
2
+ import { ActionSheet } from "./ActionSheet.js";
2
3
  import { Button } from "./Button.js";
3
4
  import { Checkbox } from "./Checkbox.js";
4
5
  import { ColorField } from "./ColorField.js";
@@ -117,6 +118,7 @@ import {
117
118
  export {
118
119
  Accordion,
119
120
  AccountItem,
121
+ ActionSheet,
120
122
  Alert,
121
123
  AppShell,
122
124
  AppearanceSettings,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@robert-stackflow/oneui",
3
- "version": "0.4.2",
3
+ "version": "0.5.1",
4
4
  "description": "OneUI React components, application shell and portable visual themes",
5
5
  "type": "module",
6
6
  "sideEffects": [
@@ -14,6 +14,12 @@
14
14
  },
15
15
  "./styles/*": "./styles/*",
16
16
  "./assets/*": "./assets/*",
17
+ "./ActionSheet": {
18
+ "types": "./components/ActionSheet.d.ts",
19
+ "import": "./components/ActionSheet.js",
20
+ "default": "./components/ActionSheet.js"
21
+ },
22
+ "./ActionSheet.module.css": "./components/ActionSheet.module.css",
17
23
  "./Accessibility.module.css": "./components/Accessibility.module.css",
18
24
  "./Accordion.module.css": "./components/Accordion.module.css",
19
25
  "./Accordion": {