@base-stone/hooks 1.0.5 → 1.0.7

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.
@@ -72,6 +72,14 @@ declare type PickFunction<T extends noop> = (this: ThisParameterType<T>, ...args
72
72
 
73
73
  declare type Placement = 'top' | 'bottom' | 'left' | 'right';
74
74
 
75
+ declare interface QueryOptions<K extends string = string, T = Record<string, any> | Record<string, any>[] | undefined> {
76
+ queryFn: (data: QueryParamsData_2) => Promise<QueryResponse_2<T>>;
77
+ params?: Record<string, any> | undefined;
78
+ dataType: DataType;
79
+ dataKey: K;
80
+ initialValue: T;
81
+ }
82
+
75
83
  export declare interface QueryParamsData {
76
84
  pageNo: number;
77
85
  pageSize: number;
@@ -92,6 +100,13 @@ declare interface QueryResponse_2<T> {
92
100
  data: T;
93
101
  }
94
102
 
103
+ declare type QueryResult<K extends string = string, T = Record<string, any> | Record<string, any>[]> = {
104
+ loading: boolean;
105
+ refresh: () => void;
106
+ } & {
107
+ [P in K]?: T;
108
+ };
109
+
95
110
  declare interface RequestFn {
96
111
  (val?: unknown): Promise<{
97
112
  status: string;
@@ -99,20 +114,6 @@ declare interface RequestFn {
99
114
  }>;
100
115
  }
101
116
 
102
- declare interface RequestQueryOptions<K extends string = string, T = Record<string, any> | Record<string, any>[]> {
103
- queryFn: (data: QueryParamsData_2) => Promise<QueryResponse_2<T>>;
104
- params?: Record<string, any>;
105
- dataType: DataType;
106
- dataKey: K;
107
- }
108
-
109
- declare type RequestQueryResult<K extends string = string, T = Record<string, any>> = {
110
- loading: boolean;
111
- refresh: () => void;
112
- } & {
113
- [P in K]?: T;
114
- };
115
-
116
117
  declare interface SelectConfig<K extends string> {
117
118
  queryFn: (data: Record<string, any>) => Promise<QueryResponse>;
118
119
  params?: Record<string, any>;
@@ -195,7 +196,7 @@ export declare function useFormSubmit(requestFn: RequestFn, successFn: SuccessFn
195
196
 
196
197
  export declare function useMemoizedFn<T extends noop>(fn: T): PickFunction<T>;
197
198
 
198
- export declare function useRequestQuery<K extends string = string, T = Record<string, any> | Record<string, any>[]>({ queryFn, params, dataType, dataKey }: RequestQueryOptions<K, T>): RequestQueryResult<K, T>;
199
+ export declare function useRequestQuery<K extends string = string, T = Record<string, any> | Record<string, any>[] | undefined>({ queryFn, params, dataType, dataKey, initialValue }: QueryOptions<K, T>): QueryResult<K, T>;
199
200
 
200
201
  export declare function useSelectOptions<T extends string | number = string | number, K extends string = string>({ queryFn, params, dataKey, fieldNames }: SelectConfig<K>): SelectOptionsResult<K, T>;
201
202
 
@@ -1,58 +1,58 @@
1
- import { useRef as x, useMemo as q, useState as v, useEffect as D, useId as B, useCallback as U } from "react";
1
+ import { useRef as b, useMemo as L, useState as S, useEffect as z, useId as B, useCallback as U } from "react";
2
2
  import { Empty as A, App as W } from "antd";
3
3
  import { create as X } from "zustand";
4
- var P = { exports: {} }, y = {};
4
+ var D = { exports: {} }, y = {};
5
5
  var N;
6
6
  function K() {
7
7
  if (N) return y;
8
8
  N = 1;
9
- var a = /* @__PURE__ */ Symbol.for("react.transitional.element"), o = /* @__PURE__ */ Symbol.for("react.fragment");
10
- function t(e, s, n) {
9
+ var s = /* @__PURE__ */ Symbol.for("react.transitional.element"), t = /* @__PURE__ */ Symbol.for("react.fragment");
10
+ function o(e, r, n) {
11
11
  var p = null;
12
- if (n !== void 0 && (p = "" + n), s.key !== void 0 && (p = "" + s.key), "key" in s) {
12
+ if (n !== void 0 && (p = "" + n), r.key !== void 0 && (p = "" + r.key), "key" in r) {
13
13
  n = {};
14
- for (var d in s)
15
- d !== "key" && (n[d] = s[d]);
16
- } else n = s;
17
- return s = n.ref, {
18
- $$typeof: a,
14
+ for (var u in r)
15
+ u !== "key" && (n[u] = r[u]);
16
+ } else n = r;
17
+ return r = n.ref, {
18
+ $$typeof: s,
19
19
  type: e,
20
20
  key: p,
21
- ref: s !== void 0 ? s : null,
21
+ ref: r !== void 0 ? r : null,
22
22
  props: n
23
23
  };
24
24
  }
25
- return y.Fragment = o, y.jsx = t, y.jsxs = t, y;
25
+ return y.Fragment = t, y.jsx = o, y.jsxs = o, y;
26
26
  }
27
27
  var j;
28
28
  function ee() {
29
- return j || (j = 1, P.exports = K()), P.exports;
29
+ return j || (j = 1, D.exports = K()), D.exports;
30
30
  }
31
31
  var te = ee();
32
- function M(a) {
33
- const o = x(a);
34
- o.current = q(() => a, [a]);
35
- const t = x(void 0);
36
- return t.current || (t.current = function(...e) {
37
- return o.current.apply(this, e);
38
- }), t.current;
32
+ function M(s) {
33
+ const t = b(s);
34
+ t.current = L(() => s, [s]);
35
+ const o = b(void 0);
36
+ return o.current || (o.current = function(...e) {
37
+ return t.current.apply(this, e);
38
+ }), o.current;
39
39
  }
40
- const R = {
40
+ const x = {
41
41
  sortField: ["orderType", "orderField"],
42
42
  sortOrder: ["ASC", "DESC"],
43
43
  pageSize: 10
44
44
  };
45
- function ne(a) {
46
- Object.keys(a).forEach((o) => {
47
- R[o] = a[o];
45
+ function ne(s) {
46
+ Object.keys(s).forEach((t) => {
47
+ x[t] = s[t];
48
48
  });
49
49
  }
50
50
  function ie({
51
- queryFn: a,
52
- params: o,
53
- rowSelection: t
51
+ queryFn: s,
52
+ params: t,
53
+ rowSelection: o
54
54
  }) {
55
- const e = R.pageSize, [s, n] = v({
55
+ const e = x.pageSize, [r, n] = S({
56
56
  pagination: {
57
57
  showSizeChanger: !0,
58
58
  showQuickJumper: !0,
@@ -64,211 +64,218 @@ function ie({
64
64
  queryParams: {
65
65
  pageNo: 1,
66
66
  pageSize: e,
67
- ...o
67
+ ...t
68
68
  }
69
- }), { pagination: p, list: d, queryParams: l } = s, { pageNo: r, pageSize: c } = l, [g, i] = v(!0), S = x(l), [f, b] = v([]), E = q(() => {
70
- if (t)
69
+ }), { pagination: p, list: u, queryParams: c } = r, { pageNo: a, pageSize: l } = c, [f, i] = S(!0), v = b(c), [g, E] = S([]), C = L(() => {
70
+ if (o)
71
71
  return {
72
- selectedRowKeys: f,
73
- onChange: (u) => b(u)
72
+ selectedRowKeys: g,
73
+ onChange: (d) => E(d)
74
74
  };
75
- }, [t, f]), z = M(
76
- (u) => `共 ${u} 条记录 第 ${r}/${Math.ceil(u / c)} 页 `
77
- ), m = async (u) => {
78
- const { pageNo: O } = u;
75
+ }, [o, g]), $ = M(
76
+ (d) => `共 ${d} 条记录 第 ${a}/${Math.ceil(d / l)} 页 `
77
+ ), m = async (d) => {
78
+ const { pageNo: O } = d;
79
79
  i(!0);
80
- const h = { ...o, pageSize: c, ...u };
81
- u.pageNo === void 0 && (h.pageNo = 1), u.pageSize === void 0 && (h.pageSize = c);
82
- const { data: $ } = await a(h), { list: T = [], totalCount: w = 0 } = $ || {};
83
- t && b([]), n({
84
- list: T,
80
+ const h = { ...t, pageSize: l, ...d };
81
+ d.pageNo === void 0 && (h.pageNo = 1), d.pageSize === void 0 && (h.pageSize = l);
82
+ const { data: w } = await s(h), { list: P = [], totalCount: T = 0 } = w || {};
83
+ o && E([]), n({
84
+ list: P,
85
85
  queryParams: h,
86
86
  pagination: {
87
87
  ...p,
88
88
  current: O,
89
89
  pageSize: h.pageSize,
90
- total: w
90
+ total: T
91
91
  }
92
92
  }), i(!1);
93
- }, k = (u) => {
94
- m({ ...l, ...u, pageNo: 1 });
95
- }, F = (u) => {
96
- m({ ...l, ...u });
97
- }, L = (u) => {
98
- m({ ...u, pageSize: c, pageNo: 1 });
99
- }, J = (u, O, h, $) => {
100
- const { action: T } = $;
101
- if (["paginate", "sort"].includes(T)) {
102
- const { current: w, pageSize: H } = u, { field: I, order: _ } = h, [G, Q] = R.sortField, [V, Y] = R.sortOrder, Z = {
103
- ...l,
104
- [G]: _ ? _ === "ascend" ? V : Y : void 0,
105
- [Q]: I,
106
- pageNo: w,
93
+ }, k = (d) => {
94
+ m({ ...c, ...d, pageNo: 1 });
95
+ }, q = (d) => {
96
+ m({ ...c, ...d });
97
+ }, F = (d) => {
98
+ m({ ...d, pageSize: l, pageNo: 1 });
99
+ }, J = (d, O, h, w) => {
100
+ const { action: P } = w;
101
+ if (["paginate", "sort"].includes(P)) {
102
+ const { current: T, pageSize: H } = d, { field: I, order: _ } = h, [G, V] = x.sortField, [Q, Y] = x.sortOrder, Z = {
103
+ ...c,
104
+ [G]: _ ? _ === "ascend" ? Q : Y : void 0,
105
+ [V]: I,
106
+ pageNo: T,
107
107
  pageSize: H
108
108
  };
109
109
  m(Z);
110
110
  }
111
111
  };
112
- return D(() => {
113
- k(S.current);
112
+ return z(() => {
113
+ k(v.current);
114
114
  }, []), {
115
- queryParams: l,
115
+ queryParams: c,
116
116
  search: M(k),
117
- refresh: M(F),
118
- reset: M(L),
119
- selectedRowKeys: f,
117
+ refresh: M(q),
118
+ reset: M(F),
119
+ selectedRowKeys: g,
120
120
  tableProps: {
121
121
  bordered: !0,
122
122
  size: "middle",
123
123
  sticky: !0,
124
- rowSelection: E,
125
- loading: g,
126
- dataSource: d,
127
- pagination: { ...p, showTotal: z },
124
+ rowSelection: C,
125
+ loading: f,
126
+ dataSource: u,
127
+ pagination: { ...p, showTotal: $ },
128
128
  onChange: M(J),
129
129
  locale: {
130
- emptyText: g ? "" : /* @__PURE__ */ te.jsx(A, { image: A.PRESENTED_IMAGE_SIMPLE })
130
+ emptyText: f ? "" : /* @__PURE__ */ te.jsx(A, { image: A.PRESENTED_IMAGE_SIMPLE })
131
131
  }
132
132
  }
133
133
  };
134
134
  }
135
- const C = X((a) => ({
135
+ const R = X((s) => ({
136
136
  modals: {},
137
- toggleModal: (o, t) => a((e) => {
138
- const n = e.modals[o]?.visible || !1;
137
+ toggleModal: (t, o) => s((e) => {
138
+ const n = e.modals[t]?.visible || !1;
139
139
  return {
140
140
  modals: {
141
141
  ...e.modals,
142
- [o]: {
142
+ [t]: {
143
143
  visible: !n,
144
- data: n ? {} : t || {}
144
+ data: n ? {} : o || {}
145
145
  }
146
146
  }
147
147
  };
148
148
  }),
149
- setModal: (o, t, e) => a((s) => ({
149
+ setModal: (t, o, e) => s((r) => ({
150
150
  modals: {
151
- ...s.modals,
152
- [o]: {
153
- visible: t,
154
- data: t ? e || {} : {}
151
+ ...r.modals,
152
+ [t]: {
153
+ visible: o,
154
+ data: o ? e || {} : {}
155
155
  }
156
156
  }
157
157
  })),
158
- clearModals: (o) => a((t) => {
159
- const e = { ...t.modals };
160
- return o.forEach((s) => {
161
- delete e[s];
158
+ clearModals: (t) => s((o) => {
159
+ const e = { ...o.modals };
160
+ return t.forEach((r) => {
161
+ delete e[r];
162
162
  }), { modals: e };
163
163
  })
164
164
  }));
165
- function le(a) {
166
- const o = C((r) => r.modals), t = C((r) => r.toggleModal), e = C((r) => r.setModal), s = C((r) => r.clearModals), n = B(), p = x([]), d = Object.keys(a);
167
- D(() => (p.current = d.map(
168
- (r) => `${n}-${r}`
165
+ function ce(s) {
166
+ const t = R((a) => a.modals), o = R((a) => a.toggleModal), e = R((a) => a.setModal), r = R((a) => a.clearModals), n = B(), p = b([]), u = Object.keys(s);
167
+ z(() => (p.current = u.map(
168
+ (a) => `${n}-${a}`
169
169
  ), () => {
170
- s(p.current);
170
+ r(p.current);
171
171
  }), []);
172
- const l = {};
173
- return d.forEach((r) => {
174
- const c = `${n}-${r}`, g = o[c] ?? { visible: !1, data: {} }, i = a[r], S = typeof i.title == "function" ? i.title(g.data) : i.title;
175
- l[`${r}Modal`] = {
176
- visible: g.visible,
177
- data: g.data,
172
+ const c = {};
173
+ return u.forEach((a) => {
174
+ const l = `${n}-${a}`, f = t[l] ?? { visible: !1, data: {} }, i = s[a], v = typeof i.title == "function" ? i.title(f.data) : i.title;
175
+ c[`${a}Modal`] = {
176
+ visible: f.visible,
177
+ data: f.data,
178
178
  modalProps: {
179
179
  width: i.width,
180
- title: S,
181
- open: g.visible,
180
+ title: v,
181
+ open: f.visible,
182
182
  maskClosable: i.maskClosable ?? !1,
183
183
  centered: i.centered ?? !0,
184
184
  destroyOnHidden: i.destroyOnHidden ?? !0,
185
- onCancel: i.onCancel ?? (() => e(c, !1))
185
+ onCancel: i.onCancel ?? (() => e(l, !1))
186
186
  },
187
187
  drawerProps: {
188
188
  size: i.width,
189
- title: S,
190
- open: g.visible,
189
+ title: v,
190
+ open: f.visible,
191
191
  maskClosable: i.maskClosable ?? !0,
192
192
  destroyOnHidden: i.destroyOnHidden ?? !0,
193
193
  placement: i.placement ?? "right",
194
- onClose: i.onClose ?? (() => e(c, !1))
194
+ onClose: i.onClose ?? (() => e(l, !1))
195
195
  },
196
- toggle: (f) => t(c, f),
197
- open: (f) => e(c, !0, f),
198
- close: () => e(c, !1)
196
+ toggle: (g) => o(l, g),
197
+ open: (g) => e(l, !0, g),
198
+ close: () => e(l, !1)
199
199
  };
200
- }), l.toggle = (r, c) => {
201
- t(`${n}-${r}`, c);
202
- }, l.open = (r, c) => {
203
- e(`${n}-${r}`, !0, c);
204
- }, l.close = (r) => {
205
- e(`${n}-${r}`, !1);
206
- }, l;
200
+ }), c.toggle = (a, l) => {
201
+ o(`${n}-${a}`, l);
202
+ }, c.open = (a, l) => {
203
+ e(`${n}-${a}`, !0, l);
204
+ }, c.close = (a) => {
205
+ e(`${n}-${a}`, !1);
206
+ }, c;
207
207
  }
208
- const oe = (a, o = "value", t = "label") => new Map(a.map((e) => [e[o], e[t]]));
209
- function ce({ queryFn: a, params: o, dataKey: t, fieldNames: e }) {
210
- const [s, n] = v({
208
+ const oe = (s, t = "value", o = "label") => new Map(s.map((e) => [e[t], e[o]]));
209
+ function le({ queryFn: s, params: t, dataKey: o, fieldNames: e }) {
210
+ const [r, n] = S({
211
211
  list: [],
212
212
  mapData: /* @__PURE__ */ new Map()
213
- }), [p, d] = v(!0), l = async () => {
214
- d(!0);
215
- const { data: i } = await a(o), S = i || [];
213
+ }), [p, u] = S(!0), c = async () => {
214
+ u(!0);
215
+ const { data: i } = await s(t), v = i || [];
216
216
  if (e) {
217
- const { label: f, value: b } = e, E = S.map((m) => ({
217
+ const { label: g, value: E } = e, C = v.map((m) => ({
218
218
  data: m,
219
- label: m[f],
220
- value: m[b]
221
- })), z = oe(E);
222
- n((m) => ({ ...m, options: E, mapData: z }));
219
+ label: m[g],
220
+ value: m[E]
221
+ })), $ = oe(C);
222
+ n((m) => ({ ...m, options: C, mapData: $ }));
223
223
  } else
224
- n((f) => ({ ...f, options: S }));
225
- d(!1);
226
- }, r = M(void 0);
227
- D(() => {
228
- l();
224
+ n((g) => ({ ...g, options: v }));
225
+ u(!1);
226
+ }, a = M(void 0);
227
+ z(() => {
228
+ c();
229
229
  }, []);
230
- const { list: c, mapData: g } = s;
230
+ const { list: l, mapData: f } = r;
231
231
  return {
232
232
  loading: p,
233
- refresh: r,
234
- [`${t}Options`]: c,
235
- [`${t}Map`]: g
233
+ refresh: a,
234
+ [`${o}Options`]: l,
235
+ [`${o}Map`]: f
236
236
  };
237
237
  }
238
- function ue(a, o) {
239
- const [t, e] = v(!1), { message: s } = W.useApp(), n = U(async (p) => {
238
+ function ue(s, t) {
239
+ const [o, e] = S(!1), { message: r } = W.useApp(), n = U(async (p) => {
240
240
  try {
241
241
  e(!0);
242
- const d = await a(p), { status: l, info: r } = d;
243
- l == "success" && (o(d), s.success(r));
242
+ const u = await s(p), { status: c, info: a } = u;
243
+ c == "success" && (t(u), r.success(a));
244
244
  } catch {
245
245
  }
246
246
  e(!1);
247
- }, [a, o, s]);
247
+ }, [s, t, r]);
248
248
  return {
249
- loading: t,
249
+ loading: o,
250
250
  submit: n
251
251
  };
252
252
  }
253
253
  function de({
254
- queryFn: a,
255
- params: o,
256
- dataType: t,
257
- dataKey: e
254
+ queryFn: s,
255
+ params: t,
256
+ dataType: o,
257
+ dataKey: e,
258
+ initialValue: r
258
259
  }) {
259
- const [s, n] = v(t === "Array" ? [] : /* @__PURE__ */ Object.create(null)), [p, d] = v(!1);
260
- return {
261
- [e]: s,
262
- loading: p,
260
+ const n = r || (o === "Array" ? [] : /* @__PURE__ */ Object.create({})), [p, u] = S(n), [c, a] = S(!1), l = b(t), f = async (i) => {
261
+ a(!0);
262
+ const v = await s(i ?? t);
263
+ u(v.data);
264
+ };
265
+ return z(() => {
266
+ f(l.current);
267
+ }, []), {
268
+ [e]: p,
269
+ loading: c,
263
270
  refresh: M(void 0)
264
271
  };
265
272
  }
266
273
  export {
267
274
  ne as configureTableOption,
268
- le as useCreateModal,
275
+ ce as useCreateModal,
269
276
  ue as useFormSubmit,
270
277
  M as useMemoizedFn,
271
278
  de as useRequestQuery,
272
- ce as useSelectOptions,
279
+ le as useSelectOptions,
273
280
  ie as useTableList
274
281
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@base-stone/hooks",
3
3
  "license": "MIT",
4
- "version": "1.0.5",
4
+ "version": "1.0.7",
5
5
  "author": {
6
6
  "name": "leafront",
7
7
  "email": "leafront@126.com"