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