@fe-free/core 2.2.6 → 2.2.8
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/CHANGELOG.md +14 -0
- package/package.json +4 -2
- package/src/crud/crud.stories.tsx +3 -0
- package/src/crud/crud.tsx +4 -3
- package/src/crud/use_row_selection.tsx +10 -2
- package/src/global/error.ts +89 -0
- package/src/global/interceptors.ts +34 -0
- package/src/index.ts +2 -0
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fe-free/core",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.8",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./src/index.ts",
|
|
6
6
|
"author": "",
|
|
@@ -28,7 +28,9 @@
|
|
|
28
28
|
"ahooks": "^3.7.8",
|
|
29
29
|
"axios": "^1.6.5",
|
|
30
30
|
"classnames": "^2.5.1",
|
|
31
|
+
"file-saver": "^2.0.5",
|
|
31
32
|
"github-markdown-css": "^5.8.1",
|
|
33
|
+
"i": "^0.3.7",
|
|
32
34
|
"localforage": "^1.10.0",
|
|
33
35
|
"lodash-es": "^4.17.21",
|
|
34
36
|
"react-ace": "^11.0.1",
|
|
@@ -39,7 +41,7 @@
|
|
|
39
41
|
"remark-gfm": "^4.0.1",
|
|
40
42
|
"vanilla-jsoneditor": "^0.23.1",
|
|
41
43
|
"zustand": "^4.5.4",
|
|
42
|
-
"@fe-free/tool": "2.2.
|
|
44
|
+
"@fe-free/tool": "2.2.8"
|
|
43
45
|
},
|
|
44
46
|
"peerDependencies": {
|
|
45
47
|
"@ant-design/pro-components": "2.8.9",
|
package/src/crud/crud.tsx
CHANGED
|
@@ -118,7 +118,7 @@ function CRUDComponent<
|
|
|
118
118
|
const disabled = readProps?.operateIsDisabled?.(record) || false;
|
|
119
119
|
if (disabled) {
|
|
120
120
|
btns.push(
|
|
121
|
-
<span key="read" className="
|
|
121
|
+
<span key="read" className="cursor-not-allowed text-desc">
|
|
122
122
|
{readProps?.operateText || '查看'}
|
|
123
123
|
</span>,
|
|
124
124
|
);
|
|
@@ -144,7 +144,7 @@ function CRUDComponent<
|
|
|
144
144
|
const disabled = readProps?.operateIsDisabled?.(record) || false;
|
|
145
145
|
if (disabled) {
|
|
146
146
|
btns.push(
|
|
147
|
-
<span key="read" className="
|
|
147
|
+
<span key="read" className="cursor-not-allowed text-desc">
|
|
148
148
|
{readProps?.operateText || '查看'}
|
|
149
149
|
</span>,
|
|
150
150
|
);
|
|
@@ -169,7 +169,7 @@ function CRUDComponent<
|
|
|
169
169
|
|
|
170
170
|
if (disabled) {
|
|
171
171
|
btns.push(
|
|
172
|
-
<span key="update" className="
|
|
172
|
+
<span key="update" className="cursor-not-allowed text-desc">
|
|
173
173
|
{updateProps?.operateText || '编辑'}
|
|
174
174
|
</span>,
|
|
175
175
|
);
|
|
@@ -284,6 +284,7 @@ function CRUDComponent<
|
|
|
284
284
|
DataSource,
|
|
285
285
|
Key
|
|
286
286
|
>({
|
|
287
|
+
rowSelection: tableProps.rowSelection,
|
|
287
288
|
batchActions,
|
|
288
289
|
actionRef,
|
|
289
290
|
});
|
|
@@ -6,13 +6,20 @@ import { LoadingButton } from '../button';
|
|
|
6
6
|
import type { CRUDProps } from './types';
|
|
7
7
|
|
|
8
8
|
function useRowSelection<DataSource, Key>({
|
|
9
|
+
rowSelection: originRowSelection,
|
|
9
10
|
batchActions,
|
|
10
11
|
actionRef,
|
|
11
12
|
}: {
|
|
13
|
+
rowSelection?: CRUDProps<DataSource, Key>['tableProps']['rowSelection'];
|
|
12
14
|
batchActions?: CRUDProps<DataSource, Key>['batchActions'];
|
|
13
15
|
actionRef?: MutableRefObject<ActionType | undefined>;
|
|
14
16
|
}) {
|
|
15
|
-
const rowSelection = useMemo(
|
|
17
|
+
const rowSelection = useMemo(
|
|
18
|
+
() => ({
|
|
19
|
+
...originRowSelection,
|
|
20
|
+
}),
|
|
21
|
+
[originRowSelection],
|
|
22
|
+
);
|
|
16
23
|
|
|
17
24
|
const tableAlertRender = useCallback(({ selectedRowKeys, onCleanSelected }) => {
|
|
18
25
|
return (
|
|
@@ -30,13 +37,14 @@ function useRowSelection<DataSource, Key>({
|
|
|
30
37
|
const tableAlertOptionRender = useCallback(
|
|
31
38
|
({ selectedRowKeys, selectedRows }) => {
|
|
32
39
|
return (
|
|
33
|
-
<div className="flex gap-2
|
|
40
|
+
<div className="flex items-center gap-2">
|
|
34
41
|
{batchActions?.map((action) => (
|
|
35
42
|
<LoadingButton
|
|
36
43
|
key={action.btnText}
|
|
37
44
|
type="link"
|
|
38
45
|
danger={action.danger}
|
|
39
46
|
className="!px-0"
|
|
47
|
+
disabled={selectedRowKeys.length === 0}
|
|
40
48
|
onClick={async (event) => {
|
|
41
49
|
if (action.danger) {
|
|
42
50
|
await new Promise((resolve) => {
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { message } from 'antd';
|
|
2
|
+
import type { AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
3
|
+
import { AxiosError } from 'axios';
|
|
4
|
+
|
|
5
|
+
class RequestError extends Error {
|
|
6
|
+
silent: boolean | undefined;
|
|
7
|
+
status: string | undefined;
|
|
8
|
+
config: AxiosRequestConfig<any> | undefined;
|
|
9
|
+
request: XMLHttpRequest | undefined;
|
|
10
|
+
response: AxiosResponse<any, any> | undefined;
|
|
11
|
+
|
|
12
|
+
constructor(
|
|
13
|
+
message: string,
|
|
14
|
+
{
|
|
15
|
+
silent,
|
|
16
|
+
config,
|
|
17
|
+
request,
|
|
18
|
+
response,
|
|
19
|
+
}: {
|
|
20
|
+
silent?: boolean;
|
|
21
|
+
status?: number;
|
|
22
|
+
config?: AxiosRequestConfig;
|
|
23
|
+
request?: XMLHttpRequest;
|
|
24
|
+
response?: AxiosResponse;
|
|
25
|
+
},
|
|
26
|
+
) {
|
|
27
|
+
super(message);
|
|
28
|
+
this.name = 'RequestError';
|
|
29
|
+
|
|
30
|
+
this.silent = silent;
|
|
31
|
+
this.status = status;
|
|
32
|
+
this.config = config;
|
|
33
|
+
this.request = request;
|
|
34
|
+
this.response = response;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function commonHandleError(event) {
|
|
39
|
+
console.log('handleError', event);
|
|
40
|
+
|
|
41
|
+
if (event.reason) {
|
|
42
|
+
if (event.reason instanceof AxiosError) {
|
|
43
|
+
if (event.reason.code === 'ERR_NETWORK') {
|
|
44
|
+
message.error('网络异常');
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (event.reason.code === 'ECONNABORTED') {
|
|
48
|
+
message.error('请求超时');
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (event.reason.code === 'ERR_CANCELED') {
|
|
53
|
+
// 正常逻辑
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (event.reason.message || event.reason.reason) {
|
|
59
|
+
message.error(event.reason.message || event.reason.reason);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (event.error) {
|
|
65
|
+
if (event.error.message || event.error.reason) {
|
|
66
|
+
message.error(event.error.message || event.error.reason);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// unknown error
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function initErrorHandle(onError) {
|
|
75
|
+
const handleError = (event) => {
|
|
76
|
+
commonHandleError(event);
|
|
77
|
+
onError?.(event);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
window.addEventListener('error', handleError);
|
|
81
|
+
window.addEventListener('unhandledrejection', handleError);
|
|
82
|
+
|
|
83
|
+
return () => {
|
|
84
|
+
window.removeEventListener('error', handleError);
|
|
85
|
+
window.removeEventListener('unhandledrejection', handleError);
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export { initErrorHandle, RequestError };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { AxiosInstance } from 'axios';
|
|
2
|
+
import { saveAs } from 'file-saver';
|
|
3
|
+
|
|
4
|
+
function downloadInterceptor(instance: AxiosInstance) {
|
|
5
|
+
instance.interceptors.response.use(async function (response) {
|
|
6
|
+
const contentDisposition = response.headers['content-disposition'];
|
|
7
|
+
|
|
8
|
+
if (contentDisposition) {
|
|
9
|
+
let filename;
|
|
10
|
+
|
|
11
|
+
// 更加健壮且简洁的写法,优先处理 filename*=,否则处理 filename=
|
|
12
|
+
if (contentDisposition.includes('filename*=')) {
|
|
13
|
+
// RFC 5987 格式:filename*=utf-8''xxx
|
|
14
|
+
const match = contentDisposition.match(/filename\*\s*=\s*([^']*)''([^;]+)/i);
|
|
15
|
+
filename = match ? decodeURIComponent(match[2]) : undefined;
|
|
16
|
+
} else if (contentDisposition.includes('filename=')) {
|
|
17
|
+
// 普通格式:filename=xxx
|
|
18
|
+
const match = contentDisposition.match(/filename\s*=\s*("?)([^";]+)\1/i);
|
|
19
|
+
filename = match ? decodeURIComponent(match[2]) : undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
filename = filename || 'download';
|
|
23
|
+
|
|
24
|
+
// 处理文件下载 - 确保正确处理二进制数据
|
|
25
|
+
const blob = response.data;
|
|
26
|
+
|
|
27
|
+
saveAs(blob, filename);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return response;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export { downloadInterceptor };
|
package/src/index.ts
CHANGED
|
@@ -30,6 +30,8 @@ export {
|
|
|
30
30
|
ProFormUploadDragger,
|
|
31
31
|
proFormSelectSearchProps,
|
|
32
32
|
} from './form';
|
|
33
|
+
export { RequestError, initErrorHandle } from './global/error';
|
|
34
|
+
export { downloadInterceptor } from './global/interceptors';
|
|
33
35
|
export { Markdown } from './markdown';
|
|
34
36
|
export { PageLayout } from './page_layout';
|
|
35
37
|
export { routeTool } from './route';
|