antd-import-export 1.0.5 → 1.0.7
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 +78 -75
- package/README_EN.md +83 -67
- package/dist/components/EditableImportCell.d.ts +12 -0
- package/dist/components/ExportButton.d.ts +12 -5
- package/dist/components/ImportButton.d.ts +11 -1
- package/dist/components/importBatchResult.d.ts +9 -0
- package/dist/components/importPreviewStatus.d.ts +20 -0
- package/dist/core/excel/exportFile.d.ts +4 -4
- package/dist/core/httpError.d.ts +15 -2
- package/dist/core/limits.d.ts +5 -1
- package/dist/core/task/exportRunner.d.ts +4 -0
- package/dist/core/task/importRunner.d.ts +21 -0
- package/dist/core/task/index.d.ts +12 -0
- package/dist/core/task/stats.d.ts +16 -0
- package/dist/core/task/store.d.ts +17 -0
- package/dist/core/task/types.d.ts +107 -0
- package/dist/index.cjs +6 -6
- package/dist/index.d.ts +4 -12
- package/dist/index.mjs +805 -771
- package/dist/react/useActiveTask.d.ts +5 -0
- package/dist/react/useTaskDetail.d.ts +1 -1
- package/package.json +1 -1
- package/dist/core/taskManager.d.ts +0 -123
package/README.md
CHANGED
|
@@ -15,19 +15,6 @@
|
|
|
15
15
|
|
|
16
16
|
[在线演示](https://demo.29dev.cn/antd-import-export/)
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
## 从 1.0.4 升级
|
|
20
|
-
|
|
21
|
-
1.0.5 重命名了两个组件、移除了批量导出组件:
|
|
22
|
-
|
|
23
|
-
| 1.0.4 及以前 | 1.0.5 |
|
|
24
|
-
| --- | --- |
|
|
25
|
-
| `ExcelImportButton` | `ImportButton` |
|
|
26
|
-
| `ExcelExportButton` | `ExportButton` |
|
|
27
|
-
| `BatchExportButton` | 改用 `exportRows()`,按钮自己渲染(见[组件](#组件)) |
|
|
28
|
-
|
|
29
|
-
项目仍在开发阶段,这类调整不单独发大版本。
|
|
30
|
-
|
|
31
18
|
## 依赖要求
|
|
32
19
|
|
|
33
20
|
| 依赖 | 版本 |
|
|
@@ -37,20 +24,6 @@
|
|
|
37
24
|
| antd | >=6.0.0 |
|
|
38
25
|
| @ant-design/icons | >=5.0.0 |
|
|
39
26
|
|
|
40
|
-
## 大文件表现
|
|
41
|
-
|
|
42
|
-
Excel 的解析和生成在主线程执行——这是 ExcelJS 自身的用法,官方没有提供 Worker 方案。代价是处理大文件时页面会短暂无响应:
|
|
43
|
-
|
|
44
|
-
| 行数 | 导入解析 | 导出生成 |
|
|
45
|
-
| --- | --- | --- |
|
|
46
|
-
| 1,000 | 约 25ms | 约 60ms |
|
|
47
|
-
| 5,000 | 约 125ms | 约 300ms |
|
|
48
|
-
| 20,000 | 约 500ms | 约 1 秒 |
|
|
49
|
-
|
|
50
|
-
单次上限 2 万行,超出会在前端直接拦截。生成文件期间取消按钮会禁用,因为主线程被占满时点击不会即时生效,避免让用户误以为没响应。
|
|
51
|
-
|
|
52
|
-
绝大多数实际文件在几千行以内,感知不到停顿。
|
|
53
|
-
|
|
54
27
|
## 字段定义
|
|
55
28
|
|
|
56
29
|
导入导出的一切都由 `FieldSchema[]` 驱动:导出用它生成表头,导入模板用它写填写要求,解析时用它逐格校验。
|
|
@@ -81,10 +54,10 @@ const fieldSchemas: FieldSchema[] = [
|
|
|
81
54
|
| `boolean` | required / true_label / false_label / default_value | 单元格填文案,接口值转成布尔 |
|
|
82
55
|
| `date` | required / default_value | 严格 `YYYY-MM-DD` |
|
|
83
56
|
| `time` | required / default_value | 严格 `HH:mm`,输出补零 |
|
|
84
|
-
| `datetime` | required / default_value | 严格 `YYYY-MM-DD HH:mm` |
|
|
85
|
-
| `list` | required / list_max_length / text_max_length / item_key? / default_value |
|
|
57
|
+
| `datetime` | required / default_value | 严格 `YYYY-MM-DD HH:mm` 或 `YYYY-MM-DD HH:mm:ss`,秒为 0 时不带秒 |
|
|
58
|
+
| `list` | required / list_max_length / text_max_length / item_key? / default_value | 中英文分号分隔,导出统一用英文分号 |
|
|
86
59
|
| `single_choice` | required / option_map / default_value | 单元格填文案,接口值取 option_map 里的值 |
|
|
87
|
-
| `multiple_choice` | required / option_map / default_value |
|
|
60
|
+
| `multiple_choice` | required / option_map / default_value | 中英文分号分隔,导出统一用英文分号 |
|
|
88
61
|
|
|
89
62
|
`importable: false` 的列只出现在导出文件里,不参与导入模板和解析。
|
|
90
63
|
|
|
@@ -106,34 +79,37 @@ const result = validateField("19.99", {
|
|
|
106
79
|
## 组件
|
|
107
80
|
|
|
108
81
|
```tsx
|
|
109
|
-
import { ImportButton, ExportButton,
|
|
82
|
+
import { ImportButton, ExportButton, exportRowsToFile } from "antd-import-export";
|
|
110
83
|
|
|
111
84
|
// 导入: 内置模板下载、文件解析、预览编辑、分批提交、行级错误回填
|
|
112
85
|
<ImportButton
|
|
113
86
|
title="供应商"
|
|
114
87
|
fieldSchemas={dataSchema}
|
|
115
|
-
importRows={(
|
|
116
|
-
|
|
88
|
+
importRows={(rows, signal) => supplierImportData(rows, signal)}
|
|
89
|
+
afterImport={onRefresh}
|
|
117
90
|
/>;
|
|
118
91
|
|
|
119
92
|
// 按当前筛选条件导出, 内部用游标分批拉取
|
|
120
93
|
<ExportButton
|
|
121
94
|
title="供应商"
|
|
122
95
|
fieldSchemas={dataSchema}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
fetchPage={(params, signal) => supplierExportData(params, signal)}
|
|
96
|
+
// 筛选条件在自己的 exportRows 里合并, 分页参数由包传入
|
|
97
|
+
exportRows={(params, signal) => supplierExportData({ ...queryForm, ...params }, signal)}
|
|
126
98
|
/>;
|
|
127
99
|
```
|
|
128
100
|
|
|
129
|
-
两个组件都不发请求,只调用你传进来的 `importRows` / `
|
|
101
|
+
两个组件都不发请求,只调用你传进来的 `importRows` / `exportRows`。
|
|
102
|
+
|
|
103
|
+
列表为空时用 `disabled` 禁用按钮,或在前置回调里拦截——组件本身不关心列表条数,上限以接口返回的总数为准。
|
|
104
|
+
|
|
105
|
+
退出登录或切换账号时调用 `resetTasks()`,会中止所有在途的导入导出并清空任务。
|
|
130
106
|
|
|
131
107
|
导出已勾选的行这类「数据已经在内存里」的场景,包内只提供函数,按钮由你自己渲染:
|
|
132
108
|
|
|
133
109
|
```tsx
|
|
134
|
-
import {
|
|
110
|
+
import { exportRowsToFile } from "antd-import-export";
|
|
135
111
|
|
|
136
|
-
await
|
|
112
|
+
await exportRowsToFile({ title: "供应商", fieldSchemas: dataSchema, rows: selectedRows });
|
|
137
113
|
```
|
|
138
114
|
|
|
139
115
|
### ImportButton
|
|
@@ -142,8 +118,12 @@ await exportRows({ title: "供应商", fieldSchemas: dataSchema, rows: selectedR
|
|
|
142
118
|
| --- | --- | --- |
|
|
143
119
|
| `title` | `string` | 按钮、模板文件名、任务标题共用 |
|
|
144
120
|
| `fieldSchemas` | `FieldSchema[]` | 字段定义 |
|
|
121
|
+
| `buttonText` | `string` | 导入按钮文案,默认「导入」 |
|
|
122
|
+
| `maxRows` | `number` | 单次导入上限,默认 2 万行;超出直接拦在提交前 |
|
|
123
|
+
| `batchSize` | `number` | 每批提交的行数,默认 20,范围 1–200 |
|
|
145
124
|
| `importRows` | `(rows, signal) => Promise<Record<string, unknown>[]>` | 一批行提交,返回等长结果;**空对象表示成功** |
|
|
146
|
-
| `
|
|
125
|
+
| `beforeImport` | `() => void \| Promise<void>` | 提交前触发,可做二次确认或自定义校验;抛错即中止 |
|
|
126
|
+
| `afterImport` | `() => void` | 任务结束(成功/失败/取消)时触发一次,通常用来刷新列表 |
|
|
147
127
|
|
|
148
128
|
### ExportButton
|
|
149
129
|
|
|
@@ -151,73 +131,96 @@ await exportRows({ title: "供应商", fieldSchemas: dataSchema, rows: selectedR
|
|
|
151
131
|
| --- | --- | --- |
|
|
152
132
|
| `title` | `string` | 文件名与任务标题 |
|
|
153
133
|
| `fieldSchemas` | `FieldSchema[]` | 字段定义 |
|
|
154
|
-
| `
|
|
155
|
-
| `
|
|
156
|
-
| `
|
|
157
|
-
| `
|
|
134
|
+
| `buttonText` | `string` | 按钮文案,默认「导出」 |
|
|
135
|
+
| `maxRows` | `number` | 单次导出上限,默认 2 万行;每页返回后都会校验 |
|
|
136
|
+
| `batchSize` | `number` | 每页请求条数,以 `page_size` 传给接口;不传由后端决定 |
|
|
137
|
+
| `exportRows` | `(params, signal) => Promise<{ count, results }>` | 游标分页;**每行必须带数字 `id`** |
|
|
138
|
+
| `beforeExport` | `() => void \| Promise<void>` | 创建任务前触发,可做二次确认或自定义校验;抛错即中止 |
|
|
139
|
+
| `afterExport` | `() => void` | 任务结束(成功/失败/取消)时触发一次 |
|
|
158
140
|
|
|
159
|
-
|
|
141
|
+
生成文件期间取消按钮会禁用:ExcelJS 的解析与生成是一整段同步执行,主线程被占满时 abort 只在结束后才生效。
|
|
142
|
+
|
|
143
|
+
### exportRowsToFile
|
|
160
144
|
|
|
161
145
|
| 参数 | 类型 | 说明 |
|
|
162
146
|
| --- | --- | --- |
|
|
163
147
|
| `title` | `string` | 用于生成 `标题_时间戳.xlsx` |
|
|
164
148
|
| `fieldSchemas` | `FieldSchema[]` | 字段定义 |
|
|
165
149
|
| `rows` | `object[]` | 已在内存中的行 |
|
|
150
|
+
| `maxRows` | `number` | 单次导出上限,默认 2 万行 |
|
|
166
151
|
|
|
167
152
|
返回 `Promise<Blob>`。空数组或超过 2 万行时**抛出可直接展示的错误**;不弹提示、不管理 loading,由调用方决定交互。
|
|
168
153
|
|
|
169
|
-
### 自定义触发器
|
|
170
|
-
|
|
171
|
-
想用自己的按钮、但要复用任务引擎和进度弹窗,可以直接组合:
|
|
172
|
-
|
|
173
|
-
```tsx
|
|
174
|
-
const [taskId, setTaskId] = useState<string>();
|
|
175
|
-
|
|
176
|
-
<button onClick={() => setTaskId(enqueueExport({ title, fileName, fieldSchemas, fetchPage }))}>导出</button>;
|
|
177
|
-
{
|
|
178
|
-
taskId ? <ExportTaskModal taskId={taskId} onClose={() => setTaskId(undefined)} /> : null;
|
|
179
|
-
}
|
|
180
|
-
```
|
|
181
|
-
|
|
182
154
|
### 导入接口需要幂等
|
|
183
155
|
|
|
184
156
|
一批请求失败时,前端无法确定服务端是否已经写入。任务会把这些行标成「结果未知,重试可能重复」,用户继续任务时会重新提交这一批数据。
|
|
185
157
|
|
|
186
158
|
因此导入接口应按业务唯一键(如编号)做「存在则更新、不存在则新增」,不要在无唯一约束的情况下纯追加。
|
|
187
159
|
|
|
188
|
-
###
|
|
160
|
+
### 导入数据约定
|
|
189
161
|
|
|
190
|
-
|
|
162
|
+
`importRows` 每批返回一个数组,**长度必须与请求的行数一致**,顺序一一对应:
|
|
191
163
|
|
|
192
164
|
| 返回 | 含义 |
|
|
193
165
|
| --- | --- |
|
|
194
166
|
| `{}` | 该行成功 |
|
|
195
|
-
| `{ "name": ["不能为空"] }` |
|
|
167
|
+
| `{ "name": ["不能为空"] }` | 该行字段级失败,错误回填到预览表格对应单元格 |
|
|
196
168
|
| `{ "detail": "名称已存在" }` | 该行整体失败 |
|
|
169
|
+
| `{ "warehouse": ["不存在"] }`(键不在字段定义里) | 同样按整体失败处理,提示里保留键名 |
|
|
170
|
+
| 返回长度与请求不一致 | 视为接口异常,任务暂停 |
|
|
197
171
|
| HTTP 4xx/5xx、网络异常 | 暂停整个任务,可继续或取消 |
|
|
198
172
|
|
|
199
|
-
|
|
173
|
+
除返回数组之外,`importRows` 抛出的任何异常都会暂停整个任务(取消时除外),不区分状态码。提示文案依次取:后端返回的 `detail` → HTTP 状态码对应的可读文案 → `error.message`。
|
|
200
174
|
|
|
201
|
-
|
|
202
|
-
2. 在 Next.js App Router 里使用时,引入这些组件的文件要加 `"use client"`。
|
|
175
|
+
### 导出数据约定
|
|
203
176
|
|
|
204
|
-
|
|
177
|
+
`exportRows` 每次拉一页数据,返回:
|
|
205
178
|
|
|
206
|
-
|
|
179
|
+
| 字段 | 类型 | 说明 |
|
|
180
|
+
| --- | --- | --- |
|
|
181
|
+
| `count` | `number` | 当前筛选条件下的总条数,用于进度显示与上限校验 |
|
|
182
|
+
| `results` | `object[]` | 本页数据;**每行必须有数字 `id`**,包用它做游标 |
|
|
207
183
|
|
|
208
|
-
|
|
209
|
-
import { parseImportFile, buildExportBlob, downloadImportTemplate } from "antd-import-export";
|
|
184
|
+
分页规则由包驱动,`exportRows` 收到的参数只有两种:
|
|
210
185
|
|
|
211
|
-
|
|
212
|
-
|
|
186
|
+
| 调用 | `params` |
|
|
187
|
+
| --- | --- |
|
|
188
|
+
| 第一页 | `{}` |
|
|
189
|
+
| 后续页 | `{ cursor_id: 上一页最后一条的 id }` |
|
|
213
190
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
191
|
+
配了 `batchSize` 时,两种调用都会多带一个 `page_size`。
|
|
192
|
+
|
|
193
|
+
`results` 返回空数组即视为没有更多数据,任务结束。每页条数由后端决定,包不传 `page_size`。
|
|
194
|
+
|
|
195
|
+
### 错误状态码的识别
|
|
217
196
|
|
|
218
|
-
|
|
197
|
+
包不认请求库,只从你抛出的错误对象上读状态码:
|
|
198
|
+
|
|
199
|
+
| 读取内容 | 顺序 |
|
|
200
|
+
| --- | --- |
|
|
201
|
+
| 状态码 | `error.response.status` → `error.status` → `error.statusCode` |
|
|
202
|
+
| 响应体 | `error.response.data` → `error.data` |
|
|
219
203
|
|
|
220
|
-
|
|
204
|
+
axios 的错误天然符合第一种形状。用 fetch 的时候,请把 `Response` 包成上面任一种形状再抛出,否则会被当成普通异常,只显示「导入失败, 任务已暂停」这类通用文案,而不是「登录已过期」这种具体提示。
|
|
205
|
+
|
|
206
|
+
### signal 的语义
|
|
207
|
+
|
|
208
|
+
`importRows` / `exportRows` 拿到的是同一个 `AbortSignal`,触发时机不同:
|
|
209
|
+
|
|
210
|
+
| 时机 | 是否触发 |
|
|
211
|
+
| --- | --- |
|
|
212
|
+
| 关闭弹窗、卸载页面 | ✅ |
|
|
213
|
+
| 退出登录 `resetTasks()` | ✅ |
|
|
214
|
+
| 导出点击「取消」 | ✅ |
|
|
215
|
+
| 导入点击「取消」 | ❌ 故意不触发 |
|
|
216
|
+
| 导入点击「暂停」 | ❌ 故意不触发 |
|
|
217
|
+
|
|
218
|
+
导入是写操作:中断已发出的批次后,无法确定服务端是否落库。所以取消和暂停都只停止领取新批次,等在途批次返回、拿到真实结果后再结束任务。导出是只读的,取消会立即中断请求。
|
|
219
|
+
|
|
220
|
+
### 两个使用前提
|
|
221
|
+
|
|
222
|
+
1. 组件用 `App.useApp()` 弹提示,根节点需要包一层 antd 的 `<App>`。
|
|
223
|
+
2. 在 Next.js App Router 里使用时,引入这些组件的文件要加 `"use client"`。
|
|
221
224
|
|
|
222
225
|
## 模块格式
|
|
223
226
|
|
package/README_EN.md
CHANGED
|
@@ -14,19 +14,6 @@
|
|
|
14
14
|
|
|
15
15
|
[Live demo](https://demo.29dev.cn/antd-import-export/)
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
## Upgrading from 1.0.4
|
|
19
|
-
|
|
20
|
-
1.0.5 renames two components and removes the batch export component:
|
|
21
|
-
|
|
22
|
-
| 1.0.4 and earlier | 1.0.5 |
|
|
23
|
-
| --- | --- |
|
|
24
|
-
| `ExcelImportButton` | `ImportButton` |
|
|
25
|
-
| `ExcelExportButton` | `ExportButton` |
|
|
26
|
-
| `BatchExportButton` | use `exportRows()` and render your own button (see [Components](#components)) |
|
|
27
|
-
|
|
28
|
-
The project is still under development; changes like this do not get their own major version yet.
|
|
29
|
-
|
|
30
17
|
## Requirements
|
|
31
18
|
|
|
32
19
|
| Package | Version |
|
|
@@ -36,20 +23,6 @@ The project is still under development; changes like this do not get their own m
|
|
|
36
23
|
| antd | >=6.0.0 |
|
|
37
24
|
| @ant-design/icons | >=5.0.0 |
|
|
38
25
|
|
|
39
|
-
## Large files
|
|
40
|
-
|
|
41
|
-
Parsing and generating happen on the main thread — that is how ExcelJS works, it ships no worker mode. Very large files block the UI briefly:
|
|
42
|
-
|
|
43
|
-
| Rows | Import parse | Export build |
|
|
44
|
-
| --- | --- | --- |
|
|
45
|
-
| 1,000 | ~25ms | ~60ms |
|
|
46
|
-
| 5,000 | ~125ms | ~300ms |
|
|
47
|
-
| 20,000 | ~500ms | ~1s |
|
|
48
|
-
|
|
49
|
-
The hard limit is 20,000 rows per operation. The cancel button is disabled while the file is being generated, because clicks cannot take effect while the main thread is busy.
|
|
50
|
-
|
|
51
|
-
Most real-world files stay in the low thousands of rows and show no noticeable pause.
|
|
52
|
-
|
|
53
26
|
## Field schema
|
|
54
27
|
|
|
55
28
|
Everything is driven by `FieldSchema[]`: export headers, template hints and cell-level validation.
|
|
@@ -74,10 +47,10 @@ const fieldSchemas: FieldSchema[] = [
|
|
|
74
47
|
| `boolean` | required / true_label / false_label / default_value | labels in the cell, boolean on the wire |
|
|
75
48
|
| `date` | required / default_value | strict `YYYY-MM-DD` |
|
|
76
49
|
| `time` | required / default_value | strict `HH:mm` |
|
|
77
|
-
| `datetime` | required / default_value | strict `YYYY-MM-DD HH:mm` |
|
|
78
|
-
| `list` | required / list_max_length / text_max_length / item_key? / default_value |
|
|
50
|
+
| `datetime` | required / default_value | strict `YYYY-MM-DD HH:mm` or `YYYY-MM-DD HH:mm:ss`; seconds are dropped when zero |
|
|
51
|
+
| `list` | required / list_max_length / text_max_length / item_key? / default_value | Chinese or English semicolons; exports always use `;` |
|
|
79
52
|
| `single_choice` | required / option_map / default_value | label in the cell, mapped value on the wire |
|
|
80
|
-
| `multiple_choice` | required / option_map / default_value |
|
|
53
|
+
| `multiple_choice` | required / option_map / default_value | Chinese or English semicolons; exports always use `;` |
|
|
81
54
|
|
|
82
55
|
Columns marked `importable: false` only appear in exported files.
|
|
83
56
|
|
|
@@ -98,32 +71,35 @@ validateField("19.99", {
|
|
|
98
71
|
## Components
|
|
99
72
|
|
|
100
73
|
```tsx
|
|
101
|
-
import { ImportButton, ExportButton,
|
|
74
|
+
import { ImportButton, ExportButton, exportRowsToFile } from "antd-import-export";
|
|
102
75
|
|
|
103
76
|
<ImportButton
|
|
104
77
|
title="Supplier"
|
|
105
78
|
fieldSchemas={fieldSchemas}
|
|
106
|
-
importRows={(
|
|
107
|
-
|
|
79
|
+
importRows={(rows, signal) => importSuppliers(rows, signal)}
|
|
80
|
+
afterImport={refresh}
|
|
108
81
|
/>;
|
|
109
82
|
|
|
110
83
|
<ExportButton
|
|
111
84
|
title="Supplier"
|
|
112
85
|
fieldSchemas={fieldSchemas}
|
|
113
|
-
filters
|
|
114
|
-
|
|
115
|
-
fetchPage={(params, signal) => exportSuppliers(params, signal)}
|
|
86
|
+
// merge your own filters inside exportRows; paging params come from the package
|
|
87
|
+
exportRows={(params, signal) => exportSuppliers({ ...queryForm, ...params }, signal)}
|
|
116
88
|
/>;
|
|
117
89
|
```
|
|
118
90
|
|
|
119
|
-
Neither component issues requests: they call the `importRows` / `
|
|
91
|
+
Neither component issues requests: they call the `importRows` / `exportRows` you provide.
|
|
92
|
+
|
|
93
|
+
Disable the button when the list is empty, or reject inside the before callback — the component does not know the row count, and the limit comes from the API response.
|
|
94
|
+
|
|
95
|
+
Call `resetTasks()` on logout or account switch to abort every in-flight import/export and clear the task list.
|
|
120
96
|
|
|
121
97
|
To export rows that are already in memory, render your own button and call the function:
|
|
122
98
|
|
|
123
99
|
```tsx
|
|
124
|
-
import {
|
|
100
|
+
import { exportRowsToFile } from "antd-import-export";
|
|
125
101
|
|
|
126
|
-
await
|
|
102
|
+
await exportRowsToFile({ title: "Supplier", fieldSchemas, rows: selectedRows });
|
|
127
103
|
```
|
|
128
104
|
|
|
129
105
|
### ImportButton
|
|
@@ -132,8 +108,12 @@ await exportRows({ title: "Supplier", fieldSchemas, rows: selectedRows });
|
|
|
132
108
|
| --- | --- | --- |
|
|
133
109
|
| `title` | `string` | shared by the button, template file name and task title |
|
|
134
110
|
| `fieldSchemas` | `FieldSchema[]` | field definitions |
|
|
111
|
+
| `buttonText` | `string` | import button label, defaults to `导入` |
|
|
112
|
+
| `maxRows` | `number` | per-import limit, defaults to 20,000 rows |
|
|
113
|
+
| `batchSize` | `number` | rows submitted per batch, defaults to 20 (1–200) |
|
|
135
114
|
| `importRows` | `(rows, signal) => Promise<Record<string, unknown>[]>` | submits one batch, returns the same length; **empty object means success** |
|
|
136
|
-
| `
|
|
115
|
+
| `beforeImport` | `() => void \| Promise<void>` | runs before submitting; throw to abort |
|
|
116
|
+
| `afterImport` | `() => void` | fires once when the task ends (completed / failed / canceled) |
|
|
137
117
|
|
|
138
118
|
### ExportButton
|
|
139
119
|
|
|
@@ -141,54 +121,90 @@ await exportRows({ title: "Supplier", fieldSchemas, rows: selectedRows });
|
|
|
141
121
|
| --- | --- | --- |
|
|
142
122
|
| `title` | `string` | file name and task title |
|
|
143
123
|
| `fieldSchemas` | `FieldSchema[]` | field definitions |
|
|
144
|
-
| `
|
|
145
|
-
| `
|
|
146
|
-
| `
|
|
147
|
-
| `
|
|
124
|
+
| `buttonText` | `string` | button label, defaults to `导出` |
|
|
125
|
+
| `maxRows` | `number` | per-export limit, defaults to 20,000 rows; checked after every page |
|
|
126
|
+
| `batchSize` | `number` | rows per page, sent as `page_size`; omitted means the backend decides |
|
|
127
|
+
| `exportRows` | `(params, signal) => Promise<{ count, results }>` | cursor paging; **every row needs a numeric `id`** |
|
|
128
|
+
| `beforeExport` | `() => void \| Promise<void>` | runs before the task is created; throw to abort |
|
|
129
|
+
| `afterExport` | `() => void` | fires once when the task ends (completed / failed / canceled) |
|
|
130
|
+
|
|
131
|
+
The cancel button is disabled while the file is generated: ExcelJS parses and writes in one synchronous block, so an abort only takes effect after it finishes.
|
|
148
132
|
|
|
149
|
-
###
|
|
133
|
+
### exportRowsToFile
|
|
150
134
|
|
|
151
135
|
| option | type | notes |
|
|
152
136
|
| --- | --- | --- |
|
|
153
137
|
| `title` | `string` | used to build `title_timestamp.xlsx` |
|
|
154
138
|
| `fieldSchemas` | `FieldSchema[]` | field definitions |
|
|
155
139
|
| `rows` | `object[]` | rows already in memory |
|
|
140
|
+
| `maxRows` | `number` | per-export limit, defaults to 20,000 rows |
|
|
156
141
|
|
|
157
142
|
Returns `Promise<Blob>`. Throws a ready-to-display error when `rows` is empty or exceeds 20,000; it does not show messages or manage loading state.
|
|
158
143
|
|
|
159
|
-
###
|
|
144
|
+
### Import contract
|
|
160
145
|
|
|
161
|
-
|
|
146
|
+
`importRows` returns one array per batch; **its length must match the submitted rows**, in the same order:
|
|
162
147
|
|
|
163
|
-
|
|
164
|
-
|
|
148
|
+
| Response | Meaning |
|
|
149
|
+
| --- | --- |
|
|
150
|
+
| `{}` | the row succeeded |
|
|
151
|
+
| `{ "name": ["required"] }` | field-level failure, mapped back to the preview cell |
|
|
152
|
+
| `{ "detail": "name already exists" }` | the whole row failed |
|
|
153
|
+
| `{ "warehouse": ["not found"] }` (key not in the field schema) | also treated as a whole-row failure, message keeps the key |
|
|
154
|
+
| length mismatch | treated as an API error, task pauses |
|
|
155
|
+
| HTTP 4xx/5xx, network error | pauses the whole task, resumable |
|
|
165
156
|
|
|
166
|
-
|
|
167
|
-
{
|
|
168
|
-
taskId ? <ExportTaskModal taskId={taskId} onClose={() => setTaskId(undefined)} /> : null;
|
|
169
|
-
}
|
|
170
|
-
```
|
|
157
|
+
Beyond returning the array, any error thrown by `importRows` pauses the whole task (except when canceled), regardless of the status code. The message is taken from, in order: the backend `detail` → a readable text for the HTTP status → `error.message`.
|
|
171
158
|
|
|
172
|
-
###
|
|
159
|
+
### Export contract
|
|
173
160
|
|
|
174
|
-
|
|
175
|
-
2. In Next.js App Router, add `"use client"` to the file that imports them.
|
|
161
|
+
`exportRows` fetches one page at a time and returns:
|
|
176
162
|
|
|
177
|
-
|
|
163
|
+
| Field | Type | Notes |
|
|
164
|
+
| --- | --- | --- |
|
|
165
|
+
| `count` | `number` | total rows under the current filters, used for progress and the row limit |
|
|
166
|
+
| `results` | `object[]` | rows of this page; **every row needs a numeric `id`** for the cursor |
|
|
178
167
|
|
|
179
|
-
|
|
168
|
+
Paging is driven by the package, so `exportRows` only ever receives:
|
|
180
169
|
|
|
181
|
-
|
|
182
|
-
|
|
170
|
+
| Call | `params` |
|
|
171
|
+
| --- | --- |
|
|
172
|
+
| first page | `{}` |
|
|
173
|
+
| following pages | `{ cursor_id: id of the last row of the previous page }` |
|
|
183
174
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
175
|
+
With `batchSize` set, both calls also carry `page_size`.
|
|
176
|
+
|
|
177
|
+
An empty `results` array ends the task. Page size is up to the backend; the package does not send `page_size`.
|
|
178
|
+
|
|
179
|
+
### How status codes are detected
|
|
180
|
+
|
|
181
|
+
The package does not know your HTTP client; it reads the status from the error you throw:
|
|
182
|
+
|
|
183
|
+
| What | Lookup order |
|
|
184
|
+
| --- | --- |
|
|
185
|
+
| status | `error.response.status` → `error.status` → `error.statusCode` |
|
|
186
|
+
| body | `error.response.data` → `error.data` |
|
|
188
187
|
|
|
189
|
-
|
|
188
|
+
axios errors already match the first shape. With fetch, wrap the `Response` into one of these shapes before throwing, otherwise it is treated as a generic error and shows a generic message instead of "session expired".
|
|
190
189
|
|
|
191
|
-
|
|
190
|
+
### What `signal` means
|
|
191
|
+
|
|
192
|
+
`importRows` / `exportRows` receive the same `AbortSignal`, but it fires at different moments:
|
|
193
|
+
|
|
194
|
+
| Moment | Fires |
|
|
195
|
+
| --- | --- |
|
|
196
|
+
| modal closed, page unmounted | ✅ |
|
|
197
|
+
| logout via `resetTasks()` | ✅ |
|
|
198
|
+
| export canceled | ✅ |
|
|
199
|
+
| import canceled | ❌ by design |
|
|
200
|
+
| import paused | ❌ by design |
|
|
201
|
+
|
|
202
|
+
Import writes data: after aborting an in-flight batch there is no way to know whether the server committed it. Cancel and pause therefore only stop claiming new batches and wait for in-flight ones to settle. Export is read-only, so canceling aborts immediately.
|
|
203
|
+
|
|
204
|
+
### Two prerequisites
|
|
205
|
+
|
|
206
|
+
1. They use `App.useApp()`, so wrap the root with antd's `<App>`.
|
|
207
|
+
2. In Next.js App Router, add `"use client"` to the file that imports them.
|
|
192
208
|
|
|
193
209
|
## Module format
|
|
194
210
|
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { ImportRow } from "../core/excel/importRow";
|
|
3
|
+
import type { FieldSchema } from "../core/schema";
|
|
4
|
+
interface EditableImportCellProps {
|
|
5
|
+
field: FieldSchema;
|
|
6
|
+
record: ImportRow;
|
|
7
|
+
disabled: boolean;
|
|
8
|
+
onChange: (value: unknown) => void;
|
|
9
|
+
onSave: (value: unknown) => void;
|
|
10
|
+
}
|
|
11
|
+
declare const EditableImportCell: React.FC<EditableImportCellProps>;
|
|
12
|
+
export default EditableImportCell;
|
|
@@ -3,13 +3,20 @@ import type { FieldSchema } from "../core/schema";
|
|
|
3
3
|
import type { PagedData } from "../core/types";
|
|
4
4
|
export interface ExportButtonProps {
|
|
5
5
|
title: string;
|
|
6
|
-
buttonText?: string;
|
|
7
6
|
fieldSchemas: FieldSchema[];
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
/** 按钮文案, 默认「导出」 */
|
|
8
|
+
buttonText?: string;
|
|
9
|
+
/** 单次导出上限, 默认 2 万行 */
|
|
10
|
+
maxRows?: number;
|
|
11
|
+
/** 每页请求的条数, 以 page_size 传给接口; 不传由后端决定 */
|
|
12
|
+
batchSize?: number;
|
|
11
13
|
disabled?: boolean;
|
|
12
|
-
|
|
14
|
+
/** 拉取一页待导出的数据; 分页参数由包传入, 筛选条件在这里合并 */
|
|
15
|
+
exportRows: (params: Record<string, unknown>, signal: AbortSignal) => Promise<PagedData<object>>;
|
|
16
|
+
/** 创建任务前触发, 可做二次确认或自定义校验; 抛错则中止, 不创建任务 */
|
|
17
|
+
beforeExport?: () => void | Promise<void>;
|
|
18
|
+
/** 导出任务结束(成功/失败/取消)时回调一次 */
|
|
19
|
+
afterExport?: () => void;
|
|
13
20
|
}
|
|
14
21
|
declare const ExportButton: React.FC<ExportButtonProps>;
|
|
15
22
|
export default ExportButton;
|
|
@@ -4,8 +4,18 @@ import type { FieldSchema } from "../core/schema";
|
|
|
4
4
|
export interface ImportButtonProps {
|
|
5
5
|
title: string;
|
|
6
6
|
fieldSchemas: FieldSchema[];
|
|
7
|
+
/** 导入按钮文案, 默认「导入」; 模板按钮文案固定 */
|
|
8
|
+
buttonText?: string;
|
|
9
|
+
/** 单次导入上限, 默认 2 万行 */
|
|
10
|
+
maxRows?: number;
|
|
11
|
+
/** 每批提交的行数, 默认 20, 范围 1-200 */
|
|
12
|
+
batchSize?: number;
|
|
13
|
+
/** 提交一批待导入的行, 返回等长的逐行结果; 空对象表示成功 */
|
|
7
14
|
importRows: (rows: Record<string, unknown>[], signal: AbortSignal) => Promise<ImportRowErrorBody[]>;
|
|
8
|
-
|
|
15
|
+
/** 开始提交前触发, 可做二次确认或自定义校验; 抛错则中止, 不创建任务 */
|
|
16
|
+
beforeImport?: () => void | Promise<void>;
|
|
17
|
+
/** 导入任务结束(成功/失败/取消)时回调一次, 通常用来刷新列表 */
|
|
18
|
+
afterImport: () => void;
|
|
9
19
|
}
|
|
10
20
|
declare const ImportButton: React.FC<ImportButtonProps>;
|
|
11
21
|
export default ImportButton;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ImportRowErrorBody, ImportRowResult } from "../core/types";
|
|
2
|
+
/** DRF 的列表类型技术错误对用户没有意义, 统一成可读提示 */
|
|
3
|
+
export declare function normalizeImportErrorMessage(message: string): string;
|
|
4
|
+
/**
|
|
5
|
+
* 把接口返回的单行错误体转成任务行结果。
|
|
6
|
+
* 只有 fieldKeys 里的键才按字段级错误处理; 其余键(后端新增字段、关联对象错误等)
|
|
7
|
+
* 在预览表格里没有对应单元格, 统一并入整体错误, 避免错误被静默吞掉。
|
|
8
|
+
*/
|
|
9
|
+
export declare function parseImportBatchResult(result: ImportRowErrorBody, fieldKeys: Set<string>): ImportRowResult;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ImportRow } from "../core/excel/importRow";
|
|
2
|
+
import type { TaskItem } from "../core/task";
|
|
3
|
+
export type DataStatusKey = "valid" | "invalid";
|
|
4
|
+
export type ImportStatusKey = "pending" | "running" | "completed" | "failed" | "canceled";
|
|
5
|
+
export declare function getTaskMeta(task: TaskItem): {
|
|
6
|
+
label: string;
|
|
7
|
+
color: string;
|
|
8
|
+
};
|
|
9
|
+
export declare function getRemainingTimeText(task: TaskItem, remainingSeconds: number | null): string | null;
|
|
10
|
+
export declare function getTaskProgressStatus(task: TaskItem): "exception" | "success" | "normal";
|
|
11
|
+
export declare const dataStatusMeta: Record<DataStatusKey, {
|
|
12
|
+
label: string;
|
|
13
|
+
color: string;
|
|
14
|
+
}>;
|
|
15
|
+
export declare const importStatusMeta: Record<ImportStatusKey, {
|
|
16
|
+
label: string;
|
|
17
|
+
color: string;
|
|
18
|
+
}>;
|
|
19
|
+
export declare function getDataStatusKey(record: ImportRow): DataStatusKey;
|
|
20
|
+
export declare function getImportStatusKey(record: ImportRow, taskId: string | undefined, task: TaskItem | undefined): ImportStatusKey;
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import type { FieldSchema } from "../schema";
|
|
2
2
|
/** 生成 `标题_20261123153300` 形式的文件名(不含扩展名) */
|
|
3
3
|
export declare function makeExportFileName(title: string): string;
|
|
4
|
-
|
|
5
|
-
export declare function formatExportRowLimitMessage(rowCount: number): string | null;
|
|
6
|
-
export interface ExportRowsOptions {
|
|
4
|
+
export interface ExportRowsToFileOptions {
|
|
7
5
|
title: string;
|
|
8
6
|
fieldSchemas: FieldSchema[];
|
|
9
7
|
rows: object[];
|
|
8
|
+
/** 单次导出上限, 默认 2 万行 */
|
|
9
|
+
maxRows?: number;
|
|
10
10
|
}
|
|
11
11
|
/**
|
|
12
12
|
* 导出内存中的一批行: 校验条数上限、按约定生成文件名并下载。
|
|
13
13
|
* 只负责数据到文件, 不弹提示也不管理 loading;
|
|
14
14
|
* 没有数据或超出上限时抛出可直接展示的错误, 由调用方决定如何反馈。
|
|
15
15
|
*/
|
|
16
|
-
export declare function
|
|
16
|
+
export declare function exportRowsToFile(options: ExportRowsToFileOptions): Promise<Blob>;
|
package/dist/core/httpError.d.ts
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* HTTP 错误的最小形状, 不绑定具体请求库:
|
|
3
|
+
* - axios / 多数 fetch 封装: `response.status` + `response.data`
|
|
4
|
+
* - 直接抛出的自定义错误: `status` / `statusCode` + `data`
|
|
5
|
+
* 认不出的错误由调用方按普通异常处理。
|
|
6
|
+
*/
|
|
2
7
|
export interface HttpErrorShape {
|
|
3
8
|
isAxiosError?: boolean;
|
|
4
9
|
response?: {
|
|
5
10
|
status?: number;
|
|
6
11
|
data?: unknown;
|
|
7
12
|
};
|
|
13
|
+
status?: number;
|
|
14
|
+
statusCode?: number;
|
|
15
|
+
data?: unknown;
|
|
8
16
|
}
|
|
9
17
|
/** HTTP 失败的大类; 具体文案由调用方决定 */
|
|
10
18
|
export type HttpFailureKind = "network" | "unauthorized" | "forbidden" | "notFound" | "badRequest" | "server" | "unknown";
|
|
11
|
-
/** 是否像一次 HTTP 请求失败:
|
|
19
|
+
/** 是否像一次 HTTP 请求失败: 带状态码, 或请求库明确标记过 */
|
|
12
20
|
export declare function isHttpError(error: unknown): error is HttpErrorShape;
|
|
13
21
|
/** 取 HTTP 状态码; 网络中断这类没有响应的错误返回 undefined */
|
|
14
22
|
export declare function httpStatusOf(error: unknown): number | undefined;
|
|
@@ -19,3 +27,8 @@ export declare function httpResponseData(error: unknown): unknown;
|
|
|
19
27
|
* 返回 undefined 表示不是 HTTP 失败, 调用方应按普通异常处理。
|
|
20
28
|
*/
|
|
21
29
|
export declare function classifyHttpFailure(error: unknown): HttpFailureKind | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* HTTP 失败的提示文案; 不是 HTTP 失败时返回 undefined, 由调用方兜底。
|
|
32
|
+
* `direction` 用于区分导入/导出, `tail` 是业务侧的动作说明。
|
|
33
|
+
*/
|
|
34
|
+
export declare function describeHttpFailure(error: unknown, direction: "导入" | "导出", tail: string): string | undefined;
|
package/dist/core/limits.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/** 单次导入/导出的默认行数上限 */
|
|
2
2
|
export declare const MAX_IMPORT_ROWS = 20000;
|
|
3
3
|
export declare const MAX_EXPORT_ROWS = 20000;
|
|
4
4
|
/** 单个导入文件大小上限 */
|
|
5
5
|
export declare const MAX_IMPORT_FILE_SIZE: number;
|
|
6
|
+
/** 条数超出上限时返回可直接展示的提示, 未超出返回 null */
|
|
7
|
+
export declare function formatRowLimitMessage(rowCount: number, maxRows: number, direction: "导入" | "导出"): string | null;
|
|
8
|
+
/** 超出上限时抛出可直接展示的错误 */
|
|
9
|
+
export declare function assertRowLimit(rowCount: number, maxRows: number, direction: "导入" | "导出"): void;
|