@base-stone/hooks 0.7.3 → 0.7.5

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,7 +15,15 @@ declare interface GlobalTableConfig {
15
15
  pageSize: number;
16
16
  }
17
17
 
18
- declare interface QueryParamsData {
18
+ declare type ModalInstance<T = any> = {
19
+ visible: boolean;
20
+ data: T;
21
+ toggle: (data?: T) => void;
22
+ open: (data?: T) => void;
23
+ close: () => void;
24
+ };
25
+
26
+ export declare interface QueryParamsData {
19
27
  pageNo: number;
20
28
  pageSize: number;
21
29
  orderType?: string;
@@ -58,6 +66,17 @@ declare interface TableResponse<T> {
58
66
  };
59
67
  }
60
68
 
69
+ /**
70
+ * 批量创建多个模态框 hook
71
+ * @param suffixes 模态框后缀数组
72
+ * @returns 包含所有模态框实例的对象,以及统一的 toggle/open/close 函数
73
+ */
74
+ export declare function useCreateModal<T extends readonly string[]>(suffixes: T): Record<`${T[number]}Modal`, ModalInstance> & {
75
+ toggle: (type: T[number], data?: any) => void;
76
+ open: (type: T[number], data?: any) => void;
77
+ close: (type: T[number]) => void;
78
+ };
79
+
61
80
  export declare function useTableList<T>(getRequestFn: (data: QueryParamsData) => Promise<TableResponse<T>>, initParams?: Record<string, any>): TableListResult<T>;
62
81
 
63
82
  export { }
@@ -1,141 +1,200 @@
1
- import { useState as z, useRef as B, useMemo as q, useCallback as d, useEffect as H } from "react";
2
- import { Empty as A } from "antd";
3
- var T = { exports: {} }, S = {};
4
- var F;
5
- function U() {
6
- if (F) return S;
7
- F = 1;
8
- var n = /* @__PURE__ */ Symbol.for("react.transitional.element"), l = /* @__PURE__ */ Symbol.for("react.fragment");
9
- function i(p, t, o) {
10
- var g = null;
11
- if (o !== void 0 && (g = "" + o), t.key !== void 0 && (g = "" + t.key), "key" in t) {
12
- o = {};
13
- for (var a in t)
14
- a !== "key" && (o[a] = t[a]);
15
- } else o = t;
16
- return t = o.ref, {
17
- $$typeof: n,
18
- type: p,
19
- key: g,
20
- ref: t !== void 0 ? t : null,
21
- props: o
1
+ import { useState as z, useRef as I, useMemo as j, useCallback as S, useEffect as J, useId as W } from "react";
2
+ import { Empty as q } from "antd";
3
+ import { create as X } from "zustand";
4
+ var T = { exports: {} }, E = {};
5
+ var A;
6
+ function K() {
7
+ if (A) return E;
8
+ A = 1;
9
+ var l = /* @__PURE__ */ Symbol.for("react.transitional.element"), i = /* @__PURE__ */ Symbol.for("react.fragment");
10
+ function s(r, o, a) {
11
+ var p = null;
12
+ if (a !== void 0 && (p = "" + a), o.key !== void 0 && (p = "" + o.key), "key" in o) {
13
+ a = {};
14
+ for (var c in o)
15
+ c !== "key" && (a[c] = o[c]);
16
+ } else a = o;
17
+ return o = a.ref, {
18
+ $$typeof: l,
19
+ type: r,
20
+ key: p,
21
+ ref: o !== void 0 ? o : null,
22
+ props: a
22
23
  };
23
24
  }
24
- return S.Fragment = l, S.jsx = i, S.jsxs = i, S;
25
+ return E.Fragment = i, E.jsx = s, E.jsxs = s, E;
25
26
  }
26
- var J;
27
- function W() {
28
- return J || (J = 1, T.exports = U()), T.exports;
27
+ var F;
28
+ function ee() {
29
+ return F || (F = 1, T.exports = K()), T.exports;
29
30
  }
30
- var X = W();
31
- const f = {
31
+ var te = ee();
32
+ const v = {
32
33
  sortField: ["orderType", "orderField"],
33
34
  sortOrder: ["ASC", "DESC"],
34
35
  pageSize: 15
35
36
  };
36
- function te(n) {
37
- Object.keys(n).forEach((l) => {
38
- f[l] = n[l];
37
+ function se(l) {
38
+ Object.keys(l).forEach((i) => {
39
+ v[i] = l[i];
39
40
  });
40
41
  }
41
- function re(n, l = {}) {
42
- const { rowSelection: i, ...p } = l, t = f.pageSize, [o, g] = z({
42
+ function ne(l, i = {}) {
43
+ const { rowSelection: s, ...r } = i, o = v.pageSize, [a, p] = z({
43
44
  pagination: {
44
45
  showSizeChanger: !0,
45
46
  showQuickJumper: !0,
46
47
  total: 0,
47
- pageSize: t,
48
+ pageSize: o,
48
49
  current: 1
49
50
  },
50
51
  list: [],
51
52
  queryParams: {
52
53
  pageNo: 1,
53
- pageSize: t,
54
- ...p
54
+ pageSize: o,
55
+ ...r
55
56
  }
56
- }), { pagination: a, list: P, queryParams: r } = o, { pageNo: h, pageSize: u } = r, [E, y] = z(!0), L = B(r), [m, C] = z([]), N = q(() => {
57
- if (i)
57
+ }), { pagination: c, list: e, queryParams: t } = a, { pageNo: g, pageSize: u } = t, [f, h] = z(!0), L = I(t), [x, P] = z([]), k = j(() => {
58
+ if (s)
58
59
  return {
59
- selectedRowKeys: m,
60
- onChange: (e) => C(e)
60
+ selectedRowKeys: x,
61
+ onChange: (n) => P(n)
61
62
  };
62
- }, [i, m]), _ = d(
63
- (e) => `共 ${e} 条记录 第 ${h}/${Math.ceil(e / u)} 页 `,
64
- [h, u]
65
- ), s = d(
66
- async (e) => {
67
- const { pageNo: w } = e;
68
- y(!0);
69
- const c = { ...p, pageSize: u, ...e };
70
- e.pageNo === void 0 && (c.pageNo = 1), e.pageSize === void 0 && (c.pageSize = u);
71
- const { data: x } = await n(c), { list: R = [], totalCount: v = 0 } = x || {};
72
- i && C([]), g({
73
- list: R,
74
- queryParams: c,
63
+ }, [s, x]), C = S(
64
+ (n) => `共 ${n} 条记录 第 ${g}/${Math.ceil(n / u)} 页 `,
65
+ [g, u]
66
+ ), d = S(
67
+ async (n) => {
68
+ const { pageNo: b } = n;
69
+ h(!0);
70
+ const m = { ...r, pageSize: u, ...n };
71
+ n.pageNo === void 0 && (m.pageNo = 1), n.pageSize === void 0 && (m.pageSize = u);
72
+ const { data: R } = await l(m), { list: y = [], totalCount: $ = 0 } = R || {};
73
+ s && P([]), p({
74
+ list: y,
75
+ queryParams: m,
75
76
  pagination: {
76
- ...a,
77
- current: w,
78
- pageSize: c.pageSize,
79
- total: v
77
+ ...c,
78
+ current: b,
79
+ pageSize: m.pageSize,
80
+ total: $
80
81
  }
81
- }), y(!1);
82
+ }), h(!1);
82
83
  },
83
- [r, p, i, u, n, a]
84
- ), M = d(
85
- (e) => {
86
- s({ ...r, ...e, pageNo: 1 });
84
+ [t, r, s, u, l, c]
85
+ ), N = S(
86
+ (n) => {
87
+ d({ ...t, ...n, pageNo: 1 });
87
88
  },
88
- [s, r]
89
- ), b = d(
90
- (e) => {
91
- s({ ...r, ...e });
89
+ [d, t]
90
+ ), D = S(
91
+ (n) => {
92
+ d({ ...t, ...n });
92
93
  },
93
- [s, r]
94
- ), I = d(
95
- (e) => {
96
- s({ ...e, pageSize: u, pageNo: 1 });
94
+ [d, t]
95
+ ), O = S(
96
+ (n) => {
97
+ d({ ...n, pageSize: u, pageNo: 1 });
97
98
  },
98
- [s, u]
99
- ), k = d(
100
- (e, w, c, x) => {
101
- const { action: R } = x;
102
- if (["paginate", "sort"].includes(R)) {
103
- const { current: v, pageSize: D } = e, { field: O, order: j } = c, [G, Q] = f.sortField, [V, Y] = f.sortOrder, Z = {
104
- ...r,
105
- [G]: j ? j === "ascend" ? V : Y : void 0,
106
- [Q]: O,
107
- pageNo: v,
108
- pageSize: D
99
+ [d, u]
100
+ ), _ = S(
101
+ (n, b, m, R) => {
102
+ const { action: y } = R;
103
+ if (["paginate", "sort"].includes(y)) {
104
+ const { current: $, pageSize: V } = n, { field: Q, order: w } = m, [Y, Z] = v.sortField, [B, H] = v.sortOrder, U = {
105
+ ...t,
106
+ [Y]: w ? w === "ascend" ? B : H : void 0,
107
+ [Z]: Q,
108
+ pageNo: $,
109
+ pageSize: V
109
110
  };
110
- s(Z);
111
+ d(U);
111
112
  }
112
113
  },
113
- [r, s]
114
- ), $ = q(() => ({
114
+ [t, d]
115
+ ), G = j(() => ({
115
116
  bordered: !0,
116
117
  size: "middle",
117
118
  sticky: !0,
118
- rowSelection: N,
119
- pagination: { ...a, showTotal: _ },
120
- loading: E,
121
- dataSource: P,
122
- onChange: k,
119
+ rowSelection: k,
120
+ pagination: { ...c, showTotal: C },
121
+ loading: f,
122
+ dataSource: e,
123
+ onChange: _,
123
124
  locale: {
124
- emptyText: E ? "" : /* @__PURE__ */ X.jsx(A, { image: A.PRESENTED_IMAGE_SIMPLE })
125
+ emptyText: f ? "" : /* @__PURE__ */ te.jsx(q, { image: q.PRESENTED_IMAGE_SIMPLE })
125
126
  }
126
- }), [N, a, _, E, P, k]);
127
- return H(() => {
128
- s(L.current);
127
+ }), [k, c, C, f, e, _]);
128
+ return J(() => {
129
+ N(L.current);
129
130
  }, []), {
130
- queryParams: r,
131
- reload: M,
132
- refresh: b,
133
- reset: I,
134
- selectedRowKeys: m,
135
- tableProps: $
131
+ queryParams: t,
132
+ reload: N,
133
+ refresh: D,
134
+ reset: O,
135
+ selectedRowKeys: x,
136
+ tableProps: G
136
137
  };
137
138
  }
139
+ const M = X((l) => ({
140
+ modals: {},
141
+ toggleModal: (i, s) => l((r) => {
142
+ const a = r.modals[i]?.visible || !1;
143
+ return {
144
+ modals: {
145
+ ...r.modals,
146
+ [i]: {
147
+ visible: !a,
148
+ data: a ? {} : s || {}
149
+ }
150
+ }
151
+ };
152
+ }),
153
+ setModal: (i, s, r) => l((o) => ({
154
+ modals: {
155
+ ...o.modals,
156
+ [i]: {
157
+ visible: s,
158
+ data: s ? r || {} : {}
159
+ }
160
+ }
161
+ })),
162
+ clearModals: (i) => l((s) => {
163
+ const r = { ...s.modals };
164
+ return i.forEach((o) => {
165
+ delete r[o];
166
+ }), { modals: r };
167
+ })
168
+ }));
169
+ function ie(l) {
170
+ const i = M((e) => e.modals), s = M((e) => e.toggleModal), r = M((e) => e.setModal), o = M((e) => e.clearModals), a = W(), p = I(l.map((e) => `${a}-${e}`));
171
+ J(() => () => {
172
+ o(p.current);
173
+ }, []);
174
+ const c = {};
175
+ for (const e of l) {
176
+ const t = `${a}-${e}`, u = i[t] ?? { visible: !1, data: {} };
177
+ c[`${e}Modal`] = {
178
+ visible: u.visible,
179
+ data: u.data,
180
+ toggle: (f) => s(t, f),
181
+ open: (f) => r(t, !0, f),
182
+ close: () => r(t, !1)
183
+ };
184
+ }
185
+ return c.toggle = (e, t) => {
186
+ const g = `${a}-${e}`;
187
+ s(g, t);
188
+ }, c.open = (e, t) => {
189
+ const g = `${a}-${e}`;
190
+ r(g, !0, t);
191
+ }, c.close = (e) => {
192
+ const t = `${a}-${e}`;
193
+ r(t, !1);
194
+ }, c;
195
+ }
138
196
  export {
139
- te as configureTableOption,
140
- re as useTableList
197
+ se as configureTableOption,
198
+ ie as useCreateModal,
199
+ ne as useTableList
141
200
  };
@@ -1 +1 @@
1
- (function(r,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("react"),require("antd")):typeof define=="function"&&define.amd?define(["exports","react","antd"],e):(r=typeof globalThis<"u"?globalThis:r||self,e(r.DesignReact={},r.React,r.antd))})(this,(function(r,e,y){"use strict";var T={exports:{}},f={};var C;function L(){if(C)return f;C=1;var u=Symbol.for("react.transitional.element"),p=Symbol.for("react.fragment");function l(g,o,s){var S=null;if(s!==void 0&&(S=""+s),o.key!==void 0&&(S=""+o.key),"key"in o){s={};for(var a in o)a!=="key"&&(s[a]=o[a])}else s=o;return o=s.ref,{$$typeof:u,type:g,key:S,ref:o!==void 0?o:null,props:s}}return f.Fragment=p,f.jsx=l,f.jsxs=l,f}var z;function A(){return z||(z=1,T.exports=L()),T.exports}var F=A();const E={sortField:["orderType","orderField"],sortOrder:["ASC","DESC"],pageSize:15};function J(u){Object.keys(u).forEach(p=>{E[p]=u[p]})}function O(u,p={}){const{rowSelection:l,...g}=p,o=E.pageSize,[s,S]=e.useState({pagination:{showSizeChanger:!0,showQuickJumper:!0,total:0,pageSize:o,current:1},list:[],queryParams:{pageNo:1,pageSize:o,...g}}),{pagination:a,list:P,queryParams:n}=s,{pageNo:k,pageSize:c}=n,[m,b]=e.useState(!0),D=e.useRef(n),[R,N]=e.useState([]),_=e.useMemo(()=>{if(l)return{selectedRowKeys:R,onChange:t=>N(t)}},[l,R]),j=e.useCallback(t=>`共 ${t} 条记录 第 ${k}/${Math.ceil(t/c)} 页 `,[k,c]),i=e.useCallback(async t=>{const{pageNo:w}=t;b(!0);const d={...g,pageSize:c,...t};t.pageNo===void 0&&(d.pageNo=1),t.pageSize===void 0&&(d.pageSize=c);const{data:x}=await u(d),{list:h=[],totalCount:v=0}=x||{};l&&N([]),S({list:h,queryParams:d,pagination:{...a,current:w,pageSize:d.pageSize,total:v}}),b(!1)},[n,g,l,c,u,a]),I=e.useCallback(t=>{i({...n,...t,pageNo:1})},[i,n]),$=e.useCallback(t=>{i({...n,...t})},[i,n]),G=e.useCallback(t=>{i({...t,pageSize:c,pageNo:1})},[i,c]),q=e.useCallback((t,w,d,x)=>{const{action:h}=x;if(["paginate","sort"].includes(h)){const{current:v,pageSize:V}=t,{field:Y,order:M}=d,[Z,B]=E.sortField,[H,U]=E.sortOrder,W={...n,[Z]:M?M==="ascend"?H:U:void 0,[B]:Y,pageNo:v,pageSize:V};i(W)}},[n,i]),Q=e.useMemo(()=>({bordered:!0,size:"middle",sticky:!0,rowSelection:_,pagination:{...a,showTotal:j},loading:m,dataSource:P,onChange:q,locale:{emptyText:m?"":F.jsx(y.Empty,{image:y.Empty.PRESENTED_IMAGE_SIMPLE})}}),[_,a,j,m,P,q]);return e.useEffect(()=>{i(D.current)},[]),{queryParams:n,reload:I,refresh:$,reset:G,selectedRowKeys:R,tableProps:Q}}r.configureTableOption=J,r.useTableList=O,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})}));
1
+ (function(c,o){typeof exports=="object"&&typeof module<"u"?o(exports,require("react"),require("antd"),require("zustand")):typeof define=="function"&&define.amd?define(["exports","react","antd","zustand"],o):(c=typeof globalThis<"u"?globalThis:c||self,o(c.DesignReact={},c.React,c.antd,c.zustand))})(this,(function(c,o,z,A){"use strict";var v={exports:{}},E={};var b;function F(){if(b)return E;b=1;var u=Symbol.for("react.transitional.element"),l=Symbol.for("react.fragment");function r(a,s,n){var g=null;if(n!==void 0&&(g=""+n),s.key!==void 0&&(g=""+s.key),"key"in s){n={};for(var d in s)d!=="key"&&(n[d]=s[d])}else n=s;return s=n.ref,{$$typeof:u,type:a,key:g,ref:s!==void 0?s:null,props:n}}return E.Fragment=l,E.jsx=r,E.jsxs=r,E}var h;function I(){return h||(h=1,v.exports=F()),v.exports}var J=I();const y={sortField:["orderType","orderField"],sortOrder:["ASC","DESC"],pageSize:15};function O(u){Object.keys(u).forEach(l=>{y[l]=u[l]})}function D(u,l={}){const{rowSelection:r,...a}=l,s=y.pageSize,[n,g]=o.useState({pagination:{showSizeChanger:!0,showQuickJumper:!0,total:0,pageSize:s,current:1},list:[],queryParams:{pageNo:1,pageSize:s,...a}}),{pagination:d,list:e,queryParams:t}=n,{pageNo:S,pageSize:p}=t,[m,$]=o.useState(!0),V=o.useRef(t),[T,P]=o.useState([]),N=o.useMemo(()=>{if(r)return{selectedRowKeys:T,onChange:i=>P(i)}},[r,T]),_=o.useCallback(i=>`共 ${i} 条记录 第 ${S}/${Math.ceil(i/p)} 页 `,[S,p]),f=o.useCallback(async i=>{const{pageNo:j}=i;$(!0);const M={...a,pageSize:p,...i};i.pageNo===void 0&&(M.pageNo=1),i.pageSize===void 0&&(M.pageSize=p);const{data:C}=await u(M),{list:k=[],totalCount:x=0}=C||{};r&&P([]),g({list:k,queryParams:M,pagination:{...d,current:j,pageSize:M.pageSize,total:x}}),$(!1)},[t,a,r,p,u,d]),q=o.useCallback(i=>{f({...t,...i,pageNo:1})},[f,t]),Q=o.useCallback(i=>{f({...t,...i})},[f,t]),Y=o.useCallback(i=>{f({...i,pageSize:p,pageNo:1})},[f,p]),w=o.useCallback((i,j,M,C)=>{const{action:k}=C;if(["paginate","sort"].includes(k)){const{current:x,pageSize:B}=i,{field:H,order:L}=M,[U,W]=y.sortField,[X,K]=y.sortOrder,ee={...t,[U]:L?L==="ascend"?X:K:void 0,[W]:H,pageNo:x,pageSize:B};f(ee)}},[t,f]),Z=o.useMemo(()=>({bordered:!0,size:"middle",sticky:!0,rowSelection:N,pagination:{...d,showTotal:_},loading:m,dataSource:e,onChange:w,locale:{emptyText:m?"":J.jsx(z.Empty,{image:z.Empty.PRESENTED_IMAGE_SIMPLE})}}),[N,d,_,m,e,w]);return o.useEffect(()=>{q(V.current)},[]),{queryParams:t,reload:q,refresh:Q,reset:Y,selectedRowKeys:T,tableProps:Z}}const R=A.create(u=>({modals:{},toggleModal:(l,r)=>u(a=>{const n=a.modals[l]?.visible||!1;return{modals:{...a.modals,[l]:{visible:!n,data:n?{}:r||{}}}}}),setModal:(l,r,a)=>u(s=>({modals:{...s.modals,[l]:{visible:r,data:r?a||{}:{}}}})),clearModals:l=>u(r=>{const a={...r.modals};return l.forEach(s=>{delete a[s]}),{modals:a}})}));function G(u){const l=R(e=>e.modals),r=R(e=>e.toggleModal),a=R(e=>e.setModal),s=R(e=>e.clearModals),n=o.useId(),g=o.useRef(u.map(e=>`${n}-${e}`));o.useEffect(()=>()=>{s(g.current)},[]);const d={};for(const e of u){const t=`${n}-${e}`,p=l[t]??{visible:!1,data:{}};d[`${e}Modal`]={visible:p.visible,data:p.data,toggle:m=>r(t,m),open:m=>a(t,!0,m),close:()=>a(t,!1)}}return d.toggle=(e,t)=>{const S=`${n}-${e}`;r(S,t)},d.open=(e,t)=>{const S=`${n}-${e}`;a(S,!0,t)},d.close=e=>{const t=`${n}-${e}`;a(t,!1)},d}c.configureTableOption=O,c.useCreateModal=G,c.useTableList=D,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})}));
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@base-stone/hooks",
3
3
  "license": "MIT",
4
- "version": "0.7.3",
4
+ "version": "0.7.5",
5
5
  "author": {
6
6
  "name": "yeliang",
7
7
  "email": "leafront@126.com"
@@ -36,9 +36,10 @@
36
36
  "lint:js": "npm run eslint && npm run prettier"
37
37
  },
38
38
  "devDependencies": {
39
+ "@eslint/js": "^9.39.2",
39
40
  "@types/node": "^25.0.3",
40
41
  "@types/react": "^19.2.7",
41
- "@eslint/js": "^9.39.2",
42
+ "@vitejs/plugin-react-swc": "^4.2.2",
42
43
  "classnames": "^2.5.1",
43
44
  "eslint": "^9.39.2",
44
45
  "eslint-plugin-prettier": "^5.5.4",
@@ -46,14 +47,14 @@
46
47
  "eslint-plugin-react-refresh": "^0.4.26",
47
48
  "prettier": "^3.7.4",
48
49
  "rollup-plugin-shell": "^1.0.9",
49
- "@vitejs/plugin-react-swc": "^4.2.2",
50
50
  "typescript": "^5.9.3",
51
+ "typescript-eslint": "^8.50.0",
51
52
  "vite": "^7.3.0",
52
- "vite-plugin-dts": "^4.5.4",
53
- "typescript-eslint": "^8.50.0"
53
+ "vite-plugin-dts": "^4.5.4"
54
54
  },
55
55
  "dependencies": {
56
56
  "antd": "^6.1.1",
57
- "react": "^19.2.3"
57
+ "react": "^19.2.3",
58
+ "zustand": "^5.0.9"
58
59
  }
59
60
  }