yuand 1.0.0
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/README.md +23 -0
- package/es/components/Table/index.d.ts +12 -0
- package/es/components/Table/index.js +326 -0
- package/es/components/Table/style.css +3 -0
- package/es/components/Table/types.d.ts +101 -0
- package/es/components/Table/types.js +1 -0
- package/es/components/Table/useTable.d.ts +3 -0
- package/es/components/Table/useTable.js +76 -0
- package/es/components/TableConfig/index.d.ts +11 -0
- package/es/components/TableConfig/index.js +19 -0
- package/es/components/TableUseFetch/index.d.ts +12 -0
- package/es/components/TableUseFetch/index.js +283 -0
- package/es/components/TableUseFetch/style.css +3 -0
- package/es/components/TableUseFetch/types.d.ts +100 -0
- package/es/components/TableUseFetch/types.js +1 -0
- package/es/components/TableUseFetch/useTable.d.ts +3 -0
- package/es/components/TableUseFetch/useTable.js +78 -0
- package/es/components/index.d.ts +3 -0
- package/es/components/index.js +3 -0
- package/es/fetch.d.ts +137 -0
- package/es/fetch.js +534 -0
- package/es/hooks/index.d.ts +4 -0
- package/es/hooks/index.js +4 -0
- package/es/hooks/useFetch.d.ts +17 -0
- package/es/hooks/useFetch.js +41 -0
- package/es/hooks/useMutation.d.ts +14 -0
- package/es/hooks/useMutation.js +26 -0
- package/es/hooks/useQuery.d.ts +14 -0
- package/es/hooks/useQuery.js +40 -0
- package/es/hooks/useX.d.ts +1 -0
- package/es/hooks/useX.js +13 -0
- package/es/index.d.ts +4 -0
- package/es/index.js +4 -0
- package/es/utils/index.d.ts +6 -0
- package/es/utils/index.js +23 -0
- package/es/utils/table.d.ts +15 -0
- package/es/utils/table.js +106 -0
- package/es/utils/util.d.ts +2 -0
- package/es/utils/util.js +6 -0
- package/lang/en_US.json +6 -0
- package/lang/zh_CN.json +6 -0
- package/lib/components/Table/index.d.ts +12 -0
- package/lib/components/Table/index.js +330 -0
- package/lib/components/Table/style.css +3 -0
- package/lib/components/Table/types.d.ts +101 -0
- package/lib/components/Table/types.js +17 -0
- package/lib/components/Table/useTable.d.ts +3 -0
- package/lib/components/Table/useTable.js +85 -0
- package/lib/components/TableConfig/index.d.ts +11 -0
- package/lib/components/TableConfig/index.js +55 -0
- package/lib/components/TableUseFetch/index.d.ts +12 -0
- package/lib/components/TableUseFetch/index.js +303 -0
- package/lib/components/TableUseFetch/style.css +3 -0
- package/lib/components/TableUseFetch/types.d.ts +100 -0
- package/lib/components/TableUseFetch/types.js +17 -0
- package/lib/components/TableUseFetch/useTable.d.ts +3 -0
- package/lib/components/TableUseFetch/useTable.js +86 -0
- package/lib/components/index.d.ts +3 -0
- package/lib/components/index.js +45 -0
- package/lib/fetch.d.ts +137 -0
- package/lib/fetch.js +366 -0
- package/lib/hooks/index.d.ts +4 -0
- package/lib/hooks/index.js +48 -0
- package/lib/hooks/useFetch.d.ts +17 -0
- package/lib/hooks/useFetch.js +62 -0
- package/lib/hooks/useMutation.d.ts +14 -0
- package/lib/hooks/useMutation.js +43 -0
- package/lib/hooks/useQuery.d.ts +14 -0
- package/lib/hooks/useQuery.js +57 -0
- package/lib/hooks/useX.d.ts +1 -0
- package/lib/hooks/useX.js +37 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +29 -0
- package/lib/utils/index.d.ts +6 -0
- package/lib/utils/index.js +60 -0
- package/lib/utils/table.d.ts +15 -0
- package/lib/utils/table.js +124 -0
- package/lib/utils/util.d.ts +2 -0
- package/lib/utils/util.js +32 -0
- package/package.json +46 -0
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# yu-component
|
|
2
|
+
|
|
3
|
+
[](https://npmjs.org/package/yu-component)
|
|
4
|
+
[](https://npmjs.org/package/yu-component)
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
$ npm install
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
$ npm run dev
|
|
14
|
+
$ npm run build
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Options
|
|
18
|
+
|
|
19
|
+
TODO
|
|
20
|
+
|
|
21
|
+
## LICENSE
|
|
22
|
+
|
|
23
|
+
MIT
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ProTableProps, ProTableConfigOptions } from "./types";
|
|
2
|
+
import "./style.css";
|
|
3
|
+
declare const ProTable: {
|
|
4
|
+
<T extends Record<string, any>>(props: ProTableProps<T>): import("react/jsx-runtime").JSX.Element;
|
|
5
|
+
useTable: (options?: import("./types").UseTableProps) => import("./types").TableInstance<any>[];
|
|
6
|
+
getQuery: ({ page, size, sorter, search, params, }: import("../../utils/table").GetQueryProps) => Record<string, unknown>;
|
|
7
|
+
formatDate: (key: string, data: Record<string, any>, format?: string) => Record<string, any>;
|
|
8
|
+
removeEmpty: (data: Record<string, any>) => Record<string, any>;
|
|
9
|
+
pageSizeOptions: number[];
|
|
10
|
+
config(options: ProTableConfigOptions): void;
|
|
11
|
+
};
|
|
12
|
+
export default ProTable;
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
+
import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
|
|
3
|
+
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
4
|
+
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
5
|
+
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
6
|
+
var _excluded = ["request", "classNames", "styles", "table", "locale", "dataKey", "totalKey", "manual", "nostyle", "columns", "form", "alert", "toolbar", "pageSizeOptions", "pagination", "scroll", "useData"],
|
|
7
|
+
_excluded2 = ["title", "extra", "right", "formItem", "layout", "items", "reset", "dataForm", "handleValues", "onResetBefore"];
|
|
8
|
+
import { rq } from "../../fetch";
|
|
9
|
+
import { I18nContext, t } from "../TableConfig";
|
|
10
|
+
import { useShallow } from "zustand/react/shallow";
|
|
11
|
+
import { useQueryClient, useQuery } from "@tanstack/react-query";
|
|
12
|
+
import { useCallback, useEffect, useMemo, useRef, useContext } from "react";
|
|
13
|
+
import { Table, Form, Button, Space } from "antd";
|
|
14
|
+
import { isObject } from "../../utils/util";
|
|
15
|
+
import { getDataSource, getQuery, getTotal, formatDate, removeEmpty } from "../../utils/table";
|
|
16
|
+
import useX from "../../hooks/useX";
|
|
17
|
+
import useTable from "./useTable";
|
|
18
|
+
import "./style.css";
|
|
19
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
20
|
+
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
21
|
+
var defaultClassNames = {
|
|
22
|
+
root: "main-container",
|
|
23
|
+
form: "search-form",
|
|
24
|
+
table: "main-table"
|
|
25
|
+
};
|
|
26
|
+
var defaultStyles = {
|
|
27
|
+
root: {},
|
|
28
|
+
form: {
|
|
29
|
+
display: "flex",
|
|
30
|
+
justifyContent: "space-between"
|
|
31
|
+
},
|
|
32
|
+
table: {},
|
|
33
|
+
toolbar: {
|
|
34
|
+
marginBottom: 15
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
var DEFAULT_PAGE_SIZE_OPTIONS = [10, 20, 50, 100];
|
|
38
|
+
var ProTable = function ProTable(props) {
|
|
39
|
+
var _scroll$x, _classNames$root, _classNames$form, _classNames$table;
|
|
40
|
+
var _props$request = props.request,
|
|
41
|
+
request = _props$request === void 0 ? {} : _props$request,
|
|
42
|
+
_props$classNames = props.classNames,
|
|
43
|
+
classNames = _props$classNames === void 0 ? defaultClassNames : _props$classNames,
|
|
44
|
+
_props$styles = props.styles,
|
|
45
|
+
styles = _props$styles === void 0 ? defaultStyles : _props$styles,
|
|
46
|
+
table = props.table,
|
|
47
|
+
locale = props.locale,
|
|
48
|
+
_props$dataKey = props.dataKey,
|
|
49
|
+
dataKey = _props$dataKey === void 0 ? "data" : _props$dataKey,
|
|
50
|
+
_props$totalKey = props.totalKey,
|
|
51
|
+
totalKey = _props$totalKey === void 0 ? "total" : _props$totalKey,
|
|
52
|
+
_props$manual = props.manual,
|
|
53
|
+
manual = _props$manual === void 0 ? false : _props$manual,
|
|
54
|
+
nostyle = props.nostyle,
|
|
55
|
+
columns = props.columns,
|
|
56
|
+
_props$form = props.form,
|
|
57
|
+
form = _props$form === void 0 ? {} : _props$form,
|
|
58
|
+
alert = props.alert,
|
|
59
|
+
_props$toolbar = props.toolbar,
|
|
60
|
+
toolbar = _props$toolbar === void 0 ? null : _props$toolbar,
|
|
61
|
+
pageSizeOptions = props.pageSizeOptions,
|
|
62
|
+
pagination = props.pagination,
|
|
63
|
+
scroll = props.scroll,
|
|
64
|
+
useData = props.useData,
|
|
65
|
+
prop = _objectWithoutProperties(props, _excluded);
|
|
66
|
+
var _useContext = useContext(I18nContext),
|
|
67
|
+
lang = _useContext.lang;
|
|
68
|
+
var formTitle = form.title,
|
|
69
|
+
formExtra = form.extra,
|
|
70
|
+
formRight = form.right,
|
|
71
|
+
formItem = form.formItem,
|
|
72
|
+
_form$layout = form.layout,
|
|
73
|
+
layout = _form$layout === void 0 ? "inline" : _form$layout,
|
|
74
|
+
items = form.items,
|
|
75
|
+
formReset = form.reset,
|
|
76
|
+
dataForm = form.dataForm,
|
|
77
|
+
formHandleValues = form.handleValues,
|
|
78
|
+
formOnResetBefore = form.onResetBefore,
|
|
79
|
+
otherFormProps = _objectWithoutProperties(form, _excluded2);
|
|
80
|
+
var forceKey = useRef(0);
|
|
81
|
+
var formItems = formItem || items;
|
|
82
|
+
var queryClient = useQueryClient();
|
|
83
|
+
var _table$useStore = table.useStore(useShallow(function (state) {
|
|
84
|
+
return {
|
|
85
|
+
page: state.page,
|
|
86
|
+
size: state.size,
|
|
87
|
+
sorter: state.sorter,
|
|
88
|
+
search: state.search,
|
|
89
|
+
ready: state.ready,
|
|
90
|
+
setState: state.setState
|
|
91
|
+
};
|
|
92
|
+
})),
|
|
93
|
+
page = _table$useStore.page,
|
|
94
|
+
size = _table$useStore.size,
|
|
95
|
+
sorter = _table$useStore.sorter,
|
|
96
|
+
search = _table$useStore.search,
|
|
97
|
+
ready = _table$useStore.ready,
|
|
98
|
+
setState = _table$useStore.setState;
|
|
99
|
+
var queryKey = useMemo(function () {
|
|
100
|
+
return [request.url, ProTable.getQuery({
|
|
101
|
+
page: page,
|
|
102
|
+
size: size,
|
|
103
|
+
sorter: sorter,
|
|
104
|
+
search: search,
|
|
105
|
+
params: request.params
|
|
106
|
+
}), forceKey.current];
|
|
107
|
+
}, [request, page, size, sorter, search]);
|
|
108
|
+
var _useQuery = useQuery({
|
|
109
|
+
queryKey: queryKey,
|
|
110
|
+
queryFn: function () {
|
|
111
|
+
var _queryFn = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
|
|
112
|
+
var _request$onBefore, _request$method;
|
|
113
|
+
var queryKey, _ref2, _ref3, url, json, lowerMethod;
|
|
114
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
115
|
+
while (1) switch (_context.prev = _context.next) {
|
|
116
|
+
case 0:
|
|
117
|
+
queryKey = _ref.queryKey;
|
|
118
|
+
if (request.url) {
|
|
119
|
+
_context.next = 3;
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
return _context.abrupt("return", Promise.resolve({}));
|
|
123
|
+
case 3:
|
|
124
|
+
(_request$onBefore = request.onBefore) === null || _request$onBefore === void 0 || _request$onBefore.call(request);
|
|
125
|
+
_ref2 = queryKey, _ref3 = _slicedToArray(_ref2, 2), url = _ref3[0], json = _ref3[1];
|
|
126
|
+
lowerMethod = (_request$method = request.method) === null || _request$method === void 0 ? void 0 : _request$method.toLocaleUpperCase();
|
|
127
|
+
return _context.abrupt("return", rq.request(url, {
|
|
128
|
+
method: lowerMethod,
|
|
129
|
+
json: json,
|
|
130
|
+
onSuccess: request.onSuccess
|
|
131
|
+
}));
|
|
132
|
+
case 7:
|
|
133
|
+
case "end":
|
|
134
|
+
return _context.stop();
|
|
135
|
+
}
|
|
136
|
+
}, _callee);
|
|
137
|
+
}));
|
|
138
|
+
function queryFn(_x) {
|
|
139
|
+
return _queryFn.apply(this, arguments);
|
|
140
|
+
}
|
|
141
|
+
return queryFn;
|
|
142
|
+
}(),
|
|
143
|
+
enabled: ready
|
|
144
|
+
}),
|
|
145
|
+
_useQuery$data = _useQuery.data,
|
|
146
|
+
data = _useQuery$data === void 0 ? {} : _useQuery$data,
|
|
147
|
+
isLoading = _useQuery.isLoading,
|
|
148
|
+
refetch = _useQuery.refetch;
|
|
149
|
+
useEffect(function () {
|
|
150
|
+
if (useData) {
|
|
151
|
+
setState({
|
|
152
|
+
data: data
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}, [data]);
|
|
156
|
+
var _useMemo = useMemo(function () {
|
|
157
|
+
return {
|
|
158
|
+
column: typeof columns === "function" ? columns(data) : columns,
|
|
159
|
+
dataSource: getDataSource(data, dataKey),
|
|
160
|
+
renderAlert: typeof alert === "function" ? alert(data) : alert,
|
|
161
|
+
total: getTotal(totalKey, data)
|
|
162
|
+
};
|
|
163
|
+
}, [columns, data, dataKey, totalKey]),
|
|
164
|
+
dataSource = _useMemo.dataSource,
|
|
165
|
+
total = _useMemo.total,
|
|
166
|
+
column = _useMemo.column,
|
|
167
|
+
renderAlert = _useMemo.renderAlert;
|
|
168
|
+
var onSearch = function onSearch() {
|
|
169
|
+
if (formItems) {
|
|
170
|
+
table.form.submit();
|
|
171
|
+
} else {
|
|
172
|
+
setState({
|
|
173
|
+
ready: true
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
var onReset = function onReset() {
|
|
178
|
+
setState({
|
|
179
|
+
size: (pageSizeOptions === null || pageSizeOptions === void 0 ? void 0 : pageSizeOptions[0]) || ProTable.pageSizeOptions[0],
|
|
180
|
+
sorter: {}
|
|
181
|
+
});
|
|
182
|
+
if (formItems) {
|
|
183
|
+
if ((formOnResetBefore === null || formOnResetBefore === void 0 ? void 0 : formOnResetBefore()) === false) return;
|
|
184
|
+
table.form.resetFields();
|
|
185
|
+
if (formReset === undefined || formReset === true) {
|
|
186
|
+
table.form.submit();
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
useEffect(function () {
|
|
191
|
+
if (table) {
|
|
192
|
+
table.run = onSearch;
|
|
193
|
+
table.queryKey = queryKey;
|
|
194
|
+
table.clear = function () {
|
|
195
|
+
return queryClient.setQueryData(queryKey, {});
|
|
196
|
+
};
|
|
197
|
+
table.refresh = refetch;
|
|
198
|
+
table.reset = function () {
|
|
199
|
+
if (formItems) {
|
|
200
|
+
onReset();
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
}, [table, queryKey]);
|
|
205
|
+
useEffect(function () {
|
|
206
|
+
if (manual) return;
|
|
207
|
+
if (formItems) {
|
|
208
|
+
table.form.submit();
|
|
209
|
+
} else {
|
|
210
|
+
setState({
|
|
211
|
+
ready: true
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
}, []);
|
|
215
|
+
useEffect(function () {
|
|
216
|
+
return function () {
|
|
217
|
+
table.resetStore();
|
|
218
|
+
};
|
|
219
|
+
}, [table]);
|
|
220
|
+
var onFinish = useCallback(function (values) {
|
|
221
|
+
if (formHandleValues) {
|
|
222
|
+
values = formHandleValues(values);
|
|
223
|
+
}
|
|
224
|
+
if (!values) return;
|
|
225
|
+
//更新 queryKey, userquery force refetch
|
|
226
|
+
forceKey.current += 1;
|
|
227
|
+
setState({
|
|
228
|
+
page: 1,
|
|
229
|
+
search: values,
|
|
230
|
+
ready: true
|
|
231
|
+
});
|
|
232
|
+
}, []);
|
|
233
|
+
var tableChange = useCallback(function (pagination, sorter) {
|
|
234
|
+
setState({
|
|
235
|
+
page: pagination.current,
|
|
236
|
+
size: pagination.pageSize,
|
|
237
|
+
sorter: sorter,
|
|
238
|
+
ready: true
|
|
239
|
+
});
|
|
240
|
+
}, []);
|
|
241
|
+
var x = (_scroll$x = scroll === null || scroll === void 0 ? void 0 : scroll.x) !== null && _scroll$x !== void 0 ? _scroll$x : useX(column);
|
|
242
|
+
var y = scroll === null || scroll === void 0 ? void 0 : scroll.y;
|
|
243
|
+
var renderTable = function renderTable() {
|
|
244
|
+
return /*#__PURE__*/_jsx(Table, _objectSpread({
|
|
245
|
+
columns: column,
|
|
246
|
+
loading: isLoading,
|
|
247
|
+
scroll: {
|
|
248
|
+
x: x,
|
|
249
|
+
y: y
|
|
250
|
+
},
|
|
251
|
+
locale: locale,
|
|
252
|
+
onChange: function onChange(p, _, sorter) {
|
|
253
|
+
return tableChange(p, sorter);
|
|
254
|
+
},
|
|
255
|
+
pagination: {
|
|
256
|
+
current: page,
|
|
257
|
+
pageSize: size,
|
|
258
|
+
showQuickJumper: pagination ? pagination.showQuickJumper : true,
|
|
259
|
+
showSizeChanger: pagination ? pagination.showSizeChanger : true,
|
|
260
|
+
hideOnSinglePage: pagination ? pagination.hideOnSinglePage : false,
|
|
261
|
+
pageSizeOptions: pageSizeOptions || ProTable.pageSizeOptions,
|
|
262
|
+
total: total,
|
|
263
|
+
showTotal: function showTotal(total) {
|
|
264
|
+
return "".concat(t("共", lang), " ").concat(total, " ").concat(t("条记录", lang));
|
|
265
|
+
}
|
|
266
|
+
},
|
|
267
|
+
dataSource: dataSource
|
|
268
|
+
}, prop));
|
|
269
|
+
};
|
|
270
|
+
return /*#__PURE__*/_jsxs("div", {
|
|
271
|
+
className: (_classNames$root = classNames === null || classNames === void 0 ? void 0 : classNames.root) !== null && _classNames$root !== void 0 ? _classNames$root : defaultClassNames.root,
|
|
272
|
+
style: styles.root,
|
|
273
|
+
children: [!!formItems && /*#__PURE__*/_jsxs("div", {
|
|
274
|
+
className: (_classNames$form = classNames === null || classNames === void 0 ? void 0 : classNames.form) !== null && _classNames$form !== void 0 ? _classNames$form : defaultClassNames.form,
|
|
275
|
+
style: styles.form,
|
|
276
|
+
children: [/*#__PURE__*/_jsxs(Form, _objectSpread(_objectSpread({
|
|
277
|
+
form: table.form,
|
|
278
|
+
layout: layout,
|
|
279
|
+
onFinish: onFinish
|
|
280
|
+
}, otherFormProps), {}, {
|
|
281
|
+
children: [formTitle && /*#__PURE__*/_jsx(Form.Item, {
|
|
282
|
+
children: formTitle
|
|
283
|
+
}), formItems, /*#__PURE__*/_jsx(Form.Item, {
|
|
284
|
+
children: /*#__PURE__*/_jsxs(Space, {
|
|
285
|
+
children: [/*#__PURE__*/_jsx(Button, {
|
|
286
|
+
type: "primary",
|
|
287
|
+
loading: isLoading,
|
|
288
|
+
htmlType: "submit",
|
|
289
|
+
children: t("搜索", lang)
|
|
290
|
+
}), /*#__PURE__*/_jsx(Button, {
|
|
291
|
+
onClick: onReset,
|
|
292
|
+
disabled: isLoading,
|
|
293
|
+
children: t("重置", lang)
|
|
294
|
+
}), formExtra]
|
|
295
|
+
})
|
|
296
|
+
})]
|
|
297
|
+
})), formRight]
|
|
298
|
+
}), /*#__PURE__*/_jsxs("div", {
|
|
299
|
+
className: (_classNames$table = classNames === null || classNames === void 0 ? void 0 : classNames.table) !== null && _classNames$table !== void 0 ? _classNames$table : defaultClassNames.table,
|
|
300
|
+
style: styles.table,
|
|
301
|
+
children: [toolbar && /*#__PURE__*/_jsx("div", {
|
|
302
|
+
style: styles.toolbar,
|
|
303
|
+
children: toolbar
|
|
304
|
+
}), renderAlert, !!dataForm ? /*#__PURE__*/_jsx(Form, _objectSpread(_objectSpread({}, dataForm), {}, {
|
|
305
|
+
children: renderTable()
|
|
306
|
+
})) : renderTable()]
|
|
307
|
+
})]
|
|
308
|
+
});
|
|
309
|
+
};
|
|
310
|
+
ProTable.useTable = useTable;
|
|
311
|
+
ProTable.getQuery = getQuery;
|
|
312
|
+
ProTable.formatDate = formatDate;
|
|
313
|
+
ProTable.removeEmpty = removeEmpty;
|
|
314
|
+
ProTable.pageSizeOptions = DEFAULT_PAGE_SIZE_OPTIONS;
|
|
315
|
+
|
|
316
|
+
//自定义配置参数组合方式. 默认提供 page,size,orderField,isAsc,...params,...search
|
|
317
|
+
ProTable.config = function (options) {
|
|
318
|
+
if (!options || !isObject(options)) return;
|
|
319
|
+
if (options.getQuery) {
|
|
320
|
+
ProTable.getQuery = options.getQuery;
|
|
321
|
+
}
|
|
322
|
+
if (options.pageSizeOptions) {
|
|
323
|
+
ProTable.pageSizeOptions = options.pageSizeOptions;
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
export default ProTable;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import type { FormInstance, TableColumnType, TableProps, FormProps } from "antd";
|
|
3
|
+
import type { HttpMethod } from "../../fetch";
|
|
4
|
+
import type { UseBoundStore, StoreApi } from "zustand";
|
|
5
|
+
import type { GetQueryProps } from "../../utils/table";
|
|
6
|
+
type RecordType = Record<string, any>;
|
|
7
|
+
interface SorterType {
|
|
8
|
+
field?: string;
|
|
9
|
+
order?: "ascend" | "descend";
|
|
10
|
+
}
|
|
11
|
+
export interface TableState<TData = any> {
|
|
12
|
+
page: number;
|
|
13
|
+
size: number;
|
|
14
|
+
sorter: SorterType;
|
|
15
|
+
data: TData;
|
|
16
|
+
search: RecordType | null | undefined;
|
|
17
|
+
params: any[] | Record<string, any> | null | undefined;
|
|
18
|
+
setState: (values: Partial<TableState>) => void;
|
|
19
|
+
ready: boolean;
|
|
20
|
+
}
|
|
21
|
+
type UseStoreType<TData> = UseBoundStore<StoreApi<TableState<TData>>>;
|
|
22
|
+
export interface TableInstance<TData = any> {
|
|
23
|
+
useStore: UseStoreType<TData>;
|
|
24
|
+
queryKey: any[];
|
|
25
|
+
run: () => void;
|
|
26
|
+
clear: () => void;
|
|
27
|
+
refresh: () => void;
|
|
28
|
+
reset: () => void;
|
|
29
|
+
sortOrder: (key: string) => any;
|
|
30
|
+
update: () => void;
|
|
31
|
+
resetStore: () => void;
|
|
32
|
+
form?: FormInstance;
|
|
33
|
+
}
|
|
34
|
+
interface FormOptions extends Omit<FormProps, "form" | "title" | "children"> {
|
|
35
|
+
title?: React.ReactNode;
|
|
36
|
+
/** @deprecated 此属性已废弃,请使用新的formItem属性代替 */
|
|
37
|
+
items?: React.ReactNode | React.ReactNode[];
|
|
38
|
+
formItem?: React.ReactNode | React.ReactNode[];
|
|
39
|
+
extra?: React.ReactNode;
|
|
40
|
+
right?: React.ReactNode;
|
|
41
|
+
handleValues?: (values: Record<string, any>) => any;
|
|
42
|
+
reset?: boolean;
|
|
43
|
+
onResetBefore?: () => void | boolean;
|
|
44
|
+
dataForm?: FormProps;
|
|
45
|
+
}
|
|
46
|
+
export interface ProTableProps<Tdata = any> extends Omit<TableProps<Tdata>, "columns"> {
|
|
47
|
+
classNames?: {
|
|
48
|
+
root?: string;
|
|
49
|
+
form?: string;
|
|
50
|
+
table?: string;
|
|
51
|
+
};
|
|
52
|
+
styles?: {
|
|
53
|
+
root?: React.CSSProperties;
|
|
54
|
+
form?: React.CSSProperties;
|
|
55
|
+
table?: React.CSSProperties;
|
|
56
|
+
toolbar?: React.CSSProperties;
|
|
57
|
+
};
|
|
58
|
+
/** API 请求配置 */
|
|
59
|
+
request: {
|
|
60
|
+
/** 请求地址方法 */
|
|
61
|
+
url?: string;
|
|
62
|
+
method?: HttpMethod;
|
|
63
|
+
/** 请求参数 */
|
|
64
|
+
params?: Record<string, any>;
|
|
65
|
+
onBefore?: () => any;
|
|
66
|
+
onSuccess?: (data: any) => any;
|
|
67
|
+
};
|
|
68
|
+
url: string;
|
|
69
|
+
table: TableInstance<Tdata> | null;
|
|
70
|
+
locale?: Record<string, any>;
|
|
71
|
+
dataKey?: string;
|
|
72
|
+
totalKey?: string;
|
|
73
|
+
manual?: boolean;
|
|
74
|
+
nostyle?: boolean;
|
|
75
|
+
params?: RecordType;
|
|
76
|
+
columns: ((data: Tdata) => TableColumnType<unknown>[]) | TableColumnType<unknown>[];
|
|
77
|
+
form?: FormOptions;
|
|
78
|
+
alert?: React.ReactNode | ((data: Tdata) => React.ReactNode);
|
|
79
|
+
toolbar?: React.ReactNode;
|
|
80
|
+
pageSizeOptions?: number[];
|
|
81
|
+
onBefore?: () => void;
|
|
82
|
+
onSuccess?: (data: Tdata) => any;
|
|
83
|
+
pagination?: {
|
|
84
|
+
showQuickJumper?: boolean;
|
|
85
|
+
showSizeChanger?: boolean;
|
|
86
|
+
hideOnSinglePage?: boolean;
|
|
87
|
+
};
|
|
88
|
+
loadingDelay?: number;
|
|
89
|
+
method?: HttpMethod;
|
|
90
|
+
useData?: boolean;
|
|
91
|
+
}
|
|
92
|
+
export interface UseTableProps {
|
|
93
|
+
page?: number;
|
|
94
|
+
size?: number;
|
|
95
|
+
sorter?: SorterType;
|
|
96
|
+
}
|
|
97
|
+
export interface ProTableConfigOptions {
|
|
98
|
+
getQuery?: (data: GetQueryProps) => Record<string, unknown>;
|
|
99
|
+
pageSizeOptions?: number[];
|
|
100
|
+
}
|
|
101
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
+
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
3
|
+
import { Form } from "antd";
|
|
4
|
+
import { useRef, useState } from "react";
|
|
5
|
+
import { create } from "zustand";
|
|
6
|
+
var useTable = function useTable() {
|
|
7
|
+
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
8
|
+
var _useState = useState(0),
|
|
9
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
10
|
+
_ = _useState2[0],
|
|
11
|
+
_update = _useState2[1];
|
|
12
|
+
var _Form$useForm = Form.useForm(),
|
|
13
|
+
_Form$useForm2 = _slicedToArray(_Form$useForm, 1),
|
|
14
|
+
form = _Form$useForm2[0];
|
|
15
|
+
var tableRef = useRef(null);
|
|
16
|
+
if (!tableRef.current) {
|
|
17
|
+
var _options$page, _options$size, _options$sorter;
|
|
18
|
+
var initState = {
|
|
19
|
+
page: (_options$page = options.page) !== null && _options$page !== void 0 ? _options$page : 1,
|
|
20
|
+
size: (_options$size = options.size) !== null && _options$size !== void 0 ? _options$size : 10,
|
|
21
|
+
sorter: (_options$sorter = options.sorter) !== null && _options$sorter !== void 0 ? _options$sorter : {},
|
|
22
|
+
search: {},
|
|
23
|
+
params: {},
|
|
24
|
+
data: {},
|
|
25
|
+
ready: false
|
|
26
|
+
};
|
|
27
|
+
var useStore = create(function (set) {
|
|
28
|
+
return _objectSpread(_objectSpread({}, initState), {}, {
|
|
29
|
+
setState: function setState() {
|
|
30
|
+
var values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
31
|
+
set(values);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
tableRef.current = {
|
|
36
|
+
form: form,
|
|
37
|
+
useStore: useStore,
|
|
38
|
+
queryKey: [],
|
|
39
|
+
run: function run() {},
|
|
40
|
+
clear: function clear() {},
|
|
41
|
+
refresh: function refresh() {},
|
|
42
|
+
reset: function reset() {},
|
|
43
|
+
sortOrder: function sortOrder(key) {
|
|
44
|
+
var sorter = useStore.getState().sorter;
|
|
45
|
+
if (sorter && sorter.field === key) {
|
|
46
|
+
return sorter.order;
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
},
|
|
50
|
+
resetStore: function resetStore() {
|
|
51
|
+
useStore.getState().setState(initState);
|
|
52
|
+
},
|
|
53
|
+
update: function update() {
|
|
54
|
+
return _update(function (v) {
|
|
55
|
+
return v + 1;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
useStore.subscribe(function (state, prevState) {
|
|
60
|
+
var _state$sorter, _prevState$sorter, _state$sorter2, _prevState$sorter2;
|
|
61
|
+
// 只在排序真正变化时才触发更新
|
|
62
|
+
var sorterChanged = state.sorter !== prevState.sorter;
|
|
63
|
+
var orderChanged = ((_state$sorter = state.sorter) === null || _state$sorter === void 0 ? void 0 : _state$sorter.order) !== ((_prevState$sorter = prevState.sorter) === null || _prevState$sorter === void 0 ? void 0 : _prevState$sorter.order);
|
|
64
|
+
var fieldChanged = ((_state$sorter2 = state.sorter) === null || _state$sorter2 === void 0 ? void 0 : _state$sorter2.field) !== ((_prevState$sorter2 = prevState.sorter) === null || _prevState$sorter2 === void 0 ? void 0 : _prevState$sorter2.field);
|
|
65
|
+
if (sorterChanged && (orderChanged || fieldChanged)) {
|
|
66
|
+
// 创建新的引用以触发使用 tableRef.current 的组件更新
|
|
67
|
+
tableRef.current = _objectSpread({}, tableRef.current);
|
|
68
|
+
_update(function (v) {
|
|
69
|
+
return v + 1;
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
return [tableRef.current];
|
|
75
|
+
};
|
|
76
|
+
export default useTable;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
interface Props {
|
|
3
|
+
children: React.ReactNode;
|
|
4
|
+
lang?: Record<string, string>;
|
|
5
|
+
}
|
|
6
|
+
export declare const I18nContext: import("react").Context<{
|
|
7
|
+
lang?: Record<string, any>;
|
|
8
|
+
}>;
|
|
9
|
+
export declare const t: (key: string, i18n: Record<string, any>) => any;
|
|
10
|
+
export default function TableConfig({ children, lang }: Props): import("react/jsx-runtime").JSX.Element;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { createContext } from 'react';
|
|
2
|
+
import zhCN from "../../../lang/zh_CN.json";
|
|
3
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
4
|
+
export var I18nContext = /*#__PURE__*/createContext({});
|
|
5
|
+
export var t = function t(key, i18n) {
|
|
6
|
+
if (!i18n) return key;
|
|
7
|
+
return i18n[key] || key;
|
|
8
|
+
};
|
|
9
|
+
export default function TableConfig(_ref) {
|
|
10
|
+
var children = _ref.children,
|
|
11
|
+
_ref$lang = _ref.lang,
|
|
12
|
+
lang = _ref$lang === void 0 ? zhCN : _ref$lang;
|
|
13
|
+
return /*#__PURE__*/_jsx(I18nContext.Provider, {
|
|
14
|
+
value: {
|
|
15
|
+
lang: lang
|
|
16
|
+
},
|
|
17
|
+
children: children
|
|
18
|
+
});
|
|
19
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ProTableProps, ProTableConfigOptions } from "./types";
|
|
2
|
+
import "./style.css";
|
|
3
|
+
declare const ProTable: {
|
|
4
|
+
<T extends Record<string, any>>(props: ProTableProps<T>): import("react/jsx-runtime").JSX.Element;
|
|
5
|
+
useTable: (options?: import("./types").UseTableProps) => import("./types").TableRef[];
|
|
6
|
+
getQuery(options: any): Record<string, unknown>;
|
|
7
|
+
formatDate: (key: string, data: Record<string, any>, format?: string) => Record<string, any>;
|
|
8
|
+
removeEmpty: (data: Record<string, any>) => Record<string, any>;
|
|
9
|
+
pageSizeOptions: number[];
|
|
10
|
+
config(options: ProTableConfigOptions): void;
|
|
11
|
+
};
|
|
12
|
+
export default ProTable;
|