antd-import-export 1.0.6 → 1.0.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/README.md +104 -22
- package/README_EN.md +113 -18
- package/dist/components/ExportButton.d.ts +4 -4
- package/dist/components/ImportButton.d.ts +7 -4
- package/dist/components/importBatchResult.d.ts +9 -0
- package/dist/core/excel/exportFile.d.ts +2 -4
- package/dist/core/httpError.d.ts +15 -2
- package/dist/core/limits.d.ts +0 -7
- package/dist/core/task/types.d.ts +2 -4
- package/dist/index.cjs +6 -6
- package/dist/index.d.ts +1 -4
- package/dist/index.mjs +608 -646
- package/dist/react/useActiveTask.d.ts +5 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -93,7 +93,6 @@ import { ImportButton, ExportButton, exportRowsToFile } from "antd-import-export
|
|
|
93
93
|
<ExportButton
|
|
94
94
|
title="供应商"
|
|
95
95
|
fieldSchemas={dataSchema}
|
|
96
|
-
totalCount={dataCount}
|
|
97
96
|
// 筛选条件在自己的 exportRows 里合并, 分页参数由包传入
|
|
98
97
|
exportRows={(params, signal) => supplierExportData({ ...queryForm, ...params }, signal)}
|
|
99
98
|
/>;
|
|
@@ -101,6 +100,8 @@ import { ImportButton, ExportButton, exportRowsToFile } from "antd-import-export
|
|
|
101
100
|
|
|
102
101
|
两个组件都不发请求,只调用你传进来的 `importRows` / `exportRows`。
|
|
103
102
|
|
|
103
|
+
列表为空时用 `disabled` 禁用按钮,或在前置回调里拦截——组件不关心列表条数,也不内置条数上限。
|
|
104
|
+
|
|
104
105
|
退出登录或切换账号时调用 `resetTasks()`,会中止所有在途的导入导出并清空任务。
|
|
105
106
|
|
|
106
107
|
导出已勾选的行这类「数据已经在内存里」的场景,包内只提供函数,按钮由你自己渲染:
|
|
@@ -117,9 +118,10 @@ await exportRowsToFile({ title: "供应商", fieldSchemas: dataSchema, rows: sel
|
|
|
117
118
|
| --- | --- | --- |
|
|
118
119
|
| `title` | `string` | 按钮、模板文件名、任务标题共用 |
|
|
119
120
|
| `fieldSchemas` | `FieldSchema[]` | 字段定义 |
|
|
120
|
-
| `
|
|
121
|
+
| `buttonText` | `string` | 导入按钮文案,默认「导入」 |
|
|
122
|
+
| `batchSize` | `number` | 每批提交的行数,默认 20,范围 1–200 |
|
|
121
123
|
| `importRows` | `(rows, signal) => Promise<Record<string, unknown>[]>` | 一批行提交,返回等长结果;**空对象表示成功** |
|
|
122
|
-
| `beforeImport` | `() => void \| Promise<void>` |
|
|
124
|
+
| `beforeImport` | `(rows) => void \| Promise<void>` | 解析完成、打开预览前触发;`rows` 是解析并校验过的行;抛错即中止 |
|
|
123
125
|
| `afterImport` | `() => void` | 任务结束(成功/失败/取消)时触发一次,通常用来刷新列表 |
|
|
124
126
|
|
|
125
127
|
### ExportButton
|
|
@@ -128,12 +130,14 @@ await exportRowsToFile({ title: "供应商", fieldSchemas: dataSchema, rows: sel
|
|
|
128
130
|
| --- | --- | --- |
|
|
129
131
|
| `title` | `string` | 文件名与任务标题 |
|
|
130
132
|
| `fieldSchemas` | `FieldSchema[]` | 字段定义 |
|
|
131
|
-
| `
|
|
132
|
-
| `
|
|
133
|
+
| `buttonText` | `string` | 按钮文案,默认「导出」 |
|
|
134
|
+
| `batchSize` | `number` | 每页请求条数,以 `page_size` 传给接口;不传由后端决定 |
|
|
133
135
|
| `exportRows` | `(params, signal) => Promise<{ count, results }>` | 游标分页;**每行必须带数字 `id`** |
|
|
134
136
|
| `beforeExport` | `() => void \| Promise<void>` | 创建任务前触发,可做二次确认或自定义校验;抛错即中止 |
|
|
135
137
|
| `afterExport` | `() => void` | 任务结束(成功/失败/取消)时触发一次 |
|
|
136
138
|
|
|
139
|
+
生成文件期间取消按钮会禁用:ExcelJS 的解析与生成是一整段同步执行,主线程被占满时 abort 只在结束后才生效。
|
|
140
|
+
|
|
137
141
|
### exportRowsToFile
|
|
138
142
|
|
|
139
143
|
| 参数 | 类型 | 说明 |
|
|
@@ -141,7 +145,6 @@ await exportRowsToFile({ title: "供应商", fieldSchemas: dataSchema, rows: sel
|
|
|
141
145
|
| `title` | `string` | 用于生成 `标题_时间戳.xlsx` |
|
|
142
146
|
| `fieldSchemas` | `FieldSchema[]` | 字段定义 |
|
|
143
147
|
| `rows` | `object[]` | 已在内存中的行 |
|
|
144
|
-
| `maxRows` | `number` | 单次导出上限,默认 2 万行 |
|
|
145
148
|
|
|
146
149
|
返回 `Promise<Blob>`。空数组或超过 2 万行时**抛出可直接展示的错误**;不弹提示、不管理 loading,由调用方决定交互。
|
|
147
150
|
|
|
@@ -151,39 +154,118 @@ await exportRowsToFile({ title: "供应商", fieldSchemas: dataSchema, rows: sel
|
|
|
151
154
|
|
|
152
155
|
因此导入接口应按业务唯一键(如编号)做「存在则更新、不存在则新增」,不要在无唯一约束的情况下纯追加。
|
|
153
156
|
|
|
154
|
-
###
|
|
157
|
+
### 导入数据约定
|
|
155
158
|
|
|
156
|
-
|
|
159
|
+
`importRows` 每批返回一个数组,**长度必须与请求的行数一致**,顺序一一对应:
|
|
157
160
|
|
|
158
161
|
| 返回 | 含义 |
|
|
159
162
|
| --- | --- |
|
|
160
163
|
| `{}` | 该行成功 |
|
|
161
|
-
| `{ "name": ["不能为空"] }` |
|
|
164
|
+
| `{ "name": ["不能为空"] }` | 该行字段级失败,错误回填到预览表格对应单元格 |
|
|
162
165
|
| `{ "detail": "名称已存在" }` | 该行整体失败 |
|
|
166
|
+
| `{ "warehouse": ["不存在"] }`(键不在字段定义里) | 同样按整体失败处理,提示里保留键名 |
|
|
167
|
+
| 返回长度与请求不一致 | 视为接口异常,任务暂停 |
|
|
163
168
|
| HTTP 4xx/5xx、网络异常 | 暂停整个任务,可继续或取消 |
|
|
164
169
|
|
|
165
|
-
|
|
170
|
+
除返回数组之外,`importRows` 抛出的任何异常都会暂停整个任务(取消时除外),不区分状态码。提示文案依次取:后端返回的 `detail` → HTTP 状态码对应的可读文案 → `error.message`。
|
|
166
171
|
|
|
167
|
-
|
|
168
|
-
2. 在 Next.js App Router 里使用时,引入这些组件的文件要加 `"use client"`。
|
|
172
|
+
### beforeImport 拿到什么
|
|
169
173
|
|
|
170
|
-
|
|
174
|
+
`beforeImport(rows)` 在**解析完成、打开预览之前**触发,拿到的是解析并逐格校验过的行:
|
|
171
175
|
|
|
172
|
-
|
|
176
|
+
| 内容 | 是否包含 |
|
|
177
|
+
| --- | --- |
|
|
178
|
+
| 解析出的行 | ✅ |
|
|
179
|
+
| 逐格校验结果 | ✅ 在 `errors` 里 |
|
|
180
|
+
| 用户的编辑 | ❌ 此时还没进入预览,自然没有编辑 |
|
|
181
|
+
| 用户删除的行 | ❌ 同上 |
|
|
173
182
|
|
|
174
|
-
|
|
175
|
-
|
|
183
|
+
抛错即中止,**预览不会打开**。典型用途是条数上限和准入判断:
|
|
184
|
+
|
|
185
|
+
```tsx
|
|
186
|
+
beforeImport={(rows) => {
|
|
187
|
+
if (rows.length > 20000) {
|
|
188
|
+
throw new Error(`导入数据 ${rows.length.toLocaleString()} 条,超过上限 20,000 条,请分批导入`);
|
|
189
|
+
}
|
|
190
|
+
}}
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
`rows` 里可能含校验失败的行(`errors` 非空),它们后续提交时会被跳过;如果只想按可提交的行数判断,自行过滤即可。
|
|
194
|
+
|
|
195
|
+
### 导出数据约定
|
|
196
|
+
|
|
197
|
+
`exportRows` 每次拉一页数据,返回:
|
|
198
|
+
|
|
199
|
+
| 字段 | 类型 | 说明 |
|
|
200
|
+
| --- | --- | --- |
|
|
201
|
+
| `count` | `number` | 当前筛选条件下的总条数,用于进度显示 |
|
|
202
|
+
| `results` | `object[]` | 本页数据;**每行必须有数字 `id`**,包用它做游标 |
|
|
203
|
+
|
|
204
|
+
分页规则由包驱动,`exportRows` 收到的参数只有两种:
|
|
205
|
+
|
|
206
|
+
| 调用 | `params` |
|
|
207
|
+
| --- | --- |
|
|
208
|
+
| 第一页 | `{}` |
|
|
209
|
+
| 后续页 | `{ cursor_id: 上一页最后一条的 id }` |
|
|
210
|
+
|
|
211
|
+
配了 `batchSize` 时,两种调用都会多带一个 `page_size`。
|
|
212
|
+
|
|
213
|
+
`results` 返回空数组即视为没有更多数据,任务结束。每页条数由后端决定,包不传 `page_size`。
|
|
176
214
|
|
|
177
|
-
|
|
178
|
-
// { headerMessages, formulaMessages, rows, includedKeys }
|
|
215
|
+
### 错误状态码的识别
|
|
179
216
|
|
|
180
|
-
|
|
181
|
-
|
|
217
|
+
包不认请求库,只从你抛出的错误对象上读状态码:
|
|
218
|
+
|
|
219
|
+
| 读取内容 | 顺序 |
|
|
220
|
+
| --- | --- |
|
|
221
|
+
| 状态码 | `error.response.status` → `error.status` → `error.statusCode` |
|
|
222
|
+
| 响应体 | `error.response.data` → `error.data` |
|
|
223
|
+
|
|
224
|
+
axios 的错误天然符合第一种形状。用 fetch 的时候,请把 `Response` 包成上面任一种形状再抛出,否则会被当成普通异常,只显示「导入失败, 任务已暂停」这类通用文案,而不是「登录已过期」这种具体提示。
|
|
225
|
+
|
|
226
|
+
### signal 的语义
|
|
227
|
+
|
|
228
|
+
`importRows` / `exportRows` 拿到的是同一个 `AbortSignal`,触发时机不同:
|
|
229
|
+
|
|
230
|
+
| 时机 | 是否触发 |
|
|
231
|
+
| --- | --- |
|
|
232
|
+
| 关闭弹窗、卸载页面 | ✅ |
|
|
233
|
+
| 退出登录 `resetTasks()` | ✅ |
|
|
234
|
+
| 导出点击「取消」 | ✅ |
|
|
235
|
+
| 导入点击「取消」 | ❌ 故意不触发 |
|
|
236
|
+
| 导入点击「暂停」 | ❌ 故意不触发 |
|
|
237
|
+
|
|
238
|
+
导入是写操作:中断已发出的批次后,无法确定服务端是否落库。所以取消和暂停都只停止领取新批次,等在途批次返回、拿到真实结果后再结束任务。导出是只读的,取消会立即中断请求。
|
|
239
|
+
|
|
240
|
+
### 条数上限由调用方决定
|
|
241
|
+
|
|
242
|
+
包不内置条数上限,也不在请求过程中拦截。是否限制、限多少,由你在前置回调里决定:
|
|
243
|
+
|
|
244
|
+
```tsx
|
|
245
|
+
<ExportButton
|
|
246
|
+
beforeExport={() => {
|
|
247
|
+
// 上限写死在这里, 按业务需要调整
|
|
248
|
+
if (dataCount > 20000) {
|
|
249
|
+
throw new Error(`导出数据 ${dataCount.toLocaleString()} 条,超过上限 20,000 条,请分批导出`);
|
|
250
|
+
}
|
|
251
|
+
}}
|
|
252
|
+
/>
|
|
253
|
+
|
|
254
|
+
<ImportButton
|
|
255
|
+
beforeImport={(rows) => {
|
|
256
|
+
if (rows.length > 20000) {
|
|
257
|
+
throw new Error(`导入数据 ${rows.length.toLocaleString()} 条,超过上限 20,000 条`);
|
|
258
|
+
}
|
|
259
|
+
}}
|
|
260
|
+
/>
|
|
182
261
|
```
|
|
183
262
|
|
|
184
|
-
|
|
263
|
+
**不写检查就不会有任何限制**——两万行以上的导出会让主线程明显卡顿,请按需设置。
|
|
185
264
|
|
|
186
|
-
|
|
265
|
+
### 两个使用前提
|
|
266
|
+
|
|
267
|
+
1. 组件用 `App.useApp()` 弹提示,根节点需要包一层 antd 的 `<App>`。
|
|
268
|
+
2. 在 Next.js App Router 里使用时,引入这些组件的文件要加 `"use client"`。
|
|
187
269
|
|
|
188
270
|
## 模块格式
|
|
189
271
|
|
package/README_EN.md
CHANGED
|
@@ -83,7 +83,6 @@ import { ImportButton, ExportButton, exportRowsToFile } from "antd-import-export
|
|
|
83
83
|
<ExportButton
|
|
84
84
|
title="Supplier"
|
|
85
85
|
fieldSchemas={fieldSchemas}
|
|
86
|
-
totalCount={totalCount}
|
|
87
86
|
// merge your own filters inside exportRows; paging params come from the package
|
|
88
87
|
exportRows={(params, signal) => exportSuppliers({ ...queryForm, ...params }, signal)}
|
|
89
88
|
/>;
|
|
@@ -91,6 +90,8 @@ import { ImportButton, ExportButton, exportRowsToFile } from "antd-import-export
|
|
|
91
90
|
|
|
92
91
|
Neither component issues requests: they call the `importRows` / `exportRows` you provide.
|
|
93
92
|
|
|
93
|
+
Disable the button when the list is empty, or reject inside the before callback — the component neither knows nor enforces the row count.
|
|
94
|
+
|
|
94
95
|
Call `resetTasks()` on logout or account switch to abort every in-flight import/export and clear the task list.
|
|
95
96
|
|
|
96
97
|
To export rows that are already in memory, render your own button and call the function:
|
|
@@ -107,9 +108,10 @@ await exportRowsToFile({ title: "Supplier", fieldSchemas, rows: selectedRows });
|
|
|
107
108
|
| --- | --- | --- |
|
|
108
109
|
| `title` | `string` | shared by the button, template file name and task title |
|
|
109
110
|
| `fieldSchemas` | `FieldSchema[]` | field definitions |
|
|
110
|
-
| `
|
|
111
|
+
| `buttonText` | `string` | import button label, defaults to `导入` |
|
|
112
|
+
| `batchSize` | `number` | rows submitted per batch, defaults to 20 (1–200) |
|
|
111
113
|
| `importRows` | `(rows, signal) => Promise<Record<string, unknown>[]>` | submits one batch, returns the same length; **empty object means success** |
|
|
112
|
-
| `beforeImport` | `() => void \| Promise<void>` | runs before
|
|
114
|
+
| `beforeImport` | `(rows) => void \| Promise<void>` | runs after parsing and before the preview opens; `rows` are parsed and validated; throw to abort |
|
|
113
115
|
| `afterImport` | `() => void` | fires once when the task ends (completed / failed / canceled) |
|
|
114
116
|
|
|
115
117
|
### ExportButton
|
|
@@ -118,12 +120,14 @@ await exportRowsToFile({ title: "Supplier", fieldSchemas, rows: selectedRows });
|
|
|
118
120
|
| --- | --- | --- |
|
|
119
121
|
| `title` | `string` | file name and task title |
|
|
120
122
|
| `fieldSchemas` | `FieldSchema[]` | field definitions |
|
|
121
|
-
| `
|
|
122
|
-
| `
|
|
123
|
+
| `buttonText` | `string` | button label, defaults to `导出` |
|
|
124
|
+
| `batchSize` | `number` | rows per page, sent as `page_size`; omitted means the backend decides |
|
|
123
125
|
| `exportRows` | `(params, signal) => Promise<{ count, results }>` | cursor paging; **every row needs a numeric `id`** |
|
|
124
126
|
| `beforeExport` | `() => void \| Promise<void>` | runs before the task is created; throw to abort |
|
|
125
127
|
| `afterExport` | `() => void` | fires once when the task ends (completed / failed / canceled) |
|
|
126
128
|
|
|
129
|
+
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.
|
|
130
|
+
|
|
127
131
|
### exportRowsToFile
|
|
128
132
|
|
|
129
133
|
| option | type | notes |
|
|
@@ -131,30 +135,121 @@ await exportRowsToFile({ title: "Supplier", fieldSchemas, rows: selectedRows });
|
|
|
131
135
|
| `title` | `string` | used to build `title_timestamp.xlsx` |
|
|
132
136
|
| `fieldSchemas` | `FieldSchema[]` | field definitions |
|
|
133
137
|
| `rows` | `object[]` | rows already in memory |
|
|
134
|
-
| `maxRows` | `number` | per-export limit, defaults to 20,000 rows |
|
|
135
138
|
|
|
136
139
|
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.
|
|
137
140
|
|
|
138
|
-
###
|
|
141
|
+
### Import contract
|
|
139
142
|
|
|
140
|
-
|
|
141
|
-
2. In Next.js App Router, add `"use client"` to the file that imports them.
|
|
143
|
+
`importRows` returns one array per batch; **its length must match the submitted rows**, in the same order:
|
|
142
144
|
|
|
143
|
-
|
|
145
|
+
| Response | Meaning |
|
|
146
|
+
| --- | --- |
|
|
147
|
+
| `{}` | the row succeeded |
|
|
148
|
+
| `{ "name": ["required"] }` | field-level failure, mapped back to the preview cell |
|
|
149
|
+
| `{ "detail": "name already exists" }` | the whole row failed |
|
|
150
|
+
| `{ "warehouse": ["not found"] }` (key not in the field schema) | also treated as a whole-row failure, message keeps the key |
|
|
151
|
+
| length mismatch | treated as an API error, task pauses |
|
|
152
|
+
| HTTP 4xx/5xx, network error | pauses the whole task, resumable |
|
|
144
153
|
|
|
145
|
-
|
|
154
|
+
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`.
|
|
146
155
|
|
|
147
|
-
|
|
148
|
-
|
|
156
|
+
### What `beforeImport` receives
|
|
157
|
+
|
|
158
|
+
`beforeImport(rows)` runs **after parsing and before the preview opens**, receiving the parsed and per-cell validated rows:
|
|
159
|
+
|
|
160
|
+
| Content | Included |
|
|
161
|
+
| --- | --- |
|
|
162
|
+
| parsed rows | ✅ |
|
|
163
|
+
| per-cell validation | ✅ in `errors` |
|
|
164
|
+
| user edits | ❌ the preview has not opened yet |
|
|
165
|
+
| rows the user deleted | ❌ same reason |
|
|
149
166
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
167
|
+
Throwing aborts the flow: **the preview never opens**. Typical use is a row limit or an admission check:
|
|
168
|
+
|
|
169
|
+
```tsx
|
|
170
|
+
beforeImport={(rows) => {
|
|
171
|
+
if (rows.length > 20000) {
|
|
172
|
+
throw new Error(`too many rows: ${rows.length}, limit 20,000`);
|
|
173
|
+
}
|
|
174
|
+
}}
|
|
153
175
|
```
|
|
154
176
|
|
|
155
|
-
|
|
177
|
+
`rows` may still contain failing rows (`errors` non-empty); those are skipped on submit. Filter them if you need the exact submitted count.
|
|
178
|
+
|
|
179
|
+
### Export contract
|
|
180
|
+
|
|
181
|
+
`exportRows` fetches one page at a time and returns:
|
|
182
|
+
|
|
183
|
+
| Field | Type | Notes |
|
|
184
|
+
| --- | --- | --- |
|
|
185
|
+
| `count` | `number` | total rows under the current filters, used for progress |
|
|
186
|
+
| `results` | `object[]` | rows of this page; **every row needs a numeric `id`** for the cursor |
|
|
187
|
+
|
|
188
|
+
Paging is driven by the package, so `exportRows` only ever receives:
|
|
189
|
+
|
|
190
|
+
| Call | `params` |
|
|
191
|
+
| --- | --- |
|
|
192
|
+
| first page | `{}` |
|
|
193
|
+
| following pages | `{ cursor_id: id of the last row of the previous page }` |
|
|
194
|
+
|
|
195
|
+
With `batchSize` set, both calls also carry `page_size`.
|
|
196
|
+
|
|
197
|
+
An empty `results` array ends the task. Page size is up to the backend; the package does not send `page_size`.
|
|
198
|
+
|
|
199
|
+
### How status codes are detected
|
|
200
|
+
|
|
201
|
+
The package does not know your HTTP client; it reads the status from the error you throw:
|
|
156
202
|
|
|
157
|
-
|
|
203
|
+
| What | Lookup order |
|
|
204
|
+
| --- | --- |
|
|
205
|
+
| status | `error.response.status` → `error.status` → `error.statusCode` |
|
|
206
|
+
| body | `error.response.data` → `error.data` |
|
|
207
|
+
|
|
208
|
+
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".
|
|
209
|
+
|
|
210
|
+
### What `signal` means
|
|
211
|
+
|
|
212
|
+
`importRows` / `exportRows` receive the same `AbortSignal`, but it fires at different moments:
|
|
213
|
+
|
|
214
|
+
| Moment | Fires |
|
|
215
|
+
| --- | --- |
|
|
216
|
+
| modal closed, page unmounted | ✅ |
|
|
217
|
+
| logout via `resetTasks()` | ✅ |
|
|
218
|
+
| export canceled | ✅ |
|
|
219
|
+
| import canceled | ❌ by design |
|
|
220
|
+
| import paused | ❌ by design |
|
|
221
|
+
|
|
222
|
+
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.
|
|
223
|
+
|
|
224
|
+
### Limits belong to the caller
|
|
225
|
+
|
|
226
|
+
The package enforces no row limit and checks nothing mid-flight. Whether and where to limit is yours:
|
|
227
|
+
|
|
228
|
+
```tsx
|
|
229
|
+
<ExportButton
|
|
230
|
+
beforeExport={() => {
|
|
231
|
+
// hardcode the limit per business needs
|
|
232
|
+
if (dataCount > 20000) {
|
|
233
|
+
throw new Error(`too many rows: ${dataCount}, limit 20,000`);
|
|
234
|
+
}
|
|
235
|
+
}}
|
|
236
|
+
/>
|
|
237
|
+
|
|
238
|
+
<ImportButton
|
|
239
|
+
beforeImport={(rows) => {
|
|
240
|
+
if (rows.length > 20000) {
|
|
241
|
+
throw new Error(`too many rows: ${rows.length}, limit 20,000`);
|
|
242
|
+
}
|
|
243
|
+
}}
|
|
244
|
+
/>
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
**Without a check there is no limit** — exports beyond ~20k rows visibly block the main thread.
|
|
248
|
+
|
|
249
|
+
### Two prerequisites
|
|
250
|
+
|
|
251
|
+
1. They use `App.useApp()`, so wrap the root with antd's `<App>`.
|
|
252
|
+
2. In Next.js App Router, add `"use client"` to the file that imports them.
|
|
158
253
|
|
|
159
254
|
## Module format
|
|
160
255
|
|
|
@@ -4,10 +4,10 @@ import type { PagedData } from "../core/types";
|
|
|
4
4
|
export interface ExportButtonProps {
|
|
5
5
|
title: string;
|
|
6
6
|
fieldSchemas: FieldSchema[];
|
|
7
|
-
/**
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
|
|
7
|
+
/** 按钮文案, 默认「导出」 */
|
|
8
|
+
buttonText?: string;
|
|
9
|
+
/** 每页请求的条数, 以 page_size 传给接口; 不传由后端决定 */
|
|
10
|
+
batchSize?: number;
|
|
11
11
|
disabled?: boolean;
|
|
12
12
|
/** 拉取一页待导出的数据; 分页参数由包传入, 筛选条件在这里合并 */
|
|
13
13
|
exportRows: (params: Record<string, unknown>, signal: AbortSignal) => Promise<PagedData<object>>;
|
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
import type { ImportRowErrorBody } from "../core/types";
|
|
3
|
+
import type { ImportRow } from "../core/excel/importRow";
|
|
3
4
|
import type { FieldSchema } from "../core/schema";
|
|
4
5
|
export interface ImportButtonProps {
|
|
5
6
|
title: string;
|
|
6
7
|
fieldSchemas: FieldSchema[];
|
|
7
|
-
/**
|
|
8
|
-
|
|
8
|
+
/** 导入按钮文案, 默认「导入」; 模板按钮文案固定 */
|
|
9
|
+
buttonText?: string;
|
|
10
|
+
/** 每批提交的行数, 默认 20, 范围 1-200 */
|
|
11
|
+
batchSize?: number;
|
|
9
12
|
/** 提交一批待导入的行, 返回等长的逐行结果; 空对象表示成功 */
|
|
10
13
|
importRows: (rows: Record<string, unknown>[], signal: AbortSignal) => Promise<ImportRowErrorBody[]>;
|
|
11
|
-
/**
|
|
12
|
-
beforeImport?: () => void | Promise<void>;
|
|
14
|
+
/** 解析完成、打开预览前触发, 拿到解析并校验过的行; 可做条数校验或准入判断, 抛错则中止 */
|
|
15
|
+
beforeImport?: (rows: ImportRow[]) => void | Promise<void>;
|
|
13
16
|
/** 导入任务结束(成功/失败/取消)时回调一次, 通常用来刷新列表 */
|
|
14
17
|
afterImport: () => void;
|
|
15
18
|
}
|
|
@@ -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;
|
|
@@ -5,12 +5,10 @@ export interface ExportRowsToFileOptions {
|
|
|
5
5
|
title: string;
|
|
6
6
|
fieldSchemas: FieldSchema[];
|
|
7
7
|
rows: object[];
|
|
8
|
-
/** 单次导出上限, 默认 2 万行 */
|
|
9
|
-
maxRows?: number;
|
|
10
8
|
}
|
|
11
9
|
/**
|
|
12
|
-
* 导出内存中的一批行:
|
|
10
|
+
* 导出内存中的一批行: 按约定生成文件名并下载。
|
|
13
11
|
* 只负责数据到文件, 不弹提示也不管理 loading;
|
|
14
|
-
*
|
|
12
|
+
* 没有数据时抛出可直接展示的错误, 由调用方决定如何反馈。
|
|
15
13
|
*/
|
|
16
14
|
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,9 +1,2 @@
|
|
|
1
|
-
/** 单次导入/导出的默认行数上限 */
|
|
2
|
-
export declare const MAX_IMPORT_ROWS = 20000;
|
|
3
|
-
export declare const MAX_EXPORT_ROWS = 20000;
|
|
4
1
|
/** 单个导入文件大小上限 */
|
|
5
2
|
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;
|
|
@@ -53,8 +53,8 @@ export interface ExportTaskOptions {
|
|
|
53
53
|
fileName: string;
|
|
54
54
|
fieldSchemas: FieldSchema[];
|
|
55
55
|
exportRows: (params: Record<string, unknown>, signal: AbortSignal) => Promise<PagedData<object>>;
|
|
56
|
-
/**
|
|
57
|
-
|
|
56
|
+
/** 每页请求的条数, 以 page_size 传给接口; 不传由后端决定 */
|
|
57
|
+
batchSize?: number;
|
|
58
58
|
/** 任务结束(成功/失败/取消)时回调一次, 用于埋点或清理 */
|
|
59
59
|
onFinished?: () => void;
|
|
60
60
|
}
|
|
@@ -70,8 +70,6 @@ export interface ImportTaskOptions {
|
|
|
70
70
|
/** 文件里实际存在的列; 更新时未包含的列表示不修改 */
|
|
71
71
|
includedKeys: string[];
|
|
72
72
|
importRows: (rows: ImportRow[], signal: AbortSignal) => Promise<ImportRowResult[]>;
|
|
73
|
-
/** 单次导入上限, 默认 2 万行 */
|
|
74
|
-
maxRows?: number;
|
|
75
73
|
/** 每批行数; 批越小请求返回越快, 默认 20 */
|
|
76
74
|
batchSize?: number;
|
|
77
75
|
/** 同时执行的批次数量, 默认 3 */
|