@jsudc/react 0.2.1 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +64 -4
- package/dist/calendar/EventDetail.js.map +1 -1
- package/dist/data-view/FilterBuilder.d.ts +4 -2
- package/dist/data-view/FilterBuilder.js +11 -2
- package/dist/data-view/FilterBuilder.js.map +1 -1
- package/dist/data-view/FilterChips.d.ts +10 -2
- package/dist/data-view/FilterChips.js +14 -2
- package/dist/data-view/FilterChips.js.map +1 -1
- package/dist/data-view/UdcDataView.js +4 -2
- package/dist/data-view/UdcDataView.js.map +1 -1
- package/dist/data-view/liveView.js +2 -2
- package/dist/data-view/liveView.js.map +1 -1
- package/dist/headless.d.ts +3 -0
- package/dist/headless.js +5 -2
- package/dist/i18n/translations.js +16 -0
- package/dist/i18n/translations.js.map +1 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.js +10 -2
- package/dist/kanban/KanbanCard.d.ts +1 -1
- package/dist/kanban/KanbanCard.js +5 -1
- package/dist/kanban/KanbanCard.js.map +1 -1
- package/dist/kanban/KanbanRenderer.js +72 -67
- package/dist/kanban/KanbanRenderer.js.map +1 -1
- package/dist/kanban/model.d.ts +11 -1
- package/dist/kanban/model.js +29 -3
- package/dist/kanban/model.js.map +1 -1
- package/dist/kanban/useKanbanBoard.d.ts +5 -1
- package/dist/kanban/useKanbanBoard.js +6 -2
- package/dist/kanban/useKanbanBoard.js.map +1 -1
- package/dist/lookup/LookupCombobox.d.ts +18 -0
- package/dist/lookup/LookupCombobox.js +146 -0
- package/dist/lookup/LookupCombobox.js.map +1 -0
- package/dist/lookup/LookupSelectChip.d.ts +17 -0
- package/dist/lookup/LookupSelectChip.js +96 -0
- package/dist/lookup/LookupSelectChip.js.map +1 -0
- package/dist/lookup/LookupValue.d.ts +27 -0
- package/dist/lookup/LookupValue.js +98 -0
- package/dist/lookup/LookupValue.js.map +1 -0
- package/dist/lookup/batch.d.ts +33 -0
- package/dist/lookup/batch.js +69 -0
- package/dist/lookup/batch.js.map +1 -0
- package/dist/lookup/label.d.ts +5 -0
- package/dist/lookup/label.js +16 -0
- package/dist/lookup/label.js.map +1 -0
- package/dist/lookup/links.d.ts +30 -0
- package/dist/lookup/links.js +49 -0
- package/dist/lookup/links.js.map +1 -0
- package/dist/lookup/options.d.ts +28 -0
- package/dist/lookup/options.js +66 -0
- package/dist/lookup/options.js.map +1 -0
- package/dist/lookup/useUdcLookup.d.ts +52 -0
- package/dist/lookup/useUdcLookup.js +96 -0
- package/dist/lookup/useUdcLookup.js.map +1 -0
- package/dist/react-router/UdcReactRouterUrlState.d.ts +6 -1
- package/dist/react-router/UdcReactRouterUrlState.js +16 -3
- package/dist/react-router/UdcReactRouterUrlState.js.map +1 -1
- package/dist/styles.css +1 -1
- package/dist/table/CellEditor.d.ts +8 -4
- package/dist/table/CellEditor.js +15 -2
- package/dist/table/CellEditor.js.map +1 -1
- package/dist/table/TableRenderer.js +122 -97
- package/dist/table/TableRenderer.js.map +1 -1
- package/dist/table/cells.d.ts +4 -2
- package/dist/table/cells.js +9 -1
- package/dist/table/cells.js.map +1 -1
- package/dist/udc/hooks.d.ts +53 -2
- package/dist/udc/hooks.js +9 -1
- package/dist/udc/hooks.js.map +1 -1
- package/dist/udc/keys.d.ts +5 -0
- package/dist/udc/keys.js +26 -0
- package/dist/udc/keys.js.map +1 -1
- package/dist/udc/provider.d.ts +8 -1
- package/dist/udc/provider.js +6 -1
- package/dist/udc/provider.js.map +1 -1
- package/package.json +4 -4
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { useI18n } from "../i18n/i18n.js";
|
|
2
|
+
import { cn } from "../lib/utils.js";
|
|
3
|
+
import { lookupLabel } from "./label.js";
|
|
4
|
+
import { useLookupOptions } from "./options.js";
|
|
5
|
+
import { keyString, lookupKeys, useUdcLookup } from "./useUdcLookup.js";
|
|
6
|
+
import { X } from "lucide-react";
|
|
7
|
+
import { useEffect, useRef, useState } from "react";
|
|
8
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
9
|
+
// src/lookup/LookupCombobox.tsx
|
|
10
|
+
/**
|
|
11
|
+
* The inline editor of a lookup cell: a search over the target collection
|
|
12
|
+
* (`contains` on `label_field`, the first 50 by label) in a list under the
|
|
13
|
+
* cell. On a `lookup` a pick commits that key; on a `lookup_array` a pick
|
|
14
|
+
* adds or removes a key, and Enter on an empty search commits the list.
|
|
15
|
+
* Arrow keys move, Escape cancels, Tab commits and moves on.
|
|
16
|
+
*/
|
|
17
|
+
function LookupCombobox({ def, lookup, initial, saving, onCommit, onCancel }) {
|
|
18
|
+
const { t } = useI18n();
|
|
19
|
+
const multi = def.type === "lookup_array";
|
|
20
|
+
const [needle, setNeedle] = useState("");
|
|
21
|
+
const [active, setActive] = useState(0);
|
|
22
|
+
const [picked, setPicked] = useState(() => multi ? Array.isArray(initial) ? [...initial] : [] : []);
|
|
23
|
+
const { options, isLoading } = useLookupOptions(lookup, needle);
|
|
24
|
+
const labels = useUdcLookup(lookup, multi ? picked : [initial]);
|
|
25
|
+
const input = useRef(null);
|
|
26
|
+
const done = useRef(false);
|
|
27
|
+
useEffect(() => input.current?.focus(), []);
|
|
28
|
+
const pickedKeys = new Set(lookupKeys(picked));
|
|
29
|
+
const rows = multi ? options : [null, ...options];
|
|
30
|
+
const commit = (value, reason) => {
|
|
31
|
+
if (done.current || saving) return;
|
|
32
|
+
done.current = true;
|
|
33
|
+
onCommit(value, reason);
|
|
34
|
+
};
|
|
35
|
+
const choose = (o, reason = "enter") => {
|
|
36
|
+
if (!multi) return commit(o ? o.raw : null, reason);
|
|
37
|
+
if (!o) return;
|
|
38
|
+
setPicked((p) => pickedKeys.has(o.key) ? p.filter((k) => keyString(k) !== o.key) : [...p, o.raw]);
|
|
39
|
+
setNeedle("");
|
|
40
|
+
input.current?.focus();
|
|
41
|
+
};
|
|
42
|
+
const onKeyDown = (e) => {
|
|
43
|
+
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
|
|
44
|
+
e.preventDefault();
|
|
45
|
+
const step = e.key === "ArrowDown" ? 1 : -1;
|
|
46
|
+
setActive((a) => Math.max(0, Math.min(rows.length - 1, a + step)));
|
|
47
|
+
} else if (e.key === "Enter") {
|
|
48
|
+
e.preventDefault();
|
|
49
|
+
if (multi && needle === "") commit(picked, "enter");
|
|
50
|
+
else if (rows.length) choose(rows[active] ?? null);
|
|
51
|
+
} else if (e.key === "Escape") {
|
|
52
|
+
e.preventDefault();
|
|
53
|
+
done.current = true;
|
|
54
|
+
onCancel();
|
|
55
|
+
} else if (e.key === "Tab") {
|
|
56
|
+
e.preventDefault();
|
|
57
|
+
const reason = e.shiftKey ? "shift-tab" : "tab";
|
|
58
|
+
if (multi) commit(picked, reason);
|
|
59
|
+
else {
|
|
60
|
+
done.current = true;
|
|
61
|
+
onCancel();
|
|
62
|
+
}
|
|
63
|
+
} else if (multi && e.key === "Backspace" && needle === "" && picked.length) setPicked((p) => p.slice(0, -1));
|
|
64
|
+
};
|
|
65
|
+
const current = multi ? void 0 : lookupLabel(labels.row(initial), lookup);
|
|
66
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
67
|
+
className: "udc:relative udc:flex udc:h-full udc:w-full udc:min-w-0 udc:items-center udc:gap-1",
|
|
68
|
+
"data-testid": "lookup-combobox",
|
|
69
|
+
onBlur: (e) => {
|
|
70
|
+
if (e.currentTarget.contains(e.relatedTarget)) return;
|
|
71
|
+
if (multi) commit(picked, "blur");
|
|
72
|
+
else {
|
|
73
|
+
done.current = true;
|
|
74
|
+
onCancel();
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
children: [
|
|
78
|
+
multi ? picked.map((k) => {
|
|
79
|
+
const key = keyString(k);
|
|
80
|
+
return /* @__PURE__ */ jsxs("span", {
|
|
81
|
+
"data-testid": "lookup-picked",
|
|
82
|
+
className: "udc:inline-flex udc:h-5 udc:shrink-0 udc:items-center udc:gap-0.5 udc:rounded-full udc:bg-muted udc:pr-0.5 udc:pl-2 udc:text-[11px] udc:font-medium",
|
|
83
|
+
children: [lookupLabel(labels.row(k), lookup) ?? key, /* @__PURE__ */ jsx("button", {
|
|
84
|
+
type: "button",
|
|
85
|
+
"aria-label": `${t("APP", "CLEAR")} ${key}`,
|
|
86
|
+
className: "udc:grid udc:size-4 udc:place-items-center udc:rounded-full udc:hover:bg-primary/15",
|
|
87
|
+
onMouseDown: (e) => e.preventDefault(),
|
|
88
|
+
onClick: () => setPicked((p) => p.filter((x) => keyString(x) !== key)),
|
|
89
|
+
children: /* @__PURE__ */ jsx(X, { className: "udc:size-2.5" })
|
|
90
|
+
})]
|
|
91
|
+
}, key);
|
|
92
|
+
}) : null,
|
|
93
|
+
/* @__PURE__ */ jsx("input", {
|
|
94
|
+
ref: input,
|
|
95
|
+
type: "text",
|
|
96
|
+
role: "combobox",
|
|
97
|
+
"aria-expanded": true,
|
|
98
|
+
"aria-controls": "udc-lookup-options",
|
|
99
|
+
"aria-activedescendant": `udc-lookup-option-${active}`,
|
|
100
|
+
"data-testid": "lookup-search",
|
|
101
|
+
className: "udc:h-full udc:min-w-12 udc:flex-1 udc:bg-transparent udc:text-[13px] udc:text-foreground udc:outline-none",
|
|
102
|
+
value: needle,
|
|
103
|
+
placeholder: current ?? t("APP", "LOOKUP_SEARCH"),
|
|
104
|
+
readOnly: saving,
|
|
105
|
+
onChange: (e) => {
|
|
106
|
+
setNeedle(e.target.value);
|
|
107
|
+
setActive(0);
|
|
108
|
+
},
|
|
109
|
+
onKeyDown
|
|
110
|
+
}),
|
|
111
|
+
/* @__PURE__ */ jsxs("div", {
|
|
112
|
+
id: "udc-lookup-options",
|
|
113
|
+
role: "listbox",
|
|
114
|
+
"aria-multiselectable": multi || void 0,
|
|
115
|
+
"data-testid": "lookup-options",
|
|
116
|
+
className: "udc-scroll-thin udc:absolute udc:top-full udc:-left-1.5 udc:z-30 udc:mt-1 udc:max-h-64 udc:w-64 udc:overflow-y-auto udc:rounded-md udc:border udc:border-border udc:bg-surface-raised udc:p-1 udc:text-foreground udc:shadow-md",
|
|
117
|
+
children: [rows.map((o, i) => {
|
|
118
|
+
const selected = o ? multi ? pickedKeys.has(o.key) : keyString(initial) === o.key : false;
|
|
119
|
+
return /* @__PURE__ */ jsx("div", {
|
|
120
|
+
id: `udc-lookup-option-${i}`,
|
|
121
|
+
role: "option",
|
|
122
|
+
tabIndex: -1,
|
|
123
|
+
"aria-selected": selected,
|
|
124
|
+
"data-testid": o ? `lookup-option-${o.key}` : "lookup-option-none",
|
|
125
|
+
"data-label": o?.label,
|
|
126
|
+
onMouseDown: (e) => e.preventDefault(),
|
|
127
|
+
onMouseEnter: () => setActive(i),
|
|
128
|
+
onClick: () => choose(o),
|
|
129
|
+
onKeyDown: () => {},
|
|
130
|
+
className: cn("udc:flex udc:h-7 udc:cursor-pointer udc:items-center udc:gap-2 udc:rounded-sm udc:px-2 udc:text-[13px]", i === active && "udc:bg-hover", selected && "udc:font-semibold udc:text-primary", !o && "udc:text-muted-foreground"),
|
|
131
|
+
children: /* @__PURE__ */ jsx("span", {
|
|
132
|
+
className: "udc:truncate",
|
|
133
|
+
children: o ? o.label : t("APP", "LOOKUP_NONE")
|
|
134
|
+
})
|
|
135
|
+
}, o?.key ?? "∅");
|
|
136
|
+
}), !isLoading && options.length === 0 ? /* @__PURE__ */ jsx("div", {
|
|
137
|
+
className: "udc:px-2 udc:py-1.5 udc:text-xs udc:text-muted-foreground",
|
|
138
|
+
children: t("APP", "LOOKUP_NO_MATCH")
|
|
139
|
+
}) : null]
|
|
140
|
+
})
|
|
141
|
+
]
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
export { LookupCombobox };
|
|
145
|
+
|
|
146
|
+
//# sourceMappingURL=LookupCombobox.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LookupCombobox.js","names":[],"sources":["../../src/lookup/LookupCombobox.tsx"],"sourcesContent":["import type { FieldDef, LookupDef } from \"@jsudc/core\";\nimport { X } from \"lucide-react\";\nimport { type KeyboardEvent, useEffect, useRef, useState } from \"react\";\nimport { useI18n } from \"../i18n/i18n.js\";\nimport { cn } from \"../lib/utils.js\";\nimport { lookupLabel } from \"./label.js\";\nimport { type LookupOption, useLookupOptions } from \"./options.js\";\nimport { keyString, lookupKeys, useUdcLookup } from \"./useUdcLookup.js\";\n\nexport type ComboboxCommitReason = \"enter\" | \"tab\" | \"shift-tab\" | \"blur\";\n\n/**\n * The inline editor of a lookup cell: a search over the target collection\n * (`contains` on `label_field`, the first 50 by label) in a list under the\n * cell. On a `lookup` a pick commits that key; on a `lookup_array` a pick\n * adds or removes a key, and Enter on an empty search commits the list.\n * Arrow keys move, Escape cancels, Tab commits and moves on.\n */\nexport function LookupCombobox({\n def,\n lookup,\n initial,\n saving,\n onCommit,\n onCancel,\n}: {\n def: FieldDef;\n lookup: LookupDef;\n initial: unknown;\n saving: boolean;\n /** `value` is the key (or `null`), on a lookup_array the list of keys. */\n onCommit: (value: unknown, reason: ComboboxCommitReason) => void;\n onCancel: () => void;\n}) {\n const { t } = useI18n();\n const multi = def.type === \"lookup_array\";\n const [needle, setNeedle] = useState(\"\");\n const [active, setActive] = useState(0);\n // Keys as stored: a number key stays a number when it is written back.\n const [picked, setPicked] = useState<unknown[]>(() =>\n multi ? (Array.isArray(initial) ? [...initial] : []) : [],\n );\n const { options, isLoading } = useLookupOptions(lookup, needle);\n const labels = useUdcLookup(lookup, multi ? picked : [initial]);\n const input = useRef<HTMLInputElement>(null);\n const done = useRef(false);\n\n useEffect(() => input.current?.focus(), []);\n\n const pickedKeys = new Set(lookupKeys(picked));\n // A single lookup offers \"none\" first, so a key can be cleared.\n const rows: (LookupOption | null)[] = multi ? options : [null, ...options];\n\n const commit = (value: unknown, reason: ComboboxCommitReason) => {\n if (done.current || saving) return;\n done.current = true;\n onCommit(value, reason);\n };\n\n const choose = (o: LookupOption | null, reason: ComboboxCommitReason = \"enter\") => {\n if (!multi) return commit(o ? o.raw : null, reason);\n if (!o) return;\n setPicked((p) =>\n pickedKeys.has(o.key) ? p.filter((k) => keyString(k) !== o.key) : [...p, o.raw],\n );\n setNeedle(\"\");\n input.current?.focus();\n };\n\n const onKeyDown = (e: KeyboardEvent) => {\n if (e.key === \"ArrowDown\" || e.key === \"ArrowUp\") {\n e.preventDefault();\n const step = e.key === \"ArrowDown\" ? 1 : -1;\n setActive((a) => Math.max(0, Math.min(rows.length - 1, a + step)));\n } else if (e.key === \"Enter\") {\n e.preventDefault();\n if (multi && needle === \"\") commit(picked, \"enter\");\n else if (rows.length) choose(rows[active] ?? null);\n } else if (e.key === \"Escape\") {\n e.preventDefault();\n done.current = true;\n onCancel();\n } else if (e.key === \"Tab\") {\n e.preventDefault();\n const reason = e.shiftKey ? \"shift-tab\" : \"tab\";\n if (multi) commit(picked, reason);\n else {\n done.current = true;\n onCancel();\n }\n } else if (multi && e.key === \"Backspace\" && needle === \"\" && picked.length) {\n setPicked((p) => p.slice(0, -1));\n }\n };\n\n const current = multi ? undefined : lookupLabel(labels.row(initial), lookup);\n\n return (\n // biome-ignore lint/a11y/noStaticElementInteractions: focus leaving the whole editor is the commit, not an interaction\n <div\n className=\"udc:relative udc:flex udc:h-full udc:w-full udc:min-w-0 udc:items-center udc:gap-1\"\n data-testid=\"lookup-combobox\"\n onBlur={(e) => {\n // Leaving the editor (not moving inside it) commits a list and drops a single pick.\n if (e.currentTarget.contains(e.relatedTarget as Node | null)) return;\n if (multi) commit(picked, \"blur\");\n else {\n done.current = true;\n onCancel();\n }\n }}\n >\n {multi\n ? picked.map((k) => {\n const key = keyString(k);\n return (\n <span\n key={key}\n data-testid=\"lookup-picked\"\n className=\"udc:inline-flex udc:h-5 udc:shrink-0 udc:items-center udc:gap-0.5 udc:rounded-full udc:bg-muted udc:pr-0.5 udc:pl-2 udc:text-[11px] udc:font-medium\"\n >\n {lookupLabel(labels.row(k), lookup) ?? key}\n <button\n type=\"button\"\n aria-label={`${t(\"APP\", \"CLEAR\")} ${key}`}\n className=\"udc:grid udc:size-4 udc:place-items-center udc:rounded-full udc:hover:bg-primary/15\"\n onMouseDown={(e) => e.preventDefault()}\n onClick={() => setPicked((p) => p.filter((x) => keyString(x) !== key))}\n >\n <X className=\"udc:size-2.5\" />\n </button>\n </span>\n );\n })\n : null}\n <input\n ref={input}\n type=\"text\"\n role=\"combobox\"\n aria-expanded\n aria-controls=\"udc-lookup-options\"\n aria-activedescendant={`udc-lookup-option-${active}`}\n data-testid=\"lookup-search\"\n className=\"udc:h-full udc:min-w-12 udc:flex-1 udc:bg-transparent udc:text-[13px] udc:text-foreground udc:outline-none\"\n value={needle}\n placeholder={current ?? t(\"APP\", \"LOOKUP_SEARCH\")}\n readOnly={saving}\n onChange={(e) => {\n setNeedle(e.target.value);\n setActive(0);\n }}\n onKeyDown={onKeyDown}\n />\n <div\n id=\"udc-lookup-options\"\n role=\"listbox\"\n aria-multiselectable={multi || undefined}\n data-testid=\"lookup-options\"\n className=\"udc-scroll-thin udc:absolute udc:top-full udc:-left-1.5 udc:z-30 udc:mt-1 udc:max-h-64 udc:w-64 udc:overflow-y-auto udc:rounded-md udc:border udc:border-border udc:bg-surface-raised udc:p-1 udc:text-foreground udc:shadow-md\"\n >\n {rows.map((o, i) => {\n const selected = o\n ? multi\n ? pickedKeys.has(o.key)\n : keyString(initial) === o.key\n : false;\n return (\n <div\n key={o?.key ?? \"∅\"}\n id={`udc-lookup-option-${i}`}\n role=\"option\"\n tabIndex={-1}\n aria-selected={selected}\n data-testid={o ? `lookup-option-${o.key}` : \"lookup-option-none\"}\n data-label={o?.label}\n onMouseDown={(e) => e.preventDefault()}\n onMouseEnter={() => setActive(i)}\n onClick={() => choose(o)}\n onKeyDown={() => {}}\n className={cn(\n \"udc:flex udc:h-7 udc:cursor-pointer udc:items-center udc:gap-2 udc:rounded-sm udc:px-2 udc:text-[13px]\",\n i === active && \"udc:bg-hover\",\n selected && \"udc:font-semibold udc:text-primary\",\n !o && \"udc:text-muted-foreground\",\n )}\n >\n <span className=\"udc:truncate\">{o ? o.label : t(\"APP\", \"LOOKUP_NONE\")}</span>\n </div>\n );\n })}\n {!isLoading && options.length === 0 ? (\n <div className=\"udc:px-2 udc:py-1.5 udc:text-xs udc:text-muted-foreground\">\n {t(\"APP\", \"LOOKUP_NO_MATCH\")}\n </div>\n ) : null}\n </div>\n </div>\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAkBA,SAAgB,eAAe,EAC7B,KACA,QACA,SACA,QACA,UACA,YASC;CACD,MAAM,EAAE,MAAM,QAAQ;CACtB,MAAM,QAAQ,IAAI,SAAS;CAC3B,MAAM,CAAC,QAAQ,aAAa,SAAS,EAAE;CACvC,MAAM,CAAC,QAAQ,aAAa,SAAS,CAAC;CAEtC,MAAM,CAAC,QAAQ,aAAa,eAC1B,QAAS,MAAM,QAAQ,OAAO,IAAI,CAAC,GAAG,OAAO,IAAI,CAAC,IAAK,CAAC,CAC1D;CACA,MAAM,EAAE,SAAS,cAAc,iBAAiB,QAAQ,MAAM;CAC9D,MAAM,SAAS,aAAa,QAAQ,QAAQ,SAAS,CAAC,OAAO,CAAC;CAC9D,MAAM,QAAQ,OAAyB,IAAI;CAC3C,MAAM,OAAO,OAAO,KAAK;CAEzB,gBAAgB,MAAM,SAAS,MAAM,GAAG,CAAC,CAAC;CAE1C,MAAM,aAAa,IAAI,IAAI,WAAW,MAAM,CAAC;CAE7C,MAAM,OAAgC,QAAQ,UAAU,CAAC,MAAM,GAAG,OAAO;CAEzE,MAAM,UAAU,OAAgB,WAAiC;EAC/D,IAAI,KAAK,WAAW,QAAQ;EAC5B,KAAK,UAAU;EACf,SAAS,OAAO,MAAM;CACxB;CAEA,MAAM,UAAU,GAAwB,SAA+B,YAAY;EACjF,IAAI,CAAC,OAAO,OAAO,OAAO,IAAI,EAAE,MAAM,MAAM,MAAM;EAClD,IAAI,CAAC,GAAG;EACR,WAAW,MACT,WAAW,IAAI,EAAE,GAAG,IAAI,EAAE,QAAQ,MAAM,UAAU,CAAC,MAAM,EAAE,GAAG,IAAI,C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aAAa;MAAQ,CAAA;KACzE,GAnBE,GAAG,OAAO,GAmBZ;IAET,CAAC,GACA,CAAC,aAAa,QAAQ,WAAW,IAChC,oBAAC,OAAD;KAAK,WAAU;KACZ,UAAA,EAAE,OAAO,iBAAiB;IACxB,CAAA,IACH,IACD;;EACF;;AAET"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { FilterChipConfig, LookupDef } from "@jsudc/core";
|
|
2
|
+
/**
|
|
3
|
+
* A `select` chip over a lookup field: its options are the target's rows,
|
|
4
|
+
* shown by `label_field` and stored by key. Up to `LOOKUP_OPTIONS_LIMIT`
|
|
5
|
+
* rows are listed as they are; a larger target gets a search box that asks
|
|
6
|
+
* the server (`contains` on `label_field`).
|
|
7
|
+
*/
|
|
8
|
+
export declare function LookupSelectChip({ cfg, lookup, label, value, onChange, onClear, }: {
|
|
9
|
+
cfg: FilterChipConfig;
|
|
10
|
+
lookup: LookupDef;
|
|
11
|
+
label: string;
|
|
12
|
+
value: {
|
|
13
|
+
values: string[];
|
|
14
|
+
} | undefined;
|
|
15
|
+
onChange: (values: string[]) => void;
|
|
16
|
+
onClear: () => void;
|
|
17
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { ChipTrigger } from "../data-view/FilterChips.js";
|
|
2
|
+
import { useI18n } from "../i18n/i18n.js";
|
|
3
|
+
import { useUiKit } from "../ui-kit/context.js";
|
|
4
|
+
import { lookupLabel } from "./label.js";
|
|
5
|
+
import { useLookupOptions } from "./options.js";
|
|
6
|
+
import { useUdcLookup } from "./useUdcLookup.js";
|
|
7
|
+
import { useState } from "react";
|
|
8
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
9
|
+
// src/lookup/LookupSelectChip.tsx
|
|
10
|
+
/**
|
|
11
|
+
* A `select` chip over a lookup field: its options are the target's rows,
|
|
12
|
+
* shown by `label_field` and stored by key. Up to `LOOKUP_OPTIONS_LIMIT`
|
|
13
|
+
* rows are listed as they are; a larger target gets a search box that asks
|
|
14
|
+
* the server (`contains` on `label_field`).
|
|
15
|
+
*/
|
|
16
|
+
function LookupSelectChip({ cfg, lookup, label, value, onChange, onClear }) {
|
|
17
|
+
const { Button, Checkbox, Input, Popover, PopoverContent, Spinner } = useUiKit();
|
|
18
|
+
const { t } = useI18n();
|
|
19
|
+
const [open, setOpen] = useState(false);
|
|
20
|
+
const [needle, setNeedle] = useState("");
|
|
21
|
+
const selected = value?.values ?? [];
|
|
22
|
+
const all = useLookupOptions(lookup, "", { enabled: open });
|
|
23
|
+
const searchable = (all.total ?? 0) > 50;
|
|
24
|
+
const found = useLookupOptions(lookup, needle, { enabled: open && searchable && needle !== "" });
|
|
25
|
+
const shown = searchable && needle ? found : all;
|
|
26
|
+
const picked = useUdcLookup(lookup, selected);
|
|
27
|
+
const name = (key) => lookupLabel(picked.row(key), lookup) ?? key;
|
|
28
|
+
const summary = selected.length === 0 ? null : selected.length === 1 ? name(selected[0]) : `${name(selected[0])} +${selected.length - 1}`;
|
|
29
|
+
const toggle = (k) => onChange(selected.includes(k) ? selected.filter((s) => s !== k) : [...selected, k]);
|
|
30
|
+
return /* @__PURE__ */ jsxs(Popover, {
|
|
31
|
+
open,
|
|
32
|
+
onOpenChange: (next) => {
|
|
33
|
+
setOpen(next);
|
|
34
|
+
if (!next) setNeedle("");
|
|
35
|
+
},
|
|
36
|
+
children: [/* @__PURE__ */ jsx(ChipTrigger, {
|
|
37
|
+
field: cfg.field,
|
|
38
|
+
label,
|
|
39
|
+
summary,
|
|
40
|
+
onClear
|
|
41
|
+
}), /* @__PURE__ */ jsxs(PopoverContent, {
|
|
42
|
+
className: "udc:w-64 udc:p-2",
|
|
43
|
+
"data-testid": `chip-panel-${cfg.field}`,
|
|
44
|
+
children: [
|
|
45
|
+
searchable ? /* @__PURE__ */ jsx(Input, {
|
|
46
|
+
autoFocus: true,
|
|
47
|
+
value: needle,
|
|
48
|
+
onChange: (e) => setNeedle(e.target.value),
|
|
49
|
+
placeholder: t("APP", "LOOKUP_SEARCH"),
|
|
50
|
+
"data-testid": `chip-search-${cfg.field}`,
|
|
51
|
+
className: "udc:mb-2 udc:h-7 udc:text-xs"
|
|
52
|
+
}) : null,
|
|
53
|
+
/* @__PURE__ */ jsxs("div", {
|
|
54
|
+
className: "udc-scroll-thin udc:-mx-1 udc:max-h-64 udc:overflow-y-auto udc:px-1",
|
|
55
|
+
children: [
|
|
56
|
+
shown.isLoading ? /* @__PURE__ */ jsx("div", {
|
|
57
|
+
className: "udc:flex udc:h-10 udc:items-center udc:justify-center",
|
|
58
|
+
children: /* @__PURE__ */ jsx(Spinner, {})
|
|
59
|
+
}) : shown.options.map((o) => /* @__PURE__ */ jsxs("label", {
|
|
60
|
+
"data-testid": `chip-option-${o.key}`,
|
|
61
|
+
"data-label": o.label,
|
|
62
|
+
className: "udc:flex udc:h-7 udc:cursor-pointer udc:items-center udc:gap-2 udc:rounded-md udc:px-1.5 udc:text-[13px] udc:hover:bg-hover",
|
|
63
|
+
children: [/* @__PURE__ */ jsx(Checkbox, {
|
|
64
|
+
checked: selected.includes(o.key),
|
|
65
|
+
onCheckedChange: () => toggle(o.key)
|
|
66
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
67
|
+
className: "udc:truncate",
|
|
68
|
+
children: o.label
|
|
69
|
+
})]
|
|
70
|
+
}, o.key)),
|
|
71
|
+
!shown.isLoading && shown.options.length === 0 ? /* @__PURE__ */ jsx("div", {
|
|
72
|
+
className: "udc:px-1.5 udc:py-2 udc:text-xs udc:text-muted-foreground",
|
|
73
|
+
children: t("APP", "LOOKUP_NO_MATCH")
|
|
74
|
+
}) : null,
|
|
75
|
+
searchable && !needle ? /* @__PURE__ */ jsx("div", {
|
|
76
|
+
className: "udc:px-1.5 udc:pt-1 udc:text-[11px] udc:text-muted-foreground",
|
|
77
|
+
children: t("APP", "LOOKUP_MORE", { n: (all.total ?? 0) - all.options.length })
|
|
78
|
+
}) : null
|
|
79
|
+
]
|
|
80
|
+
}),
|
|
81
|
+
selected.length ? /* @__PURE__ */ jsxs("div", {
|
|
82
|
+
className: "udc:mt-2 udc:flex udc:items-center udc:justify-between udc:border-t udc:border-border udc:pt-2 udc:text-xs udc:text-muted-foreground",
|
|
83
|
+
children: [/* @__PURE__ */ jsx("span", { children: t("APP", "SELECTED_N", { n: selected.length }) }), /* @__PURE__ */ jsx(Button, {
|
|
84
|
+
size: "sm",
|
|
85
|
+
variant: "ghost",
|
|
86
|
+
onClick: onClear,
|
|
87
|
+
children: t("APP", "CLEAR")
|
|
88
|
+
})]
|
|
89
|
+
}) : null
|
|
90
|
+
]
|
|
91
|
+
})]
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
export { LookupSelectChip };
|
|
95
|
+
|
|
96
|
+
//# sourceMappingURL=LookupSelectChip.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LookupSelectChip.js","names":[],"sources":["../../src/lookup/LookupSelectChip.tsx"],"sourcesContent":["import type { FilterChipConfig, LookupDef } from \"@jsudc/core\";\nimport { useState } from \"react\";\nimport { ChipTrigger } from \"../data-view/FilterChips.js\";\nimport { useI18n } from \"../i18n/i18n.js\";\nimport { useUiKit } from \"../ui-kit/context.js\";\nimport { lookupLabel } from \"./label.js\";\nimport { LOOKUP_OPTIONS_LIMIT, useLookupOptions } from \"./options.js\";\nimport { useUdcLookup } from \"./useUdcLookup.js\";\n\n/**\n * A `select` chip over a lookup field: its options are the target's rows,\n * shown by `label_field` and stored by key. Up to `LOOKUP_OPTIONS_LIMIT`\n * rows are listed as they are; a larger target gets a search box that asks\n * the server (`contains` on `label_field`).\n */\nexport function LookupSelectChip({\n cfg,\n lookup,\n label,\n value,\n onChange,\n onClear,\n}: {\n cfg: FilterChipConfig;\n lookup: LookupDef;\n label: string;\n value: { values: string[] } | undefined;\n onChange: (values: string[]) => void;\n onClear: () => void;\n}) {\n const { Button, Checkbox, Input, Popover, PopoverContent, Spinner } = useUiKit();\n const { t } = useI18n();\n const [open, setOpen] = useState(false);\n const [needle, setNeedle] = useState(\"\");\n const selected = value?.values ?? [];\n const all = useLookupOptions(lookup, \"\", { enabled: open });\n const searchable = (all.total ?? 0) > LOOKUP_OPTIONS_LIMIT;\n const found = useLookupOptions(lookup, needle, { enabled: open && searchable && needle !== \"\" });\n const shown = searchable && needle ? found : all;\n // The labels of what is selected, for the summary (the options may not hold them).\n const picked = useUdcLookup(lookup, selected);\n const name = (key: string) => lookupLabel(picked.row(key), lookup) ?? key;\n const summary =\n selected.length === 0\n ? null\n : selected.length === 1\n ? name(selected[0] as string)\n : `${name(selected[0] as string)} +${selected.length - 1}`;\n\n const toggle = (k: string) =>\n onChange(selected.includes(k) ? selected.filter((s) => s !== k) : [...selected, k]);\n\n return (\n <Popover\n open={open}\n onOpenChange={(next) => {\n setOpen(next);\n if (!next) setNeedle(\"\");\n }}\n >\n <ChipTrigger field={cfg.field} label={label} summary={summary} onClear={onClear} />\n <PopoverContent className=\"udc:w-64 udc:p-2\" data-testid={`chip-panel-${cfg.field}`}>\n {searchable ? (\n <Input\n autoFocus\n value={needle}\n onChange={(e) => setNeedle(e.target.value)}\n placeholder={t(\"APP\", \"LOOKUP_SEARCH\")}\n data-testid={`chip-search-${cfg.field}`}\n className=\"udc:mb-2 udc:h-7 udc:text-xs\"\n />\n ) : null}\n <div className=\"udc-scroll-thin udc:-mx-1 udc:max-h-64 udc:overflow-y-auto udc:px-1\">\n {shown.isLoading ? (\n <div className=\"udc:flex udc:h-10 udc:items-center udc:justify-center\">\n <Spinner />\n </div>\n ) : (\n shown.options.map((o) => (\n // biome-ignore lint/a11y/noLabelWithoutControl: the Radix checkbox inside is the control\n <label\n key={o.key}\n data-testid={`chip-option-${o.key}`}\n data-label={o.label}\n className=\"udc:flex udc:h-7 udc:cursor-pointer udc:items-center udc:gap-2 udc:rounded-md udc:px-1.5 udc:text-[13px] udc:hover:bg-hover\"\n >\n <Checkbox\n checked={selected.includes(o.key)}\n onCheckedChange={() => toggle(o.key)}\n />\n <span className=\"udc:truncate\">{o.label}</span>\n </label>\n ))\n )}\n {!shown.isLoading && shown.options.length === 0 ? (\n <div className=\"udc:px-1.5 udc:py-2 udc:text-xs udc:text-muted-foreground\">\n {t(\"APP\", \"LOOKUP_NO_MATCH\")}\n </div>\n ) : null}\n {searchable && !needle ? (\n <div className=\"udc:px-1.5 udc:pt-1 udc:text-[11px] udc:text-muted-foreground\">\n {t(\"APP\", \"LOOKUP_MORE\", { n: (all.total ?? 0) - all.options.length })}\n </div>\n ) : null}\n </div>\n {selected.length ? (\n <div className=\"udc:mt-2 udc:flex udc:items-center udc:justify-between udc:border-t udc:border-border udc:pt-2 udc:text-xs udc:text-muted-foreground\">\n <span>{t(\"APP\", \"SELECTED_N\", { n: selected.length })}</span>\n <Button size=\"sm\" variant=\"ghost\" onClick={onClear}>\n {t(\"APP\", \"CLEAR\")}\n </Button>\n </div>\n ) : null}\n </PopoverContent>\n </Popover>\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;AAeA,SAAgB,iBAAiB,EAC/B,KACA,QACA,OACA,OACA,UACA,WAQC;CACD,MAAM,EAAE,QAAQ,UAAU,OAAO,SAAS,gBAAgB,YAAY,SAAS;CAC/E,MAAM,EAAE,MAAM,QAAQ;CACtB,MAAM,CAAC,MAAM,WAAW,SAAS,KAAK;CACtC,MAAM,CAAC,QAAQ,aAAa,SAAS,EAAE;CACvC,MAAM,WAAW,OAAO,UAAU,CAAC;CACnC,MAAM,MAAM,iBAAiB,QAAQ,IAAI,EAAE,SAAS,KAAK,CAAC;CAC1D,MAAM,cAAc,IAAI,SAAS,KAAA;CACjC,MAAM,QAAQ,iBAAiB,QAAQ,QAAQ,EAAE,SAAS,QAAQ,cAAc,WAAW,GAAG,CAAC;CAC/F,MAAM,QAAQ,cAAc,SAAS,QAAQ;CAE7C,MAAM,SAAS,aAAa,QAAQ,QAAQ;CAC5C,MAAM,QAAQ,QAAgB,YAAY,OAAO,IAAI,GAAG,GAAG,MAAM,KAAK;CACtE,M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A,IACH;MACH,cAAc,CAAC,SACd,oBAAC,OAAD;OAAK,WAAU;OACZ,UAAA,EAAE,OAAO,eAAe,EAAE,IAAI,IAAI,SAAS,KAAK,IAAI,QAAQ,OAAO,CAAC;MAClE,CAAA,IACH;KACD;;IACJ,SAAS,SACR,qBAAC,OAAD;KAAK,WAAU;KAAf,UAAA,CACE,oBAAC,QAAD,EAAA,UAAO,EAAE,OAAO,cAAc,EAAE,GAAG,SAAS,OAAO,CAAC,EAAQ,CAAA,GAC5D,oBAAC,QAAD;MAAQ,MAAK;MAAK,SAAQ;MAAQ,SAAS;MACxC,UAAA,EAAE,OAAO,OAAO;KACX,CAAA,CACL;IACH,CAAA,IAAA;GACU;EACT,CAAA,CAAA;;AAEb"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type FieldDef, type LookupDef, type Row } from "@jsudc/core";
|
|
2
|
+
/**
|
|
3
|
+
* The labels of a lookup value, from the batch around (`UdcLookupBatch`) or,
|
|
4
|
+
* outside one, from a `useUdcLookup` of its own. `field` names the batch
|
|
5
|
+
* entry. A key not (yet) resolved maps to `undefined`.
|
|
6
|
+
*/
|
|
7
|
+
export declare function useLookupRows(def: FieldDef | undefined, value: unknown, field?: string): {
|
|
8
|
+
lookup: LookupDef | undefined;
|
|
9
|
+
keys: string[];
|
|
10
|
+
row: (key: string) => Row | undefined;
|
|
11
|
+
};
|
|
12
|
+
/** The labels of a lookup value as strings, the bare key where no label is known. */
|
|
13
|
+
export declare function useLookupLabels(def: FieldDef | undefined, value: unknown, field?: string): string[];
|
|
14
|
+
/**
|
|
15
|
+
* A `lookup` cell: the target's `label_field` as a link to the target
|
|
16
|
+
* collection filtered by the key (`?f=` `{field: <key field>, op: eq}`), the
|
|
17
|
+
* bare key until the label arrives or when the target has no such row. A
|
|
18
|
+
* `lookup_array` is a list of such chips.
|
|
19
|
+
*/
|
|
20
|
+
export declare function LookupValue({ def, value, field, editable, }: {
|
|
21
|
+
def: FieldDef;
|
|
22
|
+
value: unknown;
|
|
23
|
+
/** The field, so the batch around the cell (if any) answers for it. */
|
|
24
|
+
field?: string | undefined;
|
|
25
|
+
/** In an editable cell a plain click does not follow the link (Cmd / Ctrl-click does). */
|
|
26
|
+
editable?: boolean;
|
|
27
|
+
}): import("react").JSX.Element | null;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { cn } from "../lib/utils.js";
|
|
2
|
+
import { batchCovers, useLookupBatchContext } from "./batch.js";
|
|
3
|
+
import { lookupExtras, lookupLabel } from "./label.js";
|
|
4
|
+
import { filterHref, followLink, useUdcLinks } from "./links.js";
|
|
5
|
+
import { keyString, useUdcLookup, valueKeys } from "./useUdcLookup.js";
|
|
6
|
+
import { ROW_ID, lookupKeyField, lookupOf } from "@jsudc/core";
|
|
7
|
+
import { jsx } from "react/jsx-runtime";
|
|
8
|
+
// src/lookup/LookupValue.tsx
|
|
9
|
+
/**
|
|
10
|
+
* The labels of a lookup value, from the batch around (`UdcLookupBatch`) or,
|
|
11
|
+
* outside one, from a `useUdcLookup` of its own. `field` names the batch
|
|
12
|
+
* entry. A key not (yet) resolved maps to `undefined`.
|
|
13
|
+
*/
|
|
14
|
+
function useLookupRows(def, value, field) {
|
|
15
|
+
const lookup = lookupOf(def);
|
|
16
|
+
const keys = valueKeys([value]);
|
|
17
|
+
const batch = useLookupBatchContext();
|
|
18
|
+
const fromBatch = lookup ? batchCovers(batch, field, lookup, keys) : void 0;
|
|
19
|
+
const own = useUdcLookup(lookup ?? {
|
|
20
|
+
collection: "",
|
|
21
|
+
label_field: ""
|
|
22
|
+
}, keys, { enabled: lookup !== void 0 && fromBatch === void 0 });
|
|
23
|
+
const res = fromBatch ?? own;
|
|
24
|
+
return {
|
|
25
|
+
lookup,
|
|
26
|
+
keys,
|
|
27
|
+
row: (key) => res.row(key)
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/** The labels of a lookup value as strings, the bare key where no label is known. */
|
|
31
|
+
function useLookupLabels(def, value, field) {
|
|
32
|
+
const { lookup, keys, row } = useLookupRows(def, value, field);
|
|
33
|
+
if (!lookup) return keys;
|
|
34
|
+
return keys.map((k) => lookupLabel(row(k), lookup) ?? k);
|
|
35
|
+
}
|
|
36
|
+
function LookupLink({ lookup, keyValue, row, chip, editable }) {
|
|
37
|
+
const links = useUdcLinks();
|
|
38
|
+
const label = lookupLabel(row, lookup);
|
|
39
|
+
const field = lookupKeyField(lookup);
|
|
40
|
+
const href = filterHref(links, lookup.collection, {
|
|
41
|
+
field: field === ROW_ID ? ROW_ID : field,
|
|
42
|
+
op: "eq",
|
|
43
|
+
value: keyValue
|
|
44
|
+
});
|
|
45
|
+
const onClick = (e) => {
|
|
46
|
+
if (editable && !(e.metaKey || e.ctrlKey || e.button === 1)) {
|
|
47
|
+
e.preventDefault();
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
e.stopPropagation();
|
|
51
|
+
followLink(e, href, links);
|
|
52
|
+
};
|
|
53
|
+
return /* @__PURE__ */ jsx("a", {
|
|
54
|
+
href,
|
|
55
|
+
onClick,
|
|
56
|
+
"data-testid": "lookup-link",
|
|
57
|
+
"data-key": keyValue,
|
|
58
|
+
"data-resolved": label !== void 0 || void 0,
|
|
59
|
+
title: lookupExtras(row, lookup) ?? label ?? keyValue,
|
|
60
|
+
className: cn("udc:min-w-0 udc:truncate udc:text-foreground udc:underline-offset-2 udc:hover:text-primary udc:hover:underline udc:focus-visible:underline udc:focus-visible:outline-none", chip && "udc:inline-flex udc:h-5 udc:shrink-0 udc:items-center udc:rounded-full udc:bg-muted udc:px-2 udc:text-[11px] udc:font-medium udc:hover:no-underline udc:hover:bg-primary/12", label === void 0 && "udc:font-mono udc:text-muted-foreground"),
|
|
61
|
+
children: label ?? keyValue
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* A `lookup` cell: the target's `label_field` as a link to the target
|
|
66
|
+
* collection filtered by the key (`?f=` `{field: <key field>, op: eq}`), the
|
|
67
|
+
* bare key until the label arrives or when the target has no such row. A
|
|
68
|
+
* `lookup_array` is a list of such chips.
|
|
69
|
+
*/
|
|
70
|
+
function LookupValue({ def, value, field, editable = false }) {
|
|
71
|
+
const { lookup, keys, row } = useLookupRows(def, value, field);
|
|
72
|
+
if (!lookup) return /* @__PURE__ */ jsx("span", {
|
|
73
|
+
className: "udc:truncate",
|
|
74
|
+
children: keys.join(", ")
|
|
75
|
+
});
|
|
76
|
+
if (def.type === "lookup_array") return /* @__PURE__ */ jsx("span", {
|
|
77
|
+
"data-testid": "lookup-chips",
|
|
78
|
+
className: "udc:flex udc:min-w-0 udc:items-center udc:gap-1 udc:overflow-hidden",
|
|
79
|
+
children: keys.map((k) => /* @__PURE__ */ jsx(LookupLink, {
|
|
80
|
+
lookup,
|
|
81
|
+
keyValue: k,
|
|
82
|
+
row: row(k),
|
|
83
|
+
chip: true,
|
|
84
|
+
editable
|
|
85
|
+
}, k))
|
|
86
|
+
});
|
|
87
|
+
const key = keys[0];
|
|
88
|
+
return key === void 0 ? null : /* @__PURE__ */ jsx(LookupLink, {
|
|
89
|
+
lookup,
|
|
90
|
+
keyValue: keyString(key),
|
|
91
|
+
row: row(key),
|
|
92
|
+
chip: false,
|
|
93
|
+
editable
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
export { LookupValue, useLookupLabels, useLookupRows };
|
|
97
|
+
|
|
98
|
+
//# sourceMappingURL=LookupValue.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LookupValue.js","names":[],"sources":["../../src/lookup/LookupValue.tsx"],"sourcesContent":["import {\n type FieldDef,\n type LookupDef,\n lookupKeyField,\n lookupOf,\n ROW_ID,\n type Row,\n} from \"@jsudc/core\";\nimport type { MouseEvent } from \"react\";\nimport { cn } from \"../lib/utils.js\";\nimport { batchCovers, useLookupBatchContext } from \"./batch.js\";\nimport { lookupExtras, lookupLabel } from \"./label.js\";\nimport { filterHref, followLink, useUdcLinks } from \"./links.js\";\nimport { keyString, useUdcLookup, valueKeys } from \"./useUdcLookup.js\";\n\n/**\n * The labels of a lookup value, from the batch around (`UdcLookupBatch`) or,\n * outside one, from a `useUdcLookup` of its own. `field` names the batch\n * entry. A key not (yet) resolved maps to `undefined`.\n */\nexport function useLookupRows(\n def: FieldDef | undefined,\n value: unknown,\n field?: string,\n): { lookup: LookupDef | undefined; keys: string[]; row: (key: string) => Row | undefined } {\n const lookup = lookupOf(def);\n const keys = valueKeys([value]);\n const batch = useLookupBatchContext();\n const fromBatch = lookup ? batchCovers(batch, field, lookup, keys) : undefined;\n const own = useUdcLookup(lookup ?? { collection: \"\", label_field: \"\" }, keys, {\n enabled: lookup !== undefined && fromBatch === undefined,\n });\n const res = fromBatch ?? own;\n return { lookup, keys, row: (key) => res.row(key) };\n}\n\n/** The labels of a lookup value as strings, the bare key where no label is known. */\nexport function useLookupLabels(\n def: FieldDef | undefined,\n value: unknown,\n field?: string,\n): string[] {\n const { lookup, keys, row } = useLookupRows(def, value, field);\n if (!lookup) return keys;\n return keys.map((k) => lookupLabel(row(k), lookup) ?? k);\n}\n\nfunction LookupLink({\n lookup,\n keyValue,\n row,\n chip,\n editable,\n}: {\n lookup: LookupDef;\n keyValue: string;\n row: Row | undefined;\n chip: boolean;\n editable: boolean;\n}) {\n const links = useUdcLinks();\n const label = lookupLabel(row, lookup);\n const field = lookupKeyField(lookup);\n const href = filterHref(links, lookup.collection, {\n field: field === ROW_ID ? ROW_ID : field,\n op: \"eq\",\n value: keyValue,\n });\n const onClick = (e: MouseEvent) => {\n // In an editable cell a plain click belongs to the cell (double-click edits);\n // Cmd / Ctrl-click still opens the target.\n if (editable && !(e.metaKey || e.ctrlKey || e.button === 1)) {\n e.preventDefault();\n return;\n }\n // The row under the link may have a row_link of its own: the link wins.\n e.stopPropagation();\n followLink(e, href, links);\n };\n return (\n <a\n href={href}\n onClick={onClick}\n data-testid=\"lookup-link\"\n data-key={keyValue}\n data-resolved={label !== undefined || undefined}\n title={lookupExtras(row, lookup) ?? label ?? keyValue}\n className={cn(\n \"udc:min-w-0 udc:truncate udc:text-foreground udc:underline-offset-2 udc:hover:text-primary udc:hover:underline udc:focus-visible:underline udc:focus-visible:outline-none\",\n chip &&\n \"udc:inline-flex udc:h-5 udc:shrink-0 udc:items-center udc:rounded-full udc:bg-muted udc:px-2 udc:text-[11px] udc:font-medium udc:hover:no-underline udc:hover:bg-primary/12\",\n label === undefined && \"udc:font-mono udc:text-muted-foreground\",\n )}\n >\n {label ?? keyValue}\n </a>\n );\n}\n\n/**\n * A `lookup` cell: the target's `label_field` as a link to the target\n * collection filtered by the key (`?f=` `{field: <key field>, op: eq}`), the\n * bare key until the label arrives or when the target has no such row. A\n * `lookup_array` is a list of such chips.\n */\nexport function LookupValue({\n def,\n value,\n field,\n editable = false,\n}: {\n def: FieldDef;\n value: unknown;\n /** The field, so the batch around the cell (if any) answers for it. */\n field?: string | undefined;\n /** In an editable cell a plain click does not follow the link (Cmd / Ctrl-click does). */\n editable?: boolean;\n}) {\n const { lookup, keys, row } = useLookupRows(def, value, field);\n if (!lookup) return <span className=\"udc:truncate\">{keys.join(\", \")}</span>;\n if (def.type === \"lookup_array\") {\n return (\n <span\n data-testid=\"lookup-chips\"\n className=\"udc:flex udc:min-w-0 udc:items-center udc:gap-1 udc:overflow-hidden\"\n >\n {keys.map((k) => (\n <LookupLink key={k} lookup={lookup} keyValue={k} row={row(k)} chip editable={editable} />\n ))}\n </span>\n );\n }\n const key = keys[0];\n return key === undefined ? null : (\n <LookupLink\n lookup={lookup}\n keyValue={keyString(key)}\n row={row(key)}\n chip={false}\n editable={editable}\n />\n );\n}\n"],"mappings":";;;;;;;;;;;;;AAoBA,SAAgB,cACd,KACA,OACA,OAC0F;CAC1F,MAAM,SAAS,SAAS,GAAG;CAC3B,MAAM,OAAO,UAAU,CAAC,KAAK,CAAC;CAC9B,MAAM,QAAQ,sBAAsB;CACpC,MAAM,YAAY,SAAS,YAAY,OAAO,OAAO,QAAQ,IAAI,IAAI,KAAA;CACr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Q,OAAO,oBAAC,QAAD;EAAM,WAAU;EAAgB,UAAA,KAAK,KAAK,IAAI;CAAQ,CAAA;CAC1E,IAAI,IAAI,SAAS,gBACf,OACE,oBAAC,QAAD;EACE,eAAY;EACZ,WAAU;EAET,UAAA,KAAK,KAAK,MACT,oBAAC,YAAD;GAA4B;GAAQ,UAAU;GAAG,KAAK,IAAI,CAAC;GAAG,MAAA;GAAe;EAAW,GAAvE,CAAuE,CACzF;CACG,CAAA;CAGV,MAAM,MAAM,KAAK;CACjB,OAAO,QAAQ,KAAA,IAAY,OACzB,oBAAC,YAAD;EACU;EACR,UAAU,UAAU,GAAG;EACvB,KAAK,IAAI,GAAG;EACZ,MAAM;EACI;CACX,CAAA;AAEL"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type LookupDef, type Row, type SchemaDoc } from "@jsudc/core";
|
|
2
|
+
import { type ReactNode } from "react";
|
|
3
|
+
import { keyString, type UdcLookupResult } from "./useUdcLookup.js";
|
|
4
|
+
/**
|
|
5
|
+
* The labels a renderer resolved for the rows it shows, per lookup field:
|
|
6
|
+
* one `list` per field for the whole page, instead of one per cell.
|
|
7
|
+
*/
|
|
8
|
+
export type LookupBatch = ReadonlyMap<string, BatchEntry>;
|
|
9
|
+
/** One field's labels, and the keys it asked for. */
|
|
10
|
+
export interface BatchEntry extends UdcLookupResult {
|
|
11
|
+
lookup: LookupDef;
|
|
12
|
+
asked: ReadonlySet<string>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* One `useUdcLookup` per lookup field of `schema` (limited to `fields` when
|
|
16
|
+
* given) over the keys `rows` hold. Put the answer in a
|
|
17
|
+
* `<UdcLookupBatch value>` around the cells: each `LookupValue` then reads its
|
|
18
|
+
* labels from it.
|
|
19
|
+
*/
|
|
20
|
+
export declare function useLookupBatch(rows: readonly Row[], schema: Pick<SchemaDoc, "fields">, fields?: readonly string[]): LookupBatch;
|
|
21
|
+
export declare function UdcLookupBatch({ value, children }: {
|
|
22
|
+
value: LookupBatch;
|
|
23
|
+
children?: ReactNode;
|
|
24
|
+
}): import("react").JSX.Element;
|
|
25
|
+
/** The batch around this component, if any. */
|
|
26
|
+
export declare function useLookupBatchContext(): LookupBatch | null;
|
|
27
|
+
/**
|
|
28
|
+
* The batch's result for `field` when it asked for every key of `keys`;
|
|
29
|
+
* `undefined` otherwise (a row added since the batch ran, a cell outside the
|
|
30
|
+
* batch's fields), and the cell then resolves its keys itself.
|
|
31
|
+
*/
|
|
32
|
+
export declare function batchCovers(batch: LookupBatch | null, field: string | undefined, lookup: LookupDef, keys: readonly string[]): UdcLookupResult | undefined;
|
|
33
|
+
export { keyString };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { lookupKeys, useUdcLookups } from "./useUdcLookup.js";
|
|
2
|
+
import { createContext, useContext, useMemo } from "react";
|
|
3
|
+
import { lookupOf } from "@jsudc/core";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
// src/lookup/batch.tsx
|
|
6
|
+
var LookupBatchContext = createContext(null);
|
|
7
|
+
/**
|
|
8
|
+
* One `useUdcLookup` per lookup field of `schema` (limited to `fields` when
|
|
9
|
+
* given) over the keys `rows` hold. Put the answer in a
|
|
10
|
+
* `<UdcLookupBatch value>` around the cells: each `LookupValue` then reads its
|
|
11
|
+
* labels from it.
|
|
12
|
+
*/
|
|
13
|
+
function useLookupBatch(rows, schema, fields) {
|
|
14
|
+
const requests = useMemo(() => {
|
|
15
|
+
const names = fields ?? Object.keys(schema.fields);
|
|
16
|
+
const out = [];
|
|
17
|
+
for (const field of new Set(names)) {
|
|
18
|
+
const lookup = lookupOf(schema.fields[field]);
|
|
19
|
+
if (!lookup || schema.fields[field]?.deleted) continue;
|
|
20
|
+
out.push({
|
|
21
|
+
id: field,
|
|
22
|
+
lookup,
|
|
23
|
+
keys: rows.map((r) => r[field])
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
return out;
|
|
27
|
+
}, [
|
|
28
|
+
rows,
|
|
29
|
+
schema.fields,
|
|
30
|
+
fields
|
|
31
|
+
]);
|
|
32
|
+
const results = useUdcLookups(requests);
|
|
33
|
+
return useMemo(() => {
|
|
34
|
+
const out = /* @__PURE__ */ new Map();
|
|
35
|
+
for (const r of requests) {
|
|
36
|
+
const res = results.get(r.id);
|
|
37
|
+
if (res) out.set(r.id, {
|
|
38
|
+
...res,
|
|
39
|
+
lookup: r.lookup,
|
|
40
|
+
asked: new Set(lookupKeys(r.keys))
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return out;
|
|
44
|
+
}, [requests, results]);
|
|
45
|
+
}
|
|
46
|
+
function UdcLookupBatch({ value, children }) {
|
|
47
|
+
return /* @__PURE__ */ jsx(LookupBatchContext.Provider, {
|
|
48
|
+
value,
|
|
49
|
+
children
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/** The batch around this component, if any. */
|
|
53
|
+
function useLookupBatchContext() {
|
|
54
|
+
return useContext(LookupBatchContext);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The batch's result for `field` when it asked for every key of `keys`;
|
|
58
|
+
* `undefined` otherwise (a row added since the batch ran, a cell outside the
|
|
59
|
+
* batch's fields), and the cell then resolves its keys itself.
|
|
60
|
+
*/
|
|
61
|
+
function batchCovers(batch, field, lookup, keys) {
|
|
62
|
+
if (!batch || !field) return void 0;
|
|
63
|
+
const entry = batch.get(field);
|
|
64
|
+
if (!entry || entry.lookup.collection !== lookup.collection) return void 0;
|
|
65
|
+
return keys.every((k) => entry.asked.has(k)) ? entry : void 0;
|
|
66
|
+
}
|
|
67
|
+
export { UdcLookupBatch, batchCovers, useLookupBatch, useLookupBatchContext };
|
|
68
|
+
|
|
69
|
+
//# sourceMappingURL=batch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"batch.js","names":[],"sources":["../../src/lookup/batch.tsx"],"sourcesContent":["import { type LookupDef, lookupOf, type Row, type SchemaDoc } from \"@jsudc/core\";\nimport { createContext, type ReactNode, useContext, useMemo } from \"react\";\nimport {\n keyString,\n type LookupRequest,\n lookupKeys,\n type UdcLookupResult,\n useUdcLookups,\n} from \"./useUdcLookup.js\";\n\n/**\n * The labels a renderer resolved for the rows it shows, per lookup field:\n * one `list` per field for the whole page, instead of one per cell.\n */\nexport type LookupBatch = ReadonlyMap<string, BatchEntry>;\n\n/** One field's labels, and the keys it asked for. */\nexport interface BatchEntry extends UdcLookupResult {\n lookup: LookupDef;\n asked: ReadonlySet<string>;\n}\n\nconst LookupBatchContext = createContext<LookupBatch | null>(null);\n\n/**\n * One `useUdcLookup` per lookup field of `schema` (limited to `fields` when\n * given) over the keys `rows` hold. Put the answer in a\n * `<UdcLookupBatch value>` around the cells: each `LookupValue` then reads its\n * labels from it.\n */\nexport function useLookupBatch(\n rows: readonly Row[],\n schema: Pick<SchemaDoc, \"fields\">,\n fields?: readonly string[],\n): LookupBatch {\n const requests = useMemo<LookupRequest[]>(() => {\n const names = fields ?? Object.keys(schema.fields);\n const out: LookupRequest[] = [];\n for (const field of new Set(names)) {\n const lookup = lookupOf(schema.fields[field]);\n if (!lookup || schema.fields[field]?.deleted) continue;\n out.push({ id: field, lookup, keys: rows.map((r) => r[field]) });\n }\n return out;\n }, [rows, schema.fields, fields]);\n const results = useUdcLookups(requests);\n return useMemo(() => {\n const out = new Map<string, BatchEntry>();\n for (const r of requests) {\n const res = results.get(r.id);\n if (res) out.set(r.id, { ...res, lookup: r.lookup, asked: new Set(lookupKeys(r.keys)) });\n }\n return out;\n }, [requests, results]);\n}\n\nexport function UdcLookupBatch({ value, children }: { value: LookupBatch; children?: ReactNode }) {\n return <LookupBatchContext.Provider value={value}>{children}</LookupBatchContext.Provider>;\n}\n\n/** The batch around this component, if any. */\nexport function useLookupBatchContext(): LookupBatch | null {\n return useContext(LookupBatchContext);\n}\n\n/**\n * The batch's result for `field` when it asked for every key of `keys`;\n * `undefined` otherwise (a row added since the batch ran, a cell outside the\n * batch's fields), and the cell then resolves its keys itself.\n */\nexport function batchCovers(\n batch: LookupBatch | null,\n field: string | undefined,\n lookup: LookupDef,\n keys: readonly string[],\n): UdcLookupResult | undefined {\n if (!batch || !field) return undefined;\n const entry = batch.get(field);\n if (!entry || entry.lookup.collection !== lookup.collection) return undefined;\n return keys.every((k) => entry.asked.has(k)) ? entry : undefined;\n}\n\nexport { keyString };\n"],"mappings":";;;;;AAsBA,IAAM,qBAAqB,cAAkC,IAAI;;;;;;;AAQjE,SAAgB,eACd,MACA,QACA,QACa;CACb,MAAM,WAAW,cAA+B;EAC9C,MAAM,QAAQ,UAAU,OAAO,KAAK,OAAO,MAAM;EACjD,MAAM,MAAuB,CAAC;EAC9B,KAAK,MAAM,SAAS,IAAI,IAAI,KAAK,GAAG;GAClC,MAAM,SAAS,SAAS,OAAO,OAAO,MAAM;GAC5C,IAAI,CAAC,UAAU,OAAO,OAAO,MAAM,EAAE,SAAS;GAC9C,IAAI,KAAK;IAAE,IAAI;IAAO;IAAQ,MAAM,KAAK,KAAK,MAAM,EAAE,MAAM;GAAE,CAAC;EACjE;EACA,OAAO;CACT,GAAG;EAAC;EAAM,OAAO;EAAQ;CAAM,CAAC;CAChC,MAAM,UAAU,cAAc,QAAQ;CACtC,OAAO,cAAc;EACnB,MAAM,sBAAM,IAAI,IAAwB;EACxC,KAAK,MAAM,KAAK,UAAU;GACxB,MAAM,MAAM,QAAQ,IAAI,EAAE,EAAE;GAC5B,IAAI,KAAK,IAAI,IAAI,EAAE,IAAI;IAAE,GAAG;IAAK,QAAQ,EAAE;IAAQ,OAAO,IAAI,IAAI,WAAW,EAAE,IAAI,CAAC;GAAE,CAAC;EACzF;EACA,OAAO;CACT,GAAG,CAAC,UAAU,OAAO,CAAC;AACxB;AAEA,SAAgB,eAAe,EAAE,OAAO,YAA0D;CAChG,OAAO,oBAAC,mBAAmB,UAApB;EAAoC;EAAQ;CAAsC,CAAA;AAC3F;;AAGA,SAAgB,wBAA4C;CAC1D,OAAO,WAAW,kBAAkB;AACtC;;;;;;AAOA,SAAgB,YACd,OACA,OACA,QACA,MAC6B;CAC7B,IAAI,CAAC,SAAS,CAAC,OAAO,OAAO,KAAA;CAC7B,MAAM,QAAQ,MAAM,IAAI,KAAK;CAC7B,IAAI,CAAC,SAAS,MAAM,OAAO,eAAe,OAAO,YAAY,OAAO,KAAA;CACpE,OAAO,KAAK,OAAO,MAAM,MAAM,MAAM,IAAI,CAAC,CAAC,IAAI,QAAQ,KAAA;AACzD"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { LookupDef, Row } from "@jsudc/core";
|
|
2
|
+
/** What a target row is called: its `label_field`, or `undefined` when it has none. */
|
|
3
|
+
export declare function lookupLabel(row: Row | undefined, lookup: LookupDef): string | undefined;
|
|
4
|
+
/** The extra fields of a target row, `name: value` per line (the link's tooltip). */
|
|
5
|
+
export declare function lookupExtras(row: Row | undefined, lookup: LookupDef): string | undefined;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// src/lookup/label.ts
|
|
2
|
+
/** What a target row is called: its `label_field`, or `undefined` when it has none. */
|
|
3
|
+
function lookupLabel(row, lookup) {
|
|
4
|
+
const v = row?.[lookup.label_field];
|
|
5
|
+
if (v === null || v === void 0 || v === "") return void 0;
|
|
6
|
+
return typeof v === "string" ? v : String(v);
|
|
7
|
+
}
|
|
8
|
+
/** The extra fields of a target row, `name: value` per line (the link's tooltip). */
|
|
9
|
+
function lookupExtras(row, lookup) {
|
|
10
|
+
if (!row || !lookup.extra_fields?.length) return void 0;
|
|
11
|
+
const lines = lookup.extra_fields.map((f) => [f, row[f]]).filter(([, v]) => v !== null && v !== void 0 && v !== "").map(([f, v]) => `${f}: ${typeof v === "string" ? v : JSON.stringify(v)}`);
|
|
12
|
+
return lines.length ? lines.join("\n") : void 0;
|
|
13
|
+
}
|
|
14
|
+
export { lookupExtras, lookupLabel };
|
|
15
|
+
|
|
16
|
+
//# sourceMappingURL=label.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"label.js","names":[],"sources":["../../src/lookup/label.ts"],"sourcesContent":["import type { LookupDef, Row } from \"@jsudc/core\";\n\n/** What a target row is called: its `label_field`, or `undefined` when it has none. */\nexport function lookupLabel(row: Row | undefined, lookup: LookupDef): string | undefined {\n const v = row?.[lookup.label_field];\n if (v === null || v === undefined || v === \"\") return undefined;\n return typeof v === \"string\" ? v : String(v);\n}\n\n/** The extra fields of a target row, `name: value` per line (the link's tooltip). */\nexport function lookupExtras(row: Row | undefined, lookup: LookupDef): string | undefined {\n if (!row || !lookup.extra_fields?.length) return undefined;\n const lines = lookup.extra_fields\n .map((f) => [f, row[f]] as const)\n .filter(([, v]) => v !== null && v !== undefined && v !== \"\")\n .map(([f, v]) => `${f}: ${typeof v === \"string\" ? v : JSON.stringify(v)}`);\n return lines.length ? lines.join(\"\\n\") : undefined;\n}\n"],"mappings":";;AAGA,SAAgB,YAAY,KAAsB,QAAuC;CACvF,MAAM,IAAI,MAAM,OAAO;CACvB,IAAI,MAAM,QAAQ,MAAM,KAAA,KAAa,MAAM,IAAI,OAAO,KAAA;CACtD,OAAO,OAAO,MAAM,WAAW,IAAI,OAAO,CAAC;AAC7C;;AAGA,SAAgB,aAAa,KAAsB,QAAuC;CACxF,IAAI,CAAC,OAAO,CAAC,OAAO,cAAc,QAAQ,OAAO,KAAA;CACjD,MAAM,QAAQ,OAAO,aAClB,KAAK,MAAM,CAAC,GAAG,IAAI,EAAE,CAAU,CAAC,CAChC,QAAQ,GAAG,OAAO,MAAM,QAAQ,MAAM,KAAA,KAAa,MAAM,EAAE,CAAC,CAC5D,KAAK,CAAC,GAAG,OAAO,GAAG,EAAE,IAAI,OAAO,MAAM,WAAW,IAAI,KAAK,UAAU,CAAC,GAAG;CAC3E,OAAO,MAAM,SAAS,MAAM,KAAK,IAAI,IAAI,KAAA;AAC3C"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { FilterNode } from "@jsudc/core";
|
|
2
|
+
import { type MouseEvent, type ReactNode } from "react";
|
|
3
|
+
/**
|
|
4
|
+
* How the library links from one collection to another (a lookup cell, a
|
|
5
|
+
* `row_link`): the address of a collection's view, and how to go there
|
|
6
|
+
* without a reload. The library knows no routes of its own, so the host says
|
|
7
|
+
* where `<UdcDataView collection>` is mounted.
|
|
8
|
+
*/
|
|
9
|
+
export interface UdcLinks {
|
|
10
|
+
/** The address of `collection`'s view with `search` as its query string. */
|
|
11
|
+
href(collection: string, search: URLSearchParams): string;
|
|
12
|
+
/** Go to `href` inside the app. */
|
|
13
|
+
navigate(href: string): void;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* `/c/<collection>?…`, followed with the History API: `pushState`, then a
|
|
17
|
+
* `popstate` event, which a router on the History API (react-router's
|
|
18
|
+
* `BrowserRouter` among them) reads as a navigation.
|
|
19
|
+
*/
|
|
20
|
+
export declare const defaultUdcLinks: UdcLinks;
|
|
21
|
+
/** Links to other collections through `links`; without it, `defaultUdcLinks`. */
|
|
22
|
+
export declare function UdcLinksProvider({ links, children }: {
|
|
23
|
+
links: UdcLinks;
|
|
24
|
+
children?: ReactNode;
|
|
25
|
+
}): import("react").JSX.Element;
|
|
26
|
+
export declare function useUdcLinks(): UdcLinks;
|
|
27
|
+
/** The address of `collection` opened with `filter` as its filter tree (`?f=`). */
|
|
28
|
+
export declare function filterHref(links: UdcLinks, collection: string, filter: FilterNode): string;
|
|
29
|
+
/** A plain click navigates in the app; Cmd / Ctrl / Shift-click (and the middle button) open a new tab. */
|
|
30
|
+
export declare function followLink(e: Pick<MouseEvent, "metaKey" | "ctrlKey" | "shiftKey" | "button" | "preventDefault">, href: string, links: UdcLinks): void;
|