antd-import-export 1.0.12 → 1.0.13
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 +125 -23
- package/README_EN.md +125 -23
- package/dist/components/EditableImportCell.d.cts +12 -0
- package/dist/components/EditableImportCell.d.ts +2 -2
- package/dist/components/ExportButton.d.cts +20 -0
- package/dist/components/ExportButton.d.ts +2 -2
- package/dist/components/ExportTaskModal.d.cts +7 -0
- package/dist/components/ImportButton.d.cts +25 -0
- package/dist/components/ImportButton.d.ts +6 -6
- package/dist/components/ImportCheckContent.d.cts +10 -0
- package/dist/components/ImportPreviewModal.d.cts +19 -0
- package/dist/components/ImportPreviewModal.d.ts +2 -2
- package/dist/components/importBatchResult.d.cts +7 -0
- package/dist/components/importBatchResult.d.ts +4 -6
- package/dist/components/importPreviewStatus.d.cts +20 -0
- package/dist/components/importPreviewStatus.d.ts +2 -2
- package/dist/core/dateTime.d.cts +56 -0
- package/dist/core/dateTime.d.ts +56 -0
- package/dist/core/excel/exportFile.d.cts +14 -0
- package/dist/core/excel/exportFile.d.ts +1 -1
- package/dist/core/excel/importEditor.d.cts +7 -0
- package/dist/core/excel/importEditor.d.ts +2 -2
- package/dist/core/excel/importRow.d.cts +28 -0
- package/dist/core/excel/importRow.d.ts +13 -10
- package/dist/core/excel/parseSheet.d.cts +31 -0
- package/dist/core/excel/parseSheet.d.ts +18 -5
- package/dist/core/excel/runtime.d.cts +18 -0
- package/dist/core/excel/runtime.d.ts +8 -3
- package/dist/core/excel/worksheet.d.cts +8 -0
- package/dist/core/excel/worksheet.d.ts +2 -7
- package/dist/core/httpError.d.cts +34 -0
- package/dist/core/limits.d.cts +2 -0
- package/dist/core/schema/defaultValue.d.cts +17 -0
- package/dist/core/schema/defaultValue.d.ts +17 -0
- package/dist/core/schema/index.d.cts +91 -0
- package/dist/core/schema/index.d.ts +4 -1
- package/dist/core/schema/multiValue.d.cts +4 -0
- package/dist/core/schema/optionMap.d.cts +10 -0
- package/dist/core/schema/optionMap.d.ts +10 -0
- package/dist/core/schema/validate.d.cts +21 -0
- package/dist/core/schema/validate.d.ts +2 -2
- package/dist/core/task/exportRunner.d.cts +4 -0
- package/dist/core/task/exportRunner.d.ts +1 -1
- package/dist/core/task/importRunner.d.cts +21 -0
- package/dist/core/task/importRunner.d.ts +4 -4
- package/dist/core/task/index.d.cts +12 -0
- package/dist/core/task/index.d.ts +6 -6
- package/dist/core/task/stats.d.cts +16 -0
- package/dist/core/task/stats.d.ts +3 -3
- package/dist/core/task/store.d.cts +17 -0
- package/dist/core/task/store.d.ts +1 -1
- package/dist/core/task/types.d.cts +106 -0
- package/dist/core/task/types.d.ts +10 -5
- package/dist/core/types.d.cts +16 -0
- package/dist/index.cjs +9 -6
- package/dist/index.d.cts +15 -0
- package/dist/index.d.ts +15 -14
- package/dist/index.mjs +1122 -1012
- package/dist/react/useActiveTask.d.cts +5 -0
- package/dist/react/useActiveTask.d.ts +1 -1
- package/dist/react/useDebouncedValue.d.cts +2 -0
- package/dist/react/useTaskDetail.d.cts +2 -0
- package/dist/react/useTaskDetail.d.ts +1 -1
- package/dist/react/useThrottledValue.d.cts +2 -0
- package/package.json +18 -8
package/README.md
CHANGED
|
@@ -24,10 +24,31 @@
|
|
|
24
24
|
| antd | >=6.0.0 |
|
|
25
25
|
| @ant-design/icons | >=5.0.0 |
|
|
26
26
|
|
|
27
|
+
## 安装
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npm i antd-import-export
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
React / react-dom / antd / @ant-design/icons 由宿主项目提供(版本见上方「依赖要求」)。
|
|
34
|
+
|
|
35
|
+
组件内的 `message` / `modal` 使用 antd 的 `App` context,**请把组件放在 `<App>` 内使用**:
|
|
36
|
+
|
|
37
|
+
```tsx
|
|
38
|
+
import { App } from "antd";
|
|
39
|
+
import { ImportButton } from "antd-import-export";
|
|
40
|
+
|
|
41
|
+
<App>
|
|
42
|
+
<ImportButton title="供应商" fieldSchemas={fieldSchemas} importRows={importSuppliers} />
|
|
43
|
+
</App>
|
|
44
|
+
```
|
|
45
|
+
|
|
27
46
|
## 字段定义
|
|
28
47
|
|
|
29
48
|
导入导出的一切都由 `FieldSchema[]` 驱动:导出用它生成表头,导入模板用它写填写要求,解析时用它逐格校验。**数组顺序就是列顺序。**
|
|
30
49
|
|
|
50
|
+
字段定义的 `key` 与 `title` 需由调用方保证唯一且规范(运行期不做字段定义校验);`title` 会原样作为表头,首尾不要带空格或换行。
|
|
51
|
+
|
|
31
52
|
每个字段由公共部分和类型相关的 `property` 组成:
|
|
32
53
|
|
|
33
54
|
| 属性 | 必填 | 说明 |
|
|
@@ -36,7 +57,7 @@
|
|
|
36
57
|
| `title` | 是 | 列名:导出表头、模板里的填写说明、预览表格的列标题都用它 |
|
|
37
58
|
| `type` | 是 | 九种类型之一,决定 `property` 的结构和校验规则 |
|
|
38
59
|
| `property` | 是 | 类型相关配置,见下表 |
|
|
39
|
-
| `importable` | 否 | 默认 `true`。设为 `false` 表示该列**只导出不导入**:不进模板、不参与解析,导出表头会标注「(仅导出)
|
|
60
|
+
| `importable` | 否 | 默认 `true`。设为 `false` 表示该列**只导出不导入**:不进模板、不参与解析,导出表头会标注「(仅导出)」。重新导入时按 `title` 匹配字段定义,命中 `importable: false` 的列直接忽略;表头里的「(仅导出)」只是提示文字,解析不依赖它 |
|
|
40
61
|
|
|
41
62
|
```ts
|
|
42
63
|
import type { FieldSchema } from "antd-import-export";
|
|
@@ -60,15 +81,39 @@ const fieldSchemas: FieldSchema[] = [
|
|
|
60
81
|
| type | property | 说明 |
|
|
61
82
|
| --- | --- | --- |
|
|
62
83
|
| `text` | required / max_length / default_value | 按字符数校验长度 |
|
|
63
|
-
| `number` | required / precision / max_digits? / min_value? / max_value? / default_value |
|
|
84
|
+
| `number` | required / precision / max_digits? / min_value? / max_value? / default_value | 精度按小数位数判断,不受浮点误差影响;只接受十进制写法(可带指数,如 `1e3`),不支持 `0x`/二进制/下划线;模板与导出的 number 列按文本格式写,长数字不会在 Excel 里丢精度 |
|
|
64
85
|
| `boolean` | required / true_label / false_label / default_value | 单元格填文案,接口值转成布尔 |
|
|
65
86
|
| `date` | required / default_value | 严格 `YYYY-MM-DD` |
|
|
66
|
-
| `time` | required / default_value |
|
|
67
|
-
| `datetime` | required / default_value |
|
|
87
|
+
| `time` | required / default_value | 接受 `H:mm` / `HH:mm`(可带 `:ss`),输出统一补零成 `HH:mm` 或 `HH:mm:ss`,秒为 0 时不带秒 |
|
|
88
|
+
| `datetime` | required / default_value | 日期部分严格 `YYYY-MM-DD`,时间部分同 `time`;秒为 0 时不带秒 |
|
|
68
89
|
| `list` | required / list_max_length / text_max_length / item_key? / default_value | 中英文分号分隔,导出统一用英文分号 |
|
|
69
|
-
| `single_choice` | required / option_map / default_value | 单元格填文案,接口值取 option_map
|
|
90
|
+
| `single_choice` | required / option_map / default_value | 单元格填文案,接口值取 option_map 的 key |
|
|
70
91
|
| `multiple_choice` | required / option_map / default_value | 中英文分号分隔,导出统一用英文分号 |
|
|
71
92
|
|
|
93
|
+
`option_map` 的 **key 是接口内部值**(必须唯一),**value 是 Excel/界面展示文案**(**也必须唯一**,重复时按对象属性顺序取第一个)。例如 `{ ON_SALE: "在售", OFF_SALE: "停售" }`:Excel 里填「在售」,接口传 `"ON_SALE"`。(历史写法是反的:旧 `{ 在售: "ON_SALE" }` 升级时需反转为 `{ ON_SALE: "在售" }`,见 RELEASING。)
|
|
94
|
+
|
|
95
|
+
### 日期时间的输入与导出
|
|
96
|
+
|
|
97
|
+
- 导入时 `date` 严格 `YYYY-MM-DD`(4-2-2,且必须是真实日期);`time` / `datetime` 的小时可以是 1 位,分秒必须是 2 位;日期与时间之间允许多个空白,首尾空白会被忽略。
|
|
98
|
+
- 输出统一补零:`9:05` → `09:05`,`2026-11-22 9:05` → `2026-11-22 09:05`;秒为 0 时不带秒(`09:05:00` → `09:05`)。
|
|
99
|
+
- 接口值与 `default_value` 请使用上述规范字符串。导出遇到 `Date` 按 UTC 解释、遇到 ISO-8601 字符串按字面墙钟取日期时间,不做时区换算;其他格式(如 `2026/11/22`、`20261122`、dayjs 等日期库对象)不再猜测转换,原样输出。
|
|
100
|
+
|
|
101
|
+
### default_value 的取值
|
|
102
|
+
|
|
103
|
+
`default_value` 统一使用字段的**接口值**(和提交给后端的 payload 同类型),不是 Excel 里的展示文案:
|
|
104
|
+
|
|
105
|
+
| 字段类型 | default_value | 示例 |
|
|
106
|
+
| --- | --- | --- |
|
|
107
|
+
| text | string | `"无"` |
|
|
108
|
+
| number | number | `0` |
|
|
109
|
+
| boolean | boolean | `false`(不是 `"否"`) |
|
|
110
|
+
| date / time / datetime | string | `"2026-01-01"` / `"09:00"` / `"2026-01-01 09:00"` |
|
|
111
|
+
| list | string[] | `["A", "B"]` |
|
|
112
|
+
| single_choice | option_map 的 key | `"ON_SALE"`(不是 `"在售"`) |
|
|
113
|
+
| multiple_choice | option_map 的 key 数组 | `["ONLINE"]` |
|
|
114
|
+
|
|
115
|
+
空单元格会填入默认值。`default_value` 为 `null` / `undefined`(或未配置)表示没有默认值:必填字段会报「不能为空」,非必填字段提交 `null`(清空该字段)。只要配置了默认值(包括空字符串 `""`、空数组 `[]`、纯空白字符串)就按配置值提交,必填字段也不再报错——如果必填字段配置空默认值,等于允许提交空值,请自行确认业务语义。模板展示时会自动把内部值转换成选项文案(例如 `ON_SALE` → `在售`)。
|
|
116
|
+
|
|
72
117
|
## 逐字段校验
|
|
73
118
|
|
|
74
119
|
```ts
|
|
@@ -81,7 +126,7 @@ const result = validateField("19.99", {
|
|
|
81
126
|
property: { required: false, precision: 2 },
|
|
82
127
|
});
|
|
83
128
|
|
|
84
|
-
// { validateSuccess: true, validateValue: 19.99 }
|
|
129
|
+
// { validateSuccess: true, validateValue: "19.99" }
|
|
85
130
|
```
|
|
86
131
|
|
|
87
132
|
## 组件
|
|
@@ -117,7 +162,7 @@ await exportRowsToFile({ title: "供应商", fieldSchemas: dataSchema, rows: sel
|
|
|
117
162
|
| `buttonText` | `string` | 导入按钮文案,默认「导入」 |
|
|
118
163
|
| `batchSize` | `number` | 每批提交的行数,默认 20,上限 200(与后端约定);非法值回落到 20 |
|
|
119
164
|
| `importRows` | `(rows) => Promise<Record<string, unknown>[]>` | 一批行提交,返回等长结果;**空对象表示成功** |
|
|
120
|
-
| `beforeImport` | `(rows) => void \|
|
|
165
|
+
| `beforeImport` | `(rows) => void \| Promise<void>` | 解析完成、打开预览前触发(行只读);抛错即中止,返回值会被忽略 |
|
|
121
166
|
| `afterImport` | `() => void` | 任务结束(成功/失败/取消)时触发一次,通常用来刷新列表;暂停不触发 |
|
|
122
167
|
| `onModalClose` | `() => void` | 弹窗关闭后触发一次,完成、取消、中途关闭都会触发 |
|
|
123
168
|
|
|
@@ -128,7 +173,7 @@ await exportRowsToFile({ title: "供应商", fieldSchemas: dataSchema, rows: sel
|
|
|
128
173
|
| `title` | `string` | 文件名与任务标题 |
|
|
129
174
|
| `fieldSchemas` | `FieldSchema[]` | 字段定义 |
|
|
130
175
|
| `buttonText` | `string` | 按钮文案,默认「导出」 |
|
|
131
|
-
| `exportRows` | `(params) => Promise<{ count, results }>` | 游标分页;**每行必须带数字 `id
|
|
176
|
+
| `exportRows` | `(params) => Promise<{ count, results }>` | 游标分页;**每行必须带数字 `id`,且 results 必须按 `id` 递增** |
|
|
132
177
|
| `beforeExport` | `() => void \| Promise<void>` | 创建任务前触发,可做二次确认或自定义校验;抛错即中止 |
|
|
133
178
|
| `afterExport` | `() => void` | 任务结束(成功/失败/取消)时触发一次 |
|
|
134
179
|
| `onModalClose` | `() => void` | 弹窗关闭后触发一次,无论任务是否结束 |
|
|
@@ -149,40 +194,43 @@ await exportRowsToFile({ title: "供应商", fieldSchemas: dataSchema, rows: sel
|
|
|
149
194
|
| --- | --- |
|
|
150
195
|
| `{}` | 该行成功 |
|
|
151
196
|
| `{ "name": ["不能为空"] }` | 该行字段级失败,错误回填到预览表格对应单元格 |
|
|
152
|
-
| `{ "
|
|
153
|
-
| `{ "warehouse": ["不存在"] }`(键不在字段定义里) |
|
|
197
|
+
| `{ "message": "名称已存在" }`(键不在字段定义里) | 该行整体失败,提示显示「名称已存在」 |
|
|
198
|
+
| `{ "warehouse": ["不存在"] }`(键不在字段定义里) | 同样按整体失败处理,提示显示「不存在」 |
|
|
154
199
|
| 返回条数与请求行数不一致 | 视为接口异常,任务暂停 |
|
|
155
200
|
| 抛出异常(含 HTTP 4xx/5xx、网络异常) | 暂停整个任务,可继续或取消 |
|
|
156
201
|
|
|
202
|
+
错误体的 key 是否在 `FieldSchema` 中决定错误类型:**key 在字段定义里 → 字段级错误**,回填到预览对应单元格;**key 不在字段定义里 → 整行错误**,直接展示错误文案(不显示 key)。库不依赖 `detail` / `non_field_errors` 等具体后端约定,任何后端在 `importRows` 中转换成这个形状即可。
|
|
203
|
+
|
|
204
|
+
导入文件不支持合并单元格:解析时检测到任意合并单元格会**整份拒绝**并提示范围(最多列前 3 处及总处数),请先在 Excel 里取消合并后重新导入。
|
|
205
|
+
|
|
206
|
+
导入文件的表头(列名)不允许重复:同一列名出现多次会**整份拒绝**并提示具体列(如 `名称(A 列、C 列)`),无法判断该取哪一列。字段定义中 `importable: false` 的导出专用列(导出表头会标注「(仅导出)」)本身不参与解析,也不参与重复判断;识别依据是字段定义,不是表头文字。
|
|
207
|
+
|
|
208
|
+
单个导入文件大小上限为 **20MB**(公开常量 `MAX_IMPORT_FILE_SIZE`),超限文件会被直接拦截;库不限制导入行数,示例代码里的 20,000 条是使用方自己的业务校验。
|
|
209
|
+
|
|
157
210
|
### beforeImport 拿到什么
|
|
158
211
|
|
|
159
|
-
`beforeImport(rows)` 在**解析完成、打开预览之前**触发,`rows`
|
|
212
|
+
`beforeImport(rows)` 在**解析完成、打开预览之前**触发,`rows` 是解析并逐格校验过的行(只读),可用于校验/埋点。**不支持通过返回值替换或过滤预览数据**:返回值会被忽略,预览始终展示文件里的全部数据行。
|
|
160
213
|
|
|
161
214
|
```ts
|
|
162
215
|
import type { ImportRow } from "antd-import-export";
|
|
163
216
|
|
|
164
217
|
interface ImportRow {
|
|
165
|
-
key: string; // 行唯一键(解析时按顺序生成)
|
|
218
|
+
key: string; // 行唯一键(解析时按顺序生成)
|
|
166
219
|
rowNumber: number; // Excel 里的真实行号, 便于用户对照
|
|
167
|
-
values: Record<string, unknown>; // 编辑器值:
|
|
220
|
+
values: Record<string, unknown>; // 编辑器值: 布尔/单选是文案, 多选是文案数组, 列表是分号拼接的字符串
|
|
168
221
|
errors: Record<string, string>; // 字段级校验错误, key 是字段名
|
|
169
222
|
taskIndex?: number; // 内部字段: beforeImport 里恒为 undefined, 不需要处理
|
|
170
223
|
}
|
|
171
224
|
```
|
|
172
225
|
|
|
173
|
-
|
|
|
226
|
+
| 行为 | 效果 |
|
|
174
227
|
| --- | --- |
|
|
175
|
-
|
|
|
176
|
-
| 返回 `ImportRow[]` | 用它替换预览数据,并重新校验 |
|
|
228
|
+
| 不返回(`void` / `Promise<void>`) | 忽略返回值,继续进入预览 |
|
|
177
229
|
| 抛错 | 中止,预览不打开 |
|
|
178
230
|
|
|
179
|
-
|
|
180
|
-
// 过滤:编号已存在的行不进预览
|
|
181
|
-
beforeImport={(rows) => rows.filter((row) => !existingNumbers.has(String(row.values.number)))}
|
|
182
|
-
|
|
183
|
-
// 截断:超过上限时只保留前 2 万行
|
|
184
|
-
beforeImport={(rows) => rows.slice(0, 20000)}
|
|
231
|
+
> TypeScript 下返回其它值会编译报错(旧版「返回 `ImportRow[]`」的写法属于破坏性变更);JS 调用方返回其它值时返回值会被忽略,并打印一次 `console.warn` 提示。
|
|
185
232
|
|
|
233
|
+
```tsx
|
|
186
234
|
// 中止:不合规就提示用户
|
|
187
235
|
beforeImport={(rows) => {
|
|
188
236
|
if (rows.length > 20000) {
|
|
@@ -198,7 +246,7 @@ beforeImport={(rows) => {
|
|
|
198
246
|
| 字段 | 类型 | 说明 |
|
|
199
247
|
| --- | --- | --- |
|
|
200
248
|
| `count` | `number` | 当前筛选条件下的总条数,用于进度显示 |
|
|
201
|
-
| `results` | `object[]` | 本页数据;**每行必须有数字 `id
|
|
249
|
+
| `results` | `object[]` | 本页数据;**每行必须有数字 `id`**,且**同页内必须按 `id` 递增(升序)**,包用它做游标 |
|
|
202
250
|
|
|
203
251
|
分页规则由包驱动,`exportRows` 收到的参数只有两种:
|
|
204
252
|
|
|
@@ -209,6 +257,8 @@ beforeImport={(rows) => {
|
|
|
209
257
|
|
|
210
258
|
`results` 返回空数组即视为没有更多数据,任务结束。
|
|
211
259
|
|
|
260
|
+
`results` 必须按 `id` **递增(升序)**返回;包会把本页最后一条 `id` 作为下一页的 `cursor_id`。相邻两页不能重叠(下一页第一条 `id` 必须大于上一页最后一条);如果下一页的 `id` 没有严格变大(例如接口忽略了 cursor、重复返回同一页),任务会失败并提示检查 `cursor_id` 分页是否生效。
|
|
261
|
+
|
|
212
262
|
### 取消与暂停的时机
|
|
213
263
|
|
|
214
264
|
取消和暂停都**不会中断已经发出去的请求**,当前这一步跑完后才生效:
|
|
@@ -219,6 +269,58 @@ beforeImport={(rows) => {
|
|
|
219
269
|
| 导入的提交过程中 | 等在途数据导入完成后进入「已取消」或「已暂停」 |
|
|
220
270
|
| 导入已暂停 | 取消立即生效 |
|
|
221
271
|
|
|
272
|
+
## 后端适配
|
|
273
|
+
|
|
274
|
+
库不绑定请求库或后端框架,只要求使用方在回调里转换成以下最小契约。
|
|
275
|
+
|
|
276
|
+
### `importRows`
|
|
277
|
+
|
|
278
|
+
- 入参:一批已校验行的 payload;
|
|
279
|
+
- 返回:与入参**等长**的数组,`{}` 表示该行成功;
|
|
280
|
+
- 错误体 key 在 `FieldSchema` 中 → 字段错误;不在 → 整行错误,展示错误文案。
|
|
281
|
+
|
|
282
|
+
```ts
|
|
283
|
+
// 任意后端响应 -> 通用错误体
|
|
284
|
+
importRows: async (rows) => {
|
|
285
|
+
const result = await api.importRows(rows);
|
|
286
|
+
return result.items.map((item) => (item.ok ? {} : { message: item.errorMessage }));
|
|
287
|
+
};
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
请求库抛错时直接 `throw`;库会暂停任务(能识别 HTTP 状态码时会给出对应文案)。
|
|
291
|
+
|
|
292
|
+
### `exportRows`
|
|
293
|
+
|
|
294
|
+
- 入参:第一页 `{}`,后续 `{ cursor_id: number }`;
|
|
295
|
+
- 返回:`{ count, results }`,`results` 每行必须有**数字 `id`**,并按 `id` 递增返回;
|
|
296
|
+
- 游标契约由库驱动:`cursor_id` 是上一页最后一条的 `id`,相邻页不能重叠。
|
|
297
|
+
|
|
298
|
+
其他分页方式需要适配。例如 offset 分页:
|
|
299
|
+
|
|
300
|
+
```ts
|
|
301
|
+
exportRows: async ({ cursor_id }) => {
|
|
302
|
+
const offset = typeof cursor_id === "number" ? cursor_id : 0;
|
|
303
|
+
const page = await api.list({ offset, limit: 20 });
|
|
304
|
+
return {
|
|
305
|
+
count: page.total,
|
|
306
|
+
// 给每行补一个单调递增的数字 id 作为游标
|
|
307
|
+
results: page.list.map((row, index) => ({ ...row, id: offset + index + 1 })),
|
|
308
|
+
};
|
|
309
|
+
};
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
### 请求错误对象
|
|
313
|
+
|
|
314
|
+
请求库如果抛出的错误不是 `{ status, data }` / `{ response: { status, data } }` 形态,可以包装成该形态,库会给出更准确的错误文案;否则回退到 `error.message`。
|
|
315
|
+
|
|
316
|
+
## 公开 API
|
|
317
|
+
|
|
318
|
+
- 组件:`ImportButton`、`ExportButton`
|
|
319
|
+
- 函数:`exportRowsToFile`、`validateField`、单类型校验器(`validateTextField` / `validateNumberField` / `validateBooleanField` / `validateDateField` / `validateTimeField` / `validateDateTimeField` / `validateListField` / `validateSingleChoiceField` / `validateMultipleChoiceField`)、`resetTasks`
|
|
320
|
+
- 常量:`MAX_IMPORT_FILE_SIZE`、`MULTI_VALUE_SEPARATOR`
|
|
321
|
+
- 工具:`importableFields`、`joinMultiValues`、`splitMultiValues`
|
|
322
|
+
- 类型:`FieldSchema` 及各类型字段定义、`ImportRow`、`ImportRowResult`、`ImportRowErrorBody`、`ImportRowFieldError`、`PagedData`、`ExportRowsToFileOptions`、`ImportButtonProps`、`ExportButtonProps`、`ValidateResult`
|
|
323
|
+
|
|
222
324
|
## 本地开发
|
|
223
325
|
|
|
224
326
|
```bash
|
package/README_EN.md
CHANGED
|
@@ -23,10 +23,31 @@
|
|
|
23
23
|
| antd | >=6.0.0 |
|
|
24
24
|
| @ant-design/icons | >=5.0.0 |
|
|
25
25
|
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm i antd-import-export
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
React / react-dom / antd / @ant-design/icons are provided by the host app (versions above).
|
|
33
|
+
|
|
34
|
+
`message` / `modal` inside the components use antd's `App` context, so **wrap them in `<App>`**:
|
|
35
|
+
|
|
36
|
+
```tsx
|
|
37
|
+
import { App } from "antd";
|
|
38
|
+
import { ImportButton } from "antd-import-export";
|
|
39
|
+
|
|
40
|
+
<App>
|
|
41
|
+
<ImportButton title="Supplier" fieldSchemas={fieldSchemas} importRows={importSuppliers} />
|
|
42
|
+
</App>
|
|
43
|
+
```
|
|
44
|
+
|
|
26
45
|
## Field schema
|
|
27
46
|
|
|
28
47
|
Everything is driven by `FieldSchema[]`: export headers, template hints and cell-level validation. **The array order is the column order.**
|
|
29
48
|
|
|
49
|
+
Field `key`s and `title`s must be unique and clean (field definitions are not validated at runtime); `title` is used verbatim as the Excel header, so avoid leading/trailing spaces or newlines.
|
|
50
|
+
|
|
30
51
|
Each field is the common part plus a type-specific `property`:
|
|
31
52
|
|
|
32
53
|
| Field | Required | Notes |
|
|
@@ -35,7 +56,7 @@ Each field is the common part plus a type-specific `property`:
|
|
|
35
56
|
| `title` | yes | Column name: export header, template hint and preview column all use it |
|
|
36
57
|
| `type` | yes | One of nine types; decides the shape of `property` and the validation rules |
|
|
37
58
|
| `property` | yes | Type-specific config, see the table below |
|
|
38
|
-
| `importable` | no | Defaults to `true`. `false` means **export only**: excluded from the template and parsing, and the export header is marked "(仅导出)" |
|
|
59
|
+
| `importable` | no | Defaults to `true`. `false` means **export only**: excluded from the template and parsing, and the export header is marked "(仅导出)". On re-import, columns are matched by `title` against the field definitions, and columns whose field has `importable: false` are ignored; the marker text is only a hint and is not used for matching |
|
|
39
60
|
|
|
40
61
|
```ts
|
|
41
62
|
import type { FieldSchema } from "antd-import-export";
|
|
@@ -53,15 +74,39 @@ const fieldSchemas: FieldSchema[] = [
|
|
|
53
74
|
| type | property | notes |
|
|
54
75
|
| --- | --- | --- |
|
|
55
76
|
| `text` | required / max_length / default_value | length checked in characters |
|
|
56
|
-
| `number` | required / precision / max_digits? / min_value? / max_value? / default_value | precision judged on decimal places |
|
|
77
|
+
| `number` | required / precision / max_digits? / min_value? / max_value? / default_value | precision judged on decimal places; only decimal literals are accepted (exponent notation like `1e3` is allowed, `0x`/binary/underscores are not); template and export columns use text format so long numbers keep full precision in Excel |
|
|
57
78
|
| `boolean` | required / true_label / false_label / default_value | labels in the cell, boolean on the wire |
|
|
58
79
|
| `date` | required / default_value | strict `YYYY-MM-DD` |
|
|
59
|
-
| `time` | required / default_value |
|
|
60
|
-
| `datetime` | required / default_value | strict `YYYY-MM-DD
|
|
80
|
+
| `time` | required / default_value | accepts `H:mm` / `HH:mm` (optionally `:ss`); output is zero-padded to `HH:mm` or `HH:mm:ss`; seconds are dropped when zero |
|
|
81
|
+
| `datetime` | required / default_value | date part is strict `YYYY-MM-DD`, time part follows `time`; seconds are dropped when zero |
|
|
61
82
|
| `list` | required / list_max_length / text_max_length / item_key? / default_value | Chinese or English semicolons; exports always use `;` |
|
|
62
|
-
| `single_choice` | required / option_map / default_value | label in the cell,
|
|
83
|
+
| `single_choice` | required / option_map / default_value | label in the cell, `option_map` key on the wire |
|
|
63
84
|
| `multiple_choice` | required / option_map / default_value | Chinese or English semicolons; exports always use `;` |
|
|
64
85
|
|
|
86
|
+
`option_map` maps **API value → display label**: the **key is the internal API value** (must be unique), while the **value is the label** shown in Excel/UI (**and labels must be unique too**; duplicates resolve to the first one by object property order). For example `{ "ON_SALE": "On sale", "OFF_SALE": "Off sale" }`: the Excel cell contains `On sale` and the API receives `"ON_SALE"`. (Older versions used the opposite direction: `{ "On sale": "ON_SALE" }` must become `{ "ON_SALE": "On sale" }` when upgrading, see RELEASING.)
|
|
87
|
+
|
|
88
|
+
### Date and time input/export
|
|
89
|
+
|
|
90
|
+
- Import: `date` is strict `YYYY-MM-DD` (4-2-2 and a real calendar date); `time` / `datetime` accept a 1-digit hour, minutes/seconds must have 2 digits; multiple spaces between date and time are allowed, and surrounding whitespace is ignored.
|
|
91
|
+
- Output is zero-padded: `9:05` → `09:05`, `2026-11-22 9:05` → `2026-11-22 09:05`; seconds are dropped when zero (`09:05:00` → `09:05`).
|
|
92
|
+
- Use these canonical strings for interface values and `default_value`. On export a `Date` is read as UTC and an ISO-8601 string by its literal wall clock, with no timezone conversion; other formats (e.g. `2026/11/22`, `20261122`, dayjs-like objects) are no longer guessed and are written as-is.
|
|
93
|
+
|
|
94
|
+
### `default_value`
|
|
95
|
+
|
|
96
|
+
`default_value` always uses the field's **API value** (the same type as the payload), never the display label:
|
|
97
|
+
|
|
98
|
+
| type | default_value | example |
|
|
99
|
+
| --- | --- | --- |
|
|
100
|
+
| text | string | `"N/A"` |
|
|
101
|
+
| number | number | `0` |
|
|
102
|
+
| boolean | boolean | `false` (not `"No"`) |
|
|
103
|
+
| date / time / datetime | string | `"2026-01-01"` / `"09:00"` / `"2026-01-01 09:00"` |
|
|
104
|
+
| list | string[] | `["A", "B"]` |
|
|
105
|
+
| single_choice | a key of `option_map` | `"ON_SALE"` (not `"On sale"`) |
|
|
106
|
+
| multiple_choice | array of `option_map` keys | `["ONLINE"]` |
|
|
107
|
+
|
|
108
|
+
Empty cells fall back to the configured default. `null` / `undefined` (or not configured) means no default: a `required` field then fails with `不能为空`, and an optional field submits `null` (clears the field). Any other configured value — including an empty string `""`, an empty array `[]`, or whitespace-only text — is submitted as-is, and a `required` field with a default no longer fails; configuring an empty default on a required field effectively allows empty values, so make sure this matches your business semantics. The template renders labels automatically (e.g. `ON_SALE` → `On sale`).
|
|
109
|
+
|
|
65
110
|
## Validation
|
|
66
111
|
|
|
67
112
|
```ts
|
|
@@ -73,7 +118,7 @@ validateField("19.99", {
|
|
|
73
118
|
type: "number",
|
|
74
119
|
property: { required: false, precision: 2 },
|
|
75
120
|
});
|
|
76
|
-
// { validateSuccess: true, validateValue: 19.99 }
|
|
121
|
+
// { validateSuccess: true, validateValue: "19.99" }
|
|
77
122
|
```
|
|
78
123
|
|
|
79
124
|
## Components
|
|
@@ -109,7 +154,7 @@ await exportRowsToFile({ title: "Supplier", fieldSchemas, rows: selectedRows });
|
|
|
109
154
|
| `buttonText` | `string` | import button label, defaults to `导入` |
|
|
110
155
|
| `batchSize` | `number` | rows per batch, defaults to 20, capped at 200 (backend contract); invalid values fall back to 20 |
|
|
111
156
|
| `importRows` | `(rows) => Promise<Record<string, unknown>[]>` | submits one batch, returns the same length; **empty object means success** |
|
|
112
|
-
| `beforeImport` | `(rows) => void \|
|
|
157
|
+
| `beforeImport` | `(rows) => void \| Promise<void>` | runs after parsing and before the preview opens (rows are read-only); throw to abort, return values are ignored |
|
|
113
158
|
| `afterImport` | `() => void` | fires once when the task ends (completed / failed / canceled), not on pause |
|
|
114
159
|
| `onModalClose` | `() => void` | fires once after the modal closes, whatever the outcome |
|
|
115
160
|
|
|
@@ -120,7 +165,7 @@ await exportRowsToFile({ title: "Supplier", fieldSchemas, rows: selectedRows });
|
|
|
120
165
|
| `title` | `string` | file name and task title |
|
|
121
166
|
| `fieldSchemas` | `FieldSchema[]` | field definitions |
|
|
122
167
|
| `buttonText` | `string` | button label, defaults to `导出` |
|
|
123
|
-
| `exportRows` | `(params) => Promise<{ count, results }>` | cursor paging; **every row needs a numeric `id
|
|
168
|
+
| `exportRows` | `(params) => Promise<{ count, results }>` | cursor paging; **every row needs a numeric `id` and rows must be `id`-ascending** |
|
|
124
169
|
| `beforeExport` | `() => void \| Promise<void>` | runs before the task is created; throw to abort |
|
|
125
170
|
| `afterExport` | `() => void` | fires once when the task ends (completed / failed / canceled) |
|
|
126
171
|
| `onModalClose` | `() => void` | fires once after the modal closes |
|
|
@@ -141,40 +186,43 @@ await exportRowsToFile({ title: "Supplier", fieldSchemas, rows: selectedRows });
|
|
|
141
186
|
| --- | --- |
|
|
142
187
|
| `{}` | the row succeeded |
|
|
143
188
|
| `{ "name": ["required"] }` | field-level failure, mapped back to the preview cell |
|
|
144
|
-
| `{ "
|
|
145
|
-
| `{ "warehouse": ["not found"] }` (key not in the field schema) | also treated as a whole-row failure
|
|
189
|
+
| `{ "message": "name already exists" }` (key not in the field schema) | the whole row failed; the message is shown as `name already exists` |
|
|
190
|
+
| `{ "warehouse": ["not found"] }` (key not in the field schema) | also treated as a whole-row failure; the message is shown as `not found` |
|
|
146
191
|
| number of results ≠ number of rows | treated as an API error, task pauses |
|
|
147
192
|
| any thrown error (incl. HTTP 4xx/5xx, network) | pauses the whole task, resumable |
|
|
148
193
|
|
|
194
|
+
Whether an error body key is in the `FieldSchema` decides the error type: **key in the field schema → field-level error** (mapped back to the preview cell); **key not in the field schema → whole-row error**, showing the raw message only (no key). The package does not rely on `detail` / `non_field_errors` or any other backend convention — any backend can adapt its response to this shape inside `importRows`.
|
|
195
|
+
|
|
196
|
+
Merged cells are not supported: if the file contains any merged cell the whole file is **rejected** with the affected ranges shown (first 3 plus the total count); unmerge the cells and re-import.
|
|
197
|
+
|
|
198
|
+
Duplicate header names are not supported: if the same header appears in multiple columns the whole file is **rejected** with the affected columns shown (e.g. `Name (A, C)`); the package cannot tell which column to use. Export-only columns are identified by their field definition (`importable: false`), not by the `(仅导出)` marker text; they are ignored and do not participate in duplicate detection.
|
|
199
|
+
|
|
200
|
+
A single import file is limited to **20MB** (public constant `MAX_IMPORT_FILE_SIZE`); larger files are rejected up front. The package does not limit the number of rows — the 20,000-row check in the example is a business rule of the host app.
|
|
201
|
+
|
|
149
202
|
### What `beforeImport` receives
|
|
150
203
|
|
|
151
|
-
`beforeImport(rows)` runs **after parsing and before the preview opens**; `rows` are the parsed, per-cell validated rows (read-only)
|
|
204
|
+
`beforeImport(rows)` runs **after parsing and before the preview opens**; `rows` are the parsed, per-cell validated rows (read-only), useful for validation and telemetry. **Replacing or filtering the preview data via the return value is not supported**: the return value is ignored and the preview always shows every parsed data row.
|
|
152
205
|
|
|
153
206
|
```ts
|
|
154
207
|
import type { ImportRow } from "antd-import-export";
|
|
155
208
|
|
|
156
209
|
interface ImportRow {
|
|
157
|
-
key: string; // unique row key (assigned in order while parsing)
|
|
210
|
+
key: string; // unique row key (assigned in order while parsing)
|
|
158
211
|
rowNumber: number; // the real Excel row number
|
|
159
|
-
values: Record<string, unknown>; // editor values: booleans/choices are labels, lists are semicolon-joined
|
|
212
|
+
values: Record<string, unknown>; // editor values: booleans/single choices are labels, multiple choices are label arrays, lists are semicolon-joined
|
|
160
213
|
errors: Record<string, string>; // field-level validation errors
|
|
161
214
|
taskIndex?: number; // internal: always undefined in beforeImport, ignore it
|
|
162
215
|
}
|
|
163
216
|
```
|
|
164
217
|
|
|
165
|
-
|
|
|
218
|
+
| Behavior | Effect |
|
|
166
219
|
| --- | --- |
|
|
167
|
-
| nothing
|
|
168
|
-
| `ImportRow[]` | replace the preview data; rows are re-validated |
|
|
220
|
+
| returns nothing (`void` / `Promise<void>`) | ignored; the preview opens |
|
|
169
221
|
| throws | abort; the preview never opens |
|
|
170
222
|
|
|
171
|
-
|
|
172
|
-
// filter: skip rows whose code already exists
|
|
173
|
-
beforeImport={(rows) => rows.filter((row) => !existingNumbers.has(String(row.values.number)))}
|
|
174
|
-
|
|
175
|
-
// truncate: keep only the first 20,000 rows
|
|
176
|
-
beforeImport={(rows) => rows.slice(0, 20000)}
|
|
223
|
+
> In TypeScript, returning any other value is a compile error (the old `ImportRow[]` return is a breaking change); in plain JS the return value is ignored and a single `console.warn` is printed.
|
|
177
224
|
|
|
225
|
+
```tsx
|
|
178
226
|
// abort with a message
|
|
179
227
|
beforeImport={(rows) => {
|
|
180
228
|
if (rows.length > 20000) {
|
|
@@ -190,7 +238,7 @@ beforeImport={(rows) => {
|
|
|
190
238
|
| Field | Type | Notes |
|
|
191
239
|
| --- | --- | --- |
|
|
192
240
|
| `count` | `number` | total rows under the current filters, used for progress |
|
|
193
|
-
| `results` | `object[]` | rows of this page; **every row needs a numeric `id
|
|
241
|
+
| `results` | `object[]` | rows of this page; **every row needs a numeric `id`**, and **rows must be sorted by `id` ascending** for the cursor |
|
|
194
242
|
|
|
195
243
|
Paging is driven by the package, so `exportRows` only ever receives:
|
|
196
244
|
|
|
@@ -201,6 +249,8 @@ Paging is driven by the package, so `exportRows` only ever receives:
|
|
|
201
249
|
|
|
202
250
|
An empty `results` array ends the task.
|
|
203
251
|
|
|
252
|
+
`results` must be returned with `id` **ascending**; the package uses the last `id` of the page as the next `cursor_id`. Pages must not overlap (the first `id` of the next page must be greater than the last `id` of the previous page). If the next page's `id` does not strictly increase (e.g. the API ignores the cursor and repeats the same page), the task fails with a hint to check `cursor_id` paging.
|
|
253
|
+
|
|
204
254
|
### When cancel and pause take effect
|
|
205
255
|
|
|
206
256
|
Cancel and pause never abort an in-flight request — they take effect once the current step finishes:
|
|
@@ -211,6 +261,58 @@ Cancel and pause never abort an in-flight request — they take effect once the
|
|
|
211
261
|
| import while submitting | enters "canceled" or "paused" after in-flight data finishes importing |
|
|
212
262
|
| import already paused | cancel takes effect immediately |
|
|
213
263
|
|
|
264
|
+
## Backend adaptation
|
|
265
|
+
|
|
266
|
+
The package does not bind to a request library or backend framework. The host app only needs to adapt its responses to these minimal contracts inside the callbacks.
|
|
267
|
+
|
|
268
|
+
### `importRows`
|
|
269
|
+
|
|
270
|
+
- Input: a batch of validated row payloads;
|
|
271
|
+
- Output: an array with the **same length**; `{}` means the row succeeded;
|
|
272
|
+
- Error-body key in the `FieldSchema` → field-level error; key not in the schema → whole-row error (message shown).
|
|
273
|
+
|
|
274
|
+
```ts
|
|
275
|
+
// any backend response -> generic error body
|
|
276
|
+
importRows: async (rows) => {
|
|
277
|
+
const result = await api.importRows(rows);
|
|
278
|
+
return result.items.map((item) => (item.ok ? {} : { message: item.errorMessage }));
|
|
279
|
+
};
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
If the request library throws, just `throw`; the task pauses (HTTP status codes are recognized when available).
|
|
283
|
+
|
|
284
|
+
### `exportRows`
|
|
285
|
+
|
|
286
|
+
- Input: `{}` for the first page, `{ cursor_id: number }` for following pages;
|
|
287
|
+
- Output: `{ count, results }`; every row in `results` must have a **numeric `id`**, returned in ascending `id` order;
|
|
288
|
+
- The cursor is driven by the package: `cursor_id` is the last `id` of the previous page, and pages must not overlap.
|
|
289
|
+
|
|
290
|
+
Other paging styles need adaptation. Example with offset paging:
|
|
291
|
+
|
|
292
|
+
```ts
|
|
293
|
+
exportRows: async ({ cursor_id }) => {
|
|
294
|
+
const offset = typeof cursor_id === "number" ? cursor_id : 0;
|
|
295
|
+
const page = await api.list({ offset, limit: 20 });
|
|
296
|
+
return {
|
|
297
|
+
count: page.total,
|
|
298
|
+
// add a monotonically increasing numeric id used as the cursor
|
|
299
|
+
results: page.list.map((row, index) => ({ ...row, id: offset + index + 1 })),
|
|
300
|
+
};
|
|
301
|
+
};
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
### Request error objects
|
|
305
|
+
|
|
306
|
+
If your request library throws errors that are not shaped like `{ status, data }` / `{ response: { status, data } }`, wrap them into that shape for better messages; otherwise the package falls back to `error.message`.
|
|
307
|
+
|
|
308
|
+
## Public API
|
|
309
|
+
|
|
310
|
+
- Components: `ImportButton`, `ExportButton`
|
|
311
|
+
- Functions: `exportRowsToFile`, `validateField`, single-type validators (`validateTextField` / `validateNumberField` / `validateBooleanField` / `validateDateField` / `validateTimeField` / `validateDateTimeField` / `validateListField` / `validateSingleChoiceField` / `validateMultipleChoiceField`), `resetTasks`
|
|
312
|
+
- Constants: `MAX_IMPORT_FILE_SIZE`, `MULTI_VALUE_SEPARATOR`
|
|
313
|
+
- Utilities: `importableFields`, `joinMultiValues`, `splitMultiValues`
|
|
314
|
+
- Types: `FieldSchema` and its per-type variants, `ImportRow`, `ImportRowResult`, `ImportRowErrorBody`, `ImportRowFieldError`, `PagedData`, `ExportRowsToFileOptions`, `ImportButtonProps`, `ExportButtonProps`, `ValidateResult`
|
|
315
|
+
|
|
214
316
|
## Development
|
|
215
317
|
|
|
216
318
|
```bash
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { ImportRow } from "../core/excel/importRow.cjs";
|
|
3
|
+
import type { FieldSchema } from "../core/schema/index.cjs";
|
|
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;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
-
import type { ImportRow } from "../core/excel/importRow";
|
|
3
|
-
import type { FieldSchema } from "../core/schema";
|
|
2
|
+
import type { ImportRow } from "../core/excel/importRow.js";
|
|
3
|
+
import type { FieldSchema } from "../core/schema/index.js";
|
|
4
4
|
interface EditableImportCellProps {
|
|
5
5
|
field: FieldSchema;
|
|
6
6
|
record: ImportRow;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { FieldSchema } from "../core/schema/index.cjs";
|
|
3
|
+
import type { PagedData } from "../core/types.cjs";
|
|
4
|
+
export interface ExportButtonProps {
|
|
5
|
+
title: string;
|
|
6
|
+
fieldSchemas: FieldSchema[];
|
|
7
|
+
/** 按钮文案, 默认「导出」 */
|
|
8
|
+
buttonText?: string;
|
|
9
|
+
disabled?: boolean;
|
|
10
|
+
/** 拉取一页待导出的数据; 分页参数由包传入, 筛选条件在这里合并 */
|
|
11
|
+
exportRows: (params: Record<string, unknown>) => Promise<PagedData<object>>;
|
|
12
|
+
/** 创建任务前触发, 可做二次确认或自定义校验; 抛错则中止, 不创建任务 */
|
|
13
|
+
beforeExport?: () => void | Promise<void>;
|
|
14
|
+
/** 导出任务结束(成功/失败/取消)时回调一次 */
|
|
15
|
+
afterExport?: () => void;
|
|
16
|
+
/** 弹窗关闭后触发, 无论任务是否结束 */
|
|
17
|
+
onModalClose?: () => void;
|
|
18
|
+
}
|
|
19
|
+
declare const ExportButton: React.FC<ExportButtonProps>;
|
|
20
|
+
export default ExportButton;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
-
import type { FieldSchema } from "../core/schema";
|
|
3
|
-
import type { PagedData } from "../core/types";
|
|
2
|
+
import type { FieldSchema } from "../core/schema/index.js";
|
|
3
|
+
import type { PagedData } from "../core/types.js";
|
|
4
4
|
export interface ExportButtonProps {
|
|
5
5
|
title: string;
|
|
6
6
|
fieldSchemas: FieldSchema[];
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { ImportRowErrorBody } from "../core/types.cjs";
|
|
3
|
+
import type { ImportRow } from "../core/excel/importRow.cjs";
|
|
4
|
+
import type { FieldSchema } from "../core/schema/index.cjs";
|
|
5
|
+
export interface ImportButtonProps {
|
|
6
|
+
title: string;
|
|
7
|
+
fieldSchemas: FieldSchema[];
|
|
8
|
+
/** 导入按钮文案, 默认「导入」; 模板按钮文案固定 */
|
|
9
|
+
buttonText?: string;
|
|
10
|
+
/** 每批提交的行数, 默认 20, 范围 1-200 */
|
|
11
|
+
batchSize?: number;
|
|
12
|
+
/** 提交一批待导入的行, 返回等长的逐行结果; 空对象表示成功 */
|
|
13
|
+
importRows: (rows: Record<string, unknown>[]) => Promise<ImportRowErrorBody[]>;
|
|
14
|
+
/**
|
|
15
|
+
* 解析完成、打开预览前触发, 拿到解析并校验过的只读行, 可用于校验/埋点。
|
|
16
|
+
* 抛错即中止; 返回值会被忽略, 不支持通过返回值替换或过滤预览数据。
|
|
17
|
+
*/
|
|
18
|
+
beforeImport?: (rows: readonly ImportRow[]) => void | Promise<void>;
|
|
19
|
+
/** 导入任务结束(成功/失败/取消)时回调一次; 用 onModalClose 刷新时可以不传 */
|
|
20
|
+
afterImport?: () => void;
|
|
21
|
+
/** 弹窗关闭后触发, 无论是导入完成、取消还是中途关闭 */
|
|
22
|
+
onModalClose?: () => void;
|
|
23
|
+
}
|
|
24
|
+
declare const ImportButton: React.FC<ImportButtonProps>;
|
|
25
|
+
export default ImportButton;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
-
import type { ImportRowErrorBody } from "../core/types";
|
|
3
|
-
import type { ImportRow } from "../core/excel/importRow";
|
|
4
|
-
import type { FieldSchema } from "../core/schema";
|
|
2
|
+
import type { ImportRowErrorBody } from "../core/types.js";
|
|
3
|
+
import type { ImportRow } from "../core/excel/importRow.js";
|
|
4
|
+
import type { FieldSchema } from "../core/schema/index.js";
|
|
5
5
|
export interface ImportButtonProps {
|
|
6
6
|
title: string;
|
|
7
7
|
fieldSchemas: FieldSchema[];
|
|
@@ -12,10 +12,10 @@ export interface ImportButtonProps {
|
|
|
12
12
|
/** 提交一批待导入的行, 返回等长的逐行结果; 空对象表示成功 */
|
|
13
13
|
importRows: (rows: Record<string, unknown>[]) => Promise<ImportRowErrorBody[]>;
|
|
14
14
|
/**
|
|
15
|
-
* 解析完成、打开预览前触发,
|
|
16
|
-
* 抛错即中止;
|
|
15
|
+
* 解析完成、打开预览前触发, 拿到解析并校验过的只读行, 可用于校验/埋点。
|
|
16
|
+
* 抛错即中止; 返回值会被忽略, 不支持通过返回值替换或过滤预览数据。
|
|
17
17
|
*/
|
|
18
|
-
beforeImport?: (rows: readonly ImportRow[]) => void |
|
|
18
|
+
beforeImport?: (rows: readonly ImportRow[]) => void | Promise<void>;
|
|
19
19
|
/** 导入任务结束(成功/失败/取消)时回调一次; 用 onModalClose 刷新时可以不传 */
|
|
20
20
|
afterImport?: () => void;
|
|
21
21
|
/** 弹窗关闭后触发, 无论是导入完成、取消还是中途关闭 */
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
export interface ImportCheckMessages {
|
|
3
|
+
/** 没有对上的必填列名 */
|
|
4
|
+
missingTitles: string[];
|
|
5
|
+
/** 表头级问题: 文件打不开、没有工作表等 */
|
|
6
|
+
headerMessages: string[];
|
|
7
|
+
}
|
|
8
|
+
/** 导入校验的明细内容, 使用普通正文样式, 不再叠加图标和强调色 */
|
|
9
|
+
declare const ImportCheckContent: React.FC<ImportCheckMessages>;
|
|
10
|
+
export default ImportCheckContent;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { ImportRow } from "../core/excel/importRow.cjs";
|
|
3
|
+
import type { FieldSchema } from "../core/schema/index.cjs";
|
|
4
|
+
interface ImportPreviewModalProps {
|
|
5
|
+
open: boolean;
|
|
6
|
+
title: string;
|
|
7
|
+
fieldSchemas: FieldSchema[];
|
|
8
|
+
rows: ImportRow[];
|
|
9
|
+
taskId?: string;
|
|
10
|
+
/** 文件里实际存在的列; 预览只展示这些列, 缺列不渲染也不提交 */
|
|
11
|
+
includedKeys: string[];
|
|
12
|
+
onChangeCell: (row: ImportRow, field: FieldSchema, value: unknown) => void;
|
|
13
|
+
onSaveCell: (row: ImportRow, field: FieldSchema, value: unknown) => void;
|
|
14
|
+
onDelete: (row: ImportRow) => void;
|
|
15
|
+
onConfirm: () => void;
|
|
16
|
+
onClose: () => void;
|
|
17
|
+
}
|
|
18
|
+
declare const ImportPreviewModal: React.FC<ImportPreviewModalProps>;
|
|
19
|
+
export default ImportPreviewModal;
|