@hkyhy/marsun-components-core 0.1.26 → 0.1.28

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.
@@ -15,6 +15,12 @@ export interface FilterTreeSelectProps extends BaseFilterProps {
15
15
  onChange?: (value: string | string[] | undefined) => void;
16
16
  showSearch?: boolean;
17
17
  multiple?: boolean;
18
+ /**
19
+ * 仅叶子写入最终值。
20
+ * - 单选:点父节点只展开
21
+ * - 多选:点父节点 / 勾选框 = 全选或取消其下全部叶子;父勾选框支持半选
22
+ */
23
+ leafOnly?: boolean;
18
24
  /** 自定义节点展示标签(默认 name) */
19
25
  getNodeLabel?: (node: TreeFilterNode) => string;
20
26
  }
@@ -1,249 +1,319 @@
1
- import { jsxs as F, jsx as m } from "react/jsx-runtime";
2
- import { ChevronDown as ie, ChevronRight as ne, Check as le, X as oe } from "../../Icons/index.js";
3
- import { useFetchData as fe } from "../../../hooks/useFetchData.js";
4
- import { Input as ae, Checkbox as de, Space as he } from "antd";
5
- import V from "classnames";
6
- import { useState as R, useEffect as X, useMemo as S, useCallback as j } from "react";
7
- import pe from "../FilterPopover/index.js";
8
- import { resolveFilterVisible as Z } from "../types.js";
9
- import { useFilterRegister as ue } from "../useFilterState.js";
10
- import u from "./style.module.scss.js";
11
- import { useMarsunFetch as me } from "../../../provider/context.js";
12
- const { Search: ge } = ae;
13
- function _(d, i = 0, g = null) {
14
- var w;
15
- const f = [];
16
- for (const a of d)
17
- f.push({ id: a.id, name: a.name, depth: i, parentId: g }), (w = a.children) != null && w.length && f.push(..._(a.children, i + 1, a.id));
18
- return f;
1
+ import { jsxs as D, jsx as x } from "react/jsx-runtime";
2
+ import { ChevronDown as oe, ChevronRight as ae, Check as fe, X as de } from "../../Icons/index.js";
3
+ import { useFetchData as he } from "../../../hooks/useFetchData.js";
4
+ import { Input as pe, Checkbox as ue, Space as me } from "antd";
5
+ import Y from "classnames";
6
+ import { useState as V, useEffect as Z, useMemo as L, useCallback as C } from "react";
7
+ import ge from "../FilterPopover/index.js";
8
+ import { resolveFilterVisible as O } from "../types.js";
9
+ import { useFilterRegister as xe } from "../useFilterState.js";
10
+ import g from "./style.module.scss.js";
11
+ import { useMarsunFetch as ve } from "../../../provider/context.js";
12
+ const { Search: we } = pe;
13
+ function ee(u, f = 0, v = null) {
14
+ var N;
15
+ const h = [];
16
+ for (const p of u)
17
+ h.push({ id: p.id, name: p.name, depth: f, parentId: v }), (N = p.children) != null && N.length && h.push(...ee(p.children, f + 1, p.id));
18
+ return h;
19
19
  }
20
- function xe(d, i) {
21
- if (!i) return d;
22
- const g = i.toLowerCase();
23
- function f(w) {
24
- var L;
25
- const a = [];
26
- for (const n of w) {
27
- const r = (L = n.children) != null && L.length ? f(n.children) : [];
28
- (n.name.toLowerCase().includes(g) || r.length > 0) && a.push({ ...n, children: r.length > 0 ? r : void 0 });
20
+ function Me(u, f) {
21
+ if (!f) return u;
22
+ const v = f.toLowerCase();
23
+ function h(N) {
24
+ var P;
25
+ const p = [];
26
+ for (const d of N) {
27
+ const l = (P = d.children) != null && P.length ? h(d.children) : [];
28
+ (d.name.toLowerCase().includes(v) || l.length > 0) && p.push({ ...d, children: l.length > 0 ? l : void 0 });
29
29
  }
30
- return a;
30
+ return p;
31
31
  }
32
- return f(d);
32
+ return h(u);
33
33
  }
34
- function C(d) {
35
- var g;
36
- const i = [];
37
- for (const f of d)
38
- (g = f.children) != null && g.length && (i.push(f.id), i.push(...C(f.children)));
39
- return i;
34
+ function te(u) {
35
+ var v;
36
+ const f = [];
37
+ for (const h of u)
38
+ (v = h.children) != null && v.length && (f.push(h.id), f.push(...te(h.children)));
39
+ return f;
40
40
  }
41
- function Me(d, i) {
42
- const g = /* @__PURE__ */ new Map(), f = (w) => {
43
- for (const a of w)
44
- g.set(a.id, i(a)), a.children && f(a.children);
41
+ function Se(u, f) {
42
+ const v = /* @__PURE__ */ new Map(), h = (N) => {
43
+ for (const p of N)
44
+ v.set(p.id, f(p)), p.children && h(p.children);
45
45
  };
46
- return f(d), g;
46
+ return h(u), v;
47
47
  }
48
- const ze = ({
49
- filterKey: d,
50
- label: i,
51
- treeData: g,
52
- fetchUrl: f,
53
- fetchOptions: w,
54
- transformData: a,
55
- enabled: L = !0,
56
- value: n,
57
- onChange: r,
58
- showSearch: b = !1,
59
- multiple: l = !1,
60
- active: I,
61
- hidden: D,
62
- display: P,
63
- getNodeLabel: $ = (T) => T.name
48
+ const Re = ({
49
+ filterKey: u,
50
+ label: f,
51
+ treeData: v,
52
+ fetchUrl: h,
53
+ fetchOptions: N,
54
+ transformData: p,
55
+ enabled: P = !0,
56
+ value: d,
57
+ onChange: l,
58
+ showSearch: E = !1,
59
+ multiple: o = !1,
60
+ leafOnly: k = !1,
61
+ active: se,
62
+ hidden: U,
63
+ display: B,
64
+ getNodeLabel: _ = (z) => z.name
64
65
  }) => {
65
- const T = me(), { data: K } = fe({
66
- data: g,
67
- fetchUrl: f,
68
- fetchOptions: w,
69
- transformData: a,
70
- enabled: L,
71
- baseUrl: T.baseUrl,
72
- defaultHeaders: T.headers,
73
- timeoutMs: T.timeoutMs
74
- }), x = K ?? [], [ee, y] = R(!1), [h, M] = R(
75
- n ?? (l ? [] : void 0)
76
- ), [O, q] = R(/* @__PURE__ */ new Set()), [N, U] = R(""), E = ue(), A = { filterKey: d, label: i, value: n };
77
- X(() => {
78
- M(n ?? (l ? [] : void 0)), n == null && U("");
79
- }, [n, l]), X(() => {
80
- x.length && q(new Set(C(x)));
81
- }, [x]);
82
- const z = S(
83
- () => Me(x, $),
84
- [x, $]
85
- ), B = S(() => {
86
- if (!x.length || n == null) return "";
87
- if (l) {
88
- const e = n;
89
- return e.length > 0 ? e.map((t) => z.get(t) ?? t).join("、") : "";
66
+ const z = ve(), { data: re } = he({
67
+ data: v,
68
+ fetchUrl: h,
69
+ fetchOptions: N,
70
+ transformData: p,
71
+ enabled: P,
72
+ baseUrl: z.baseUrl,
73
+ defaultHeaders: z.headers,
74
+ timeoutMs: z.timeoutMs
75
+ }), M = re ?? [], [ce, R] = V(!1), [m, w] = V(
76
+ d ?? (o ? [] : void 0)
77
+ ), [H, y] = V(/* @__PURE__ */ new Set()), [T, X] = V(""), A = xe(), $ = { filterKey: u, label: f, value: d };
78
+ Z(() => {
79
+ w(d ?? (o ? [] : void 0)), d == null && X("");
80
+ }, [d, o]), Z(() => {
81
+ M.length && y(new Set(te(M)));
82
+ }, [M]);
83
+ const j = L(
84
+ () => Se(M, _),
85
+ [M, _]
86
+ ), q = L(() => {
87
+ if (!M.length || d == null) return "";
88
+ if (o) {
89
+ const e = d;
90
+ return e.length > 0 ? e.map((t) => j.get(t) ?? t).join("、") : "";
90
91
  }
91
- return z.get(n) ?? "";
92
- }, [x, n, l, z]);
93
- X(() => {
94
- if (E) {
95
- if (!Z({ display: P, hidden: D }, A)) {
96
- E.unregister(d);
92
+ return j.get(d) ?? "";
93
+ }, [M, d, o, j]);
94
+ Z(() => {
95
+ if (A) {
96
+ if (!O({ display: B, hidden: U }, $)) {
97
+ A.unregister(u);
97
98
  return;
98
99
  }
99
- B ? E.register(d, {
100
- label: i,
101
- valueLabel: B,
102
- onRemove: () => r == null ? void 0 : r(l ? [] : void 0)
103
- }) : E.unregister(d);
100
+ q ? A.register(u, {
101
+ label: f,
102
+ valueLabel: q,
103
+ onRemove: () => l == null ? void 0 : l(o ? [] : void 0)
104
+ }) : A.unregister(u);
104
105
  }
105
- }, [B, d, i, P, D, E, r, l, A]);
106
- const k = S(
107
- () => b && N ? xe(x, N) : x,
108
- [b, N, x]
109
- ), G = S(() => _(k), [k]), te = S(() => {
110
- const e = /* @__PURE__ */ new Map(), t = (o) => {
111
- var c;
112
- for (const s of o)
113
- e.set(s.id, !!((c = s.children) != null && c.length)), s.children && t(s.children);
106
+ }, [
107
+ q,
108
+ u,
109
+ f,
110
+ B,
111
+ U,
112
+ A,
113
+ l,
114
+ o,
115
+ $
116
+ ]);
117
+ const S = L(
118
+ () => E && T ? Me(M, T) : M,
119
+ [E, T, M]
120
+ ), K = L(() => ee(S), [S]), F = L(() => {
121
+ const e = /* @__PURE__ */ new Map(), t = (a) => {
122
+ var r;
123
+ for (const s of a)
124
+ e.set(s.id, !!((r = s.children) != null && r.length)), s.children && t(s.children);
114
125
  };
115
- return t(k), e;
116
- }, [k]), H = S(() => {
117
- const e = /* @__PURE__ */ new Map(), t = (o) => {
118
- var c;
119
- for (const s of o)
120
- if ((c = s.children) != null && c.length) {
121
- const p = [], v = (W) => {
122
- var Y;
123
- p.push(W.id), (Y = W.children) == null || Y.forEach(v);
126
+ return t(S), e;
127
+ }, [S]), G = L(() => {
128
+ const e = /* @__PURE__ */ new Map(), t = (a) => {
129
+ var r;
130
+ for (const s of a)
131
+ if ((r = s.children) != null && r.length) {
132
+ const c = [], i = (n) => {
133
+ var b;
134
+ c.push(n.id), (b = n.children) == null || b.forEach(i);
124
135
  };
125
- s.children.forEach(v), e.set(s.id, p), t(s.children);
136
+ s.children.forEach(i), e.set(s.id, c), t(s.children);
126
137
  }
127
138
  };
128
- return t(k), e;
129
- }, [k]), J = S(() => {
130
- const e = /* @__PURE__ */ new Map(), t = (o) => {
131
- var c;
132
- for (const s of o)
133
- if ((c = s.children) != null && c.length) {
134
- for (const p of s.children) e.set(p.id, s.id);
139
+ return t(S), e;
140
+ }, [S]), I = L(() => {
141
+ const e = /* @__PURE__ */ new Map(), t = (a) => {
142
+ var r;
143
+ for (const s of a)
144
+ if ((r = s.children) != null && r.length) {
145
+ for (const c of s.children) e.set(c.id, s.id);
135
146
  t(s.children);
136
147
  }
137
148
  };
138
- return t(k), e;
139
- }, [k]), se = j(
149
+ return t(S), e;
150
+ }, [S]), J = L(() => {
151
+ const e = /* @__PURE__ */ new Map(), t = (a) => {
152
+ var r;
153
+ for (const s of a) {
154
+ if (!((r = s.children) != null && r.length)) continue;
155
+ const c = [], i = (n) => {
156
+ var b;
157
+ if (!((b = n.children) != null && b.length)) {
158
+ c.push(n.id);
159
+ return;
160
+ }
161
+ n.children.forEach(i);
162
+ };
163
+ s.children.forEach(i), e.set(s.id, c), t(s.children);
164
+ }
165
+ };
166
+ return t(S), e;
167
+ }, [S]), Q = C((e) => {
168
+ y((t) => {
169
+ const a = new Set(t);
170
+ return a.has(e) ? a.delete(e) : a.add(e), a;
171
+ });
172
+ }, []), ne = C(
140
173
  (e) => {
141
- const t = h === e ? void 0 : e;
142
- M(t), r == null || r(t), y(!1);
174
+ if (k && F.get(e)) {
175
+ Q(e);
176
+ return;
177
+ }
178
+ const t = m === e ? void 0 : e;
179
+ w(t), l == null || l(t), R(!1);
143
180
  },
144
- [h, r]
145
- ), Q = j(
181
+ [m, l, k, F, Q]
182
+ ), W = C(
146
183
  (e) => {
147
- const t = (h || []).slice(), o = H.get(e), c = J.get(e);
148
- if (o)
184
+ const t = (m || []).slice(), a = G.get(e), r = J.get(e), s = I.get(e);
185
+ if (k && (r != null && r.length)) {
186
+ if (r.every((i) => t.includes(i))) {
187
+ const i = new Set(r);
188
+ w(t.filter((n) => !i.has(n)));
189
+ } else
190
+ w([.../* @__PURE__ */ new Set([...t, ...r])]);
191
+ return;
192
+ }
193
+ if (a)
149
194
  if (t.includes(e)) {
150
- const p = /* @__PURE__ */ new Set([e, ...o]);
151
- M(t.filter((v) => !p.has(v)));
195
+ const i = /* @__PURE__ */ new Set([e, ...a]);
196
+ w(t.filter((n) => !i.has(n)));
152
197
  } else
153
- M([.../* @__PURE__ */ new Set([...t, e, ...o])]);
198
+ w([.../* @__PURE__ */ new Set([...t, e, ...a])]);
154
199
  else if (t.includes(e)) {
155
- const s = t.filter((p) => p !== e);
156
- M(c && s.includes(c) ? s.filter((p) => p !== c) : s);
200
+ const c = t.filter((i) => i !== e);
201
+ w(
202
+ s && c.includes(s) ? c.filter((i) => i !== s) : c
203
+ );
157
204
  } else {
158
- const s = [...t, e];
159
- c && (H.get(c) || []).every((v) => s.includes(v)) ? M([.../* @__PURE__ */ new Set([...s, c])]) : M(s);
205
+ const c = [...t, e];
206
+ s && !k && (G.get(s) || []).every((n) => c.includes(n)) ? w([.../* @__PURE__ */ new Set([...c, s])]) : w(c);
160
207
  }
161
208
  },
162
- [h, H, J]
163
- ), re = j((e) => {
164
- q((t) => {
165
- const o = new Set(t);
166
- return o.has(e) ? o.delete(e) : o.add(e), o;
167
- });
168
- }, []), ce = j(
169
- (e) => e.parentId === null || b && N ? !0 : O.has(e.parentId),
170
- [O, b, N]
209
+ [m, G, I, k, J]
210
+ ), ie = C(
211
+ (e) => e.parentId === null || E && T ? !0 : H.has(e.parentId),
212
+ [H, E, T]
171
213
  );
172
- return Z({ display: P, hidden: D }, A) ? /* @__PURE__ */ F(
173
- pe,
214
+ return O({ display: B, hidden: U }, $) ? /* @__PURE__ */ D(
215
+ ge,
174
216
  {
175
- label: i,
176
- active: I || (l ? (h == null ? void 0 : h.length) > 0 : !!n),
177
- open: ee,
178
- onOpenChange: y,
179
- onConfirm: l ? () => {
180
- r == null || r(h), y(!1);
217
+ label: f,
218
+ active: se || (o ? (m == null ? void 0 : m.length) > 0 : !!d),
219
+ open: ce,
220
+ onOpenChange: R,
221
+ onConfirm: o ? () => {
222
+ let e = m;
223
+ k && Array.isArray(e) && (e = e.filter((t) => !F.get(t))), l == null || l(e), R(!1);
181
224
  } : void 0,
182
225
  onReset: () => {
183
- const e = l ? [] : void 0;
184
- M(e), U(""), r == null || r(e), y(!1);
226
+ const e = o ? [] : void 0;
227
+ w(e), X(""), l == null || l(e), R(!1);
185
228
  },
186
229
  children: [
187
- b && /* @__PURE__ */ m(
188
- ge,
230
+ E && /* @__PURE__ */ x(
231
+ we,
189
232
  {
190
- placeholder: `搜索${i}`,
233
+ placeholder: `搜索${f}`,
191
234
  allowClear: !0,
192
235
  size: "middle",
193
- className: V("filter-tree-select-search", u["filter-tree-select-search"]),
194
- value: N,
195
- onChange: (e) => U(e.target.value)
236
+ className: Y("filter-tree-select-search", g["filter-tree-select-search"]),
237
+ value: T,
238
+ onChange: (e) => X(e.target.value)
239
+ }
240
+ ),
241
+ /* @__PURE__ */ D(
242
+ "div",
243
+ {
244
+ className: Y("filter-tree-select-options", g["filter-tree-select-options"]),
245
+ children: [
246
+ K.map((e) => {
247
+ if (!ie(e)) return null;
248
+ const t = !F.get(e.id), a = H.has(e.id), r = !o && m === e.id, s = o ? m || [] : [];
249
+ let c = o && s.includes(e.id), i = !1;
250
+ if (o && k && !t) {
251
+ const n = J.get(e.id) || [], b = n.filter((le) => s.includes(le)).length;
252
+ c = n.length > 0 && b === n.length, i = b > 0 && b < n.length;
253
+ }
254
+ return /* @__PURE__ */ D(
255
+ "div",
256
+ {
257
+ className: Y(
258
+ "filter-tree-select-option",
259
+ g["filter-tree-select-option"],
260
+ r && g["filter-tree-select-option-selected"]
261
+ ),
262
+ style: { paddingLeft: 8 + e.depth * 20 },
263
+ onClick: () => o ? W(e.id) : ne(e.id),
264
+ children: [
265
+ t ? /* @__PURE__ */ x("span", { className: g["filter-tree-select-expand-placeholder"] }) : /* @__PURE__ */ x(
266
+ "span",
267
+ {
268
+ className: g["filter-tree-select-expand-icon"],
269
+ onClick: (n) => {
270
+ n.stopPropagation(), Q(e.id);
271
+ },
272
+ children: a || E && T ? /* @__PURE__ */ x(oe, { style: { fontSize: 10 } }) : /* @__PURE__ */ x(ae, { style: { fontSize: 10 } })
273
+ }
274
+ ),
275
+ o && /* @__PURE__ */ x(
276
+ ue,
277
+ {
278
+ checked: c,
279
+ indeterminate: i,
280
+ onClick: (n) => n.stopPropagation(),
281
+ onChange: () => W(e.id)
282
+ }
283
+ ),
284
+ /* @__PURE__ */ x("span", { className: g["filter-tree-select-node-label"], children: e.name }),
285
+ !o && r && /* @__PURE__ */ x(fe, { className: g["filter-tree-select-option-check"] })
286
+ ]
287
+ },
288
+ e.id
289
+ );
290
+ }),
291
+ K.length === 0 && /* @__PURE__ */ x("div", { className: g["filter-tree-select-empty"], children: "暂无数据" })
292
+ ]
196
293
  }
197
294
  ),
198
- /* @__PURE__ */ F("div", { className: V("filter-tree-select-options", u["filter-tree-select-options"]), children: [
199
- G.map((e) => {
200
- if (!ce(e)) return null;
201
- const t = !te.get(e.id), o = O.has(e.id), c = !l && h === e.id, s = l && (h || []).includes(e.id);
202
- return /* @__PURE__ */ F(
203
- "div",
204
- {
205
- className: V(
206
- "filter-tree-select-option",
207
- u["filter-tree-select-option"],
208
- c && u["filter-tree-select-option-selected"]
209
- ),
210
- style: { paddingLeft: 8 + e.depth * 20 },
211
- onClick: () => l ? Q(e.id) : se(e.id),
212
- children: [
213
- t ? /* @__PURE__ */ m("span", { className: u["filter-tree-select-expand-placeholder"] }) : /* @__PURE__ */ m(
214
- "span",
215
- {
216
- className: u["filter-tree-select-expand-icon"],
217
- onClick: (p) => {
218
- p.stopPropagation(), re(e.id);
219
- },
220
- children: o || b && N ? /* @__PURE__ */ m(ie, { style: { fontSize: 10 } }) : /* @__PURE__ */ m(ne, { style: { fontSize: 10 } })
221
- }
222
- ),
223
- l && /* @__PURE__ */ m(de, { checked: s }),
224
- /* @__PURE__ */ m("span", { className: u["filter-tree-select-node-label"], children: e.name }),
225
- !l && c && /* @__PURE__ */ m(le, { className: u["filter-tree-select-option-check"] })
226
- ]
295
+ o && (k ? (m || []).some((e) => !F.get(e)) : (m || []).length > 0) && /* @__PURE__ */ D("div", { className: g["filter-tree-select-selected-bar"], children: [
296
+ /* @__PURE__ */ x("div", { className: g["filter-tree-select-selected-label"], children: "已选:" }),
297
+ /* @__PURE__ */ x(me, { size: [4, 4], wrap: !0, children: (m || []).filter((e) => !(k && F.get(e))).map((e) => /* @__PURE__ */ D(
298
+ "span",
299
+ {
300
+ className: g["filter-tree-select-selected-tag"],
301
+ onClick: (t) => {
302
+ t.stopPropagation(), W(e);
227
303
  },
228
- e.id
229
- );
230
- }),
231
- G.length === 0 && /* @__PURE__ */ m("div", { className: u["filter-tree-select-empty"], children: "暂无数据" })
232
- ] }),
233
- l && (h == null ? void 0 : h.length) > 0 && /* @__PURE__ */ F("div", { className: u["filter-tree-select-selected-bar"], children: [
234
- /* @__PURE__ */ m("div", { className: u["filter-tree-select-selected-label"], children: "已选:" }),
235
- /* @__PURE__ */ m(he, { size: [4, 4], wrap: !0, children: h.map((e) => /* @__PURE__ */ F("span", { className: u["filter-tree-select-selected-tag"], onClick: (t) => {
236
- t.stopPropagation(), Q(e);
237
- }, children: [
238
- z.get(e) ?? e,
239
- /* @__PURE__ */ m(oe, { className: u["filter-tree-select-selected-remove"] })
240
- ] }, e)) })
304
+ children: [
305
+ j.get(e) ?? e,
306
+ /* @__PURE__ */ x(de, { className: g["filter-tree-select-selected-remove"] })
307
+ ]
308
+ },
309
+ e
310
+ )) })
241
311
  ] })
242
312
  ]
243
313
  }
244
314
  ) : null;
245
315
  };
246
316
  export {
247
- ze as default
317
+ Re as default
248
318
  };
249
319
  //# sourceMappingURL=index.js.map