@volvel/ui 0.0.0-stage → 0.7.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/LICENSE +202 -0
- package/THIRD_PARTY_NOTICES.md +23036 -0
- package/dist/Badge.d.ts +20 -0
- package/dist/Badge.js +35 -0
- package/dist/Button.d.ts +56 -0
- package/dist/Button.js +52 -0
- package/dist/Chart.d.ts +23 -0
- package/dist/Chart.js +98 -0
- package/dist/Combobox.d.ts +45 -0
- package/dist/Combobox.js +42 -0
- package/dist/ContextMenu.d.ts +16 -0
- package/dist/ContextMenu.js +38 -0
- package/dist/DateInput.d.ts +31 -0
- package/dist/DateInput.js +329 -0
- package/dist/Details.d.ts +12 -0
- package/dist/Details.js +9 -0
- package/dist/Dialog.d.ts +44 -0
- package/dist/Dialog.js +95 -0
- package/dist/Dialogs.d.ts +56 -0
- package/dist/Dialogs.js +95 -0
- package/dist/Doc.d.ts +25 -0
- package/dist/Doc.js +118 -0
- package/dist/DocServed.d.ts +6 -0
- package/dist/DocServed.js +154 -0
- package/dist/DocZoom.d.ts +11 -0
- package/dist/DocZoom.js +133 -0
- package/dist/Facts.d.ts +12 -0
- package/dist/Facts.js +10 -0
- package/dist/Fields.d.ts +184 -0
- package/dist/Fields.js +249 -0
- package/dist/Form.d.ts +47 -0
- package/dist/Form.js +189 -0
- package/dist/Grid.d.ts +118 -0
- package/dist/Grid.js +475 -0
- package/dist/HeadingSlot.d.ts +13 -0
- package/dist/HeadingSlot.js +19 -0
- package/dist/JsonView.d.ts +11 -0
- package/dist/JsonView.js +68 -0
- package/dist/LogViewer.d.ts +45 -0
- package/dist/LogViewer.js +299 -0
- package/dist/Markdown.d.ts +21 -0
- package/dist/Markdown.js +27 -0
- package/dist/MarkdownBody.d.ts +6 -0
- package/dist/MarkdownBody.js +14 -0
- package/dist/Menu.d.ts +29 -0
- package/dist/Menu.js +8 -0
- package/dist/MoveButtons.d.ts +19 -0
- package/dist/MoveButtons.js +22 -0
- package/dist/Notice.d.ts +47 -0
- package/dist/Notice.js +50 -0
- package/dist/PageShell.d.ts +42 -0
- package/dist/PageShell.js +14 -0
- package/dist/Pagination.d.ts +13 -0
- package/dist/Pagination.js +9 -0
- package/dist/Panel.d.ts +31 -0
- package/dist/Panel.js +33 -0
- package/dist/Pickers.d.ts +37 -0
- package/dist/Pickers.js +44 -0
- package/dist/Popover.d.ts +24 -0
- package/dist/Popover.js +12 -0
- package/dist/ResultView.d.ts +28 -0
- package/dist/ResultView.js +140 -0
- package/dist/SchemaForm.d.ts +45 -0
- package/dist/SchemaForm.js +626 -0
- package/dist/SideSheet.d.ts +31 -0
- package/dist/SideSheet.js +136 -0
- package/dist/Sparkline.d.ts +14 -0
- package/dist/Sparkline.js +23 -0
- package/dist/Surfaces.d.ts +30 -0
- package/dist/Surfaces.js +34 -0
- package/dist/Switch.d.ts +18 -0
- package/dist/Switch.js +11 -0
- package/dist/Table.d.ts +15 -0
- package/dist/Table.js +6 -0
- package/dist/Tabs.d.ts +32 -0
- package/dist/Tabs.js +11 -0
- package/dist/Term.d.ts +82 -0
- package/dist/Term.js +153 -0
- package/dist/Timeline.d.ts +29 -0
- package/dist/Timeline.js +39 -0
- package/dist/Toast.d.ts +43 -0
- package/dist/Toast.js +140 -0
- package/dist/Tooltip.d.ts +34 -0
- package/dist/Tooltip.js +70 -0
- package/dist/Tree.d.ts +51 -0
- package/dist/Tree.js +233 -0
- package/dist/chartEngine.d.ts +32 -0
- package/dist/chartEngine.js +72 -0
- package/dist/chartTheme.d.ts +41 -0
- package/dist/chartTheme.js +131 -0
- package/dist/contrast.d.ts +27 -0
- package/dist/contrast.js +110 -0
- package/dist/dates.d.ts +61 -0
- package/dist/dates.js +294 -0
- package/dist/drawer/Chips.d.ts +37 -0
- package/dist/drawer/Chips.js +32 -0
- package/dist/drawer/Choices.d.ts +47 -0
- package/dist/drawer/Choices.js +81 -0
- package/dist/drawer/Drawer.d.ts +125 -0
- package/dist/drawer/Drawer.js +150 -0
- package/dist/drawer/EditorBodies.d.ts +95 -0
- package/dist/drawer/EditorBodies.js +169 -0
- package/dist/drawer/NumberFormat.d.ts +36 -0
- package/dist/drawer/NumberFormat.js +98 -0
- package/dist/drawer/SortableRows.d.ts +35 -0
- package/dist/drawer/SortableRows.js +23 -0
- package/dist/drawer/edgeShadows.d.ts +7 -0
- package/dist/drawer/edgeShadows.js +17 -0
- package/dist/drawer/index.d.ts +8 -0
- package/dist/drawer/index.js +8 -0
- package/dist/drawer/useSortable.d.ts +53 -0
- package/dist/drawer/useSortable.js +323 -0
- package/dist/format.d.ts +6 -0
- package/dist/format.js +17 -0
- package/dist/icons.d.ts +20 -0
- package/dist/icons.js +16 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.js +48 -0
- package/dist/markdownKnowledge.d.ts +45 -0
- package/dist/markdownKnowledge.js +37 -0
- package/dist/overlay.d.ts +7 -0
- package/dist/overlay.js +10 -0
- package/dist/pageIcons.d.ts +12 -0
- package/dist/pageIcons.js +32 -0
- package/dist/presence.d.ts +41 -0
- package/dist/presence.js +108 -0
- package/dist/returnFocus.d.ts +9 -0
- package/dist/returnFocus.js +44 -0
- package/dist/tokens.css +437 -0
- package/dist/type-scale.css +34 -0
- package/dist/useFlip.d.ts +32 -0
- package/dist/useFlip.js +118 -0
- package/fonts/OFL.txt +193 -0
- package/fonts/geist-latin.woff2 +0 -0
- package/fonts/geist-mono-latin.woff2 +0 -0
- package/package.json +52 -3
- package/README.md +0 -3
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Popover } from "@base-ui/react/popover";
|
|
3
|
+
import { ChevronRight, X } from "lucide-react";
|
|
4
|
+
import { useId, useRef, } from "react";
|
|
5
|
+
import { useOpening } from "../presence.js";
|
|
6
|
+
import { Tooltip } from "../Tooltip.js";
|
|
7
|
+
import { edgeShadows } from "./edgeShadows.js";
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
// Header
|
|
10
|
+
/** The head of a drawer: the widget's icon tile, its name, its kind, and a ⋯ menu. */
|
|
11
|
+
export function DrawerHeader({ icon: Icon, name, kind, menu, }) {
|
|
12
|
+
return (_jsxs("header", { className: "flex items-center gap-3", children: [Icon ? (_jsx("span", { "aria-hidden": "true", className: "grid size-9 shrink-0 place-items-center rounded-control border border-line bg-action-soft text-action", children: _jsx(Icon, { size: 16 }) })) : null, _jsxs("div", { className: "min-w-0 flex-1", children: [_jsx("h2", { className: "m-0 truncate text-emphasis font-semibold text-ink", children: name }), _jsx("p", { className: "m-0 truncate text-body-sm text-ink-muted", children: kind })] }), menu ? _jsx("div", { className: "shrink-0", children: menu }) : null] }));
|
|
13
|
+
}
|
|
14
|
+
const tabId = (prefix, id) => `${prefix}-tab-${id}`;
|
|
15
|
+
const panelId = (prefix, id) => `${prefix}-panel-${id}`;
|
|
16
|
+
/**
|
|
17
|
+
* The segmented tab bar. Left and Right (and Home and End) move between the tabs and open the
|
|
18
|
+
* one they land on. Pair it with `DrawerPanel`, with the same `idPrefix`.
|
|
19
|
+
*/
|
|
20
|
+
export function DrawerTabs({ tabs, value, onChange, idPrefix, label = "Settings", }) {
|
|
21
|
+
const refs = useRef(new Map());
|
|
22
|
+
const move = (event, at) => {
|
|
23
|
+
const last = tabs.length - 1;
|
|
24
|
+
const to = event.key === "ArrowRight"
|
|
25
|
+
? (at + 1) % tabs.length
|
|
26
|
+
: event.key === "ArrowLeft"
|
|
27
|
+
? (at - 1 + tabs.length) % tabs.length
|
|
28
|
+
: event.key === "Home"
|
|
29
|
+
? 0
|
|
30
|
+
: event.key === "End"
|
|
31
|
+
? last
|
|
32
|
+
: -1;
|
|
33
|
+
if (to < 0)
|
|
34
|
+
return;
|
|
35
|
+
event.preventDefault();
|
|
36
|
+
const next = tabs[to];
|
|
37
|
+
if (!next)
|
|
38
|
+
return;
|
|
39
|
+
onChange(next.id);
|
|
40
|
+
refs.current.get(next.id)?.focus();
|
|
41
|
+
};
|
|
42
|
+
return (_jsx("div", { role: "tablist", "aria-label": label, className: "flex gap-0.5 rounded-control border border-line bg-sunken p-0.5", children: tabs.map((tab, at) => {
|
|
43
|
+
const on = tab.id === value;
|
|
44
|
+
const button = (_jsxs("button", { ref: (node) => {
|
|
45
|
+
if (node)
|
|
46
|
+
refs.current.set(tab.id, node);
|
|
47
|
+
else
|
|
48
|
+
refs.current.delete(tab.id);
|
|
49
|
+
}, type: "button", role: "tab", id: tabId(idPrefix, tab.id), "aria-selected": on, "aria-controls": panelId(idPrefix, tab.id), tabIndex: on ? 0 : -1, onClick: () => onChange(tab.id), onKeyDown: (event) => move(event, at), className: `inline-flex h-8 flex-auto cursor-pointer items-center justify-center gap-1.5 rounded-inner px-2 text-body-sm font-medium outline-offset-1 focus-visible:outline-2 focus-visible:outline-focus ${on ? "bg-raised text-ink ring-1 ring-line" : "text-ink-muted hover:text-ink"}`, children: [tab.label, tab.count !== undefined ? (_jsx("span", { className: "text-meta text-ink-muted tabular-nums", children: tab.count })) : tab.attention ? (_jsxs(_Fragment, { children: [_jsx("span", { "aria-hidden": "true", "data-attention": "", className: "size-1.5 rounded-full bg-attention" }), _jsxs("span", { className: "sr-only", children: [", needs fixing: ", tab.attention] })] })) : null] }, tab.id));
|
|
50
|
+
return tab.attention ? (_jsx(Tooltip, { label: tab.attention, side: "bottom", children: button }, tab.id)) : (button);
|
|
51
|
+
}) }));
|
|
52
|
+
}
|
|
53
|
+
/** What a tab holds. */
|
|
54
|
+
export function DrawerPanel({ idPrefix, tab, children, }) {
|
|
55
|
+
return (_jsx("div", { role: "tabpanel", id: panelId(idPrefix, tab), "aria-labelledby": tabId(idPrefix, tab), className: "flex flex-col gap-5", children: children }));
|
|
56
|
+
}
|
|
57
|
+
// ---------------------------------------------------------------------------
|
|
58
|
+
// Groups and rows
|
|
59
|
+
/** An UPPERCASE small label, a bordered list of rows, and a note under it. */
|
|
60
|
+
export function DrawerGroup({ label, aside, note, children, }) {
|
|
61
|
+
const id = useId();
|
|
62
|
+
return (_jsxs("section", { "aria-labelledby": id, className: "flex flex-col gap-1.5", children: [_jsxs("div", { className: "flex items-baseline justify-between gap-2 px-0.5", children: [_jsx("h3", { id: id, className: "m-0 text-label font-semibold text-ink-muted uppercase", children: label }), aside ? _jsx("span", { className: "text-meta text-ink-muted", children: aside }) : null] }), _jsx("div", { className: "divide-y divide-line overflow-hidden rounded-panel border border-line bg-surface", children: children }), note ? _jsx("p", { className: "m-0 px-0.5 text-meta text-ink-muted", children: note }) : null] }));
|
|
63
|
+
}
|
|
64
|
+
/** Free content in a group, padded as a row is: chips, a sentence, a preview. */
|
|
65
|
+
export function DrawerBlock({ children, label, className = "", }) {
|
|
66
|
+
return (_jsxs("div", { className: `flex flex-col gap-2 px-3 py-3 ${className}`, children: [label ? _jsx("p", { className: "m-0 text-body-sm text-ink-muted", children: label }) : null, children] }));
|
|
67
|
+
}
|
|
68
|
+
const rowFace = "relative flex min-h-10 w-full items-center gap-3 px-3 py-2 text-left text-body-sm text-ink";
|
|
69
|
+
/**
|
|
70
|
+
* One row of a group: its label on the left, an icon and the current value, a chevron. With an
|
|
71
|
+
* `editor` it opens a small editor beside it, and shows as selected while it is open; with
|
|
72
|
+
* `onClick` it runs an action; with neither it only reads.
|
|
73
|
+
*/
|
|
74
|
+
export function DrawerRow({ label, icon: Icon, value, mono = false, hint, active, editor, editorTitle, editorFooter, open, onOpenChange, onClick, side = "right", }) {
|
|
75
|
+
const body = (_jsxs(_Fragment, { children: [_jsx("span", { className: "w-24 shrink-0 text-ink-muted", children: label }), _jsxs("span", { className: "flex min-w-0 flex-1 items-center gap-2", children: [Icon ? _jsx(Icon, { size: 14, "aria-hidden": true, className: "shrink-0 text-ink-muted" }) : null, _jsx("span", { className: `min-w-[4ch] truncate ${mono ? "font-mono text-action" : ""}`, children: value ?? _jsx("span", { className: "text-ink-muted", children: "Not set" }) }), hint ? _jsxs("span", { className: "min-w-0 shrink-[4] truncate text-ink-muted", children: ["\u00B7 ", hint] }) : null] }), editor || onClick ? (_jsx(ChevronRight, { "aria-hidden": "true", size: 14, className: "shrink-0 text-ink-muted" })) : null] }));
|
|
76
|
+
const rail = "before:absolute before:inset-y-0 before:left-0 before:w-0.5 before:bg-action before:opacity-0 data-[active]:bg-selected data-[active]:before:opacity-100 data-[popup-open]:bg-selected data-[popup-open]:before:opacity-100";
|
|
77
|
+
const interactive = `${rowFace} ${rail} cursor-pointer outline-offset-[-2px] hover:bg-selected focus-visible:outline-2 focus-visible:outline-focus`;
|
|
78
|
+
if (editor) {
|
|
79
|
+
return (_jsx(DrawerEditor, { title: editorTitle ?? label, footer: editorFooter ?? true, side: side, ...(open === undefined ? {} : { open }), ...(onOpenChange ? { onOpenChange } : {}), trigger: _jsx("button", { type: "button", "data-active": active ? "" : undefined, className: interactive, children: body }), children: editor }));
|
|
80
|
+
}
|
|
81
|
+
if (onClick) {
|
|
82
|
+
return (_jsx("button", { type: "button", onClick: onClick, "data-active": active ? "" : undefined, className: interactive, children: body }));
|
|
83
|
+
}
|
|
84
|
+
return (_jsx("div", { "data-active": active ? "" : undefined, className: `${rowFace} ${rail}`, children: body }));
|
|
85
|
+
}
|
|
86
|
+
// ---------------------------------------------------------------------------
|
|
87
|
+
// The small editor
|
|
88
|
+
/**
|
|
89
|
+
* A small editor that opens beside a row or a chip: a title, a close ×, a body, and a "Done"
|
|
90
|
+
* footer. Escape and a click outside close it, and focus goes back to what opened it.
|
|
91
|
+
*/
|
|
92
|
+
export function DrawerEditor({ trigger, title, children, footer = true, doneLabel = "Done", open, onOpenChange, onDone, side = "right", boundary, }) {
|
|
93
|
+
// One put on the page already open (a row added and opened at once, a column's editor beside its heading) is
|
|
94
|
+
// told it is shut first, so it arrives with the starting style that a click on its trigger gives it.
|
|
95
|
+
const opening = useOpening(open === true);
|
|
96
|
+
const controlled = open === undefined ? {} : { open: opening };
|
|
97
|
+
const popup = useRef(null);
|
|
98
|
+
return (_jsxs(Popover.Root, { ...controlled, ...(onOpenChange ? { onOpenChange: (next) => onOpenChange(next) } : {}), children: [_jsx(Popover.Trigger, { render: trigger }), _jsx(Popover.Portal, { children: _jsx(Popover.Positioner, { side: side, align: "start", sideOffset: 8,
|
|
99
|
+
// Below the app's top bar (3.5rem), never over it: the list shrinks and scrolls, its footer stays.
|
|
100
|
+
collisionPadding: { top: 56, right: 8, bottom: 8, left: 8 }, ...(boundary ? { collisionBoundary: boundary } : {}), className: "layer-popover", children: _jsxs(Popover.Popup, { ref: popup,
|
|
101
|
+
// A single-line field that takes focus as the editor opens, holding a default text such as
|
|
102
|
+
// "Table", has it selected, so typing replaces it instead of continuing it.
|
|
103
|
+
onFocus: (event) => {
|
|
104
|
+
const field = event.target;
|
|
105
|
+
const from = event.relatedTarget;
|
|
106
|
+
if (field instanceof HTMLInputElement &&
|
|
107
|
+
/^(text|search|)$/.test(field.type) &&
|
|
108
|
+
!(from instanceof Node && event.currentTarget.contains(from)) &&
|
|
109
|
+
event.currentTarget.querySelector("input:not([type=hidden]):not([type=radio]):not([type=checkbox])") === field)
|
|
110
|
+
field.select();
|
|
111
|
+
},
|
|
112
|
+
// Focus stays in it while it is open, as in a dialog: Tab from the last control goes to the first.
|
|
113
|
+
onKeyDown: (event) => {
|
|
114
|
+
if (event.key !== "Tab")
|
|
115
|
+
return;
|
|
116
|
+
const back = event.shiftKey;
|
|
117
|
+
// Let the browser move on; if that took it out of the editor, bring it round to the other end.
|
|
118
|
+
requestAnimationFrame(() => {
|
|
119
|
+
const box = popup.current;
|
|
120
|
+
if (!box || box.contains(document.activeElement))
|
|
121
|
+
return;
|
|
122
|
+
const tabbable = [
|
|
123
|
+
...box.querySelectorAll("a[href], button:not([disabled]), input:not([disabled]):not([type=hidden]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex='-1'])"),
|
|
124
|
+
].filter((one) => one.getClientRects().length > 0);
|
|
125
|
+
(back ? tabbable.at(-1) : tabbable[0])?.focus();
|
|
126
|
+
});
|
|
127
|
+
},
|
|
128
|
+
// Focus starts on what the editor is for (a search, the chosen row, the first field), not on Close.
|
|
129
|
+
initialFocus: () => popup.current?.querySelector("[data-autofocus]") ??
|
|
130
|
+
popup.current?.querySelector("input[type=radio]:checked, input[type=radio], [role=radio][aria-checked=true], textarea:not([disabled]), input:not([disabled]):not([type=hidden]):not([type=checkbox])") ??
|
|
131
|
+
true, className: "flex max-h-[min(34rem,var(--available-height))] w-[22rem] max-w-[calc(100vw-1rem)] flex-col overflow-hidden rounded-panel border border-line bg-raised text-ink shadow-overlay transition-[opacity,translate] motion-base data-[ending-style]:opacity-0 data-[starting-style]:opacity-0", children: [_jsxs("div", { className: "flex items-center justify-between gap-2 border-b border-line px-3 py-3", children: [_jsx(Popover.Title, { className: "m-0 text-body-sm font-semibold", children: title }), _jsx(Popover.Close, { "aria-label": `Close ${title}`, className: "grid size-6 cursor-pointer place-items-center rounded-inner text-ink-muted outline-offset-1 hover:text-ink focus-visible:outline-2 focus-visible:outline-focus", children: _jsx(X, { "aria-hidden": "true", size: 14 }) })] }), _jsx("div", { className: "min-h-0 flex-1 overflow-y-auto p-3",
|
|
132
|
+
// A soft edge at the foot while there is more to see: a small screen shows part of a long editor.
|
|
133
|
+
style: edgeShadows("var(--color-raised)"), children: children }), footer ? (_jsx("div", { className: "flex justify-end border-t border-line px-3 py-2", children: _jsx(Popover.Close, { onClick: onDone, className: "h-8 cursor-pointer rounded-control bg-action px-3 text-body-sm font-medium text-action-ink outline-offset-2 focus-visible:outline-2 focus-visible:outline-focus", children: doneLabel }) })) : null] }) }) })] }));
|
|
134
|
+
}
|
|
135
|
+
// ---------------------------------------------------------------------------
|
|
136
|
+
// Switch rows and dashed adds
|
|
137
|
+
/** A switch: on or off. */
|
|
138
|
+
export function DrawerSwitch({ checked, onChange, labelledBy, describedBy, label, }) {
|
|
139
|
+
return (_jsx("button", { type: "button", role: "switch", "aria-checked": checked, ...(labelledBy ? { "aria-labelledby": labelledBy } : {}), ...(describedBy ? { "aria-describedby": describedBy } : {}), ...(label ? { "aria-label": label } : {}), onClick: () => onChange(!checked), className: `relative h-5 w-9 shrink-0 cursor-pointer rounded-full border outline-offset-2 transition-colors motion-fast focus-visible:outline-2 focus-visible:outline-focus ${checked ? "border-action bg-action" : "border-line-control bg-sunken"}`, children: _jsx("span", { "aria-hidden": "true", className: `absolute top-0.5 left-0.5 size-3.5 rounded-full transition-[translate] motion-fast ${checked ? "translate-x-4 bg-action-ink" : "bg-ink-muted"}` }) }));
|
|
140
|
+
}
|
|
141
|
+
/** A label, a help line, and a switch: "Show values on bars". */
|
|
142
|
+
export function ToggleRow({ label, help, checked, onChange, }) {
|
|
143
|
+
const id = useId();
|
|
144
|
+
const helpId = `${id}-help`;
|
|
145
|
+
return (_jsxs("div", { className: "flex items-center justify-between gap-3 px-3 py-3", children: [_jsxs("div", { className: "min-w-0 cursor-pointer", onClick: () => onChange(!checked), children: [_jsx("p", { id: id, className: "m-0 text-body-sm text-ink", children: label }), help ? (_jsx("p", { id: helpId, className: "m-0 text-meta text-ink-muted", children: help })) : null] }), _jsx(DrawerSwitch, { checked: checked, onChange: onChange, labelledBy: id, ...(help ? { describedBy: helpId } : {}) })] }));
|
|
146
|
+
}
|
|
147
|
+
/** The dashed "+ Add a step", "+ Column": full width, or as wide as its words. */
|
|
148
|
+
export function DashedAdd({ children, onClick, inline = false, }) {
|
|
149
|
+
return (_jsxs("button", { type: "button", onClick: onClick, className: `inline-flex h-8 cursor-pointer items-center justify-center gap-1.5 rounded-control border border-dashed border-line-strong px-3 text-body-sm text-ink-muted outline-offset-2 hover:border-action hover:text-ink focus-visible:outline-2 focus-visible:outline-focus ${inline ? "" : "w-full"}`, children: [_jsx("span", { "aria-hidden": "true", children: "+" }), children] }));
|
|
150
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { type DrawerIcon } from "./Drawer.tsx";
|
|
2
|
+
/** A condition as the manifest's `Condition` has it: `{ field, test, value }`. */
|
|
3
|
+
export interface ConditionValue {
|
|
4
|
+
field: string;
|
|
5
|
+
test: string;
|
|
6
|
+
value?: unknown;
|
|
7
|
+
}
|
|
8
|
+
export interface ConditionColumn {
|
|
9
|
+
id: string;
|
|
10
|
+
label: string;
|
|
11
|
+
/** "choice", "text", "number", "date", "boolean": what words and values it takes. */
|
|
12
|
+
type: string;
|
|
13
|
+
icon?: DrawerIcon;
|
|
14
|
+
}
|
|
15
|
+
export interface ConditionOperator {
|
|
16
|
+
test: string;
|
|
17
|
+
/** The column's own words: "is in the last…", "is before". */
|
|
18
|
+
label: string;
|
|
19
|
+
/** What is typed after it. Default: by the column's type; `none` for "is empty". */
|
|
20
|
+
input?: "text" | "number" | "date" | "none" | "last" | "between";
|
|
21
|
+
}
|
|
22
|
+
/** What a value may be taken from besides being typed: a column of the record the page is open on, or whoever is looking. */
|
|
23
|
+
export interface ConditionValueFrom {
|
|
24
|
+
/** The columns of the page's record, when the page opens on one. */
|
|
25
|
+
record?: readonly {
|
|
26
|
+
id: string;
|
|
27
|
+
label: string;
|
|
28
|
+
}[];
|
|
29
|
+
/** "The current user". */
|
|
30
|
+
user?: boolean;
|
|
31
|
+
}
|
|
32
|
+
/** How a column's money is kept, so it is typed as money and written the way the column holds it. */
|
|
33
|
+
export interface ConditionMoney {
|
|
34
|
+
currency: string;
|
|
35
|
+
minorUnits: boolean;
|
|
36
|
+
}
|
|
37
|
+
/** `{ record: "client" }`, `{ user: "id" }`, or neither. */
|
|
38
|
+
export declare const valueSource: (value: unknown) => "record" | "user" | "value";
|
|
39
|
+
export interface ConditionChoice {
|
|
40
|
+
value: string;
|
|
41
|
+
label?: string;
|
|
42
|
+
/** How many rows have it: "1,204 rows". */
|
|
43
|
+
count?: number;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The body of the condition editor: a field with its type's icon, the operators written in that
|
|
47
|
+
* type's words, and, for a choice column, its values as a checked list with how many rows each
|
|
48
|
+
* has. Nothing is typed that could be mistyped. It writes `{ field, test, value }`; several
|
|
49
|
+
* values chosen are an array ("any of"), one is itself. Put it in a `DrawerEditor`.
|
|
50
|
+
*/
|
|
51
|
+
export declare function ConditionEditor({ columns, operators, choices, value, onChange, note, valueFrom, money, }: {
|
|
52
|
+
columns: readonly ConditionColumn[];
|
|
53
|
+
/** The operator words for a column type. */
|
|
54
|
+
operators: (type: string) => readonly ConditionOperator[];
|
|
55
|
+
/** The values of a choice column, with their row counts. */
|
|
56
|
+
choices?: (field: string) => readonly ConditionChoice[] | undefined;
|
|
57
|
+
value: ConditionValue | undefined;
|
|
58
|
+
onChange: (next: ConditionValue) => void;
|
|
59
|
+
/** Under the list: "Status is a choice column, so you pick from its values". */
|
|
60
|
+
note?: string;
|
|
61
|
+
/** Offers "this record's…" and "the current user" as what a value is compared with. */
|
|
62
|
+
valueFrom?: (field: string) => ConditionValueFrom | undefined;
|
|
63
|
+
/** For a money column, how it is kept: the value is typed as money. */
|
|
64
|
+
money?: (field: string) => ConditionMoney | undefined;
|
|
65
|
+
}): import("react").JSX.Element | null;
|
|
66
|
+
export interface SortKey {
|
|
67
|
+
field: string;
|
|
68
|
+
direction: "asc" | "desc";
|
|
69
|
+
}
|
|
70
|
+
export interface SortField {
|
|
71
|
+
id: string;
|
|
72
|
+
label: string;
|
|
73
|
+
icon?: DrawerIcon;
|
|
74
|
+
/** The two words for its direction: dates "Newest" / "Oldest", names "A to Z" / "Z to A". */
|
|
75
|
+
words?: {
|
|
76
|
+
desc: string;
|
|
77
|
+
asc: string;
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* The body of the sort editor: a field and its direction in the column's words, "+ Then by",
|
|
82
|
+
* and "Show at most". Put it in a `DrawerEditor` titled "Sorted by".
|
|
83
|
+
*/
|
|
84
|
+
export declare function SortEditor({ fields, sorts, onChange, limit, limitOptions, onLimit, }: {
|
|
85
|
+
fields: readonly SortField[];
|
|
86
|
+
sorts: readonly SortKey[];
|
|
87
|
+
onChange: (next: SortKey[]) => void;
|
|
88
|
+
limit?: number | undefined;
|
|
89
|
+
/** What "Show at most" offers; `value` undefined is "All rows". */
|
|
90
|
+
limitOptions?: readonly {
|
|
91
|
+
value: number | undefined;
|
|
92
|
+
label: string;
|
|
93
|
+
}[];
|
|
94
|
+
onLimit?: (next: number | undefined) => void;
|
|
95
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { X } from "lucide-react";
|
|
3
|
+
import { useEffect, useId, useRef } from "react";
|
|
4
|
+
import { DateInput } from "../DateInput.js";
|
|
5
|
+
import { localeOf } from "../dates.js";
|
|
6
|
+
import { MoneyField } from "../Fields.js";
|
|
7
|
+
import { SegmentedControl } from "../Pickers.js";
|
|
8
|
+
import { DashedAdd } from "./Drawer.js";
|
|
9
|
+
const control = "h-9 min-w-0 rounded-control border border-line-control bg-surface px-2 text-body-sm text-ink focus-visible:outline-2 focus-visible:outline-focus";
|
|
10
|
+
/** `{ record: "client" }`, `{ user: "id" }`, or neither. */
|
|
11
|
+
export const valueSource = (value) => typeof value === "object" && value !== null && !Array.isArray(value)
|
|
12
|
+
? "record" in value
|
|
13
|
+
? "record"
|
|
14
|
+
: "user" in value
|
|
15
|
+
? "user"
|
|
16
|
+
: "value"
|
|
17
|
+
: "value";
|
|
18
|
+
const UNITS = ["day", "week", "month", "quarter", "year"];
|
|
19
|
+
const inputOf = (column, operator) => operator?.input ??
|
|
20
|
+
(column?.type === "number" ? "number" : column?.type === "date" ? "date" : "text");
|
|
21
|
+
const selected = (value) => Array.isArray(value)
|
|
22
|
+
? value.map(String)
|
|
23
|
+
: value === undefined || value === ""
|
|
24
|
+
? []
|
|
25
|
+
: [String(value)];
|
|
26
|
+
/**
|
|
27
|
+
* The body of the condition editor: a field with its type's icon, the operators written in that
|
|
28
|
+
* type's words, and, for a choice column, its values as a checked list with how many rows each
|
|
29
|
+
* has. Nothing is typed that could be mistyped. It writes `{ field, test, value }`; several
|
|
30
|
+
* values chosen are an array ("any of"), one is itself. Put it in a `DrawerEditor`.
|
|
31
|
+
*/
|
|
32
|
+
export function ConditionEditor({ columns, operators, choices, value, onChange, note, valueFrom, money, }) {
|
|
33
|
+
const id = useId();
|
|
34
|
+
// What is typed after the words is kept in view: the list of words is long, the editor scrolls.
|
|
35
|
+
const valueArea = useRef(null);
|
|
36
|
+
const chosenTest = value?.test;
|
|
37
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: the chosen test is what moves the view
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
valueArea.current?.scrollIntoView?.({ block: "nearest" });
|
|
40
|
+
}, [chosenTest]);
|
|
41
|
+
const column = columns.find((one) => one.id === value?.field) ?? columns[0];
|
|
42
|
+
if (!column)
|
|
43
|
+
return null;
|
|
44
|
+
const words = operators(column.type);
|
|
45
|
+
const operator = words.find((one) => one.test === value?.test) ?? words[0];
|
|
46
|
+
const options = choices?.(column.id);
|
|
47
|
+
const asChoice = column.type === "choice" && options !== undefined;
|
|
48
|
+
const Icon = column.icon;
|
|
49
|
+
const current = {
|
|
50
|
+
field: column.id,
|
|
51
|
+
test: operator?.test ?? "is",
|
|
52
|
+
...(value?.value === undefined ? {} : { value: value.value }),
|
|
53
|
+
};
|
|
54
|
+
const picked = selected(current.value);
|
|
55
|
+
const input = inputOf(column, operator);
|
|
56
|
+
const source = valueSource(current.value);
|
|
57
|
+
// Only a test that compares with one value can compare with a column or the user.
|
|
58
|
+
const other = valueFrom?.(column.id);
|
|
59
|
+
const fromOther = other !== undefined &&
|
|
60
|
+
((other.record?.length ?? 0) > 0 || other.user === true) &&
|
|
61
|
+
operator?.test !== "contains" &&
|
|
62
|
+
operator?.test !== "in";
|
|
63
|
+
const kept = input === "number" ? money?.(column.id) : undefined;
|
|
64
|
+
const chooseField = (field) => {
|
|
65
|
+
const next = columns.find((one) => one.id === field);
|
|
66
|
+
const first = next ? operators(next.type)[0] : undefined;
|
|
67
|
+
onChange({ field, test: first?.test ?? "is" });
|
|
68
|
+
};
|
|
69
|
+
const chooseOperator = (test) => {
|
|
70
|
+
const next = words.find((one) => one.test === test);
|
|
71
|
+
const { value: _, ...rest } = current;
|
|
72
|
+
onChange(inputOf(column, next) === "none" || inputOf(column, next) !== input
|
|
73
|
+
? { ...rest, test }
|
|
74
|
+
: { ...current, test });
|
|
75
|
+
};
|
|
76
|
+
const toggle = (choice, on) => {
|
|
77
|
+
const next = on ? [...picked, choice] : picked.filter((one) => one !== choice);
|
|
78
|
+
const { value: _, ...rest } = current;
|
|
79
|
+
onChange(next.length === 0 ? rest : { ...rest, value: next.length === 1 ? next[0] : next });
|
|
80
|
+
};
|
|
81
|
+
return (_jsxs("div", { className: "flex flex-col gap-3", children: [_jsxs("div", { className: "grid grid-cols-2 gap-2", children: [_jsxs("div", { className: "relative", children: [_jsx("label", { htmlFor: `${id}-field`, className: "sr-only", children: "Field" }), Icon ? (_jsx(Icon, { size: 14, "aria-hidden": true, className: "pointer-events-none absolute top-1/2 left-2 -translate-y-1/2 text-ink-muted" })) : null, _jsx("select", { id: `${id}-field`, value: column.id, onChange: (event) => chooseField(event.target.value), className: `${control} w-full ${Icon ? "pl-8" : ""}`, children: columns.map((one) => (_jsx("option", { value: one.id, children: one.label }, one.id))) })] }), asChoice ? (_jsxs("div", { children: [_jsx("label", { htmlFor: `${id}-test`, className: "sr-only", children: "Test" }), _jsx("select", { id: `${id}-test`, value: operator?.test, onChange: (event) => chooseOperator(event.target.value), className: `${control} w-full`, children: words.map((one) => (_jsx("option", { value: one.test, children: one.label }, one.test))) })] })) : null] }), asChoice ? (_jsxs("fieldset", { className: "m-0 min-w-0 border-0 p-0", children: [_jsx("legend", { className: "mb-1.5 text-body-sm text-ink-muted", children: "Any of" }), _jsx("div", { className: "divide-y divide-line rounded-control border border-line", children: options.map((choice) => (_jsxs("label", { className: "flex min-h-9 cursor-pointer items-center gap-3 px-3 text-body-sm has-[:focus-visible]:outline-2 has-[:focus-visible]:outline-focus", children: [_jsx("input", { type: "checkbox", checked: picked.includes(choice.value), onChange: (event) => toggle(choice.value, event.target.checked), className: "size-4 accent-[var(--vx-action)]" }), _jsx("span", { className: "flex-1", children: choice.label ?? choice.value }), choice.count !== undefined ? (_jsxs("span", { className: "font-mono text-meta text-ink-muted tabular-nums", children: [choice.count.toLocaleString(localeOf()), " ", choice.count === 1 ? "row" : "rows"] })) : null] }, choice.value))) })] })) : (_jsxs(_Fragment, { children: [_jsxs("fieldset", { className: "m-0 flex min-w-0 flex-col gap-0.5 border-0 p-0", children: [_jsxs("legend", { className: "mb-1.5 text-body-sm text-ink-muted", children: ["The words change with the column. ", column.label, " gets its own."] }), words.map((one) => {
|
|
82
|
+
const on = one.test === operator?.test;
|
|
83
|
+
return (_jsxs("label", { className: `flex h-8 cursor-pointer items-center rounded-inner px-3 text-body-sm has-[:focus-visible]:outline-2 has-[:focus-visible]:outline-focus ${on ? "bg-selected text-ink" : "text-ink-muted hover:bg-selected"}`, children: [_jsx("input", { type: "radio", name: `${id}-test`, checked: on, onChange: () => chooseOperator(one.test), className: "sr-only" }), one.label] }, one.test));
|
|
84
|
+
})] }), _jsx("div", { ref: valueArea, children: input === "last" ? (_jsx(LastInput, { id: id, value: current.value, onChange: (next) => {
|
|
85
|
+
const { value: _, ...rest } = current;
|
|
86
|
+
onChange(next === undefined ? rest : { ...rest, value: next });
|
|
87
|
+
} })) : input === "between" ? (_jsx(BetweenInput, { id: id, kind: column.type === "date" ? "date" : "number", money: money?.(column.id), value: current.value, onChange: (next) => {
|
|
88
|
+
const { value: _, ...rest } = current;
|
|
89
|
+
onChange(next === undefined ? rest : { ...rest, value: next });
|
|
90
|
+
} })) : input !== "none" ? (_jsxs("div", { className: "flex flex-col gap-2", children: [fromOther ? (_jsx(SegmentedControl, { label: "Compared with", value: source, options: [
|
|
91
|
+
{ value: "value", label: "A value" },
|
|
92
|
+
...(other?.record && other.record.length > 0
|
|
93
|
+
? [{ value: "record", label: "This record's" }]
|
|
94
|
+
: []),
|
|
95
|
+
...(other?.user ? [{ value: "user", label: "The current user" }] : []),
|
|
96
|
+
], onChange: (next) => {
|
|
97
|
+
const { value: _, ...rest } = current;
|
|
98
|
+
if (next === "record") {
|
|
99
|
+
const first = other?.record?.[0];
|
|
100
|
+
onChange(first ? { ...rest, value: { record: first.id } } : rest);
|
|
101
|
+
}
|
|
102
|
+
else if (next === "user")
|
|
103
|
+
onChange({ ...rest, value: { user: "id" } });
|
|
104
|
+
else
|
|
105
|
+
onChange(rest);
|
|
106
|
+
} })) : null, fromOther && source === "record" ? (_jsxs(_Fragment, { children: [_jsx("label", { htmlFor: `${id}-record`, className: "sr-only", children: "Column of this record" }), _jsx("select", { id: `${id}-record`, value: current.value.record, onChange: (event) => onChange({ ...current, value: { record: event.target.value } }), className: `${control} w-full`, children: other?.record?.map((one) => (_jsx("option", { value: one.id, children: one.label }, one.id))) })] })) : fromOther && source === "user" ? (_jsx("p", { className: "m-0 text-body-sm text-ink-muted", children: "Whoever is looking: the rows are the ones that name them." })) : (_jsxs(_Fragment, { children: [_jsx("label", { htmlFor: `${id}-value`, className: "sr-only", children: "Value" }), input === "number" && kept ? (_jsx(MoneyField, { name: `${id}-value`, label: "Value", required: false, currency: kept.currency, minorUnits: kept.minorUnits, value: typeof current.value === "number" ? current.value : undefined, onChange: (next, problem) => {
|
|
107
|
+
if (problem !== undefined)
|
|
108
|
+
return;
|
|
109
|
+
const { value: _, ...rest } = current;
|
|
110
|
+
onChange(next === undefined ? rest : { ...rest, value: next });
|
|
111
|
+
} })) : input === "date" ? (_jsx(DateInput, { id: `${id}-value`, compact: true, value: current.value === undefined ? "" : String(current.value), onChange: (text) => {
|
|
112
|
+
const { value: _, ...rest } = current;
|
|
113
|
+
onChange(text === "" ? rest : { ...rest, value: text });
|
|
114
|
+
} })) : (_jsx("input", { id: `${id}-value`, type: input, value: current.value === undefined ? "" : String(current.value), onChange: (event) => {
|
|
115
|
+
const text = event.target.value;
|
|
116
|
+
const { value: _, ...rest } = current;
|
|
117
|
+
onChange(text === ""
|
|
118
|
+
? rest
|
|
119
|
+
: { ...rest, value: input === "number" ? Number(text) : text });
|
|
120
|
+
}, className: `${control} w-full` }))] }))] })) : null })] })), note ? _jsx("p", { className: "m-0 text-meta text-ink-muted", children: note }) : null] }));
|
|
121
|
+
}
|
|
122
|
+
/** "is in the last [30] [days]": a count and a unit, as `{ count, unit }`. */
|
|
123
|
+
function LastInput({ id, value, onChange, }) {
|
|
124
|
+
const now = (value ?? {});
|
|
125
|
+
const unit = now.unit ?? "day";
|
|
126
|
+
return (_jsxs("div", { className: "grid grid-cols-2 gap-2", children: [_jsxs("div", { children: [_jsx("label", { htmlFor: `${id}-count`, className: "sr-only", children: "How many" }), _jsx("input", { id: `${id}-count`, type: "number", min: 1, max: 1000, value: now.count === undefined ? "" : String(now.count), onChange: (event) => onChange(event.target.value === "" ? undefined : { count: Number(event.target.value), unit }), className: `${control} w-full` })] }), _jsxs("div", { children: [_jsx("label", { htmlFor: `${id}-unit`, className: "sr-only", children: "Unit" }), _jsx("select", { id: `${id}-unit`, value: unit, onChange: (event) => onChange({ count: now.count ?? 30, unit: event.target.value }), className: `${control} w-full`, children: UNITS.map((one) => (_jsx("option", { value: one, children: now.count === 1 ? one : `${one}s` }, one))) })] })] }));
|
|
127
|
+
}
|
|
128
|
+
/** "is between [from] and [to]": two days or two numbers, as `[from, to]`. */
|
|
129
|
+
function BetweenInput({ id, kind, value, money, onChange, }) {
|
|
130
|
+
const pair = Array.isArray(value) ? value : [];
|
|
131
|
+
const set = (at, text) => {
|
|
132
|
+
const next = [pair[0] ?? "", pair[1] ?? ""];
|
|
133
|
+
next[at] = text === "" ? "" : kind === "number" ? Number(text) : text;
|
|
134
|
+
onChange(next[0] === "" && next[1] === "" ? undefined : next);
|
|
135
|
+
};
|
|
136
|
+
const setMoney = (at, amount) => {
|
|
137
|
+
const next = [pair[0] ?? "", pair[1] ?? ""];
|
|
138
|
+
next[at] = amount ?? "";
|
|
139
|
+
onChange(next[0] === "" && next[1] === "" ? undefined : next);
|
|
140
|
+
};
|
|
141
|
+
const end = (at, name, label) => money && kind === "number" ? (_jsx(MoneyField, { name: `${id}-${name}`, label: label, required: false, currency: money.currency, minorUnits: money.minorUnits, value: typeof pair[at] === "number" ? pair[at] : undefined, onChange: (next, problem) => {
|
|
142
|
+
if (problem === undefined)
|
|
143
|
+
setMoney(at, next);
|
|
144
|
+
} })) : null;
|
|
145
|
+
return (_jsxs("div", { className: "grid grid-cols-[1fr_auto_1fr] items-center gap-2", children: [_jsxs("div", { children: [_jsx("label", { htmlFor: `${id}-from`, className: "sr-only", children: "From" }), end(0, "from", "From") ??
|
|
146
|
+
(kind === "date" ? (_jsx(DateInput, { id: `${id}-from`, compact: true, value: pair[0] === undefined ? "" : String(pair[0]), onChange: (text) => set(0, text) })) : (_jsx("input", { id: `${id}-from`, type: kind, value: pair[0] === undefined ? "" : String(pair[0]), onChange: (event) => set(0, event.target.value), className: `${control} w-full` })))] }), _jsx("span", { className: "text-body-sm text-ink-muted", children: "and" }), _jsxs("div", { children: [_jsx("label", { htmlFor: `${id}-to`, className: "sr-only", children: "To" }), end(1, "to", "To") ??
|
|
147
|
+
(kind === "date" ? (_jsx(DateInput, { id: `${id}-to`, compact: true, value: pair[1] === undefined ? "" : String(pair[1]), onChange: (text) => set(1, text) })) : (_jsx("input", { id: `${id}-to`, type: kind, value: pair[1] === undefined ? "" : String(pair[1]), onChange: (event) => set(1, event.target.value), className: `${control} w-full` })))] })] }));
|
|
148
|
+
}
|
|
149
|
+
const DEFAULT_WORDS = { desc: "Newest", asc: "Oldest" };
|
|
150
|
+
/**
|
|
151
|
+
* The body of the sort editor: a field and its direction in the column's words, "+ Then by",
|
|
152
|
+
* and "Show at most". Put it in a `DrawerEditor` titled "Sorted by".
|
|
153
|
+
*/
|
|
154
|
+
export function SortEditor({ fields, sorts, onChange, limit, limitOptions, onLimit, }) {
|
|
155
|
+
const id = useId();
|
|
156
|
+
const unused = fields.filter((field) => !sorts.some((sort) => sort.field === field.id));
|
|
157
|
+
const put = (at, next) => onChange(sorts.map((one, i) => (i === at ? next : one)));
|
|
158
|
+
return (_jsxs("div", { className: "flex flex-col gap-3", children: [_jsx("ol", { className: "m-0 flex list-none flex-col gap-2 p-0", children: sorts.map((sort, at) => {
|
|
159
|
+
const field = fields.find((one) => one.id === sort.field);
|
|
160
|
+
const words = field?.words ?? DEFAULT_WORDS;
|
|
161
|
+
const Icon = field?.icon;
|
|
162
|
+
return (_jsxs("li", { className: "flex items-center gap-2", children: [_jsxs("div", { className: "relative min-w-0 flex-1", children: [_jsx("label", { htmlFor: `${id}-${at}`, className: "sr-only", children: at === 0 ? "Sort by" : "Then by" }), Icon ? (_jsx(Icon, { size: 14, "aria-hidden": true, className: "pointer-events-none absolute top-1/2 left-2 -translate-y-1/2 text-ink-muted" })) : null, _jsx("select", { id: `${id}-${at}`, value: sort.field, onChange: (event) => put(at, { ...sort, field: event.target.value }), className: `${control} w-full ${Icon ? "pl-8" : ""}`, children: fields
|
|
163
|
+
.filter((one) => one.id === sort.field || unused.includes(one))
|
|
164
|
+
.map((one) => (_jsx("option", { value: one.id, children: one.label }, one.id))) })] }), _jsx(SegmentedControl, { label: `Direction of ${field?.label ?? "sort"}`, size: "small", value: sort.direction, onChange: (direction) => put(at, { ...sort, direction }), options: [
|
|
165
|
+
{ value: "desc", label: words.desc },
|
|
166
|
+
{ value: "asc", label: words.asc },
|
|
167
|
+
] }), sorts.length > 1 ? (_jsx("button", { type: "button", "aria-label": `Remove sort by ${field?.label ?? sort.field}`, onClick: () => onChange(sorts.filter((_, i) => i !== at)), className: "grid size-6 shrink-0 cursor-pointer place-items-center rounded-inner text-ink-muted hover:text-ink focus-visible:outline-2 focus-visible:outline-focus", children: _jsx(X, { "aria-hidden": "true", size: 14 }) })) : null] }, sort.field));
|
|
168
|
+
}) }), unused[0] ? (_jsx(DashedAdd, { onClick: () => onChange([...sorts, { field: unused[0].id, direction: "desc" }]), children: "Then by" })) : null, limitOptions && onLimit ? (_jsxs("div", { className: "flex items-center justify-between gap-3 pt-1", children: [_jsx("label", { htmlFor: `${id}-limit`, className: "text-body-sm text-ink-muted", children: "Show at most" }), _jsx("select", { id: `${id}-limit`, value: limit === undefined ? "" : String(limit), onChange: (event) => onLimit(event.target.value === "" ? undefined : Number(event.target.value)), className: `${control} w-40`, children: limitOptions.map((one) => (_jsx("option", { value: one.value === undefined ? "" : String(one.value), children: one.label }, one.value ?? "all"))) })] })) : null] }));
|
|
169
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type ValueFormat } from "@volvel/widgets";
|
|
2
|
+
import { DrawerRow } from "./Drawer.tsx";
|
|
3
|
+
/** How a number is written: the widget SDK's `ValueFormat`, which is also what writes it. */
|
|
4
|
+
export type NumberFormat = ValueFormat;
|
|
5
|
+
/** Writes a number the way every widget does: the SDK's one `formatValue`, in US English. */
|
|
6
|
+
export declare function sampleOf(value: number, format: NumberFormat): string;
|
|
7
|
+
type Choice = "money" | "money-whole" | "money-short" | "number" | "percent" | "plain";
|
|
8
|
+
/** What a choice writes, keeping the currency and the cents the person has set. */
|
|
9
|
+
export declare function formatOfChoice(choice: Choice, from: NumberFormat | undefined): NumberFormat;
|
|
10
|
+
/** Which choice a format is. A format the picker does not offer is none of them. */
|
|
11
|
+
export declare function choiceOf(format: NumberFormat | undefined): Choice | undefined;
|
|
12
|
+
export declare const CURRENCIES: {
|
|
13
|
+
code: string;
|
|
14
|
+
name: string;
|
|
15
|
+
}[];
|
|
16
|
+
/** What a format is called: "Money", "Money, short". */
|
|
17
|
+
export declare function formatName(format: NumberFormat | undefined): string;
|
|
18
|
+
/**
|
|
19
|
+
* The list of ways to write a number, each with a real number written that way, the chosen one
|
|
20
|
+
* highlighted; then the currency, and "Stored in cents" with what it means for the number.
|
|
21
|
+
*/
|
|
22
|
+
export declare function NumberFormatPicker({ value, onChange, sample, }: {
|
|
23
|
+
value: NumberFormat | undefined;
|
|
24
|
+
onChange: (next: NumberFormat) => void;
|
|
25
|
+
/** The number the samples write, as a person sees it (not in cents). */
|
|
26
|
+
sample?: number;
|
|
27
|
+
}): import("react").JSX.Element;
|
|
28
|
+
/** The drawer row "Numbers # $43,120.00 · Money" that opens the picker. */
|
|
29
|
+
export declare function NumberFormatRow({ label, value, onChange, sample, icon, }: {
|
|
30
|
+
label?: string;
|
|
31
|
+
value: NumberFormat | undefined;
|
|
32
|
+
onChange: (next: NumberFormat) => void;
|
|
33
|
+
sample?: number;
|
|
34
|
+
icon?: Parameters<typeof DrawerRow>[0]["icon"];
|
|
35
|
+
}): import("react").JSX.Element;
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { formatValue } from "@volvel/widgets";
|
|
3
|
+
import { useId } from "react";
|
|
4
|
+
import { DrawerRow, ToggleRow } from "./Drawer.js";
|
|
5
|
+
/** Writes a number the way every widget does: the SDK's one `formatValue`, in US English. */
|
|
6
|
+
export function sampleOf(value, format) {
|
|
7
|
+
return formatValue(value, format) ?? String(value);
|
|
8
|
+
}
|
|
9
|
+
const CHOICES = [
|
|
10
|
+
{ id: "money", label: "Money", sample: 43120 },
|
|
11
|
+
{ id: "money-whole", label: "Money, whole", sample: 43120 },
|
|
12
|
+
{ id: "money-short", label: "Money, short", sample: 43120 },
|
|
13
|
+
{ id: "number", label: "Number", sample: 43120 },
|
|
14
|
+
{ id: "percent", label: "Percent", sample: 0.125 },
|
|
15
|
+
{ id: "plain", label: "Plain", sample: 43120 },
|
|
16
|
+
];
|
|
17
|
+
/** What a choice writes, keeping the currency and the cents the person has set. */
|
|
18
|
+
export function formatOfChoice(choice, from) {
|
|
19
|
+
const currency = from?.currency ?? "USD";
|
|
20
|
+
const keep = from?.minorUnits ? { minorUnits: true } : {};
|
|
21
|
+
switch (choice) {
|
|
22
|
+
case "money":
|
|
23
|
+
return { style: "currency", currency, ...keep };
|
|
24
|
+
case "money-whole":
|
|
25
|
+
return { style: "currency", currency, decimals: 0, ...keep };
|
|
26
|
+
case "money-short":
|
|
27
|
+
return { style: "currency", currency, notation: "compact", ...keep };
|
|
28
|
+
case "number":
|
|
29
|
+
return { style: "number", ...keep };
|
|
30
|
+
case "percent":
|
|
31
|
+
return { style: "percent", ...keep };
|
|
32
|
+
case "plain":
|
|
33
|
+
return { style: "number", grouping: false, ...keep };
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** Which choice a format is. A format the picker does not offer is none of them. */
|
|
37
|
+
export function choiceOf(format) {
|
|
38
|
+
const f = format ?? { style: "number" };
|
|
39
|
+
if (f.style === "percent")
|
|
40
|
+
return "percent";
|
|
41
|
+
if (f.style === "currency") {
|
|
42
|
+
if (f.notation === "compact")
|
|
43
|
+
return "money-short";
|
|
44
|
+
if (f.decimals === 0)
|
|
45
|
+
return "money-whole";
|
|
46
|
+
return f.decimals === undefined ? "money" : undefined;
|
|
47
|
+
}
|
|
48
|
+
if (f.notation)
|
|
49
|
+
return undefined;
|
|
50
|
+
if (f.grouping === false)
|
|
51
|
+
return "plain";
|
|
52
|
+
return f.decimals === undefined ? "number" : undefined;
|
|
53
|
+
}
|
|
54
|
+
export const CURRENCIES = [
|
|
55
|
+
{ code: "USD", name: "US dollar" },
|
|
56
|
+
{ code: "EUR", name: "Euro" },
|
|
57
|
+
{ code: "GBP", name: "British pound" },
|
|
58
|
+
{ code: "CAD", name: "Canadian dollar" },
|
|
59
|
+
{ code: "AUD", name: "Australian dollar" },
|
|
60
|
+
{ code: "MXN", name: "Mexican peso" },
|
|
61
|
+
{ code: "JPY", name: "Japanese yen" },
|
|
62
|
+
{ code: "CHF", name: "Swiss franc" },
|
|
63
|
+
];
|
|
64
|
+
/** What a format is called: "Money", "Money, short". */
|
|
65
|
+
export function formatName(format) {
|
|
66
|
+
const choice = choiceOf(format);
|
|
67
|
+
return CHOICES.find((one) => one.id === choice)?.label ?? "Custom";
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The list of ways to write a number, each with a real number written that way, the chosen one
|
|
71
|
+
* highlighted; then the currency, and "Stored in cents" with what it means for the number.
|
|
72
|
+
*/
|
|
73
|
+
export function NumberFormatPicker({ value, onChange, sample = 43120, }) {
|
|
74
|
+
const name = useId();
|
|
75
|
+
const choice = choiceOf(value);
|
|
76
|
+
const isMoney = (value?.style ?? "number") === "currency";
|
|
77
|
+
const cents = value?.minorUnits === true;
|
|
78
|
+
const shown = sampleOf(sample, { ...(value ?? { style: "number" }), minorUnits: false });
|
|
79
|
+
return (_jsxs("div", { className: "flex flex-col gap-3", children: [_jsxs("fieldset", { className: "m-0 flex min-w-0 flex-col gap-0.5 border-0 p-0", children: [_jsx("legend", { className: "sr-only", children: "Number format" }), CHOICES.map((one) => {
|
|
80
|
+
const on = one.id === choice;
|
|
81
|
+
const format = formatOfChoice(one.id, value);
|
|
82
|
+
// A percent takes its own sample; the rest take the one given.
|
|
83
|
+
const number = one.id === "percent" ? one.sample : sample;
|
|
84
|
+
return (_jsxs("label", { className: `flex h-9 cursor-pointer items-center justify-between gap-3 rounded-control border px-3 text-body-sm min-[640px]:h-10 has-[:focus-visible]:outline-2 has-[:focus-visible]:outline-focus ${on ? "border-action bg-selected" : "border-transparent hover:bg-selected"}`, children: [_jsx("input", { type: "radio", name: name, checked: on, onChange: () => onChange(format),
|
|
85
|
+
// A choice already shown (a format the table gave) is written when it is chosen again.
|
|
86
|
+
onClick: () => on && onChange(format), className: "sr-only" }), _jsx("span", { children: one.label }), _jsx("span", { className: `font-mono tabular-nums ${on ? "text-action" : "text-ink-muted"}`, children: sampleOf(number, { ...format, minorUnits: false }) })] }, one.id));
|
|
87
|
+
})] }), _jsxs("div", { className: "divide-y divide-line overflow-hidden rounded-control border border-line", children: [isMoney ? (_jsxs("div", { className: "flex items-center justify-between gap-3 px-3 py-2", children: [_jsx("label", { htmlFor: `${name}-currency`, className: "text-body-sm text-ink-muted", children: "Currency" }), _jsx("select", { id: `${name}-currency`, value: value?.currency ?? "USD", onChange: (event) => onChange({ ...value, currency: event.target.value }), className: "h-8 rounded-control border border-line-control bg-surface px-2 text-body-sm text-ink focus-visible:outline-2 focus-visible:outline-focus", children: CURRENCIES.map((one) => (_jsxs("option", { value: one.code, children: [one.name, " \u00B7 ", one.code] }, one.code))) })] })) : null, _jsx(ToggleRow, { label: "Stored in cents", help: cents
|
|
88
|
+
? `Total is kept as ${Math.round(sample * 100)}, shown as ${shown}`
|
|
89
|
+
: "Switch on if the column holds cents, not dollars", checked: cents, onChange: (next) => {
|
|
90
|
+
const base = value ?? { style: "number" };
|
|
91
|
+
const { minorUnits: _, ...rest } = base;
|
|
92
|
+
onChange(next ? { ...rest, minorUnits: true } : rest);
|
|
93
|
+
} })] })] }));
|
|
94
|
+
}
|
|
95
|
+
/** The drawer row "Numbers # $43,120.00 · Money" that opens the picker. */
|
|
96
|
+
export function NumberFormatRow({ label = "Numbers", value, onChange, sample = 43120, icon, }) {
|
|
97
|
+
return (_jsx(DrawerRow, { label: label, ...(icon ? { icon } : {}), mono: true, value: sampleOf(sample, { ...(value ?? { style: "number" }), minorUnits: false }), hint: formatName(value), editorTitle: "Numbers look like", editorFooter: false, editor: _jsx(NumberFormatPicker, { value: value, onChange: onChange, sample: sample }) }));
|
|
98
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { ComponentType, ReactNode } from "react";
|
|
2
|
+
export interface SortableItem {
|
|
3
|
+
id: string;
|
|
4
|
+
label: string;
|
|
5
|
+
/** Under the label: "from Client code". */
|
|
6
|
+
sub?: string;
|
|
7
|
+
/** On the right: "Text", "$123.45". */
|
|
8
|
+
value?: ReactNode;
|
|
9
|
+
/** With an eye toggle: hidden. */
|
|
10
|
+
hidden?: boolean;
|
|
11
|
+
/** Before the label: the kind of thing the row is. */
|
|
12
|
+
icon?: ComponentType<{
|
|
13
|
+
size?: number;
|
|
14
|
+
className?: string;
|
|
15
|
+
"aria-hidden"?: boolean | "true";
|
|
16
|
+
}>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Rows with a drag handle, an optional eye, a value and a chevron. Reorder by dragging the
|
|
20
|
+
* handle (see `useSortable`: the row follows the pointer, the others slide, it settles into
|
|
21
|
+
* place), or with the keyboard: Alt+Up and Alt+Down on a row or its handle (Up and Down alone
|
|
22
|
+
* on the handle). Each move is said to screen readers.
|
|
23
|
+
*/
|
|
24
|
+
export declare function SortableRows({ items, onReorder, onOpen, onToggleHidden, activeId, label, }: {
|
|
25
|
+
items: readonly SortableItem[];
|
|
26
|
+
/** The ids in their new order. */
|
|
27
|
+
onReorder: (ids: string[]) => void;
|
|
28
|
+
/** A click on a row opens it (a column's editor). */
|
|
29
|
+
onOpen?: (id: string) => void;
|
|
30
|
+
/** Shows the eye on each row. */
|
|
31
|
+
onToggleHidden?: (id: string, hidden: boolean) => void;
|
|
32
|
+
activeId?: string | undefined;
|
|
33
|
+
/** Names the list. */
|
|
34
|
+
label: string;
|
|
35
|
+
}): import("react").JSX.Element;
|