@jsudc/react 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/README.md +59 -3
  2. package/dist/calendar/EventDetail.js.map +1 -1
  3. package/dist/data-view/FilterBuilder.d.ts +4 -2
  4. package/dist/data-view/FilterBuilder.js +11 -2
  5. package/dist/data-view/FilterBuilder.js.map +1 -1
  6. package/dist/data-view/FilterChips.d.ts +10 -2
  7. package/dist/data-view/FilterChips.js +14 -2
  8. package/dist/data-view/FilterChips.js.map +1 -1
  9. package/dist/data-view/UdcDataView.js +4 -2
  10. package/dist/data-view/UdcDataView.js.map +1 -1
  11. package/dist/data-view/liveView.js +2 -2
  12. package/dist/data-view/liveView.js.map +1 -1
  13. package/dist/headless.d.ts +3 -0
  14. package/dist/headless.js +4 -1
  15. package/dist/i18n/translations.js +16 -0
  16. package/dist/i18n/translations.js.map +1 -1
  17. package/dist/index.d.ts +8 -0
  18. package/dist/index.js +10 -2
  19. package/dist/kanban/KanbanCard.d.ts +1 -1
  20. package/dist/kanban/KanbanCard.js +5 -1
  21. package/dist/kanban/KanbanCard.js.map +1 -1
  22. package/dist/kanban/KanbanRenderer.js +72 -67
  23. package/dist/kanban/KanbanRenderer.js.map +1 -1
  24. package/dist/kanban/model.d.ts +4 -1
  25. package/dist/kanban/model.js +10 -2
  26. package/dist/kanban/model.js.map +1 -1
  27. package/dist/lookup/LookupCombobox.d.ts +18 -0
  28. package/dist/lookup/LookupCombobox.js +146 -0
  29. package/dist/lookup/LookupCombobox.js.map +1 -0
  30. package/dist/lookup/LookupSelectChip.d.ts +17 -0
  31. package/dist/lookup/LookupSelectChip.js +96 -0
  32. package/dist/lookup/LookupSelectChip.js.map +1 -0
  33. package/dist/lookup/LookupValue.d.ts +27 -0
  34. package/dist/lookup/LookupValue.js +98 -0
  35. package/dist/lookup/LookupValue.js.map +1 -0
  36. package/dist/lookup/batch.d.ts +33 -0
  37. package/dist/lookup/batch.js +69 -0
  38. package/dist/lookup/batch.js.map +1 -0
  39. package/dist/lookup/label.d.ts +5 -0
  40. package/dist/lookup/label.js +16 -0
  41. package/dist/lookup/label.js.map +1 -0
  42. package/dist/lookup/links.d.ts +30 -0
  43. package/dist/lookup/links.js +49 -0
  44. package/dist/lookup/links.js.map +1 -0
  45. package/dist/lookup/options.d.ts +28 -0
  46. package/dist/lookup/options.js +66 -0
  47. package/dist/lookup/options.js.map +1 -0
  48. package/dist/lookup/useUdcLookup.d.ts +52 -0
  49. package/dist/lookup/useUdcLookup.js +96 -0
  50. package/dist/lookup/useUdcLookup.js.map +1 -0
  51. package/dist/react-router/UdcReactRouterUrlState.d.ts +6 -1
  52. package/dist/react-router/UdcReactRouterUrlState.js +16 -3
  53. package/dist/react-router/UdcReactRouterUrlState.js.map +1 -1
  54. package/dist/styles.css +1 -1
  55. package/dist/table/CellEditor.d.ts +8 -4
  56. package/dist/table/CellEditor.js +15 -2
  57. package/dist/table/CellEditor.js.map +1 -1
  58. package/dist/table/TableRenderer.js +122 -97
  59. package/dist/table/TableRenderer.js.map +1 -1
  60. package/dist/table/cells.d.ts +4 -2
  61. package/dist/table/cells.js +9 -1
  62. package/dist/table/cells.js.map +1 -1
  63. package/dist/udc/hooks.d.ts +53 -2
  64. package/dist/udc/hooks.js +9 -1
  65. package/dist/udc/hooks.js.map +1 -1
  66. package/dist/udc/keys.d.ts +5 -0
  67. package/dist/udc/keys.js +26 -0
  68. package/dist/udc/keys.js.map +1 -1
  69. package/dist/udc/provider.d.ts +8 -1
  70. package/dist/udc/provider.js +6 -1
  71. package/dist/udc/provider.js.map +1 -1
  72. package/package.json +3 -3
@@ -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,MAAM,UACJ,SAAS,WAAW,IAChB,OACA,SAAS,WAAW,IAClB,KAAK,SAAS,EAAY,IAC1B,GAAG,KAAK,SAAS,EAAY,EAAE,IAAI,SAAS,SAAS;CAE7D,MAAM,UAAU,MACd,SAAS,SAAS,SAAS,CAAC,IAAI,SAAS,QAAQ,MAAM,MAAM,CAAC,IAAI,CAAC,GAAG,UAAU,CAAC,CAAC;CAEpF,OACE,qBAAC,SAAD;EACQ;EACN,eAAe,SAAS;GACtB,QAAQ,IAAI;GACZ,IAAI,CAAC,MAAM,UAAU,EAAE;EACzB;EALF,UAAA,CAOE,oBAAC,aAAD;GAAa,OAAO,IAAI;GAAc;GAAgB;GAAkB;EAAU,CAAA,GAClF,qBAAC,gBAAD;GAAgB,WAAU;GAAmB,eAAa,cAAc,IAAI;GAA5E,UAAA;IACG,aACC,oBAAC,OAAD;KACE,WAAA;KACA,OAAO;KACP,WAAW,MAAM,UAAU,EAAE,OAAO,KAAK;KACzC,aAAa,EAAE,OAAO,eAAe;KACrC,eAAa,eAAe,IAAI;KAChC,WAAU;IACX,CAAA,IACC;IACJ,qBAAC,OAAD;KAAK,WAAU;KAAf,UAAA;MACG,MAAM,YACL,oBAAC,OAAD;OAAK,WAAU;OACb,UAAA,oBAAC,SAAD,CAAU,CAAA;MACP,CAAA,IAEL,MAAM,QAAQ,KAAK,MAEjB,qBAAC,SAAD;OAEE,eAAa,eAAe,EAAE;OAC9B,cAAY,EAAE;OACd,WAAU;OAJZ,UAAA,CAME,oBAAC,UAAD;QACE,SAAS,SAAS,SAAS,EAAE,GAAG;QAChC,uBAAuB,OAAO,EAAE,GAAG;OACpC,CAAA,GACD,oBAAC,QAAD;QAAM,WAAU;QAAgB,UAAA,EAAE;OAAY,CAAA,CACzC;MAVA,GAAA,EAAE,GAUF,CACR;MAEF,CAAC,MAAM,aAAa,MAAM,QAAQ,WAAW,IAC5C,oBAAC,OAAD;OAAK,WAAU;OACZ,UAAA,EAAE,OAAO,iBAAiB;MACxB,CAAA,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;CACrE,MAAM,MAAM,aAAa,UAAU;EAAE,YAAY;EAAI,aAAa;CAAG,GAAG,MAAM,EAC5E,SAAS,WAAW,KAAA,KAAa,cAAc,KAAA,EACjD,CAAC;CACD,MAAM,MAAM,aAAa;CACzB,OAAO;EAAE;EAAQ;EAAM,MAAM,QAAQ,IAAI,IAAI,GAAG;CAAE;AACpD;;AAGA,SAAgB,gBACd,KACA,OACA,OACU;CACV,MAAM,EAAE,QAAQ,MAAM,QAAQ,cAAc,KAAK,OAAO,KAAK;CAC7D,IAAI,CAAC,QAAQ,OAAO;CACpB,OAAO,KAAK,KAAK,MAAM,YAAY,IAAI,CAAC,GAAG,MAAM,KAAK,CAAC;AACzD;AAEA,SAAS,WAAW,EAClB,QACA,UACA,KACA,MACA,YAOC;CACD,MAAM,QAAQ,YAAY;CAC1B,MAAM,QAAQ,YAAY,KAAK,MAAM;CACrC,MAAM,QAAQ,eAAe,MAAM;CACnC,MAAM,OAAO,WAAW,OAAO,OAAO,YAAY;EAChD,OAAO,UAAU,SAAS,SAAS;EACnC,IAAI;EACJ,OAAO;CACT,CAAC;CACD,MAAM,WAAW,MAAkB;EAGjC,IAAI,YAAY,EAAE,EAAE,WAAW,EAAE,WAAW,EAAE,WAAW,IAAI;GAC3D,EAAE,eAAe;GACjB;EACF;EAEA,EAAE,gBAAgB;EAClB,WAAW,GAAG,MAAM,KAAK;CAC3B;CACA,OACE,oBAAC,KAAD;EACQ;EACG;EACT,eAAY;EACZ,YAAU;EACV,iBAAe,UAAU,KAAA,KAAa,KAAA;EACtC,OAAO,aAAa,KAAK,MAAM,KAAK,SAAS;EAC7C,WAAW,GACT,6KACA,QACE,+KACF,UAAU,KAAA,KAAa,yCACzB;EAEC,UAAA,SAAS;CACT,CAAA;AAEP;;;;;;;AAQA,SAAgB,YAAY,EAC1B,KACA,OACA,OACA,WAAW,SAQV;CACD,MAAM,EAAE,QAAQ,MAAM,QAAQ,cAAc,KAAK,OAAO,KAAK;CAC7D,IAAI,CAAC,QAAQ,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;
@@ -0,0 +1,49 @@
1
+ import { PARAM, encodeFilterParam } from "../data-view/urlState.js";
2
+ import { createContext, useContext } from "react";
3
+ import { jsx } from "react/jsx-runtime";
4
+ // src/lookup/links.tsx
5
+ /**
6
+ * `/c/<collection>?…`, followed with the History API: `pushState`, then a
7
+ * `popstate` event, which a router on the History API (react-router's
8
+ * `BrowserRouter` among them) reads as a navigation.
9
+ */
10
+ var defaultUdcLinks = {
11
+ href: (collection, search) => {
12
+ const qs = search.toString();
13
+ return `/c/${encodeURIComponent(collection)}${qs ? `?${qs}` : ""}`;
14
+ },
15
+ navigate: (href) => {
16
+ window.history.pushState(null, "", href);
17
+ window.dispatchEvent(new PopStateEvent("popstate"));
18
+ }
19
+ };
20
+ var UdcLinksContext = createContext(defaultUdcLinks);
21
+ /** Links to other collections through `links`; without it, `defaultUdcLinks`. */
22
+ function UdcLinksProvider({ links, children }) {
23
+ return /* @__PURE__ */ jsx(UdcLinksContext.Provider, {
24
+ value: links,
25
+ children
26
+ });
27
+ }
28
+ function useUdcLinks() {
29
+ return useContext(UdcLinksContext);
30
+ }
31
+ /** The address of `collection` opened with `filter` as its filter tree (`?f=`). */
32
+ function filterHref(links, collection, filter) {
33
+ const search = new URLSearchParams();
34
+ const f = encodeFilterParam(filter);
35
+ if (f) search.set(PARAM.filter, f);
36
+ return links.href(collection, search);
37
+ }
38
+ /** A plain click navigates in the app; Cmd / Ctrl / Shift-click (and the middle button) open a new tab. */
39
+ function followLink(e, href, links) {
40
+ e.preventDefault();
41
+ if (e.metaKey || e.ctrlKey || e.shiftKey || e.button === 1) {
42
+ window.open(href, "_blank", "noopener");
43
+ return;
44
+ }
45
+ links.navigate(href);
46
+ }
47
+ export { UdcLinksProvider, defaultUdcLinks, filterHref, followLink, useUdcLinks };
48
+
49
+ //# sourceMappingURL=links.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"links.js","names":[],"sources":["../../src/lookup/links.tsx"],"sourcesContent":["import type { FilterNode } from \"@jsudc/core\";\nimport { createContext, type MouseEvent, type ReactNode, useContext } from \"react\";\nimport { encodeFilterParam, PARAM } from \"../data-view/urlState.js\";\n\n/**\n * How the library links from one collection to another (a lookup cell, a\n * `row_link`): the address of a collection's view, and how to go there\n * without a reload. The library knows no routes of its own, so the host says\n * where `<UdcDataView collection>` is mounted.\n */\nexport interface UdcLinks {\n /** The address of `collection`'s view with `search` as its query string. */\n href(collection: string, search: URLSearchParams): string;\n /** Go to `href` inside the app. */\n navigate(href: string): void;\n}\n\n/**\n * `/c/<collection>?…`, followed with the History API: `pushState`, then a\n * `popstate` event, which a router on the History API (react-router's\n * `BrowserRouter` among them) reads as a navigation.\n */\nexport const defaultUdcLinks: UdcLinks = {\n href: (collection, search) => {\n const qs = search.toString();\n return `/c/${encodeURIComponent(collection)}${qs ? `?${qs}` : \"\"}`;\n },\n navigate: (href) => {\n window.history.pushState(null, \"\", href);\n window.dispatchEvent(new PopStateEvent(\"popstate\"));\n },\n};\n\nconst UdcLinksContext = createContext<UdcLinks>(defaultUdcLinks);\n\n/** Links to other collections through `links`; without it, `defaultUdcLinks`. */\nexport function UdcLinksProvider({ links, children }: { links: UdcLinks; children?: ReactNode }) {\n return <UdcLinksContext.Provider value={links}>{children}</UdcLinksContext.Provider>;\n}\n\nexport function useUdcLinks(): UdcLinks {\n return useContext(UdcLinksContext);\n}\n\n/** The address of `collection` opened with `filter` as its filter tree (`?f=`). */\nexport function filterHref(links: UdcLinks, collection: string, filter: FilterNode): string {\n const search = new URLSearchParams();\n const f = encodeFilterParam(filter);\n if (f) search.set(PARAM.filter, f);\n return links.href(collection, search);\n}\n\n/** A plain click navigates in the app; Cmd / Ctrl / Shift-click (and the middle button) open a new tab. */\nexport function followLink(\n e: Pick<MouseEvent, \"metaKey\" | \"ctrlKey\" | \"shiftKey\" | \"button\" | \"preventDefault\">,\n href: string,\n links: UdcLinks,\n): void {\n e.preventDefault();\n if (e.metaKey || e.ctrlKey || e.shiftKey || e.button === 1) {\n window.open(href, \"_blank\", \"noopener\");\n return;\n }\n links.navigate(href);\n}\n"],"mappings":";;;;;;;;;AAsBA,IAAa,kBAA4B;CACvC,OAAO,YAAY,WAAW;EAC5B,MAAM,KAAK,OAAO,SAAS;EAC3B,OAAO,MAAM,mBAAmB,UAAU,IAAI,KAAK,IAAI,OAAO;CAChE;CACA,WAAW,SAAS;EAClB,OAAO,QAAQ,UAAU,MAAM,IAAI,IAAI;EACvC,OAAO,cAAc,IAAI,cAAc,UAAU,CAAC;CACpD;AACF;AAEA,IAAM,kBAAkB,cAAwB,eAAe;;AAG/D,SAAgB,iBAAiB,EAAE,OAAO,YAAuD;CAC/F,OAAO,oBAAC,gBAAgB,UAAjB;EAA0B,OAAO;EAAQ;CAAmC,CAAA;AACrF;AAEA,SAAgB,cAAwB;CACtC,OAAO,WAAW,eAAe;AACnC;;AAGA,SAAgB,WAAW,OAAiB,YAAoB,QAA4B;CAC1F,MAAM,SAAS,IAAI,gBAAgB;CACnC,MAAM,IAAI,kBAAkB,MAAM;CAClC,IAAI,GAAG,OAAO,IAAI,MAAM,QAAQ,CAAC;CACjC,OAAO,MAAM,KAAK,YAAY,MAAM;AACtC;;AAGA,SAAgB,WACd,GACA,MACA,OACM;CACN,EAAE,eAAe;CACjB,IAAI,EAAE,WAAW,EAAE,WAAW,EAAE,YAAY,EAAE,WAAW,GAAG;EAC1D,OAAO,KAAK,MAAM,UAAU,UAAU;EACtC;CACF;CACA,MAAM,SAAS,IAAI;AACrB"}
@@ -0,0 +1,28 @@
1
+ import { type FilterNode, type LookupDef, type Row } from "@jsudc/core";
2
+ /** How many target rows a picker lists; above this it searches by `label_field` instead. */
3
+ export declare const LOOKUP_OPTIONS_LIMIT = 50;
4
+ export interface LookupOption {
5
+ key: string;
6
+ /** The stored key as the target row has it (a number stays a number). */
7
+ raw: unknown;
8
+ label: string;
9
+ row: Row;
10
+ }
11
+ export interface LookupOptionsResult {
12
+ options: LookupOption[];
13
+ /** The target's row count for this search; more than `LOOKUP_OPTIONS_LIMIT` means "type to narrow". */
14
+ total: number | null;
15
+ isLoading: boolean;
16
+ error: Error | null;
17
+ }
18
+ /** The search condition of a picker: `label_field contains <search>`, case-insensitive. */
19
+ export declare function optionsFilter(lookup: LookupDef, search: string): FilterNode | undefined;
20
+ /**
21
+ * The first `LOOKUP_OPTIONS_LIMIT` rows of a lookup's target, by
22
+ * `label_field`, narrowed by `search` (`contains` on `label_field`). A
23
+ * picker shows its search box once `total` (of the empty search) is larger
24
+ * than the limit.
25
+ */
26
+ export declare function useLookupOptions(lookup: LookupDef | undefined, search: string, opts?: {
27
+ enabled?: boolean;
28
+ }): LookupOptionsResult;
@@ -0,0 +1,66 @@
1
+ import { useUdcClient } from "../udc/client.js";
2
+ import { udcKeys } from "../udc/keys.js";
3
+ import { lookupLabel } from "./label.js";
4
+ import { keyString } from "./useUdcLookup.js";
5
+ import { lookupKeyOf } from "@jsudc/core";
6
+ import { keepPreviousData, useQuery } from "@tanstack/react-query";
7
+ // src/lookup/options.ts
8
+ /** How many target rows a picker lists; above this it searches by `label_field` instead. */
9
+ var LOOKUP_OPTIONS_LIMIT = 50;
10
+ /** The search condition of a picker: `label_field contains <search>`, case-insensitive. */
11
+ function optionsFilter(lookup, search) {
12
+ const s = search.trim();
13
+ return s ? {
14
+ field: lookup.label_field,
15
+ op: "contains",
16
+ value: s,
17
+ case_insensitive: true
18
+ } : void 0;
19
+ }
20
+ /**
21
+ * The first `LOOKUP_OPTIONS_LIMIT` rows of a lookup's target, by
22
+ * `label_field`, narrowed by `search` (`contains` on `label_field`). A
23
+ * picker shows its search box once `total` (of the empty search) is larger
24
+ * than the limit.
25
+ */
26
+ function useLookupOptions(lookup, search, opts = {}) {
27
+ const client = useUdcClient();
28
+ const target = lookup ?? {
29
+ collection: "",
30
+ label_field: ""
31
+ };
32
+ const filter = optionsFilter(target, search);
33
+ const q = useQuery({
34
+ queryKey: udcKeys.lookupOptions(target.collection, target.label_field, search.trim()),
35
+ queryFn: ({ signal }) => client.list({
36
+ collection: target.collection,
37
+ ...filter ? { filter } : {},
38
+ sort: [{
39
+ field: target.label_field,
40
+ dir: "asc"
41
+ }],
42
+ page: { limit: 50 }
43
+ }, { signal }),
44
+ enabled: (opts.enabled ?? true) && lookup !== void 0,
45
+ staleTime: 6e4,
46
+ placeholderData: keepPreviousData
47
+ });
48
+ return {
49
+ options: (q.data?.rows ?? []).map((row) => {
50
+ const raw = lookupKeyOf(row, target);
51
+ const key = keyString(raw);
52
+ return {
53
+ key,
54
+ raw,
55
+ label: lookupLabel(row, target) ?? key,
56
+ row
57
+ };
58
+ }),
59
+ total: q.data?.total ?? null,
60
+ isLoading: q.isLoading,
61
+ error: q.error
62
+ };
63
+ }
64
+ export { LOOKUP_OPTIONS_LIMIT, optionsFilter, useLookupOptions };
65
+
66
+ //# sourceMappingURL=options.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"options.js","names":[],"sources":["../../src/lookup/options.ts"],"sourcesContent":["import { type FilterNode, type LookupDef, lookupKeyOf, type Row } from \"@jsudc/core\";\nimport { keepPreviousData, useQuery } from \"@tanstack/react-query\";\nimport { useUdcClient } from \"../udc/client.js\";\nimport { udcKeys } from \"../udc/keys.js\";\nimport { lookupLabel } from \"./label.js\";\nimport { keyString } from \"./useUdcLookup.js\";\n\n/** How many target rows a picker lists; above this it searches by `label_field` instead. */\nexport const LOOKUP_OPTIONS_LIMIT = 50;\n\nexport interface LookupOption {\n key: string;\n /** The stored key as the target row has it (a number stays a number). */\n raw: unknown;\n label: string;\n row: Row;\n}\n\nexport interface LookupOptionsResult {\n options: LookupOption[];\n /** The target's row count for this search; more than `LOOKUP_OPTIONS_LIMIT` means \"type to narrow\". */\n total: number | null;\n isLoading: boolean;\n error: Error | null;\n}\n\n/** The search condition of a picker: `label_field contains <search>`, case-insensitive. */\nexport function optionsFilter(lookup: LookupDef, search: string): FilterNode | undefined {\n const s = search.trim();\n return s\n ? { field: lookup.label_field, op: \"contains\", value: s, case_insensitive: true }\n : undefined;\n}\n\n/**\n * The first `LOOKUP_OPTIONS_LIMIT` rows of a lookup's target, by\n * `label_field`, narrowed by `search` (`contains` on `label_field`). A\n * picker shows its search box once `total` (of the empty search) is larger\n * than the limit.\n */\nexport function useLookupOptions(\n lookup: LookupDef | undefined,\n search: string,\n opts: { enabled?: boolean } = {},\n): LookupOptionsResult {\n const client = useUdcClient();\n const target = lookup ?? { collection: \"\", label_field: \"\" };\n const filter = optionsFilter(target, search);\n const q = useQuery({\n queryKey: udcKeys.lookupOptions(target.collection, target.label_field, search.trim()),\n queryFn: ({ signal }) =>\n client.list(\n {\n collection: target.collection,\n ...(filter ? { filter } : {}),\n sort: [{ field: target.label_field, dir: \"asc\" }],\n page: { limit: LOOKUP_OPTIONS_LIMIT },\n },\n { signal },\n ),\n enabled: (opts.enabled ?? true) && lookup !== undefined,\n staleTime: 60_000,\n placeholderData: keepPreviousData,\n });\n const options = (q.data?.rows ?? []).map((row) => {\n const raw = lookupKeyOf(row, target);\n const key = keyString(raw);\n return { key, raw, label: lookupLabel(row, target) ?? key, row };\n });\n return { options, total: q.data?.total ?? null, isLoading: q.isLoading, error: q.error };\n}\n"],"mappings":";;;;;;;;AAQA,IAAa,uBAAuB;;AAmBpC,SAAgB,cAAc,QAAmB,QAAwC;CACvF,MAAM,IAAI,OAAO,KAAK;CACtB,OAAO,IACH;EAAE,OAAO,OAAO;EAAa,IAAI;EAAY,OAAO;EAAG,kBAAkB;CAAK,IAC9E,KAAA;AACN;;;;;;;AAQA,SAAgB,iBACd,QACA,QACA,OAA8B,CAAC,GACV;CACrB,MAAM,SAAS,aAAa;CAC5B,MAAM,SAAS,UAAU;EAAE,YAAY;EAAI,aAAa;CAAG;CAC3D,MAAM,SAAS,cAAc,QAAQ,MAAM;CAC3C,MAAM,IAAI,SAAS;EACjB,UAAU,QAAQ,cAAc,OAAO,YAAY,OAAO,aAAa,OAAO,KAAK,CAAC;EACpF,UAAU,EAAE,aACV,OAAO,KACL;GACE,YAAY,OAAO;GACnB,GAAI,SAAS,EAAE,OAAO,IAAI,CAAC;GAC3B,MAAM,CAAC;IAAE,OAAO,OAAO;IAAa,KAAK;GAAM,CAAC;GAChD,MAAM,EAAE,OAAA,GAA4B;EACtC,GACA,EAAE,OAAO,CACX;EACF,UAAU,KAAK,WAAW,SAAS,WAAW,KAAA;EAC9C,WAAW;EACX,iBAAiB;CACnB,CAAC;CAMD,OAAO;EAAE,UALQ,EAAE,MAAM,QAAQ,CAAC,EAAA,CAAG,KAAK,QAAQ;GAChD,MAAM,MAAM,YAAY,KAAK,MAAM;GACnC,MAAM,MAAM,UAAU,GAAG;GACzB,OAAO;IAAE;IAAK;IAAK,OAAO,YAAY,KAAK,MAAM,KAAK;IAAK;GAAI;EACjE,CACS;EAAS,OAAO,EAAE,MAAM,SAAS;EAAM,WAAW,EAAE;EAAW,OAAO,EAAE;CAAM;AACzF"}