@reformer/ui-kit 6.0.0 → 7.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/ui/select.js CHANGED
@@ -1,127 +1,270 @@
1
- import { jsxs as d, jsx as e } from "react/jsx-runtime";
2
- import * as m from "react";
1
+ import { jsxs as m, jsx as r, Fragment as D } from "react/jsx-runtime";
2
+ import * as p from "react";
3
3
  import * as s from "@radix-ui/react-select";
4
- import { XIcon as V, ChevronDownIcon as S, CheckIcon as O, ChevronUpIcon as R } from "lucide-react";
5
- import { c as o } from "../utils-DtaLkIY8.js";
6
- const D = m.forwardRef(
4
+ import { XIcon as E, ChevronDownIcon as I, CheckIcon as P, ChevronUpIcon as H } from "lucide-react";
5
+ import { c as g } from "../utils-DtaLkIY8.js";
6
+ function U(e) {
7
+ switch (e) {
8
+ case "partial":
9
+ return { serverSearch: !0, paginated: !0, searchable: !0 };
10
+ case "preload":
11
+ return { serverSearch: !1, paginated: !1, searchable: !0 };
12
+ case "static":
13
+ default:
14
+ return { serverSearch: !1, paginated: !1, searchable: !1 };
15
+ }
16
+ }
17
+ function z(e) {
18
+ return e.map((t) => ({
19
+ id: t.id,
20
+ label: t.label,
21
+ value: String(t.value),
22
+ group: t.group
23
+ }));
24
+ }
25
+ function O(e, t) {
26
+ const a = new Set(e.map((o) => o.id)), n = [...e];
27
+ for (const o of t)
28
+ a.has(o.id) || (a.add(o.id), n.push(o));
29
+ return n;
30
+ }
31
+ function F(e, t) {
32
+ const a = t.trim().toLowerCase();
33
+ return a ? e.filter((n) => n.label.toLowerCase().includes(a)) : e;
34
+ }
35
+ function w() {
36
+ return {
37
+ options: [],
38
+ page: 0,
39
+ totalCount: 0,
40
+ loading: !1,
41
+ loadingMore: !1,
42
+ error: !1,
43
+ search: ""
44
+ };
45
+ }
46
+ function G(e, t) {
47
+ switch (t.kind) {
48
+ case "load-start":
49
+ return {
50
+ ...w(),
51
+ loading: !0,
52
+ search: t.search
53
+ };
54
+ case "load-success":
55
+ return {
56
+ ...e,
57
+ options: z(t.result.items),
58
+ totalCount: t.result.totalCount,
59
+ page: t.page,
60
+ loading: !1,
61
+ error: !1
62
+ };
63
+ case "load-more-start":
64
+ return { ...e, loadingMore: !0 };
65
+ case "load-more-success":
66
+ return {
67
+ ...e,
68
+ options: O(e.options, z(t.result.items)),
69
+ totalCount: t.result.totalCount,
70
+ page: t.page,
71
+ loadingMore: !1,
72
+ error: !1
73
+ };
74
+ case "load-error":
75
+ return { ...e, loading: !1, loadingMore: !1, error: !0 };
76
+ case "reset":
77
+ return w();
78
+ default:
79
+ return e;
80
+ }
81
+ }
82
+ function k(e) {
83
+ return e.options.length < e.totalCount;
84
+ }
85
+ function q(e, t = 48) {
86
+ return e.scrollTop + e.clientHeight >= e.scrollHeight - t;
87
+ }
88
+ const A = 300;
89
+ function K(e) {
90
+ const t = p.useMemo(() => U(e?.type), [e?.type]), [a, n] = p.useReducer(G, void 0, w), [o, c] = p.useState("");
91
+ p.useEffect(() => {
92
+ if (!e || t.serverSearch) return;
93
+ let i = !1;
94
+ return n({ kind: "load-start", search: "" }), e.load({}).then(
95
+ (f) => {
96
+ i || n({ kind: "load-success", result: f, page: 1 });
97
+ },
98
+ () => {
99
+ i || n({ kind: "load-error" });
100
+ }
101
+ ), () => {
102
+ i = !0;
103
+ };
104
+ }, [e, t.serverSearch]), p.useEffect(() => {
105
+ if (!e || !t.serverSearch) return;
106
+ let i = !1;
107
+ const f = setTimeout(() => {
108
+ n({ kind: "load-start", search: o }), e.load({ search: o, page: 1, pageSize: e.pageSize }).then(
109
+ (y) => {
110
+ i || n({ kind: "load-success", result: y, page: 1 });
111
+ },
112
+ () => {
113
+ i || n({ kind: "load-error" });
114
+ }
115
+ );
116
+ }, A);
117
+ return () => {
118
+ i = !0, clearTimeout(f);
119
+ };
120
+ }, [e, t.serverSearch, o]);
121
+ const x = p.useCallback(() => {
122
+ if (!e || !t.paginated || a.loading || a.loadingMore || !k(a)) return;
123
+ const i = a.page + 1;
124
+ n({ kind: "load-more-start" }), e.load({ search: a.search, page: i, pageSize: e.pageSize }).then(
125
+ (f) => n({ kind: "load-more-success", result: f, page: i }),
126
+ () => n({ kind: "load-error", more: !0 })
127
+ );
128
+ }, [e, t.paginated, a]);
129
+ return {
130
+ options: t.serverSearch ? a.options : F(a.options, o),
131
+ loading: a.loading,
132
+ loadingMore: a.loadingMore,
133
+ hasMore: k(a),
134
+ loadMore: x,
135
+ searchInput: o,
136
+ setSearchInput: c,
137
+ flags: t
138
+ };
139
+ }
140
+ const X = p.forwardRef(
7
141
  ({
8
- className: t,
9
- value: a,
10
- onChange: r,
11
- onBlur: c,
12
- resource: u,
13
- options: g,
14
- placeholder: N,
15
- disabled: h,
16
- clearable: z = !1,
17
- "data-testid": C,
18
- "aria-invalid": I,
19
- ...k
142
+ className: e,
143
+ value: t,
144
+ onChange: a,
145
+ onBlur: n,
146
+ resource: o,
147
+ options: c,
148
+ placeholder: x,
149
+ disabled: S,
150
+ clearable: i = !1,
151
+ "data-testid": f,
152
+ "aria-invalid": y,
153
+ ...M
20
154
  }, _) => {
21
- const [b, v] = m.useState([]), [p, x] = m.useState(!1);
22
- m.useEffect(() => {
23
- u && (x(!0), u.load({}).then((l) => {
24
- v(
25
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
26
- l.items.map((n) => ({
27
- id: n.id,
28
- label: n.label,
29
- value: String(n.value),
30
- group: n.group
31
- }))
32
- );
33
- }).catch(() => v([])).finally(() => x(!1)));
34
- }, [u]);
35
- const y = m.useMemo(() => g ? g.map((l) => ({
155
+ const d = K(o), C = p.useMemo(() => c ? c.map((l) => ({
36
156
  id: l.value,
37
157
  label: l.label,
38
158
  value: String(l.value),
39
159
  group: l.group
40
- })) : b, [g, b]), j = (l) => {
41
- r?.(l);
160
+ })) : d.options, [c, d.options]), L = !c && !!o && d.flags.searchable, b = !c && d.loading, R = (l) => {
161
+ a?.(l);
162
+ }, j = (l) => {
163
+ l || n?.();
42
164
  }, B = (l) => {
43
- l || c?.();
44
- }, L = (l) => {
45
- l.stopPropagation(), r?.(null);
46
- }, w = z && a && !h && !p;
47
- return /* @__PURE__ */ d("div", { className: "relative w-full", children: [
48
- /* @__PURE__ */ d(
165
+ l.stopPropagation(), a?.(null);
166
+ }, T = (l) => {
167
+ d.flags.paginated && q(l.currentTarget) && d.loadMore();
168
+ }, N = i && t && !S && !b, V = L ? /* @__PURE__ */ r("div", { className: "sticky top-0 z-10 border-b bg-white p-1", children: /* @__PURE__ */ r(
169
+ "input",
170
+ {
171
+ type: "text",
172
+ value: d.searchInput,
173
+ onChange: (l) => d.setSearchInput(l.target.value),
174
+ onKeyDown: (l) => l.stopPropagation(),
175
+ placeholder: "Search...",
176
+ className: "w-full rounded-sm border border-gray-200 px-2 py-1 text-sm outline-none focus:border-ring",
177
+ "aria-label": "Search options"
178
+ }
179
+ ) }) : null;
180
+ return /* @__PURE__ */ m("div", { className: "relative w-full", children: [
181
+ /* @__PURE__ */ m(
49
182
  s.Root,
50
183
  {
51
184
  "data-slot": "select",
52
- value: a || "",
53
- onValueChange: j,
54
- onOpenChange: B,
55
- disabled: h || p,
56
- ...k,
185
+ value: t || "",
186
+ onValueChange: R,
187
+ onOpenChange: j,
188
+ disabled: S || b,
189
+ ...M,
57
190
  children: [
58
- /* @__PURE__ */ e(
59
- U,
191
+ /* @__PURE__ */ r(
192
+ W,
60
193
  {
61
194
  ref: _,
62
- className: o(t, w && "pr-8"),
63
- disabled: p,
64
- "data-testid": C,
65
- "aria-invalid": I,
66
- children: /* @__PURE__ */ e(
67
- T,
195
+ className: g(e, N && "pr-8"),
196
+ disabled: b,
197
+ "data-testid": f,
198
+ "aria-invalid": y,
199
+ children: /* @__PURE__ */ r(
200
+ Q,
68
201
  {
69
- placeholder: p ? "Loading..." : N || "Select an option..."
202
+ placeholder: b ? "Loading..." : x || "Select an option..."
70
203
  }
71
204
  )
72
205
  }
73
206
  ),
74
- /* @__PURE__ */ e(G, { children: p ? /* @__PURE__ */ e("div", { className: "px-2 py-1.5 text-sm text-muted-foreground", children: "Loading..." }) : y.length === 0 ? /* @__PURE__ */ e("div", { className: "px-2 py-1.5 text-sm text-muted-foreground", children: "No options available" }) : (() => {
75
- const l = y.reduce(
76
- (n, f) => {
77
- const i = f.group || "default";
78
- return n[i] || (n[i] = []), n[i].push(f), n;
79
- },
80
- {}
81
- );
82
- return Object.entries(l).map(([n, f]) => /* @__PURE__ */ d(P, { children: [
83
- n !== "default" && /* @__PURE__ */ e(E, { children: n }),
84
- f.map((i) => /* @__PURE__ */ e(M, { value: i.value, children: i.label }, i.id))
85
- ] }, n));
86
- })() })
207
+ /* @__PURE__ */ r(
208
+ Y,
209
+ {
210
+ header: V,
211
+ onViewportScroll: d.flags.paginated ? T : void 0,
212
+ children: b ? /* @__PURE__ */ r("div", { className: "px-2 py-1.5 text-sm text-muted-foreground", children: "Loading..." }) : C.length === 0 ? /* @__PURE__ */ r("div", { className: "px-2 py-1.5 text-sm text-muted-foreground", children: "No options available" }) : /* @__PURE__ */ m(D, { children: [
213
+ (() => {
214
+ const l = C.reduce(
215
+ (u, v) => {
216
+ const h = v.group || "default";
217
+ return u[h] || (u[h] = []), u[h].push(v), u;
218
+ },
219
+ {}
220
+ );
221
+ return Object.entries(l).map(([u, v]) => /* @__PURE__ */ m(J, { children: [
222
+ u !== "default" && /* @__PURE__ */ r(Z, { children: u }),
223
+ v.map((h) => /* @__PURE__ */ r($, { value: h.value, children: h.label }, h.id))
224
+ ] }, u));
225
+ })(),
226
+ d.loadingMore && /* @__PURE__ */ r("div", { className: "px-2 py-1.5 text-center text-xs text-muted-foreground", children: "Loading more..." })
227
+ ] })
228
+ }
229
+ )
87
230
  ]
88
231
  }
89
232
  ),
90
- w && /* @__PURE__ */ e(
233
+ N && /* @__PURE__ */ r(
91
234
  "button",
92
235
  {
93
236
  type: "button",
94
237
  className: "absolute right-8 top-1/2 transform -translate-y-1/2 text-gray-400 hover:text-gray-600 transition-colors bg-transparent border-none p-0 cursor-pointer focus:outline-none z-10",
95
- onClick: L,
238
+ onClick: B,
96
239
  "aria-label": "Clear selection",
97
240
  tabIndex: -1,
98
- children: /* @__PURE__ */ e(V, { className: "size-4" })
241
+ children: /* @__PURE__ */ r(E, { className: "size-4" })
99
242
  }
100
243
  )
101
244
  ] });
102
245
  }
103
246
  );
104
- D.displayName = "Select";
105
- function P({ ...t }) {
106
- return /* @__PURE__ */ e(s.Group, { "data-slot": "select-group", ...t });
107
- }
108
- function T({ ...t }) {
109
- return /* @__PURE__ */ e(s.Value, { "data-slot": "select-value", ...t });
110
- }
111
- function U({
112
- className: t,
113
- size: a = "default",
114
- children: r,
115
- "aria-label": c,
116
- ...u
247
+ X.displayName = "Select";
248
+ function J({ ...e }) {
249
+ return /* @__PURE__ */ r(s.Group, { "data-slot": "select-group", ...e });
250
+ }
251
+ function Q({ ...e }) {
252
+ return /* @__PURE__ */ r(s.Value, { "data-slot": "select-value", ...e });
253
+ }
254
+ function W({
255
+ className: e,
256
+ size: t = "default",
257
+ children: a,
258
+ "aria-label": n,
259
+ ...o
117
260
  }) {
118
- return /* @__PURE__ */ d(
261
+ return /* @__PURE__ */ m(
119
262
  s.Trigger,
120
263
  {
121
264
  "data-slot": "select-trigger",
122
- "data-size": a,
123
- "aria-label": c || "Select an option",
124
- className: o(
265
+ "data-size": t,
266
+ "aria-label": n || "Select an option",
267
+ className: g(
125
268
  "h-9 w-full rounded-md border border-gray-300 px-3 py-2 text-sm shadow-xs transition-colors",
126
269
  "!bg-white !text-black",
127
270
  "placeholder:text-muted-foreground data-[placeholder]:text-gray-500",
@@ -130,112 +273,122 @@ function U({
130
273
  "aria-invalid:border-destructive aria-invalid:ring-destructive/20",
131
274
  "flex items-center justify-between gap-2",
132
275
  "[&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 [&_svg]:text-gray-500",
133
- t
276
+ e
134
277
  ),
135
278
  style: { backgroundColor: "white", color: "black" },
136
- ...u,
279
+ ...o,
137
280
  children: [
138
- r,
139
- /* @__PURE__ */ e(s.Icon, { asChild: !0, children: /* @__PURE__ */ e(S, { className: "size-4 opacity-50" }) })
281
+ a,
282
+ /* @__PURE__ */ r(s.Icon, { asChild: !0, children: /* @__PURE__ */ r(I, { className: "size-4 opacity-50" }) })
140
283
  ]
141
284
  }
142
285
  );
143
286
  }
144
- function G({
145
- className: t,
146
- children: a,
147
- position: r = "popper",
287
+ function Y({
288
+ className: e,
289
+ children: t,
290
+ position: a = "popper",
291
+ header: n,
292
+ onViewportScroll: o,
148
293
  ...c
149
294
  }) {
150
- return /* @__PURE__ */ e(s.Portal, { children: /* @__PURE__ */ d(
295
+ return /* @__PURE__ */ r(s.Portal, { children: /* @__PURE__ */ m(
151
296
  s.Content,
152
297
  {
153
298
  "data-slot": "select-content",
154
- className: o(
299
+ className: g(
155
300
  "relative z-50 max-h-96 min-w-[8rem] overflow-hidden rounded-md border bg-white text-black shadow-md",
156
301
  "data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95",
157
302
  "data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
158
- r === "popper" && "data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1",
159
- t
303
+ a === "popper" && "data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1",
304
+ e
160
305
  ),
161
- style: r === "popper" ? { width: "var(--radix-select-trigger-width)" } : void 0,
162
- position: r,
306
+ style: a === "popper" ? { width: "var(--radix-select-trigger-width)" } : void 0,
307
+ position: a,
163
308
  ...c,
164
309
  children: [
165
- /* @__PURE__ */ e(X, {}),
166
- /* @__PURE__ */ e(s.Viewport, { className: o("p-1", r === "popper" && "w-full"), children: a }),
167
- /* @__PURE__ */ e(q, {})
310
+ n,
311
+ /* @__PURE__ */ r(ee, {}),
312
+ /* @__PURE__ */ r(
313
+ s.Viewport,
314
+ {
315
+ className: g("p-1", a === "popper" && "w-full"),
316
+ onScroll: o,
317
+ children: t
318
+ }
319
+ ),
320
+ /* @__PURE__ */ r(te, {})
168
321
  ]
169
322
  }
170
323
  ) });
171
324
  }
172
- function E({ className: t, ...a }) {
173
- return /* @__PURE__ */ e(
325
+ function Z({ className: e, ...t }) {
326
+ return /* @__PURE__ */ r(
174
327
  s.Label,
175
328
  {
176
329
  "data-slot": "select-label",
177
- className: o("text-muted-foreground px-2 py-1.5 text-xs", t),
178
- ...a
330
+ className: g("text-muted-foreground px-2 py-1.5 text-xs", e),
331
+ ...t
179
332
  }
180
333
  );
181
334
  }
182
- function M({
183
- className: t,
184
- children: a,
185
- ...r
335
+ function $({
336
+ className: e,
337
+ children: t,
338
+ ...a
186
339
  }) {
187
- return /* @__PURE__ */ d(
340
+ return /* @__PURE__ */ m(
188
341
  s.Item,
189
342
  {
190
343
  "data-slot": "select-item",
191
- className: o(
344
+ className: g(
192
345
  "focus:bg-accent focus:text-accent-foreground [&_svg:not([class*='text-'])]:text-muted-foreground relative flex w-full cursor-default items-center gap-2 rounded-sm py-1.5 pr-8 pl-3 text-sm outline-hidden select-none data-[disabled]:pointer-events-none data-[disabled]:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 *:[span]:last:flex *:[span]:last:items-center *:[span]:last:gap-2",
193
- t
346
+ e
194
347
  ),
195
- ...r,
348
+ ...a,
196
349
  children: [
197
- /* @__PURE__ */ e("span", { className: "absolute right-2 flex size-3.5 items-center justify-center", children: /* @__PURE__ */ e(s.ItemIndicator, { children: /* @__PURE__ */ e(O, { className: "size-4" }) }) }),
198
- /* @__PURE__ */ e(s.ItemText, { children: a })
350
+ /* @__PURE__ */ r("span", { className: "absolute right-2 flex size-3.5 items-center justify-center", children: /* @__PURE__ */ r(s.ItemIndicator, { children: /* @__PURE__ */ r(P, { className: "size-4" }) }) }),
351
+ /* @__PURE__ */ r(s.ItemText, { children: t })
199
352
  ]
200
353
  }
201
354
  );
202
355
  }
203
- function X({
204
- className: t,
205
- ...a
356
+ function ee({
357
+ className: e,
358
+ ...t
206
359
  }) {
207
- return /* @__PURE__ */ e(
360
+ return /* @__PURE__ */ r(
208
361
  s.ScrollUpButton,
209
362
  {
210
363
  "data-slot": "select-scroll-up-button",
211
- className: o("flex cursor-default items-center justify-center py-1", t),
212
- ...a,
213
- children: /* @__PURE__ */ e(R, { className: "size-4" })
364
+ className: g("flex cursor-default items-center justify-center py-1", e),
365
+ ...t,
366
+ children: /* @__PURE__ */ r(H, { className: "size-4" })
214
367
  }
215
368
  );
216
369
  }
217
- function q({
218
- className: t,
219
- ...a
370
+ function te({
371
+ className: e,
372
+ ...t
220
373
  }) {
221
- return /* @__PURE__ */ e(
374
+ return /* @__PURE__ */ r(
222
375
  s.ScrollDownButton,
223
376
  {
224
377
  "data-slot": "select-scroll-down-button",
225
- className: o("flex cursor-default items-center justify-center py-1", t),
226
- ...a,
227
- children: /* @__PURE__ */ e(S, { className: "size-4" })
378
+ className: g("flex cursor-default items-center justify-center py-1", e),
379
+ ...t,
380
+ children: /* @__PURE__ */ r(I, { className: "size-4" })
228
381
  }
229
382
  );
230
383
  }
231
384
  export {
232
- D as Select,
233
- G as SelectContent,
234
- P as SelectGroup,
235
- M as SelectItem,
236
- E as SelectLabel,
237
- q as SelectScrollDownButton,
238
- X as SelectScrollUpButton,
239
- U as SelectTrigger,
240
- T as SelectValue
385
+ X as Select,
386
+ Y as SelectContent,
387
+ J as SelectGroup,
388
+ $ as SelectItem,
389
+ Z as SelectLabel,
390
+ te as SelectScrollDownButton,
391
+ ee as SelectScrollUpButton,
392
+ W as SelectTrigger,
393
+ Q as SelectValue
241
394
  };