@robert-stackflow/oneui 0.2.0 → 0.4.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.
Files changed (44) hide show
  1. package/CHANGELOG.md +37 -18
  2. package/README.md +110 -73
  3. package/components/AccountItem.d.ts +15 -0
  4. package/components/AccountItem.js +38 -0
  5. package/components/AccountItem.module.css +103 -0
  6. package/components/AppShell.d.ts +11 -4
  7. package/components/AppShell.js +79 -54
  8. package/components/AppShell.module.css +13 -1
  9. package/components/Calendar.d.ts +2 -1
  10. package/components/Calendar.js +4 -3
  11. package/components/DatePicker.d.ts +3 -0
  12. package/components/DatePicker.js +41 -21
  13. package/components/DatePicker.module.css +3 -0
  14. package/components/Dialog.js +6 -3
  15. package/components/Form.d.ts +2 -1
  16. package/components/Form.js +5 -2
  17. package/components/FormField.d.ts +2 -1
  18. package/components/FormField.js +45 -19
  19. package/components/FormField.module.css +40 -0
  20. package/components/FormGroup.d.ts +2 -1
  21. package/components/FormGroup.js +26 -12
  22. package/components/FormLayout.d.ts +6 -0
  23. package/components/FormLayout.js +7 -0
  24. package/components/IconPicker.d.ts +14 -4
  25. package/components/IconPicker.js +67 -25
  26. package/components/NumberInput.d.ts +12 -4
  27. package/components/NumberInput.js +41 -20
  28. package/components/PasskeyItem.d.ts +11 -0
  29. package/components/PasskeyItem.js +53 -0
  30. package/components/RadioGroup.js +5 -3
  31. package/components/SessionItem.d.ts +11 -0
  32. package/components/SessionItem.js +44 -0
  33. package/components/ToastProvider.d.ts +4 -4
  34. package/components/ToastProvider.js +19 -22
  35. package/components/VirtualPickerList.d.ts +2 -0
  36. package/components/VirtualPickerList.js +1 -1
  37. package/components/calendarNow.d.ts +5 -0
  38. package/components/calendarNow.js +27 -0
  39. package/components/createToaster.d.ts +12 -0
  40. package/components/createToaster.js +40 -0
  41. package/components/index.d.ts +10 -2
  42. package/components/index.js +13 -1
  43. package/package.json +30 -4
  44. package/styles/minimal.css +1 -0
@@ -20,14 +20,15 @@ function Calendar({
20
20
  onHover,
21
21
  minDate,
22
22
  maxDate,
23
- disabledDate
23
+ disabledDate,
24
+ today = dateKey(/* @__PURE__ */ new Date())
24
25
  }) {
25
26
  const constraints = { minDate, maxDate, disabledDate };
26
27
  const [month, setMonth] = useState(
27
- () => readDate(boundedDate(value, constraints))
28
+ () => readDate(boundedDate(value || today, constraints))
28
29
  ), [view, setView] = useState("days"), [focused, setFocused] = useState(value);
29
30
  const grid = useRef(null), choices = useRef(null), pending = useRef(null), pendingView = useRef(false);
30
- const year = month.getFullYear(), index = month.getMonth(), start = (calendarDate(year, index).getDay() + 6) % 7, base = Math.floor(year / 12) * 12, today = dateKey(/* @__PURE__ */ new Date());
31
+ const year = month.getFullYear(), index = month.getMonth(), start = (calendarDate(year, index).getDay() + 6) % 7, base = Math.floor(year / 12) * 12;
31
32
  const allowed = (key) => isDateAllowed(key, constraints);
32
33
  const monthAllowed = (y, m) => y >= 1 && y <= 9999 && !(minDate && parseDate(minDate) && dateKey(calendarDate(y, m + 1, 0)) < minDate) && !(maxDate && parseDate(maxDate) && dateKey(calendarDate(y, m)) > maxDate);
33
34
  const periodAllowed = (amount) => {
@@ -8,6 +8,9 @@ export type SingleDatePickerProps = FieldOptions & DateConstraints & {
8
8
  label: string;
9
9
  mode?: Exclude<DatePickerMode, "range">;
10
10
  seconds?: boolean;
11
+ timeZone?: string;
12
+ clearable?: boolean;
13
+ placeholder?: string;
11
14
  };
12
15
  export type DatePickerProps = SingleDatePickerProps | (DateRangePickerProps & {
13
16
  mode: "range";
@@ -5,7 +5,7 @@ import { useId, useState } from "react";
5
5
  import { CalendarDays, Clock3 } from "lucide-react";
6
6
  import { Popover } from "./Popover.js";
7
7
  import { usePopover } from "./usePopover.js";
8
- import { Calendar, dateKey } from "./Calendar.js";
8
+ import { Calendar } from "./Calendar.js";
9
9
  import { TimeColumns } from "./TimeColumns.js";
10
10
  import { Button } from "./Button.js";
11
11
  import s from "./DatePicker.module.css";
@@ -14,6 +14,7 @@ import {
14
14
  isDateAllowed
15
15
  } from "./dateConstraints.js";
16
16
  import { DateRangePicker } from "./DateRangePicker.js";
17
+ import { calendarNow } from "./calendarNow.js";
17
18
  function DatePicker(props) {
18
19
  return props.mode === "range" ? /* @__PURE__ */ jsx(DateRangePicker, { ...props }) : /* @__PURE__ */ jsx(SingleDatePicker, { ...props });
19
20
  }
@@ -23,6 +24,9 @@ function SingleDatePicker({
23
24
  label,
24
25
  mode = "date",
25
26
  seconds = false,
27
+ timeZone,
28
+ clearable = false,
29
+ placeholder = "\u8BF7\u9009\u62E9",
26
30
  disabled,
27
31
  readOnly,
28
32
  name,
@@ -47,7 +51,8 @@ function SingleDatePicker({
47
51
  hint,
48
52
  ...aria
49
53
  });
50
- const pop = usePopover(field.disabled || field.readOnly), id = useId(), [date, setDate] = useState(value.slice(0, 10) || dateKey(/* @__PURE__ */ new Date())), [time, setTime] = useState("00:00");
54
+ const now = () => calendarNow(timeZone), today = now().date;
55
+ const pop = usePopover(field.disabled || field.readOnly), id = useId(), [date, setDate] = useState(value.slice(0, 10) || today), [time, setTime] = useState("00:00");
51
56
  const close = pop.closeAndFocus;
52
57
  const show = () => {
53
58
  if (field.disabled || field.readOnly) return;
@@ -56,7 +61,7 @@ function SingleDatePicker({
56
61
  return;
57
62
  }
58
63
  setDate(
59
- mode === "time" ? dateKey(/* @__PURE__ */ new Date()) : boundedDate(value.slice(0, 10), constraints)
64
+ mode === "time" ? now().date : boundedDate(value.slice(0, 10) || now().date, constraints)
60
65
  );
61
66
  setTime(
62
67
  (mode === "time" ? value : value.split(/[T ]/)[1]) || (seconds ? "00:00:00" : "00:00")
@@ -82,6 +87,7 @@ function SingleDatePicker({
82
87
  "aria-expanded": pop.open,
83
88
  "aria-haspopup": "dialog",
84
89
  "aria-controls": id,
90
+ "data-placeholder": !value || void 0,
85
91
  onClick: show,
86
92
  onKeyDown: (e) => {
87
93
  if (e.key === "ArrowDown" && !pop.open) {
@@ -90,7 +96,7 @@ function SingleDatePicker({
90
96
  }
91
97
  },
92
98
  children: [
93
- /* @__PURE__ */ jsx("span", { children: value.replace("T", " ") || "\u8BF7\u9009\u62E9" }),
99
+ /* @__PURE__ */ jsx("span", { children: value.replace("T", " ") || placeholder }),
94
100
  mode === "time" ? /* @__PURE__ */ jsx(Clock3, {}) : /* @__PURE__ */ jsx(CalendarDays, {})
95
101
  ]
96
102
  }
@@ -119,6 +125,7 @@ function SingleDatePicker({
119
125
  Calendar,
120
126
  {
121
127
  ...constraints,
128
+ today,
122
129
  key: pop.open ? "open" : "closed",
123
130
  value: date,
124
131
  onChange: (next) => {
@@ -143,16 +150,14 @@ function SingleDatePicker({
143
150
  ] }),
144
151
  /* @__PURE__ */ jsxs("footer", { className: s.footer, children: [
145
152
  /* @__PURE__ */ jsx(
146
- "button",
153
+ Button,
147
154
  {
155
+ variant: "ghost",
156
+ size: "small",
148
157
  type: "button",
149
- disabled: mode !== "time" && !isDateAllowed(dateKey(/* @__PURE__ */ new Date()), constraints),
158
+ disabled: mode !== "time" && !isDateAllowed(today, constraints),
150
159
  onClick: () => {
151
- const now = /* @__PURE__ */ new Date(), nextDate = dateKey(now), nextTime = [
152
- now.getHours(),
153
- now.getMinutes(),
154
- ...seconds ? [now.getSeconds()] : []
155
- ].map((v) => String(v).padStart(2, "0")).join(":");
160
+ const current = now(), nextDate = current.date, nextTime = current.time.slice(0, seconds ? 8 : 5);
156
161
  if (mode !== "time" && !isDateAllowed(nextDate, constraints))
157
162
  return;
158
163
  setDate(nextDate);
@@ -165,16 +170,31 @@ function SingleDatePicker({
165
170
  children: mode === "date" ? "\u4ECA\u5929" : "\u6B64\u523B"
166
171
  }
167
172
  ),
168
- mode !== "date" && /* @__PURE__ */ jsx(
169
- Button,
170
- {
171
- variant: "primary",
172
- size: "small",
173
- onClick: commit,
174
- disabled: mode !== "time" && !isDateAllowed(date, constraints),
175
- children: "\u786E\u5B9A"
176
- }
177
- )
173
+ (clearable || mode !== "date") && /* @__PURE__ */ jsxs("div", { className: s.rangeActions, children: [
174
+ clearable && /* @__PURE__ */ jsx(
175
+ Button,
176
+ {
177
+ size: "small",
178
+ variant: "ghost",
179
+ disabled: !value,
180
+ onClick: () => {
181
+ onChange("");
182
+ close();
183
+ },
184
+ children: "\u6E05\u7A7A"
185
+ }
186
+ ),
187
+ mode !== "date" && /* @__PURE__ */ jsx(
188
+ Button,
189
+ {
190
+ variant: "primary",
191
+ size: "small",
192
+ onClick: commit,
193
+ disabled: mode !== "time" && !isDateAllowed(date, constraints),
194
+ children: "\u786E\u5B9A"
195
+ }
196
+ )
197
+ ] })
178
198
  ] })
179
199
  ]
180
200
  }
@@ -12,6 +12,9 @@
12
12
  height: 15px;
13
13
  color: var(--muted);
14
14
  }
15
+ .trigger[data-placeholder="true"] > span {
16
+ color: var(--placeholder);
17
+ }
15
18
  .body {
16
19
  display: flex;
17
20
  }
@@ -39,6 +39,9 @@ function Dialog({
39
39
  useModalScrollLock(open || closing);
40
40
  const opened = useRef(open);
41
41
  opened.current = open;
42
+ const lastContent = useRef({ title, children, footer });
43
+ if (open) lastContent.current = { title, children, footer };
44
+ const content = !open && (closing || ref.current?.open) ? lastContent.current : { title, children, footer };
42
45
  useEffect(() => {
43
46
  if (requestedOpen && owner?.active === false) onClose();
44
47
  }, [requestedOpen, owner?.active, onClose]);
@@ -109,11 +112,11 @@ function Dialog({
109
112
  },
110
113
  children: [
111
114
  /* @__PURE__ */ jsxs("header", { className: s.dialogHeader, children: [
112
- /* @__PURE__ */ jsx("h2", { id: titleId, children: title }),
115
+ /* @__PURE__ */ jsx("h2", { id: titleId, children: content.title }),
113
116
  /* @__PURE__ */ jsx(IconButton, { label: "\u5173\u95ED\u5F39\u7A97", onClick: onClose, disabled: busy, children: /* @__PURE__ */ jsx(X, {}) })
114
117
  ] }),
115
- /* @__PURE__ */ jsx("div", { className: s.dialogBody, children }),
116
- footer && /* @__PURE__ */ jsx("footer", { className: s.dialogFooter, children: footer })
118
+ /* @__PURE__ */ jsx("div", { className: s.dialogBody, children: content.children }),
119
+ content.footer && /* @__PURE__ */ jsx("footer", { className: s.dialogFooter, children: content.footer })
117
120
  ]
118
121
  }
119
122
  ) }) }),
@@ -1,6 +1,7 @@
1
1
  import type { FormHTMLAttributes, ReactNode } from "react";
2
2
  import type { FormController } from "./useFormState";
3
- export declare function Form<T extends Record<string, unknown>>({ controller, onSubmit, children, ...props }: Omit<FormHTMLAttributes<HTMLFormElement>, "onSubmit" | "onReset" | "children"> & {
3
+ import { type FormLayoutOptions } from "./FormLayout";
4
+ export declare function Form<T extends Record<string, unknown>>({ controller, onSubmit, children, layout, labelWidth, ...props }: Omit<FormHTMLAttributes<HTMLFormElement>, "onSubmit" | "onReset" | "children"> & FormLayoutOptions & {
4
5
  controller: FormController<T>;
5
6
  onSubmit(values: T): void | Promise<void>;
6
7
  children: ReactNode;
@@ -1,12 +1,15 @@
1
1
  import { jsx } from "react/jsx-runtime";
2
2
  import s from "./Form.module.css";
3
+ import { FormLayoutContext } from "./FormLayout.js";
3
4
  function Form({
4
5
  controller,
5
6
  onSubmit,
6
7
  children,
8
+ layout = "stacked",
9
+ labelWidth,
7
10
  ...props
8
11
  }) {
9
- return /* @__PURE__ */ jsx(
12
+ return /* @__PURE__ */ jsx(FormLayoutContext, { value: { layout, labelWidth }, children: /* @__PURE__ */ jsx(
10
13
  "form",
11
14
  {
12
15
  ...props,
@@ -19,7 +22,7 @@ function Form({
19
22
  },
20
23
  children: /* @__PURE__ */ jsx("fieldset", { className: s.fields, disabled: controller.isSubmitting, children })
21
24
  }
22
- );
25
+ ) });
23
26
  }
24
27
  export {
25
28
  Form
@@ -1,6 +1,7 @@
1
1
  import { type ReactNode } from "react";
2
2
  import type { FieldOptions } from "./field";
3
- export declare function FormField({ children, pending, requiredIndicator, optional, onBlur, ...options }: FieldOptions & {
3
+ import { type FormLayoutOptions } from "./FormLayout";
4
+ export declare function FormField({ children, pending, requiredIndicator, optional, onBlur, layout: suppliedLayout, labelWidth: suppliedLabelWidth, ...options }: FieldOptions & FormLayoutOptions & {
4
5
  children: ReactNode;
5
6
  pending?: boolean;
6
7
  requiredIndicator?: "asterisk" | "text" | "none";
@@ -1,28 +1,50 @@
1
- import { jsx, jsxs } from "react/jsx-runtime";
1
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
2
2
  import { useUIConfig } from "./UIProvider.js";
3
3
  import { FieldMessages } from "./FieldMessages.js";
4
4
  import { Spinner } from "./Spinner.js";
5
- import { useRef } from "react";
5
+ import { useContext, useRef } from "react";
6
6
  import { FieldContext, useFieldControl } from "./field.js";
7
7
  import { FieldLabel } from "./FieldLabel.js";
8
8
  import s from "./FormField.module.css";
9
+ import { FormLayoutContext } from "./FormLayout.js";
9
10
  function FormField({
10
11
  children,
11
12
  pending = false,
12
13
  requiredIndicator,
13
14
  optional = false,
14
15
  onBlur,
16
+ layout: suppliedLayout,
17
+ labelWidth: suppliedLabelWidth,
15
18
  ...options
16
19
  }) {
17
- const config = useUIConfig();
20
+ const config = useUIConfig(), inherited = useContext(FormLayoutContext);
18
21
  const field = useFieldControl(options), root = useRef(null);
19
- return /* @__PURE__ */ jsx(FieldContext, { value: field, children: /* @__PURE__ */ jsxs(
22
+ const showLabel = !!field.label || pending, layout = showLabel ? suppliedLayout ?? inherited.layout ?? "stacked" : "stacked", labelWidth = suppliedLabelWidth ?? inherited.labelWidth;
23
+ const label = showLabel ? /* @__PURE__ */ jsxs("div", { className: s.labelRow, children: [
24
+ field.label && /* @__PURE__ */ jsx(
25
+ FieldLabel,
26
+ {
27
+ label: field.label,
28
+ htmlFor: field.id,
29
+ required: field.required,
30
+ indicator: requiredIndicator ?? config.requiredIndicator,
31
+ optional
32
+ }
33
+ ),
34
+ " ",
35
+ pending && /* @__PURE__ */ jsx(Spinner, { size: 13, label: "\u6B63\u5728\u6821\u9A8C" + field.label })
36
+ ] }) : null;
37
+ return /* @__PURE__ */ jsx(FieldContext, { value: field, children: /* @__PURE__ */ jsx(
20
38
  "div",
21
39
  {
22
40
  ref: root,
23
41
  className: s.field,
24
42
  "data-invalid": !!field.error,
25
43
  "data-disabled": field.disabled || void 0,
44
+ "data-layout": layout,
45
+ style: labelWidth === void 0 ? void 0 : {
46
+ "--field-label-width": typeof labelWidth === "number" ? labelWidth + "px" : labelWidth
47
+ },
26
48
  onBlur: (event) => {
27
49
  const next = event.relatedTarget, node = root.current;
28
50
  if (next && node.contains(next)) return;
@@ -34,23 +56,27 @@ function FormField({
34
56
  if (next && portals.some((portal) => portal?.contains(next))) return;
35
57
  onBlur?.();
36
58
  },
37
- children: [
38
- /* @__PURE__ */ jsxs("div", { className: s.labelRow, children: [
39
- /* @__PURE__ */ jsx(
40
- FieldLabel,
41
- {
42
- label: field.label,
43
- htmlFor: field.id,
44
- required: field.required,
45
- indicator: requiredIndicator ?? config.requiredIndicator,
46
- optional
47
- }
48
- ),
49
- pending && /* @__PURE__ */ jsx(Spinner, { size: 13, label: "\u6B63\u5728\u6821\u9A8C" + field.label })
59
+ children: layout === "inline" ? /* @__PURE__ */ jsxs(Fragment, { children: [
60
+ /* @__PURE__ */ jsxs("div", { className: s.inlineLabel, children: [
61
+ label,
62
+ /* @__PURE__ */ jsx(FieldMessages, { id: field.id, hint: field.hint })
50
63
  ] }),
64
+ /* @__PURE__ */ jsxs("div", { className: s.inlineControl, children: [
65
+ /* @__PURE__ */ jsx("div", { className: s.controlContent, children }),
66
+ /* @__PURE__ */ jsx(FieldMessages, { id: field.id, error: field.error })
67
+ ] })
68
+ ] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
69
+ label,
51
70
  children,
52
- /* @__PURE__ */ jsx(FieldMessages, { id: field.id, hint: field.hint, error: field.error })
53
- ]
71
+ /* @__PURE__ */ jsx(
72
+ FieldMessages,
73
+ {
74
+ id: field.id,
75
+ hint: field.hint,
76
+ error: field.error
77
+ }
78
+ )
79
+ ] })
54
80
  }
55
81
  ) });
56
82
  }
@@ -79,6 +79,46 @@
79
79
  .label {
80
80
  font-size: var(--field-label-size);
81
81
  }
82
+ .field[data-layout="inline"] {
83
+ display: grid;
84
+ grid-template-columns: var(--field-label-width, minmax(160px, 0.8fr)) minmax(
85
+ 0,
86
+ 1.2fr
87
+ );
88
+ align-items: center;
89
+ gap: 24px;
90
+ }
91
+ .inlineLabel,
92
+ .inlineControl {
93
+ display: flex;
94
+ flex-direction: column;
95
+ gap: 6px;
96
+ min-width: 0;
97
+ }
98
+ .controlContent {
99
+ display: flex;
100
+ align-items: center;
101
+ justify-content: flex-end;
102
+ min-width: 0;
103
+ }
104
+ .controlContent > * {
105
+ max-width: 100%;
106
+ }
107
+ .inlineLabel .labelRow {
108
+ justify-content: flex-start;
109
+ }
110
+ .field[data-disabled="true"] .inlineLabel > .labelRow {
111
+ opacity: 0.55;
112
+ }
113
+ @media (max-width: 700px) {
114
+ .field[data-layout="inline"] {
115
+ display: flex;
116
+ gap: 8px;
117
+ }
118
+ .controlContent {
119
+ justify-content: flex-start;
120
+ }
121
+ }
82
122
  .hint,
83
123
  .error {
84
124
  font-size: var(--field-message-size);
@@ -1,5 +1,6 @@
1
1
  import type { ReactNode } from "react";
2
- export declare function FormGroup({ label, children, disabled, required, hint, error, }: {
2
+ import { type FormLayoutOptions } from "./FormLayout";
3
+ export declare function FormGroup({ label, children, disabled, required, hint, error, layout, labelWidth, }: FormLayoutOptions & {
3
4
  label: string;
4
5
  children: ReactNode;
5
6
  disabled?: boolean;
@@ -3,27 +3,41 @@ import { FieldMessages } from "./FieldMessages.js";
3
3
  import { useId } from "react";
4
4
  import { FieldLabel } from "./FieldLabel.js";
5
5
  import s from "./FormField.module.css";
6
+ import { useContext } from "react";
7
+ import { FormLayoutContext } from "./FormLayout.js";
6
8
  function FormGroup({
7
9
  label,
8
10
  children,
9
11
  disabled = false,
10
12
  required = false,
11
13
  hint,
12
- error
14
+ error,
15
+ layout,
16
+ labelWidth
13
17
  }) {
14
18
  const id = useId();
15
- return /* @__PURE__ */ jsxs(
16
- "fieldset",
19
+ const inherited = useContext(FormLayoutContext);
20
+ return /* @__PURE__ */ jsx(
21
+ FormLayoutContext,
17
22
  {
18
- className: s.group,
19
- disabled,
20
- "aria-invalid": !!error,
21
- "aria-describedby": [hint ? id + "-hint" : "", error ? id + "-error" : ""].filter(Boolean).join(" ") || void 0,
22
- children: [
23
- /* @__PURE__ */ jsx("legend", { children: /* @__PURE__ */ jsx(FieldLabel, { as: "span", label, required }) }),
24
- children,
25
- /* @__PURE__ */ jsx(FieldMessages, { id, hint, error })
26
- ]
23
+ value: {
24
+ layout: layout ?? inherited.layout,
25
+ labelWidth: labelWidth ?? inherited.labelWidth
26
+ },
27
+ children: /* @__PURE__ */ jsxs(
28
+ "fieldset",
29
+ {
30
+ className: s.group,
31
+ disabled,
32
+ "aria-invalid": !!error,
33
+ "aria-describedby": [hint ? id + "-hint" : "", error ? id + "-error" : ""].filter(Boolean).join(" ") || void 0,
34
+ children: [
35
+ /* @__PURE__ */ jsx("legend", { children: /* @__PURE__ */ jsx(FieldLabel, { as: "span", label, required }) }),
36
+ children,
37
+ /* @__PURE__ */ jsx(FieldMessages, { id, hint, error })
38
+ ]
39
+ }
40
+ )
27
41
  }
28
42
  );
29
43
  }
@@ -0,0 +1,6 @@
1
+ export type FormLayout = "stacked" | "inline";
2
+ export type FormLayoutOptions = {
3
+ layout?: FormLayout;
4
+ labelWidth?: number | string;
5
+ };
6
+ export declare const FormLayoutContext: import("react").Context<FormLayoutOptions>;
@@ -0,0 +1,7 @@
1
+ import { createContext } from "react";
2
+ const FormLayoutContext = createContext({
3
+ layout: "stacked"
4
+ });
5
+ export {
6
+ FormLayoutContext
7
+ };
@@ -1,6 +1,16 @@
1
- export declare function IconPicker({ value, onChange, label, disabled, }: {
1
+ import { type ReactNode } from "react";
2
+ import { type FieldOptions } from "./field";
3
+ export type IconPickerOption = {
4
+ value: string;
5
+ label: string;
6
+ icon: ReactNode;
7
+ keywords?: string[];
8
+ category?: string;
9
+ };
10
+ export type IconPickerProps = FieldOptions & {
2
11
  value: string;
3
12
  onChange(value: string): void;
4
- label?: string;
5
- disabled?: boolean;
6
- }): import("react").JSX.Element;
13
+ options?: IconPickerOption[];
14
+ valuePrefix?: string;
15
+ };
16
+ export declare function IconPicker({ value, onChange, label, options, valuePrefix, ...props }: IconPickerProps): import("react").JSX.Element;
@@ -1,5 +1,5 @@
1
1
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
2
- import { useCallback, useMemo, useRef, useState } from "react";
2
+ import { useCallback, useMemo, useState } from "react";
3
3
  import { Popover } from "./Popover.js";
4
4
  import { SearchField } from "./SearchField.js";
5
5
  import { CatalogIcon } from "./CatalogIcon.js";
@@ -10,16 +10,48 @@ import {
10
10
  VirtualPickerList
11
11
  } from "./VirtualPickerList.js";
12
12
  import { useIconCatalog, iconCategories } from "./iconCatalog.js";
13
+ import { usePopover } from "./usePopover.js";
14
+ import { useFieldControl, fieldTriggerProps } from "./field.js";
13
15
  import s from "./Picker.module.css";
14
16
  function IconPicker({
15
17
  value,
16
18
  onChange,
17
- label = "\u56FE\u6807",
18
- disabled = false
19
+ label,
20
+ options,
21
+ valuePrefix = "",
22
+ ...props
19
23
  }) {
20
- const anchor = useRef(null), [open, setOpen] = useState(false), [query, setQuery] = useState("");
21
- const { items, error, retry } = useIconCatalog(open);
24
+ const field = useFieldControl({ ...props, label }), pickerLabel = field.label || "\u56FE\u6807", blocked = field.disabled || field.readOnly, pop = usePopover(blocked), [query, setQuery] = useState("");
25
+ const name = valuePrefix && value.startsWith(valuePrefix) ? value.slice(valuePrefix.length) : value;
26
+ const selected = options?.find((option) => option.value === value);
27
+ const { items, error, retry } = useIconCatalog(
28
+ pop.open && options === void 0
29
+ );
22
30
  const groups = useMemo(() => {
31
+ if (options) {
32
+ const categories = /* @__PURE__ */ new Map();
33
+ for (const option of options) {
34
+ const category = option.category || "\u5168\u90E8";
35
+ if (!categories.has(category))
36
+ categories.set(category, {
37
+ id: category,
38
+ label: category,
39
+ items: []
40
+ });
41
+ categories.get(category).items.push({
42
+ id: option.value,
43
+ label: option.label,
44
+ content: option.icon,
45
+ search: [
46
+ option.value,
47
+ option.label,
48
+ category,
49
+ ...option.keywords || []
50
+ ].join(" ").toLowerCase()
51
+ });
52
+ }
53
+ return [...categories.values()];
54
+ }
23
55
  if (!items) return [];
24
56
  const indexed = items.map((icon) => ({
25
57
  id: icon.name,
@@ -40,43 +72,53 @@ function IconPicker({
40
72
  )
41
73
  })
42
74
  ).filter((group) => group.items.length);
43
- }, [items]);
75
+ }, [items, options]);
44
76
  const choose = useCallback(
45
77
  (item) => {
46
- onChange(item.id);
47
- setOpen(false);
48
- anchor.current?.focus();
78
+ if (blocked) return;
79
+ onChange(options ? item.id : valuePrefix + item.id);
80
+ pop.closeAndFocus();
49
81
  },
50
- [onChange]
82
+ [onChange, blocked, options, valuePrefix, pop.closeAndFocus]
51
83
  );
52
84
  return /* @__PURE__ */ jsxs(Fragment, { children: [
53
85
  /* @__PURE__ */ jsx(
54
86
  SelectTrigger,
55
87
  {
56
- ref: anchor,
57
- expanded: open,
58
- disabled,
59
- "aria-label": label,
88
+ ...fieldTriggerProps(field),
89
+ "aria-label": pickerLabel,
90
+ ref: pop.anchor,
91
+ expanded: pop.open,
92
+ "data-readonly": field.readOnly || void 0,
60
93
  "aria-haspopup": "dialog",
61
- onClick: () => setOpen((x) => !x),
94
+ onClick: pop.toggle,
62
95
  onKeyDown: (e) => {
63
96
  if (e.key === "ArrowDown" || e.key === "ArrowUp") {
64
97
  e.preventDefault();
65
- setOpen(true);
98
+ pop.show();
66
99
  }
67
100
  },
68
- icon: /* @__PURE__ */ jsx(CatalogIcon, { name: value }),
69
- children: /* @__PURE__ */ jsx("span", { children: value || "\u9009\u62E9\u56FE\u6807" })
101
+ icon: options ? selected?.icon : /* @__PURE__ */ jsx(CatalogIcon, { name }),
102
+ children: /* @__PURE__ */ jsx("span", { children: selected?.label || name || "\u9009\u62E9\u56FE\u6807" })
103
+ }
104
+ ),
105
+ field.name && /* @__PURE__ */ jsx(
106
+ "input",
107
+ {
108
+ type: "hidden",
109
+ name: field.name,
110
+ value,
111
+ disabled: field.disabled
70
112
  }
71
113
  ),
72
114
  /* @__PURE__ */ jsx(
73
115
  Popover,
74
116
  {
75
- open,
76
- onClose: () => setOpen(false),
77
- anchor,
117
+ open: pop.open,
118
+ onClose: pop.close,
119
+ anchor: pop.anchor,
78
120
  width: 470,
79
- label: label + "\u9009\u62E9\u5668",
121
+ label: pickerLabel + "\u9009\u62E9\u5668",
80
122
  children: /* @__PURE__ */ jsxs("div", { className: s.panel, children: [
81
123
  /* @__PURE__ */ jsx(
82
124
  SearchField,
@@ -87,18 +129,18 @@ function IconPicker({
87
129
  onChange: setQuery
88
130
  }
89
131
  ),
90
- error ? /* @__PURE__ */ jsx(Button, { onClick: retry, children: "\u91CD\u65B0\u52A0\u8F7D" }) : !items ? /* @__PURE__ */ jsx("div", { className: s.loading, children: /* @__PURE__ */ jsx(Spinner, { label: "\u52A0\u8F7D\u56FE\u6807" }) }) : /* @__PURE__ */ jsx(
132
+ options === void 0 && error ? /* @__PURE__ */ jsx(Button, { onClick: retry, children: "\u91CD\u65B0\u52A0\u8F7D" }) : options === void 0 && !items ? /* @__PURE__ */ jsx("div", { className: s.loading, children: /* @__PURE__ */ jsx(Spinner, { label: "\u52A0\u8F7D\u56FE\u6807" }) }) : /* @__PURE__ */ jsx(
91
133
  VirtualPickerList,
92
134
  {
93
135
  groups,
94
136
  query,
95
- value,
137
+ value: options ? value : name,
96
138
  label: "\u56FE\u6807",
97
139
  onChoose: choose
98
140
  }
99
141
  ),
100
142
  /* @__PURE__ */ jsx("div", { className: s.footer, children: /* @__PURE__ */ jsxs("span", { children: [
101
- items?.length || 0,
143
+ options?.length ?? items?.length ?? 0,
102
144
  " \u4E2A\u56FE\u6807"
103
145
  ] }) })
104
146
  ] })