antd-hooks 1.0.2 → 1.0.4
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/createHooks.d.ts +2 -2
- package/dist/hooks/useDataTable.d.ts +49 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.esm.js +1 -1
- package/dist/index.js +1 -1
- package/package.json +3 -3
package/dist/createHooks.d.ts
CHANGED
|
@@ -3,5 +3,5 @@ interface ModalInstance {
|
|
|
3
3
|
destroy: () => void;
|
|
4
4
|
update: (props: any) => void;
|
|
5
5
|
}
|
|
6
|
-
declare const
|
|
7
|
-
export
|
|
6
|
+
export declare const createHooks: <T extends Record<string, any> = {}>(ModalComponent: React.ComponentType<any>) => (props?: T) => ModalInstance;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { TableProps, PaginationProps, InputProps } from 'antd';
|
|
2
|
+
interface ResponseData<T = any> {
|
|
3
|
+
list: T[];
|
|
4
|
+
total: number;
|
|
5
|
+
current?: number;
|
|
6
|
+
pageSize?: number;
|
|
7
|
+
}
|
|
8
|
+
interface FilterConfig {
|
|
9
|
+
dataIndex: string;
|
|
10
|
+
placeholder?: string;
|
|
11
|
+
debounce?: number;
|
|
12
|
+
}
|
|
13
|
+
interface FieldMapping {
|
|
14
|
+
list?: string;
|
|
15
|
+
total?: string;
|
|
16
|
+
current?: string;
|
|
17
|
+
pageSize?: string;
|
|
18
|
+
}
|
|
19
|
+
interface UseDataTableOptions<T = any> {
|
|
20
|
+
url?: string;
|
|
21
|
+
data?: T[];
|
|
22
|
+
params?: Record<string, any>;
|
|
23
|
+
columns: TableProps<T>['columns'];
|
|
24
|
+
rowKey?: string | ((record: T) => string);
|
|
25
|
+
immediate?: boolean;
|
|
26
|
+
transformData?: (data: any) => ResponseData<T>;
|
|
27
|
+
fieldMapping?: FieldMapping;
|
|
28
|
+
filter?: FilterConfig;
|
|
29
|
+
pagination?: boolean | PaginationProps;
|
|
30
|
+
request?: (params: any) => Promise<any>;
|
|
31
|
+
}
|
|
32
|
+
interface UseDataTableReturn<T = any> {
|
|
33
|
+
tableProps: TableProps<T>;
|
|
34
|
+
data: T[];
|
|
35
|
+
loading: boolean;
|
|
36
|
+
total: number;
|
|
37
|
+
paginationProps: PaginationProps;
|
|
38
|
+
current: number;
|
|
39
|
+
pageSize: number;
|
|
40
|
+
filterProps: InputProps;
|
|
41
|
+
filterValue: string;
|
|
42
|
+
refresh: () => void;
|
|
43
|
+
search: (params: Record<string, any>) => void;
|
|
44
|
+
reset: () => void;
|
|
45
|
+
setData: (data: T[]) => void;
|
|
46
|
+
setLoading: (loading: boolean) => void;
|
|
47
|
+
}
|
|
48
|
+
export declare const useDataTable: <T = any>(options: UseDataTableOptions<T>) => UseDataTableReturn<T>;
|
|
49
|
+
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export * from './createHooks';
|
|
2
|
-
export * from './hooks/
|
|
2
|
+
export * from './hooks/useDataTable';
|
package/dist/index.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import"react";import"react-dom";"function"==typeof SuppressedError&&SuppressedError;
|
|
1
|
+
import{createElement as e,useState as n,useCallback as t,useEffect as r,useMemo as o}from"react";import{createRoot as a}from"react-dom/client";var u=function(){return u=Object.assign||function(e){for(var n,t=1,r=arguments.length;t<r;t++)for(var o in n=arguments[t])Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o]);return e},u.apply(this,arguments)};function i(e,n,t,r){return new(t||(t=Promise))(function(o,a){function u(e){try{c(r.next(e))}catch(e){a(e)}}function i(e){try{c(r.throw(e))}catch(e){a(e)}}function c(e){var n;e.done?o(e.value):(n=e.value,n instanceof t?n:new t(function(e){e(n)})).then(u,i)}c((r=r.apply(e,n||[])).next())})}function c(e,n){var t,r,o,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},u=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return u.next=i(0),u.throw=i(1),u.return=i(2),"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function i(i){return function(c){return function(i){if(t)throw new TypeError("Generator is already executing.");for(;u&&(u=0,i[0]&&(a=0)),a;)try{if(t=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,r=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=n.call(e,a)}catch(e){i=[6,e],r=0}finally{t=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,c])}}}function l(e,n,t){if(t||2===arguments.length)for(var r,o=0,a=n.length;o<a;o++)!r&&o in n||(r||(r=Array.prototype.slice.call(n,0,o)),r[o]=n[o]);return e.concat(r||Array.prototype.slice.call(n))}"function"==typeof SuppressedError&&SuppressedError;var f=function(n){return function(t){var r=document.createElement("div");document.body.appendChild(r);var o=u(u({},t),{open:!0}),i=a(r),c=function(){i.unmount(),r.parentNode&&r.parentNode.removeChild(r)},f=function(t){var r=e(n,u(u({},t),{onCancel:function(){for(var e,n=[],r=0;r<arguments.length;r++)n[r]=arguments[r];null===(e=t.onCancel)||void 0===e||e.call.apply(e,l([t],n,!1)),c()},onOk:function(){for(var e,n=[],r=0;r<arguments.length;r++)n[r]=arguments[r];null===(e=t.onOk)||void 0===e||e.call.apply(e,l([t],n,!1)),c()},afterClose:c}));i.render(r)};return f(o),{destroy:c,update:function(e){o=u(u({},o),e),f(o)}}}},s=function(e,n){if(n)return n.split(".").reduce(function(e,n){return e&&e[n]},e)},p=function(e){var a=e.url,l=e.data,f=e.params,p=void 0===f?{}:f,d=e.columns,g=e.rowKey,h=void 0===g?"id":g,v=e.immediate,y=void 0===v||v,S=e.transformData,b=e.fieldMapping,z=void 0===b?{list:"list",total:"total",current:"current",pageSize:"pageSize"}:b,m=e.filter,w=e.pagination,C=void 0===w||w,x=e.request,O=n([]),T=O[0],j=O[1],k=n(!1),P=k[0],E=k[1],I=n(0),D=I[0],L=I[1],N=n({current:1,pageSize:10}),q=N[0],A=N[1],J=n(p),K=J[0],G=J[1],M=n(""),Q=M[0],V=M[1],B=n([]),F=B[0],H=B[1],R=n(null),U=R[0],W=R[1],X=!!l,Y=t(function(){return i(void 0,void 0,void 0,function(){var e,n,t,r,o,i,f,p;return c(this,function(c){switch(c.label){case 0:if(X)return j(l),L(l.length),[2];if(!a&&!x)return console.warn("useDataTable: 请提供 url 或 request 参数"),[2];E(!0),c.label=1;case 1:return c.trys.push([1,7,8,9]),e=u({current:q.current,pageSize:q.pageSize},K),n=void 0,x?[4,x(e)]:[3,3];case 2:return n=c.sent(),[3,6];case 3:return a?[4,fetch(a,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)})]:[3,6];case 4:return[4,c.sent().json()];case 5:n=c.sent(),c.label=6;case 6:return S?t=S(n):(r=s(n,z.list)||[],o=s(n,z.total)||0,i=s(n,z.current),f=s(n,z.pageSize),t={list:r,total:o,current:i,pageSize:f}),j(t.list||[]),L(t.total||0),t.current&&A(function(e){return u(u({},e),{current:t.current})}),t.pageSize&&A(function(e){return u(u({},e),{pageSize:t.pageSize})}),[3,9];case 7:return p=c.sent(),console.error("表格数据请求失败:",p),j([]),L(0),[3,9];case 8:return E(!1),[7];case 9:return[2]}})})},[a,l,X,q.current,q.pageSize,K,S,z.list,z.total,z.current,z.pageSize,x]);r(function(){if(Q&&m){var e=T.filter(function(e){var n=s(e,m.dataIndex);return String(n||"").toLowerCase().includes(Q.toLowerCase())});H(e)}else H(T)},[T,Q,m]);var Z=t(function(e){V(e),U&&clearTimeout(U);var n=setTimeout(function(){X||A(function(e){return u(u({},e),{current:1})})},(null==m?void 0:m.debounce)||300);W(n)},[U,null==m?void 0:m.debounce,X]);r(function(){return function(){U&&clearTimeout(U)}},[U]),r(function(){y&&!X?Y():X&&(j(l),L(l.length))},[Y,y,X,l]);var $=t(function(){X?(j(l),L(l.length)):Y()},[Y,X,l]),_=t(function(e){G(function(n){return u(u({},n),e)}),X||A(function(e){return u(u({},e),{current:1})})},[X]),ee=t(function(){G(p),V(""),X||A({current:1,pageSize:10})},[p,X]),ne=t(function(e,n){A(function(t){return{current:e,pageSize:n||t.pageSize}})},[]),te=o(function(){return m?F:T},[m,F,T]),re=o(function(){if(!1===C||X)return te;var e=(q.current-1)*q.pageSize,n=e+q.pageSize;return te.slice(e,n)},[te,q.current,q.pageSize,C,X]),oe=o(function(){return X?m?F.length:T.length:m?F.length:D},[X,m,F.length,T.length,D]),ae=o(function(){return{columns:d,dataSource:re,loading:P,rowKey:h,pagination:!1,scroll:{x:800}}},[d,re,P,h]),ue=o(function(){if(!1===C)return{};var e={current:q.current,pageSize:q.pageSize,total:oe,showSizeChanger:!0,showQuickJumper:!0,showTotal:function(e,n){return"第 ".concat(n[0],"-").concat(n[1]," 条,共 ").concat(e," 条")},onChange:ne,onShowSizeChange:ne};return"object"==typeof C?u(u({},e),C):e},[q.current,q.pageSize,oe,ne,C]),ie=o(function(){return{value:Q,onChange:function(e){return Z(e.target.value)},placeholder:(null==m?void 0:m.placeholder)||"搜索".concat(null==m?void 0:m.dataIndex),allowClear:!0,style:{width:250}}},[Q,Z,m]);return{tableProps:ae,data:re,loading:P,total:oe,paginationProps:ue,current:q.current,pageSize:q.pageSize,filterProps:ie,filterValue:Q,refresh:$,search:_,reset:ee,setData:j,setLoading:E}};export{f as createHooks,p as useDataTable};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";require("react"),require("react-dom"),"function"==typeof SuppressedError&&SuppressedError;
|
|
1
|
+
"use strict";var e=require("react"),t=require("react-dom/client"),n=function(){return n=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},n.apply(this,arguments)};function r(e,t,n,r){return new(n||(n=Promise))(function(o,a){function u(e){try{c(r.next(e))}catch(e){a(e)}}function i(e){try{c(r.throw(e))}catch(e){a(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(u,i)}c((r=r.apply(e,t||[])).next())})}function o(e,t){var n,r,o,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},u=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return u.next=i(0),u.throw=i(1),u.return=i(2),"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function i(i){return function(c){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;u&&(u=0,i[0]&&(a=0)),a;)try{if(n=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,r=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=t.call(e,a)}catch(e){i=[6,e],r=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,c])}}}function a(e,t,n){if(n||2===arguments.length)for(var r,o=0,a=t.length;o<a;o++)!r&&o in t||(r||(r=Array.prototype.slice.call(t,0,o)),r[o]=t[o]);return e.concat(r||Array.prototype.slice.call(t))}"function"==typeof SuppressedError&&SuppressedError;var u=function(e,t){if(t)return t.split(".").reduce(function(e,t){return e&&e[t]},e)};exports.createHooks=function(r){return function(o){var u=document.createElement("div");document.body.appendChild(u);var i=n(n({},o),{open:!0}),c=t.createRoot(u),l=function(){c.unmount(),u.parentNode&&u.parentNode.removeChild(u)},s=function(t){var o=e.createElement(r,n(n({},t),{onCancel:function(){for(var e,n=[],r=0;r<arguments.length;r++)n[r]=arguments[r];null===(e=t.onCancel)||void 0===e||e.call.apply(e,a([t],n,!1)),l()},onOk:function(){for(var e,n=[],r=0;r<arguments.length;r++)n[r]=arguments[r];null===(e=t.onOk)||void 0===e||e.call.apply(e,a([t],n,!1)),l()},afterClose:l}));c.render(o)};return s(i),{destroy:l,update:function(e){i=n(n({},i),e),s(i)}}}},exports.useDataTable=function(t){var a=t.url,i=t.data,c=t.params,l=void 0===c?{}:c,s=t.columns,f=t.rowKey,p=void 0===f?"id":f,d=t.immediate,g=void 0===d||d,h=t.transformData,v=t.fieldMapping,S=void 0===v?{list:"list",total:"total",current:"current",pageSize:"pageSize"}:v,y=t.filter,b=t.pagination,m=void 0===b||b,z=t.request,w=e.useState([]),C=w[0],k=w[1],x=e.useState(!1),O=x[0],T=x[1],E=e.useState(0),j=E[0],M=E[1],P=e.useState({current:1,pageSize:10}),q=P[0],D=P[1],I=e.useState(l),L=I[0],N=I[1],A=e.useState(""),J=A[0],K=A[1],G=e.useState([]),H=G[0],Q=G[1],R=e.useState(null),V=R[0],B=R[1],F=!!i,U=e.useCallback(function(){return r(void 0,void 0,void 0,function(){var e,t,r,c,l,s,f,p;return o(this,function(o){switch(o.label){case 0:if(F)return k(i),M(i.length),[2];if(!a&&!z)return console.warn("useDataTable: 请提供 url 或 request 参数"),[2];T(!0),o.label=1;case 1:return o.trys.push([1,7,8,9]),e=n({current:q.current,pageSize:q.pageSize},L),t=void 0,z?[4,z(e)]:[3,3];case 2:return t=o.sent(),[3,6];case 3:return a?[4,fetch(a,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)})]:[3,6];case 4:return[4,o.sent().json()];case 5:t=o.sent(),o.label=6;case 6:return h?r=h(t):(c=u(t,S.list)||[],l=u(t,S.total)||0,s=u(t,S.current),f=u(t,S.pageSize),r={list:c,total:l,current:s,pageSize:f}),k(r.list||[]),M(r.total||0),r.current&&D(function(e){return n(n({},e),{current:r.current})}),r.pageSize&&D(function(e){return n(n({},e),{pageSize:r.pageSize})}),[3,9];case 7:return p=o.sent(),console.error("表格数据请求失败:",p),k([]),M(0),[3,9];case 8:return T(!1),[7];case 9:return[2]}})})},[a,i,F,q.current,q.pageSize,L,h,S.list,S.total,S.current,S.pageSize,z]);e.useEffect(function(){if(J&&y){var e=C.filter(function(e){var t=u(e,y.dataIndex);return String(t||"").toLowerCase().includes(J.toLowerCase())});Q(e)}else Q(C)},[C,J,y]);var W=e.useCallback(function(e){K(e),V&&clearTimeout(V);var t=setTimeout(function(){F||D(function(e){return n(n({},e),{current:1})})},(null==y?void 0:y.debounce)||300);B(t)},[V,null==y?void 0:y.debounce,F]);e.useEffect(function(){return function(){V&&clearTimeout(V)}},[V]),e.useEffect(function(){g&&!F?U():F&&(k(i),M(i.length))},[U,g,F,i]);var X=e.useCallback(function(){F?(k(i),M(i.length)):U()},[U,F,i]),Y=e.useCallback(function(e){N(function(t){return n(n({},t),e)}),F||D(function(e){return n(n({},e),{current:1})})},[F]),Z=e.useCallback(function(){N(l),K(""),F||D({current:1,pageSize:10})},[l,F]),$=e.useCallback(function(e,t){D(function(n){return{current:e,pageSize:t||n.pageSize}})},[]),_=e.useMemo(function(){return y?H:C},[y,H,C]),ee=e.useMemo(function(){if(!1===m||F)return _;var e=(q.current-1)*q.pageSize,t=e+q.pageSize;return _.slice(e,t)},[_,q.current,q.pageSize,m,F]),te=e.useMemo(function(){return F?y?H.length:C.length:y?H.length:j},[F,y,H.length,C.length,j]),ne=e.useMemo(function(){return{columns:s,dataSource:ee,loading:O,rowKey:p,pagination:!1,scroll:{x:800}}},[s,ee,O,p]),re=e.useMemo(function(){if(!1===m)return{};var e={current:q.current,pageSize:q.pageSize,total:te,showSizeChanger:!0,showQuickJumper:!0,showTotal:function(e,t){return"第 ".concat(t[0],"-").concat(t[1]," 条,共 ").concat(e," 条")},onChange:$,onShowSizeChange:$};return"object"==typeof m?n(n({},e),m):e},[q.current,q.pageSize,te,$,m]),oe=e.useMemo(function(){return{value:J,onChange:function(e){return W(e.target.value)},placeholder:(null==y?void 0:y.placeholder)||"搜索".concat(null==y?void 0:y.dataIndex),allowClear:!0,style:{width:250}}},[J,W,y]);return{tableProps:ne,data:ee,loading:O,total:te,paginationProps:re,current:q.current,pageSize:q.pageSize,filterProps:oe,filterValue:J,refresh:X,search:Y,reset:Z,setData:k,setLoading:T}};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "antd-hooks",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "A collection of enhanced hooks for Ant Design",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
"license": "ISC",
|
|
24
24
|
"peerDependencies": {
|
|
25
25
|
"antd": ">=4.0.0",
|
|
26
|
-
"react": ">=
|
|
27
|
-
"react-dom": ">=
|
|
26
|
+
"react": ">=18.0.0",
|
|
27
|
+
"react-dom": ">=18.0.0"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@rollup/plugin-commonjs": "^25.0.0",
|