@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.
- package/CHANGELOG.md +37 -18
- package/README.md +110 -73
- package/components/AccountItem.d.ts +15 -0
- package/components/AccountItem.js +38 -0
- package/components/AccountItem.module.css +103 -0
- package/components/AppShell.d.ts +11 -4
- package/components/AppShell.js +79 -54
- package/components/AppShell.module.css +13 -1
- package/components/Calendar.d.ts +2 -1
- package/components/Calendar.js +4 -3
- package/components/DatePicker.d.ts +3 -0
- package/components/DatePicker.js +41 -21
- package/components/DatePicker.module.css +3 -0
- package/components/Dialog.js +6 -3
- package/components/Form.d.ts +2 -1
- package/components/Form.js +5 -2
- package/components/FormField.d.ts +2 -1
- package/components/FormField.js +45 -19
- package/components/FormField.module.css +40 -0
- package/components/FormGroup.d.ts +2 -1
- package/components/FormGroup.js +26 -12
- package/components/FormLayout.d.ts +6 -0
- package/components/FormLayout.js +7 -0
- package/components/IconPicker.d.ts +14 -4
- package/components/IconPicker.js +67 -25
- package/components/NumberInput.d.ts +12 -4
- package/components/NumberInput.js +41 -20
- package/components/PasskeyItem.d.ts +11 -0
- package/components/PasskeyItem.js +53 -0
- package/components/RadioGroup.js +5 -3
- package/components/SessionItem.d.ts +11 -0
- package/components/SessionItem.js +44 -0
- package/components/ToastProvider.d.ts +4 -4
- package/components/ToastProvider.js +19 -22
- package/components/VirtualPickerList.d.ts +2 -0
- package/components/VirtualPickerList.js +1 -1
- package/components/calendarNow.d.ts +5 -0
- package/components/calendarNow.js +27 -0
- package/components/createToaster.d.ts +12 -0
- package/components/createToaster.js +40 -0
- package/components/index.d.ts +10 -2
- package/components/index.js +13 -1
- package/package.json +30 -4
- package/styles/minimal.css +1 -0
package/components/Calendar.js
CHANGED
|
@@ -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
|
|
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";
|
package/components/DatePicker.js
CHANGED
|
@@ -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
|
|
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
|
|
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" ?
|
|
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", " ") ||
|
|
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
|
-
|
|
153
|
+
Button,
|
|
147
154
|
{
|
|
155
|
+
variant: "ghost",
|
|
156
|
+
size: "small",
|
|
148
157
|
type: "button",
|
|
149
|
-
disabled: mode !== "time" && !isDateAllowed(
|
|
158
|
+
disabled: mode !== "time" && !isDateAllowed(today, constraints),
|
|
150
159
|
onClick: () => {
|
|
151
|
-
const
|
|
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__ */
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
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
|
}
|
package/components/Dialog.js
CHANGED
|
@@ -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
|
) }) }),
|
package/components/Form.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { FormHTMLAttributes, ReactNode } from "react";
|
|
2
2
|
import type { FormController } from "./useFormState";
|
|
3
|
-
|
|
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;
|
package/components/Form.js
CHANGED
|
@@ -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
|
-
|
|
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";
|
package/components/FormField.js
CHANGED
|
@@ -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
|
-
|
|
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.
|
|
39
|
-
|
|
40
|
-
|
|
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(
|
|
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
|
-
|
|
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;
|
package/components/FormGroup.js
CHANGED
|
@@ -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
|
-
|
|
16
|
-
|
|
19
|
+
const inherited = useContext(FormLayoutContext);
|
|
20
|
+
return /* @__PURE__ */ jsx(
|
|
21
|
+
FormLayoutContext,
|
|
17
22
|
{
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
children:
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
}
|
|
@@ -1,6 +1,16 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
13
|
+
options?: IconPickerOption[];
|
|
14
|
+
valuePrefix?: string;
|
|
15
|
+
};
|
|
16
|
+
export declare function IconPicker({ value, onChange, label, options, valuePrefix, ...props }: IconPickerProps): import("react").JSX.Element;
|
package/components/IconPicker.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { useCallback, useMemo,
|
|
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
|
|
18
|
-
|
|
19
|
+
label,
|
|
20
|
+
options,
|
|
21
|
+
valuePrefix = "",
|
|
22
|
+
...props
|
|
19
23
|
}) {
|
|
20
|
-
const
|
|
21
|
-
const
|
|
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
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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:
|
|
94
|
+
onClick: pop.toggle,
|
|
62
95
|
onKeyDown: (e) => {
|
|
63
96
|
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
|
|
64
97
|
e.preventDefault();
|
|
65
|
-
|
|
98
|
+
pop.show();
|
|
66
99
|
}
|
|
67
100
|
},
|
|
68
|
-
icon: /* @__PURE__ */ jsx(CatalogIcon, { name
|
|
69
|
-
children: /* @__PURE__ */ jsx("span", { children:
|
|
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:
|
|
77
|
-
anchor,
|
|
117
|
+
open: pop.open,
|
|
118
|
+
onClose: pop.close,
|
|
119
|
+
anchor: pop.anchor,
|
|
78
120
|
width: 470,
|
|
79
|
-
label:
|
|
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
|
|
143
|
+
options?.length ?? items?.length ?? 0,
|
|
102
144
|
" \u4E2A\u56FE\u6807"
|
|
103
145
|
] }) })
|
|
104
146
|
] })
|