@nivaro/react 0.3.118 → 0.3.119

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.
@@ -1,29 +1,29 @@
1
1
  import { jsxs as c, jsx as l } from "react/jsx-runtime";
2
- import { ChevronRight as M, X as F, Search as R, Loader2 as k } from "lucide-react";
2
+ import { ChevronRight as M, X as k, Search as F, Loader2 as R } from "lucide-react";
3
3
  import { useState as E, useRef as O, useLayoutEffect as z, useEffect as H } from "react";
4
4
  import { createPortal as $ } from "react-dom";
5
5
  import { titleCase as _ } from "../../../lib/utils.js";
6
- import { Popover as B, PopoverTrigger as T, PopoverContent as q } from "../../ui/popover.js";
7
- import { useCollectionBody as Q } from "../collection-meta.js";
6
+ import { Popover as B, PopoverTrigger as T, PopoverContent as W } from "../../ui/popover.js";
7
+ import { useCollectionBody as q } from "../collection-meta.js";
8
8
  function V({
9
9
  collection: n,
10
- fields: d,
10
+ fields: f,
11
11
  relations: u,
12
- value: a,
12
+ value: r,
13
13
  valueLabel: h,
14
- onChange: r,
15
- onClear: f,
16
- placeholder: i,
17
- walk: w
14
+ onChange: a,
15
+ onClear: p,
16
+ placeholder: s,
17
+ walk: v
18
18
  }) {
19
- const [o, s] = E(!1), [m, p] = E(""), x = O(null), y = O(null), [b, L] = E(null);
19
+ const [o, i] = E(!1), [x, m] = E(""), y = O(null), b = O(null), [g, L] = E(null);
20
20
  z(() => {
21
21
  if (!o) return;
22
22
  const t = () => {
23
23
  var N;
24
- const e = (N = x.current) == null ? void 0 : N.getBoundingClientRect();
25
- if (!e) return;
26
- const C = Math.min(e.left, window.innerWidth - 288 - 8), j = Math.min(e.bottom + 4, window.innerHeight - 60);
24
+ const d = (N = y.current) == null ? void 0 : N.getBoundingClientRect();
25
+ if (!d) return;
26
+ const C = Math.min(d.left, window.innerWidth - 288 - 8), j = Math.min(d.bottom + 4, window.innerHeight - 60);
27
27
  L({ top: j, left: Math.max(8, C) });
28
28
  };
29
29
  return t(), window.addEventListener("scroll", t, !0), window.addEventListener("resize", t), () => {
@@ -31,67 +31,70 @@ function V({
31
31
  };
32
32
  }, [o]), H(() => {
33
33
  if (!o) return;
34
- const t = (e) => {
34
+ const t = (d) => {
35
35
  var C, j, N;
36
- const v = e.target;
37
- (C = x.current) != null && C.contains(v) || (j = y.current) != null && j.contains(v) || (N = v.closest) != null && N.call(v, "[data-radix-popper-content-wrapper]") || s(!1);
36
+ const e = d.target;
37
+ (C = y.current) != null && C.contains(e) || (j = b.current) != null && j.contains(e) || (N = e.closest) != null && N.call(e, "[data-radix-popper-content-wrapper]") || i(!1);
38
38
  };
39
39
  return document.addEventListener("mousedown", t), () => document.removeEventListener("mousedown", t);
40
40
  }, [o]);
41
- const g = [...d].sort((t, e) => t.field.localeCompare(e.field)).filter((t) => t.field.toLowerCase().includes(m.trim().toLowerCase()));
41
+ const w = [...f].sort((t, d) => t.field.localeCompare(d.field)).filter((t) => t.field.toLowerCase().includes(x.trim().toLowerCase()));
42
42
  return /* @__PURE__ */ c("div", { className: "relative", children: [
43
43
  /* @__PURE__ */ c(
44
44
  "button",
45
45
  {
46
46
  type: "button",
47
- ref: x,
48
- onClick: () => s((t) => !t),
49
- className: `flex h-8 w-full items-center justify-between gap-1.5 rounded-md border border-slate-200 bg-white text-[13px] text-slate-700 hover:border-slate-300 ${a && f ? "pl-2.5 pr-8" : "px-2.5"}`,
47
+ ref: y,
48
+ onClick: () => i((t) => !t),
49
+ className: `flex h-8 w-full items-center justify-between gap-1.5 rounded-md border border-slate-200 bg-white text-[13px] text-slate-700 hover:border-slate-300 ${r && p ? "pl-2.5 pr-8" : "px-2.5"}`,
50
50
  children: [
51
51
  /* @__PURE__ */ l(
52
52
  "span",
53
53
  {
54
- className: a ? "font-mono text-[12px] flex-1 text-left truncate" : "text-slate-400 flex-1 text-left truncate whitespace-nowrap",
55
- children: h || a || i || "Select field…"
54
+ className: (
55
+ // A readable label (valueLabel) reads like the rest of the UI; only a raw path is mono.
56
+ r && h ? "text-[12.5px] flex-1 text-left truncate" : r ? "font-mono text-[12px] flex-1 text-left truncate" : "text-slate-400 flex-1 text-left truncate whitespace-nowrap"
57
+ ),
58
+ children: h || r || s || "Select field…"
56
59
  }
57
60
  ),
58
61
  /* @__PURE__ */ l(M, { className: "h-3.5 w-3.5 text-slate-400 shrink-0" })
59
62
  ]
60
63
  }
61
64
  ),
62
- a && f && /* @__PURE__ */ l(
65
+ r && p && /* @__PURE__ */ l(
63
66
  "button",
64
67
  {
65
68
  type: "button",
66
69
  onClick: (t) => {
67
- t.stopPropagation(), f();
70
+ t.stopPropagation(), p();
68
71
  },
69
72
  className: "absolute right-2 top-1/2 -translate-y-1/2 flex h-5 w-5 items-center justify-center rounded text-slate-400 hover:bg-slate-100 hover:text-slate-600 dark:hover:bg-muted dark:hover:text-slate-300",
70
- children: /* @__PURE__ */ l(F, { className: "h-3 w-3" })
73
+ children: /* @__PURE__ */ l(k, { className: "h-3 w-3" })
71
74
  }
72
75
  ),
73
- o && b && $(
76
+ o && g && $(
74
77
  /* @__PURE__ */ l(
75
78
  "div",
76
79
  {
77
- ref: y,
78
- style: { position: "fixed", top: b.top, left: b.left },
80
+ ref: b,
81
+ style: { position: "fixed", top: g.top, left: g.left },
79
82
  className: "z-50 w-72 rounded-xl border border-slate-200 bg-white shadow-lg dark:border-border dark:bg-card",
80
83
  children: /* @__PURE__ */ c("div", { className: "p-2", children: [
81
84
  /* @__PURE__ */ c("div", { className: "relative mb-1.5", children: [
82
- /* @__PURE__ */ l(R, { className: "absolute left-2.5 top-1/2 h-3.5 w-3.5 -translate-y-1/2 text-slate-400" }),
85
+ /* @__PURE__ */ l(F, { className: "absolute left-2.5 top-1/2 h-3.5 w-3.5 -translate-y-1/2 text-slate-400" }),
83
86
  /* @__PURE__ */ l(
84
87
  "input",
85
88
  {
86
- value: m,
87
- onChange: (t) => p(t.target.value),
89
+ value: x,
90
+ onChange: (t) => m(t.target.value),
88
91
  placeholder: "Search fields…",
89
92
  className: "h-8 w-full rounded-md border border-slate-200 bg-slate-50 pl-8 pr-3 text-[13px] placeholder-slate-400 focus:border-nvr-cyan/50 focus:outline-none focus:ring-2 focus:ring-nvr-cyan/30"
90
93
  }
91
94
  )
92
95
  ] }),
93
96
  /* @__PURE__ */ c("div", { className: "max-h-72 overflow-y-auto", children: [
94
- g.map((t) => /* @__PURE__ */ l(
97
+ w.map((t) => /* @__PURE__ */ l(
95
98
  P,
96
99
  {
97
100
  field: t,
@@ -100,14 +103,14 @@ function V({
100
103
  pathPrefix: [],
101
104
  labelPrefix: [],
102
105
  depth: 0,
103
- walk: w,
104
- onSelect: (e) => {
105
- r(e), s(!1);
106
+ walk: v,
107
+ onSelect: (d) => {
108
+ a(d), i(!1);
106
109
  }
107
110
  },
108
111
  t.field
109
112
  )),
110
- g.length === 0 && /* @__PURE__ */ l("div", { className: "px-2 py-3 text-center text-[12px] text-slate-400", children: "No fields" })
113
+ w.length === 0 && /* @__PURE__ */ l("div", { className: "px-2 py-3 text-center text-[12px] text-slate-400", children: "No fields" })
111
114
  ] })
112
115
  ] })
113
116
  }
@@ -116,70 +119,70 @@ function V({
116
119
  )
117
120
  ] });
118
121
  }
119
- function W({
122
+ function Q({
120
123
  relatedCollection: n,
121
- pathPrefix: d,
124
+ pathPrefix: f,
122
125
  labelPrefix: u,
123
- onSelect: a,
126
+ onSelect: r,
124
127
  walk: h
125
128
  }) {
126
- const { data: r, isLoading: f } = Q(n);
127
- if (f)
129
+ const { data: a, isLoading: p } = q(n);
130
+ if (p)
128
131
  return /* @__PURE__ */ c("div", { className: "py-2 text-[12px] text-slate-400 flex items-center gap-2 px-2", children: [
129
- /* @__PURE__ */ l(k, { className: "h-3.5 w-3.5 animate-spin" }),
132
+ /* @__PURE__ */ l(R, { className: "h-3.5 w-3.5 animate-spin" }),
130
133
  "Loading…"
131
134
  ] });
132
- const i = [...((r == null ? void 0 : r.fields) ?? []).filter((o) => !o.hidden)].sort(
133
- (o, s) => o.field.localeCompare(s.field)
134
- ), w = (r == null ? void 0 : r.relations) ?? [];
135
+ const s = [...((a == null ? void 0 : a.fields) ?? []).filter((o) => !o.hidden)].sort(
136
+ (o, i) => o.field.localeCompare(i.field)
137
+ ), v = (a == null ? void 0 : a.relations) ?? [];
135
138
  return /* @__PURE__ */ c("div", { className: "max-h-64 overflow-y-auto", children: [
136
- i.map((o) => /* @__PURE__ */ l(
139
+ s.map((o) => /* @__PURE__ */ l(
137
140
  P,
138
141
  {
139
142
  field: o,
140
143
  collection: n,
141
- relations: w,
142
- pathPrefix: d,
144
+ relations: v,
145
+ pathPrefix: f,
143
146
  labelPrefix: u,
144
147
  depth: 0,
145
148
  walk: h,
146
- onSelect: a
149
+ onSelect: r
147
150
  },
148
151
  o.field
149
152
  )),
150
- i.length === 0 && /* @__PURE__ */ l("div", { className: "px-2 py-1.5 text-[12px] text-slate-400", children: "No fields" })
153
+ s.length === 0 && /* @__PURE__ */ l("div", { className: "px-2 py-1.5 text-[12px] text-slate-400", children: "No fields" })
151
154
  ] });
152
155
  }
153
156
  function P({
154
157
  field: n,
155
- collection: d,
158
+ collection: f,
156
159
  relations: u,
157
- pathPrefix: a,
160
+ pathPrefix: r,
158
161
  labelPrefix: h,
159
- depth: r,
160
- onSelect: f,
161
- walk: i
162
+ depth: a,
163
+ onSelect: p,
164
+ walk: s
162
165
  }) {
163
- const [w, o] = E(!1), s = u.find(
164
- (e) => e.many_collection === d && e.many_field === n.field && e.junction_field === null && e.one_collection
165
- ) ?? null, m = s ? null : u.find(
166
- (e) => e.many_collection === d && e.many_field === n.field && e.junction_field !== null && e.one_collection
167
- ) ?? null, p = !s && !m ? u.find(
168
- (e) => e.one_collection === d && e.one_field === n.field && e.junction_field !== null && e.many_collection
169
- ) ?? null : null, x = m ?? p, y = !s && !x ? u.find(
170
- (e) => e.one_collection === d && e.one_field === n.field && e.junction_field === null && e.many_collection
171
- ) ?? null : null, b = p ? u.find(
172
- (e) => e.many_collection === p.many_collection && e.many_field === p.junction_field && e.one_collection
173
- ) ?? null : null, L = (s == null ? void 0 : s.one_collection) ?? (m == null ? void 0 : m.one_collection) ?? (b == null ? void 0 : b.one_collection) ?? (p == null ? void 0 : p.many_collection) ?? (y == null ? void 0 : y.many_collection) ?? null;
174
- if (i != null && i.m2oOnly && L && !s) return null;
175
- const g = (i == null ? void 0 : i.maxHops) != null && a.length >= i.maxHops ? null : L, t = s ? "M2O" : x ? "M2M" : y ? "O2M" : null;
176
- return g ? /* @__PURE__ */ c(B, { open: w, onOpenChange: o, children: [
166
+ const [v, o] = E(!1), i = u.find(
167
+ (e) => e.many_collection === f && e.many_field === n.field && e.junction_field === null && e.one_collection
168
+ ) ?? null, x = i ? null : u.find(
169
+ (e) => e.many_collection === f && e.many_field === n.field && e.junction_field !== null && e.one_collection
170
+ ) ?? null, m = !i && !x ? u.find(
171
+ (e) => e.one_collection === f && e.one_field === n.field && e.junction_field !== null && e.many_collection
172
+ ) ?? null : null, y = x ?? m, b = !i && !y ? u.find(
173
+ (e) => e.one_collection === f && e.one_field === n.field && e.junction_field === null && e.many_collection
174
+ ) ?? null : null, g = m ? u.find(
175
+ (e) => e.many_collection === m.many_collection && e.many_field === m.junction_field && e.one_collection
176
+ ) ?? null : null, L = (i == null ? void 0 : i.one_collection) ?? (x == null ? void 0 : x.one_collection) ?? (g == null ? void 0 : g.one_collection) ?? (m == null ? void 0 : m.many_collection) ?? (b == null ? void 0 : b.many_collection) ?? null;
177
+ if (s != null && s.m2oOnly && L && !i) return null;
178
+ const w = (s == null ? void 0 : s.maxHops) != null && r.length >= s.maxHops ? null : L, t = i ? "M2O" : y ? "M2M" : b ? "O2M" : null, d = typeof window < "u" && window.innerWidth < 640;
179
+ return w ? /* @__PURE__ */ c(B, { open: v, onOpenChange: o, children: [
177
180
  /* @__PURE__ */ l(T, { asChild: !0, children: /* @__PURE__ */ c(
178
181
  "button",
179
182
  {
180
183
  type: "button",
181
- className: "flex w-full items-center gap-2 rounded-md px-2 py-1.5 text-left text-[13px] text-slate-700 hover:bg-slate-50 data-[state=open]:bg-slate-100",
182
- style: { paddingLeft: `${0.5 + r * 1}rem` },
184
+ className: "flex w-full items-center gap-2 rounded-md px-2 py-1.5 text-left text-[13px] text-slate-700 hover:bg-slate-50 data-[state=open]:bg-slate-100 dark:data-[state=open]:bg-muted",
185
+ style: { paddingLeft: `${0.5 + a * 1}rem` },
183
186
  children: [
184
187
  /* @__PURE__ */ l("span", { className: "flex-1", children: _(n.field) }),
185
188
  t && /* @__PURE__ */ l("span", { className: "rounded px-1 py-0.5 text-[10px] font-medium bg-slate-100 text-slate-500", children: t }),
@@ -187,48 +190,58 @@ function P({
187
190
  ]
188
191
  }
189
192
  ) }),
190
- /* @__PURE__ */ c(q, { side: "right", align: "start", sideOffset: 4, className: "w-56 p-1", children: [
191
- (s || x) && /* @__PURE__ */ l(
192
- "button",
193
- {
194
- type: "button",
195
- onClick: () => {
196
- f({
197
- path: [...a, n.field],
198
- pathLabels: [...h, _(n.field)],
199
- fieldType: n.type,
200
- relatedCollection: g ?? void 0
201
- }), o(!1);
202
- },
203
- className: "w-full rounded-md px-2 py-1 text-left text-[12px] text-slate-500 hover:bg-slate-50 italic",
204
- children: "(select this field)"
205
- }
206
- ),
207
- /* @__PURE__ */ l(
208
- W,
209
- {
210
- relatedCollection: g,
211
- pathPrefix: [...a, n.field],
212
- labelPrefix: [...h, _(n.field)],
213
- walk: i,
214
- onSelect: (e) => {
215
- f(e), o(!1);
216
- }
217
- }
218
- )
219
- ] })
193
+ /* @__PURE__ */ c(
194
+ W,
195
+ {
196
+ side: d ? "bottom" : "right",
197
+ align: d ? "end" : "start",
198
+ sideOffset: 4,
199
+ collisionPadding: 8,
200
+ className: "w-56 p-1",
201
+ children: [
202
+ (i || y) && /* @__PURE__ */ l(
203
+ "button",
204
+ {
205
+ type: "button",
206
+ onClick: () => {
207
+ p({
208
+ path: [...r, n.field],
209
+ pathLabels: [...h, _(n.field)],
210
+ fieldType: n.type,
211
+ relatedCollection: w ?? void 0
212
+ }), o(!1);
213
+ },
214
+ className: "w-full rounded-md px-2 py-1 text-left text-[12px] text-slate-500 hover:bg-slate-50 italic",
215
+ children: "(select this field)"
216
+ }
217
+ ),
218
+ /* @__PURE__ */ l(
219
+ Q,
220
+ {
221
+ relatedCollection: w,
222
+ pathPrefix: [...r, n.field],
223
+ labelPrefix: [...h, _(n.field)],
224
+ walk: s,
225
+ onSelect: (e) => {
226
+ p(e), o(!1);
227
+ }
228
+ }
229
+ )
230
+ ]
231
+ }
232
+ )
220
233
  ] }) : /* @__PURE__ */ l(
221
234
  "button",
222
235
  {
223
236
  type: "button",
224
- onClick: () => f({
225
- path: [...a, n.field],
237
+ onClick: () => p({
238
+ path: [...r, n.field],
226
239
  pathLabels: [...h, _(n.field)],
227
240
  fieldType: n.type,
228
- relatedCollection: r > 0 ? d : void 0
241
+ relatedCollection: a > 0 ? f : void 0
229
242
  }),
230
243
  className: "w-full rounded-md px-2 py-1.5 text-left text-[13px] text-slate-700 hover:bg-slate-50",
231
- style: { paddingLeft: `${0.5 + r * 1}rem` },
244
+ style: { paddingLeft: `${0.5 + a * 1}rem` },
232
245
  children: _(n.field)
233
246
  }
234
247
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nivaro/react",
3
- "version": "0.3.118",
3
+ "version": "0.3.119",
4
4
  "description": "React hooks and components for Nivaro CMS forms",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",