@jsudc/react 0.8.0 → 0.8.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.
Files changed (41) hide show
  1. package/README.md +99 -22
  2. package/dist/actions/RowActionsMenu.d.ts +14 -5
  3. package/dist/actions/RowActionsMenu.js +66 -17
  4. package/dist/actions/RowActionsMenu.js.map +1 -1
  5. package/dist/actions/row.d.ts +50 -5
  6. package/dist/actions/row.js +41 -3
  7. package/dist/actions/row.js.map +1 -1
  8. package/dist/actions/rowIcon.d.ts +4 -0
  9. package/dist/actions/rowIcon.js +13 -0
  10. package/dist/actions/rowIcon.js.map +1 -0
  11. package/dist/actions/useCanHover.d.ts +11 -0
  12. package/dist/actions/useCanHover.js +37 -0
  13. package/dist/actions/useCanHover.js.map +1 -0
  14. package/dist/actions/useRowActionRunner.d.ts +3 -1
  15. package/dist/actions/useRowActionRunner.js +9 -3
  16. package/dist/actions/useRowActionRunner.js.map +1 -1
  17. package/dist/data-view/BulkBar.js +1 -1
  18. package/dist/data-view/FilePreview.js +1 -1
  19. package/dist/data-view/UdcDataView.js +2 -1
  20. package/dist/data-view/UdcDataView.js.map +1 -1
  21. package/dist/headless.d.ts +1 -0
  22. package/dist/headless.js +4 -3
  23. package/dist/index.d.ts +2 -1
  24. package/dist/index.js +5 -4
  25. package/dist/kanban/KanbanRenderer.js +1 -1
  26. package/dist/lookup/LookupValue.d.ts +4 -0
  27. package/dist/lookup/LookupValue.js +40 -9
  28. package/dist/lookup/LookupValue.js.map +1 -1
  29. package/dist/lookup/links.d.ts +12 -4
  30. package/dist/lookup/links.js +4 -1
  31. package/dist/lookup/links.js.map +1 -1
  32. package/dist/react-router/UdcReactRouterUrlState.d.ts +3 -2
  33. package/dist/react-router/UdcReactRouterUrlState.js +3 -1
  34. package/dist/react-router/UdcReactRouterUrlState.js.map +1 -1
  35. package/dist/styles.css +1 -1
  36. package/dist/styles.scoped.css +1 -1
  37. package/dist/table/TableRenderer.js +37 -10
  38. package/dist/table/TableRenderer.js.map +1 -1
  39. package/dist/tree/TreeRenderer.js +1 -1
  40. package/dist/udc/hooks.d.ts +4 -1
  41. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -1,17 +1,18 @@
1
1
  import { MATCH_ALL, UdcActionsProvider, bulkActionEligible, planBulkAction, selectionTarget, useBulkActionHandlers } from "./actions/bulk.js";
2
2
  import { humanize, storage } from "./lib/storage.js";
3
3
  import { RowActionsControls, rowActionsColumnWidth, rowMenuHandlers, useRowMenu } from "./actions/RowActionsMenu.js";
4
- import { eligibleRowActions, isRowMenuKey, rowActionDefs, rowActionsDisplay } from "./actions/row.js";
4
+ import { DEFAULT_ROW_ACTIONS_MAX, eligibleRowActions, isRowMenuKey, rowActionDefs, rowActionPlacement, rowActionsDisplay, rowActionsMax, splitRowActions } from "./actions/row.js";
5
+ import { effectiveRowActionsDisplay, useCanHover } from "./actions/useCanHover.js";
5
6
  import { UdcCellsProvider, cellComponent, createCellRegistry, mergeCellRegistries, useCellRegistry } from "./cells/registry.js";
6
7
  import { LiveIndicator } from "./data-view/LiveIndicator.js";
7
8
  import { columnSortable, sourceSortable } from "./data-view/liveView.js";
8
- import { createUdcQueryClient, shouldRetry, useUdcClient } from "./udc/client.js";
9
9
  import { UdcDataView, resolveChrome } from "./data-view/UdcDataView.js";
10
10
  import { INTL_LOCALE, LOCALE_STORAGE_KEY, UdcI18nProvider, intlLocaleOf, isLocale, lookup, makeT, mergeMessages, messageLocales, schemaLabelTerms, tableSection, useI18n } from "./i18n/i18n.js";
11
- import { applyToListRows } from "./udc/listCache.js";
12
11
  import { LOCALES, TRANSLATIONS } from "./i18n/translations.js";
12
+ import { createUdcQueryClient, shouldRetry, useUdcClient } from "./udc/client.js";
13
13
  import { LUCIDE_ICONS } from "./icons/lucide.generated.js";
14
14
  import { FALLBACK_ICON, resolveIcon } from "./icons/resolve.js";
15
+ import { applyToListRows } from "./udc/listCache.js";
15
16
  import { UDC_SCOPE_CLASS } from "./lib/scope.js";
16
17
  import { THEME_STORAGE_KEY, ThemeProvider, useTheme } from "./lib/theme.js";
17
18
  import { cn } from "./lib/utils.js";
@@ -42,4 +43,4 @@ import { UdcUiProvider, useUiKit } from "./ui-kit/context.js";
42
43
  import { createMemoryUrlStateAdapter, prefixUrlStateAdapter } from "./url-state/adapters.js";
43
44
  import { UdcUrlStateProvider, useUrlSearchParams, useUrlStateAdapter } from "./url-state/UrlState.js";
44
45
  import { windowLocationAdapter } from "./url-state/adapter.js";
45
- export { ALL_ROWS_MAX, BROWSER_TZ, COMMON_ZONES, DEFAULT_VIRTUAL_OVERSCAN, DEFAULT_VIRTUAL_THRESHOLD, EMPTY_SELECTION, EmptyState, ErrorState, FALLBACK_ICON, INTL_LOCALE, LOCALES, LOCALE_STORAGE_KEY, LOOKUP_OPTIONS_LIMIT, LUCIDE_ICONS, LiveIndicator, LookupCombobox, LookupSelectChip, LookupValue, MATCH_ALL, NoRendererState, RowActionsControls, THEME_STORAGE_KEY, TRANSLATIONS, TZ_STORAGE_KEY, ThemeProvider, TimezoneProvider, UDC_SCOPE_CLASS, UdcActionsProvider, UdcCellsProvider, UdcChangeFeed, UdcChangeFeedContext, UdcDataView, UdcI18nProvider, UdcLinksProvider, UdcLookupBatch, UdcProvider, UdcRenderersProvider, UdcUiProvider, UdcUrlStateProvider, allZones, applyToListRows, asLookup, batchCovers, browserZone, bulkActionEligible, callAction, cellComponent, cn, columnSortable, createCellRegistry, createMemoryUrlStateAdapter, createRendererRegistry, createUdcQueryClient, defaultUdcLinks, eligibleRowActions, fetchAllRows, fetchLookupRows, filterHref, followLink, freshnessOf, humanize, i18nKey, intlLocaleOf, isFolderRow, isLocale, isRowMenuKey, isValidZone, keyString, lookup, lookupColor, lookupExtras, lookupKeys, lookupLabel, lookupQuery, makeT, mergeCellRegistries, mergeMessages, messageLocales, mutateManyTouches, offsetLabel, offsetMinutes, optionsFilter, ownsKeys, pageOffset, pageSelectionState, patchRowInList, planBulkAction, pollInterval, prefixUrlStateAdapter, rendererEntry, rendererTabs, resolveChrome, resolveIcon, resolveTab, resolveVirtualization, rowActionDefs, rowActionsColumnWidth, rowActionsDisplay, rowActivationEnabled, rowMenuHandlers, schemaLabelTerms, selectAllMatching, selectRange, selectionCount, selectionHas, selectionIsEmpty, selectionTarget, setPageSelection, shouldRetry, sourceSortable, storage, tableSection, toggleSelection, udcKeys, useBulkActionHandlers, useCellRegistry, useI18n, useLookupBatch, useLookupBatchContext, useLookupLabels, useLookupOptions, useLookupRows, useRendererRegistry, useRowActivation, useRowMenu, useRowSelection, useTheme, useTimezone, useUdcAction, useUdcAllRows, useUdcChangeFeed, useUdcChanges, useUdcClient, useUdcCollection, useUdcCollections, useUdcDescribe, useUdcLinks, useUdcLookup, useUdcLookups, useUdcMutate, useUdcMutateMany, useUdcSchema, useUdcSource, useUiKit, useUrlSearchParams, useUrlStateAdapter, valueKeys, virtualizationApplies, windowLocationAdapter, zoneCity };
46
+ export { ALL_ROWS_MAX, BROWSER_TZ, COMMON_ZONES, DEFAULT_ROW_ACTIONS_MAX, DEFAULT_VIRTUAL_OVERSCAN, DEFAULT_VIRTUAL_THRESHOLD, EMPTY_SELECTION, EmptyState, ErrorState, FALLBACK_ICON, INTL_LOCALE, LOCALES, LOCALE_STORAGE_KEY, LOOKUP_OPTIONS_LIMIT, LUCIDE_ICONS, LiveIndicator, LookupCombobox, LookupSelectChip, LookupValue, MATCH_ALL, NoRendererState, RowActionsControls, THEME_STORAGE_KEY, TRANSLATIONS, TZ_STORAGE_KEY, ThemeProvider, TimezoneProvider, UDC_SCOPE_CLASS, UdcActionsProvider, UdcCellsProvider, UdcChangeFeed, UdcChangeFeedContext, UdcDataView, UdcI18nProvider, UdcLinksProvider, UdcLookupBatch, UdcProvider, UdcRenderersProvider, UdcUiProvider, UdcUrlStateProvider, allZones, applyToListRows, asLookup, batchCovers, browserZone, bulkActionEligible, callAction, cellComponent, cn, columnSortable, createCellRegistry, createMemoryUrlStateAdapter, createRendererRegistry, createUdcQueryClient, defaultUdcLinks, effectiveRowActionsDisplay, eligibleRowActions, fetchAllRows, fetchLookupRows, filterHref, followLink, freshnessOf, humanize, i18nKey, intlLocaleOf, isFolderRow, isLocale, isRowMenuKey, isValidZone, keyString, lookup, lookupColor, lookupExtras, lookupKeys, lookupLabel, lookupQuery, makeT, mergeCellRegistries, mergeMessages, messageLocales, mutateManyTouches, offsetLabel, offsetMinutes, optionsFilter, ownsKeys, pageOffset, pageSelectionState, patchRowInList, planBulkAction, pollInterval, prefixUrlStateAdapter, rendererEntry, rendererTabs, resolveChrome, resolveIcon, resolveTab, resolveVirtualization, rowActionDefs, rowActionPlacement, rowActionsColumnWidth, rowActionsDisplay, rowActionsMax, rowActivationEnabled, rowMenuHandlers, schemaLabelTerms, selectAllMatching, selectRange, selectionCount, selectionHas, selectionIsEmpty, selectionTarget, setPageSelection, shouldRetry, sourceSortable, splitRowActions, storage, tableSection, toggleSelection, udcKeys, useBulkActionHandlers, useCanHover, useCellRegistry, useI18n, useLookupBatch, useLookupBatchContext, useLookupLabels, useLookupOptions, useLookupRows, useRendererRegistry, useRowActivation, useRowMenu, useRowSelection, useTheme, useTimezone, useUdcAction, useUdcAllRows, useUdcChangeFeed, useUdcChanges, useUdcClient, useUdcCollection, useUdcCollections, useUdcDescribe, useUdcLinks, useUdcLookup, useUdcLookups, useUdcMutate, useUdcMutateMany, useUdcSchema, useUdcSource, useUiKit, useUrlSearchParams, useUrlStateAdapter, valueKeys, virtualizationApplies, windowLocationAdapter, zoneCity };
@@ -16,8 +16,8 @@ import { useKanbanBoard } from "./useKanbanBoard.js";
16
16
  import { detectCollision, isVersionConflict, orderBetween } from "@jsudc/core";
17
17
  import { useCallback, useEffect, useMemo, useRef, useState } from "react";
18
18
  import { KanbanSquare } from "lucide-react";
19
- import { useQueryClient } from "@tanstack/react-query";
20
19
  import { jsx, jsxs } from "react/jsx-runtime";
20
+ import { useQueryClient } from "@tanstack/react-query";
21
21
  import { DndContext, DragOverlay, KeyboardSensor, PointerSensor, TouchSensor, closestCenter, closestCorners, pointerWithin, useSensor, useSensors } from "@dnd-kit/core";
22
22
  import { SortableContext, arrayMove, horizontalListSortingStrategy, sortableKeyboardCoordinates } from "@dnd-kit/sortable";
23
23
  // src/kanban/KanbanRenderer.tsx
@@ -18,6 +18,10 @@ export declare function useLookupLabels(def: FieldDef | undefined, value: unknow
18
18
  * `lookup_array` is a list of such chips; with `lookup.color_field` each chip
19
19
  * takes its row's colour (`data-color`; a tone such as `danger` draws as that
20
20
  * badge, any other name through the host's `--udc-chip-bg` / `--udc-chip-fg`).
21
+ * A long chip ends in "…" inside its rounded box, at most
22
+ * `--udc-chip-max-width` (default `12em`) wide; when the chips do not fit, the
23
+ * first keep their width and the last visible one shrinks. Where `UdcLinks.href`
24
+ * gives `null` for the target, the label and the chips are plain text.
21
25
  */
22
26
  export declare function LookupValue({ def, value, field, editable, }: {
23
27
  def: FieldDef;
@@ -44,7 +44,15 @@ var NAMED_CHIP = {
44
44
  backgroundColor: "var(--udc-chip-bg, var(--muted))",
45
45
  color: "var(--udc-chip-fg, var(--foreground))"
46
46
  };
47
- function LookupLink({ lookup, keyValue, row, chip, editable }) {
47
+ /**
48
+ * How fast chip `index` of a `lookup_array` gives up width when the chips do
49
+ * not fit: each chip a million times faster than the one before it, so the
50
+ * first chips keep their width and the last visible one ends in "…" (down to
51
+ * its minimum, then the one before it). Capped so the factor stays a finite
52
+ * single-precision float; chips that far along are past the cell's edge.
53
+ */
54
+ var chipShrink = (index) => 1e6 ** Math.min(index, 6);
55
+ function LookupLink({ lookup, keyValue, row, chip, editable, shrink }) {
48
56
  const links = useUdcLinks();
49
57
  const label = lookupLabel(row, lookup);
50
58
  const color = chip ? lookupColor(row, lookup) : void 0;
@@ -56,6 +64,7 @@ function LookupLink({ lookup, keyValue, row, chip, editable }) {
56
64
  value: keyValue
57
65
  });
58
66
  const onClick = (e) => {
67
+ if (href === null) return;
59
68
  if (editable && !(e.metaKey || e.ctrlKey || e.button === 1)) {
60
69
  e.preventDefault();
61
70
  return;
@@ -63,18 +72,35 @@ function LookupLink({ lookup, keyValue, row, chip, editable }) {
63
72
  e.stopPropagation();
64
73
  followLink(e, href, links);
65
74
  };
66
- return /* @__PURE__ */ jsx("a", {
67
- href,
68
- onClick,
75
+ const text = label ?? keyValue;
76
+ const style = {
77
+ ...color !== void 0 && !tone ? NAMED_CHIP : {},
78
+ ...shrink !== void 0 ? { flexShrink: shrink } : {}
79
+ };
80
+ const props = {
69
81
  "data-testid": "lookup-link",
70
82
  "data-key": keyValue,
71
83
  "data-resolved": label !== void 0 || void 0,
72
84
  "data-color": color,
73
85
  "data-tone": tone,
86
+ "data-link": href !== null || void 0,
74
87
  title: lookupExtras(row, lookup) ?? label ?? keyValue,
75
- 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-lookup-chip udc:inline-flex udc:h-5 udc:shrink-0 udc:items-center udc:rounded-full udc:px-2 udc:text-[11px] udc:font-medium udc:hover:no-underline", chip && color === void 0 && "udc:bg-muted udc:hover:bg-primary/12", chip && color !== void 0 && "udc:hover:opacity-80", tone && BADGE_TONE[tone], label === void 0 && "udc:font-mono udc:text-muted-foreground"),
76
- style: color !== void 0 && !tone ? NAMED_CHIP : void 0,
77
- children: label ?? keyValue
88
+ className: cn("udc:min-w-0 udc:truncate udc:text-foreground", href !== null && "udc:underline-offset-2 udc:hover:text-primary udc:hover:underline udc:focus-visible:underline udc:focus-visible:outline-none", chip && "udc-lookup-chip udc:inline-flex udc:h-5 udc:justify-center udc:max-w-[var(--udc-chip-max-width,12em)] udc:min-w-[min(var(--udc-chip-min-width,2.5rem),var(--udc-chip-max-width,12em))] udc:items-center udc:rounded-full udc:px-2 udc:text-[length:var(--udc-chip-font-size,11px)] udc:font-[number:var(--udc-chip-font-weight,500)]", chip && href !== null && "udc:hover:no-underline", chip && color === void 0 && "udc:bg-muted", chip && color === void 0 && href !== null && "udc:hover:bg-primary/12", chip && color !== void 0 && href !== null && "udc:hover:opacity-80", tone && BADGE_TONE[tone], label === void 0 && "udc:font-mono udc:text-muted-foreground"),
89
+ style: Object.keys(style).length > 0 ? style : void 0
90
+ };
91
+ const body = chip ? /* @__PURE__ */ jsx("span", {
92
+ "data-testid": "lookup-chip-text",
93
+ className: "udc:min-w-0 udc:truncate",
94
+ children: text
95
+ }) : text;
96
+ return href === null ? /* @__PURE__ */ jsx("span", {
97
+ ...props,
98
+ children: body
99
+ }) : /* @__PURE__ */ jsx("a", {
100
+ ...props,
101
+ href,
102
+ onClick,
103
+ children: body
78
104
  });
79
105
  }
80
106
  /**
@@ -84,6 +110,10 @@ function LookupLink({ lookup, keyValue, row, chip, editable }) {
84
110
  * `lookup_array` is a list of such chips; with `lookup.color_field` each chip
85
111
  * takes its row's colour (`data-color`; a tone such as `danger` draws as that
86
112
  * badge, any other name through the host's `--udc-chip-bg` / `--udc-chip-fg`).
113
+ * A long chip ends in "…" inside its rounded box, at most
114
+ * `--udc-chip-max-width` (default `12em`) wide; when the chips do not fit, the
115
+ * first keep their width and the last visible one shrinks. Where `UdcLinks.href`
116
+ * gives `null` for the target, the label and the chips are plain text.
87
117
  */
88
118
  function LookupValue({ def, value, field, editable = false }) {
89
119
  const { lookup, keys, row } = useLookupRows(def, value, field);
@@ -94,12 +124,13 @@ function LookupValue({ def, value, field, editable = false }) {
94
124
  if (def.type === "lookup_array") return /* @__PURE__ */ jsx("span", {
95
125
  "data-testid": "lookup-chips",
96
126
  className: "udc:flex udc:min-w-0 udc:items-center udc:gap-1 udc:overflow-hidden",
97
- children: keys.map((k) => /* @__PURE__ */ jsx(LookupLink, {
127
+ children: keys.map((k, i) => /* @__PURE__ */ jsx(LookupLink, {
98
128
  lookup,
99
129
  keyValue: k,
100
130
  row: row(k),
101
131
  chip: true,
102
- editable
132
+ editable,
133
+ shrink: chipShrink(i)
103
134
  }, k))
104
135
  });
105
136
  const key = keys[0];
@@ -1 +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 { CSSProperties, MouseEvent } from \"react\";\nimport { cn } from \"../lib/utils.js\";\nimport { BADGE_TONE, isTone } from \"../table/format.js\";\nimport { batchCovers, useLookupBatchContext } from \"./batch.js\";\nimport { lookupColor, 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\n/**\n * The fill of a chip whose colour is no tone: the host maps the name to\n * `--udc-chip-bg` / `--udc-chip-fg`, e.g.\n * `.udc-lookup-chip[data-color=\"red\"] { --udc-chip-bg: …; --udc-chip-fg: …; }`\n * (and again under `.dark`); a name it does not map draws as the default chip.\n */\nconst NAMED_CHIP: CSSProperties = {\n backgroundColor: \"var(--udc-chip-bg, var(--muted))\",\n color: \"var(--udc-chip-fg, var(--foreground))\",\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 // A chip takes the colour of its row (`lookup.color_field`): a tone draws as\n // that tone's badge, any other name is the host's (`data-color`, NAMED_CHIP).\n const color = chip ? lookupColor(row, lookup) : undefined;\n const tone = isTone(color) ? color : undefined;\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 data-color={color}\n data-tone={tone}\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-lookup-chip udc:inline-flex udc:h-5 udc:shrink-0 udc:items-center udc:rounded-full udc:px-2 udc:text-[11px] udc:font-medium udc:hover:no-underline\",\n chip && color === undefined && \"udc:bg-muted udc:hover:bg-primary/12\",\n chip && color !== undefined && \"udc:hover:opacity-80\",\n tone && BADGE_TONE[tone],\n label === undefined && \"udc:font-mono udc:text-muted-foreground\",\n )}\n style={color !== undefined && !tone ? NAMED_CHIP : undefined}\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; with `lookup.color_field` each chip\n * takes its row's colour (`data-color`; a tone such as `danger` draws as that\n * badge, any other name through the host's `--udc-chip-bg` / `--udc-chip-fg`).\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":";;;;;;;;;;;;;;AAqBA,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;CA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AC1B,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"}
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 { CSSProperties, MouseEvent } from \"react\";\nimport { cn } from \"../lib/utils.js\";\nimport { BADGE_TONE, isTone } from \"../table/format.js\";\nimport { batchCovers, useLookupBatchContext } from \"./batch.js\";\nimport { lookupColor, 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\n/**\n * The fill of a chip whose colour is no tone: the host maps the name to\n * `--udc-chip-bg` / `--udc-chip-fg`, e.g.\n * `.udc-lookup-chip[data-color=\"red\"] { --udc-chip-bg: …; --udc-chip-fg: …; }`\n * (and again under `.dark`); a name it does not map draws as the default chip.\n */\nconst NAMED_CHIP: CSSProperties = {\n backgroundColor: \"var(--udc-chip-bg, var(--muted))\",\n color: \"var(--udc-chip-fg, var(--foreground))\",\n};\n\n/**\n * How fast chip `index` of a `lookup_array` gives up width when the chips do\n * not fit: each chip a million times faster than the one before it, so the\n * first chips keep their width and the last visible one ends in \"…\" (down to\n * its minimum, then the one before it). Capped so the factor stays a finite\n * single-precision float; chips that far along are past the cell's edge.\n */\nconst chipShrink = (index: number): number => 1e6 ** Math.min(index, 6);\n\nfunction LookupLink({\n lookup,\n keyValue,\n row,\n chip,\n editable,\n shrink,\n}: {\n lookup: LookupDef;\n keyValue: string;\n row: Row | undefined;\n chip: boolean;\n editable: boolean;\n /** A chip's `flex-shrink` in its row of chips. */\n shrink?: number;\n}) {\n const links = useUdcLinks();\n const label = lookupLabel(row, lookup);\n // A chip takes the colour of its row (`lookup.color_field`): a tone draws as\n // that tone's badge, any other name is the host's (`data-color`, NAMED_CHIP).\n const color = chip ? lookupColor(row, lookup) : undefined;\n const tone = isTone(color) ? color : undefined;\n const field = lookupKeyField(lookup);\n // `null`: the host has no view of the target (`UdcLinks.href`), so the value\n // is plain text and a click on it is the row's.\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 if (href === null) return;\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 const text = label ?? keyValue;\n const style: CSSProperties = {\n ...(color !== undefined && !tone ? NAMED_CHIP : {}),\n ...(shrink !== undefined ? { flexShrink: shrink } : {}),\n };\n const props = {\n \"data-testid\": \"lookup-link\",\n \"data-key\": keyValue,\n \"data-resolved\": label !== undefined || undefined,\n \"data-color\": color,\n \"data-tone\": tone,\n \"data-link\": href !== null || undefined,\n title: lookupExtras(row, lookup) ?? label ?? keyValue,\n className: cn(\n \"udc:min-w-0 udc:truncate udc:text-foreground\",\n href !== null &&\n \"udc:underline-offset-2 udc:hover:text-primary udc:hover:underline udc:focus-visible:underline udc:focus-visible:outline-none\",\n // The chip's box is a flex container, whose text would be an anonymous flex\n // item that `text-overflow` does not reach: the text is a span of its own\n // that truncates, inside a box at most `--udc-chip-max-width` wide that may\n // shrink to `--udc-chip-min-width` (default 2.5rem, \"K…\") but keeps its rounded ends.\n chip &&\n \"udc-lookup-chip udc:inline-flex udc:h-5 udc:justify-center udc:max-w-[var(--udc-chip-max-width,12em)] udc:min-w-[min(var(--udc-chip-min-width,2.5rem),var(--udc-chip-max-width,12em))] udc:items-center udc:rounded-full udc:px-2 udc:text-[length:var(--udc-chip-font-size,11px)] udc:font-[number:var(--udc-chip-font-weight,500)]\",\n chip && href !== null && \"udc:hover:no-underline\",\n chip && color === undefined && \"udc:bg-muted\",\n chip && color === undefined && href !== null && \"udc:hover:bg-primary/12\",\n chip && color !== undefined && href !== null && \"udc:hover:opacity-80\",\n tone && BADGE_TONE[tone],\n label === undefined && \"udc:font-mono udc:text-muted-foreground\",\n ),\n style: Object.keys(style).length > 0 ? style : undefined,\n };\n const body = chip ? (\n <span data-testid=\"lookup-chip-text\" className=\"udc:min-w-0 udc:truncate\">\n {text}\n </span>\n ) : (\n text\n );\n return href === null ? (\n <span {...props}>{body}</span>\n ) : (\n <a {...props} href={href} onClick={onClick}>\n {body}\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; with `lookup.color_field` each chip\n * takes its row's colour (`data-color`; a tone such as `danger` draws as that\n * badge, any other name through the host's `--udc-chip-bg` / `--udc-chip-fg`).\n * A long chip ends in \"…\" inside its rounded box, at most\n * `--udc-chip-max-width` (default `12em`) wide; when the chips do not fit, the\n * first keep their width and the last visible one shrinks. Where `UdcLinks.href`\n * gives `null` for the target, the label and the chips are plain text.\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, i) => (\n <LookupLink\n key={k}\n lookup={lookup}\n keyValue={k}\n row={row(k)}\n chip\n editable={editable}\n shrink={chipShrink(i)}\n />\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":";;;;;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;CAC1E,IAAI,IAAI,SAAS,gBACf,OACE,oBAAC,QAAD;EACE,eAAY;EACZ,WAAU;EAET,UAAA,KAAK,KAAK,GAAG,MACZ,oBAAC,YAAD;GAEU;GACR,UAAU;GACV,KAAK,IAAI,CAAC;GACV,MAAA;GACU;GACV,QAAQ,WAAW,CAAC;EACrB,GAPM,CAON,CACF;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"}
@@ -7,8 +7,13 @@ import { type MouseEvent, type ReactNode } from "react";
7
7
  * where `<UdcDataView collection>` is mounted.
8
8
  */
9
9
  export interface UdcLinks {
10
- /** The address of `collection`'s view with `search` as its query string. */
11
- href(collection: string, search: URLSearchParams): string;
10
+ /**
11
+ * The address of `collection`'s view with `search` as its query string, or
12
+ * `null` when the host has no view of `collection`: a lookup chip or label
13
+ * to it is then plain text (a click on it falls through to the row), and a
14
+ * `row_link` to it does nothing.
15
+ */
16
+ href(collection: string, search: URLSearchParams): string | null;
12
17
  /** Go to `href` inside the app. */
13
18
  navigate(href: string): void;
14
19
  }
@@ -24,7 +29,10 @@ export declare function UdcLinksProvider({ links, children }: {
24
29
  children?: ReactNode;
25
30
  }): import("react").JSX.Element;
26
31
  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;
32
+ /**
33
+ * The address of `collection` opened with `filter` as its filter tree (`?f=`);
34
+ * `null` when the host has no view of `collection` (`UdcLinks.href`).
35
+ */
36
+ export declare function filterHref(links: UdcLinks, collection: string, filter: FilterNode): string | null;
29
37
  /** A plain click navigates in the app; Cmd / Ctrl / Shift-click (and the middle button) open a new tab. */
30
38
  export declare function followLink(e: Pick<MouseEvent, "metaKey" | "ctrlKey" | "shiftKey" | "button" | "preventDefault">, href: string, links: UdcLinks): void;
@@ -28,7 +28,10 @@ function UdcLinksProvider({ links, children }) {
28
28
  function useUdcLinks() {
29
29
  return useContext(UdcLinksContext);
30
30
  }
31
- /** The address of `collection` opened with `filter` as its filter tree (`?f=`). */
31
+ /**
32
+ * The address of `collection` opened with `filter` as its filter tree (`?f=`);
33
+ * `null` when the host has no view of `collection` (`UdcLinks.href`).
34
+ */
32
35
  function filterHref(links, collection, filter) {
33
36
  const search = new URLSearchParams();
34
37
  const f = encodeFilterParam(filter);
@@ -1 +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"}
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 /**\n * The address of `collection`'s view with `search` as its query string, or\n * `null` when the host has no view of `collection`: a lookup chip or label\n * to it is then plain text (a click on it falls through to the row), and a\n * `row_link` to it does nothing.\n */\n href(collection: string, search: URLSearchParams): string | null;\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/**\n * The address of `collection` opened with `filter` as its filter tree (`?f=`);\n * `null` when the host has no view of `collection` (`UdcLinks.href`).\n */\nexport function filterHref(links: UdcLinks, collection: string, filter: FilterNode): string | null {\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":";;;;;;;;;AA2BA,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;;;;;AAMA,SAAgB,WAAW,OAAiB,YAAoB,QAAmC;CACjG,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"}
@@ -9,7 +9,8 @@ export declare function UdcReactRouterUrlState({ children, collectionPath, }: {
9
9
  children?: ReactNode;
10
10
  /**
11
11
  * The route of a collection's view, e.g. `` (c) => `/udc/${c}` ``. With it, lookup
12
- * links and `row_link`s go there through the router's `navigate`.
12
+ * links and `row_link`s go there through the router's `navigate`; `null` for a
13
+ * collection with no view in the app makes its lookup chips plain text.
13
14
  */
14
- collectionPath?: (collection: string) => string;
15
+ collectionPath?: (collection: string) => string | null;
15
16
  }): import("react").JSX.Element;
@@ -16,8 +16,10 @@ function UdcReactRouterUrlState({ children, collectionPath }) {
16
16
  const navigate = useNavigate();
17
17
  const links = useMemo(() => collectionPath ? {
18
18
  href: (collection, search) => {
19
+ const path = collectionPath(collection);
20
+ if (path === null) return null;
19
21
  const qs = search.toString();
20
- return `${collectionPath(collection)}${qs ? `?${qs}` : ""}`;
22
+ return `${path}${qs ? `?${qs}` : ""}`;
21
23
  },
22
24
  navigate: (href) => void navigate(href)
23
25
  } : null, [collectionPath, navigate]);
@@ -1 +1 @@
1
- {"version":3,"file":"UdcReactRouterUrlState.js","names":[],"sources":["../../src/react-router/UdcReactRouterUrlState.tsx"],"sourcesContent":["import { type ReactNode, useMemo, useRef } from \"react\";\nimport { useNavigate, useSearchParams } from \"react-router\";\nimport { type UdcLinks, UdcLinksProvider } from \"../lookup/links.js\";\nimport type { UrlStateAdapter } from \"../url-state/adapter.js\";\nimport { UdcUrlStateProvider } from \"../url-state/UrlState.js\";\n\nconst noSubscription = () => () => {};\n\n/**\n * Mount inside a react-router router (`BrowserRouter`, `MemoryRouter`, a data\n * router) to keep the data view's URL state in the router's location. The\n * adapter is rebuilt when the router's query string changes, which re-renders\n * the hooks under it in the router's own render; nothing to subscribe to.\n */\nexport function UdcReactRouterUrlState({\n children,\n collectionPath,\n}: {\n children?: ReactNode;\n /**\n * The route of a collection's view, e.g. `` (c) => `/udc/${c}` ``. With it, lookup\n * links and `row_link`s go there through the router's `navigate`.\n */\n collectionPath?: (collection: string) => string;\n}) {\n const [searchParams, setSearchParams] = useSearchParams();\n const navigate = useNavigate();\n const links = useMemo<UdcLinks | null>(\n () =>\n collectionPath\n ? {\n href: (collection, search) => {\n const qs = search.toString();\n return `${collectionPath(collection)}${qs ? `?${qs}` : \"\"}`;\n },\n navigate: (href) => void navigate(href),\n }\n : null,\n [collectionPath, navigate],\n );\n const search = searchParams.toString();\n // What the last `set` wrote, until the router renders it: a second update in the\n // same transition starts from the first one's result, as with `window.location`.\n const latest = useRef(search);\n const adapter = useMemo<UrlStateAdapter>(() => {\n latest.current = search;\n return {\n get: () => new URLSearchParams(latest.current),\n set(next, opts) {\n latest.current = next.toString();\n setSearchParams(next, { replace: opts?.replace });\n },\n subscribe: noSubscription,\n };\n }, [search, setSearchParams]);\n const inner = <UdcUrlStateProvider adapter={adapter}>{children}</UdcUrlStateProvider>;\n return links ? <UdcLinksProvider links={links}>{inner}</UdcLinksProvider> : inner;\n}\n"],"mappings":";;;;;;AAMA,IAAM,6BAA6B,CAAC;;;;;;;AAQpC,SAAgB,uBAAuB,EACrC,UACA,kBAQC;CACD,MAAM,CAAC,cAAc,mBAAmB,gBAAgB;CACxD,MAAM,WAAW,YAAY;CAC7B,MAAM,QAAQ,cAEV,iBACI;EACE,OAAO,YAAY,WAAW;GAC5B,MAAM,KAAK,OAAO,SAAS;GAC3B,OAAO,GAAG,eAAe,UAAU,IAAI,KAAK,IAAI,OAAO;EACzD;EACA,WAAW,SAAS,KAAK,SAAS,IAAI;CACxC,IACA,MACN,CAAC,gBAAgB,QAAQ,CAC3B;CACA,MAAM,SAAS,aAAa,SAAS;CAGrC,MAAM,SAAS,OAAO,MAAM;CAC5B,MAAM,UAAU,cAA+B;EAC7C,OAAO,UAAU;EACjB,OAAO;GACL,WAAW,IAAI,gBAAgB,OAAO,OAAO;GAC7C,IAAI,MAAM,MAAM;IACd,OAAO,UAAU,KAAK,SAAS;IAC/B,gBAAgB,MAAM,EAAE,SAAS,MAAM,QAAQ,CAAC;GAClD;GACA,WAAW;EACb;CACF,GAAG,CAAC,QAAQ,eAAe,CAAC;CAC5B,MAAM,QAAQ,oBAAC,qBAAD;EAA8B;EAAU;CAA8B,CAAA;CACpF,OAAO,QAAQ,oBAAC,kBAAD;EAAyB;EAAQ,UAAA;CAAwB,CAAA,IAAI;AAC9E"}
1
+ {"version":3,"file":"UdcReactRouterUrlState.js","names":[],"sources":["../../src/react-router/UdcReactRouterUrlState.tsx"],"sourcesContent":["import { type ReactNode, useMemo, useRef } from \"react\";\nimport { useNavigate, useSearchParams } from \"react-router\";\nimport { type UdcLinks, UdcLinksProvider } from \"../lookup/links.js\";\nimport type { UrlStateAdapter } from \"../url-state/adapter.js\";\nimport { UdcUrlStateProvider } from \"../url-state/UrlState.js\";\n\nconst noSubscription = () => () => {};\n\n/**\n * Mount inside a react-router router (`BrowserRouter`, `MemoryRouter`, a data\n * router) to keep the data view's URL state in the router's location. The\n * adapter is rebuilt when the router's query string changes, which re-renders\n * the hooks under it in the router's own render; nothing to subscribe to.\n */\nexport function UdcReactRouterUrlState({\n children,\n collectionPath,\n}: {\n children?: ReactNode;\n /**\n * The route of a collection's view, e.g. `` (c) => `/udc/${c}` ``. With it, lookup\n * links and `row_link`s go there through the router's `navigate`; `null` for a\n * collection with no view in the app makes its lookup chips plain text.\n */\n collectionPath?: (collection: string) => string | null;\n}) {\n const [searchParams, setSearchParams] = useSearchParams();\n const navigate = useNavigate();\n const links = useMemo<UdcLinks | null>(\n () =>\n collectionPath\n ? {\n href: (collection, search) => {\n const path = collectionPath(collection);\n if (path === null) return null;\n const qs = search.toString();\n return `${path}${qs ? `?${qs}` : \"\"}`;\n },\n navigate: (href) => void navigate(href),\n }\n : null,\n [collectionPath, navigate],\n );\n const search = searchParams.toString();\n // What the last `set` wrote, until the router renders it: a second update in the\n // same transition starts from the first one's result, as with `window.location`.\n const latest = useRef(search);\n const adapter = useMemo<UrlStateAdapter>(() => {\n latest.current = search;\n return {\n get: () => new URLSearchParams(latest.current),\n set(next, opts) {\n latest.current = next.toString();\n setSearchParams(next, { replace: opts?.replace });\n },\n subscribe: noSubscription,\n };\n }, [search, setSearchParams]);\n const inner = <UdcUrlStateProvider adapter={adapter}>{children}</UdcUrlStateProvider>;\n return links ? <UdcLinksProvider links={links}>{inner}</UdcLinksProvider> : inner;\n}\n"],"mappings":";;;;;;AAMA,IAAM,6BAA6B,CAAC;;;;;;;AAQpC,SAAgB,uBAAuB,EACrC,UACA,kBASC;CACD,MAAM,CAAC,cAAc,mBAAmB,gBAAgB;CACxD,MAAM,WAAW,YAAY;CAC7B,MAAM,QAAQ,cAEV,iBACI;EACE,OAAO,YAAY,WAAW;GAC5B,MAAM,OAAO,eAAe,UAAU;GACtC,IAAI,SAAS,MAAM,OAAO;GAC1B,MAAM,KAAK,OAAO,SAAS;GAC3B,OAAO,GAAG,OAAO,KAAK,IAAI,OAAO;EACnC;EACA,WAAW,SAAS,KAAK,SAAS,IAAI;CACxC,IACA,MACN,CAAC,gBAAgB,QAAQ,CAC3B;CACA,MAAM,SAAS,aAAa,SAAS;CAGrC,MAAM,SAAS,OAAO,MAAM;CAC5B,MAAM,UAAU,cAA+B;EAC7C,OAAO,UAAU;EACjB,OAAO;GACL,WAAW,IAAI,gBAAgB,OAAO,OAAO;GAC7C,IAAI,MAAM,MAAM;IACd,OAAO,UAAU,KAAK,SAAS;IAC/B,gBAAgB,MAAM,EAAE,SAAS,MAAM,QAAQ,CAAC;GAClD;GACA,WAAW;EACb;CACF,GAAG,CAAC,QAAQ,eAAe,CAAC;CAC5B,MAAM,QAAQ,oBAAC,qBAAD;EAA8B;EAAU;CAA8B,CAAA;CACpF,OAAO,QAAQ,oBAAC,kBAAD;EAAyB;EAAQ,UAAA;CAAwB,CAAA,IAAI;AAC9E"}