diff --git a/frontend/app/features/items/fileInput.test.ts b/frontend/app/features/items/fileInput.test.ts new file mode 100644 index 0000000..7440247 --- /dev/null +++ b/frontend/app/features/items/fileInput.test.ts @@ -0,0 +1,36 @@ +// @vitest-environment jsdom +// 本测试需要浏览器文件控件与 File API,因此固定使用 jsdom 环境。 +import { describe,expect,test } from 'vitest'; +import { consumeSelectedFile } from './fileInput'; + +// ItemList 文件选择测试覆盖同一路径的表格在保存后能够被重新读取。 +describe('consumeSelectedFile', + // 测试组回调验证读取文件后重置原生 input 的浏览器兼容行为。 + () => { + // 连续两次选择同名文件时,每次都返回对应 File 快照并清空上一轮原生选择值。 + test('clears the native input after each selected file', + // 测试回调模拟浏览器在用户重新选择同一路径前写入的文件列表和伪路径值。 + () => { + // input 保存可被测试替换 files 与 value 行为的文件控件。 + const input = document.createElement('input'); + // currentValue 保存浏览器文件控件的当前伪路径值,空值代表下次同一路径选择会重新触发 change。 + let currentValue = 'C:\\fakepath\\products.xlsx'; + Object.defineProperty(input, 'value', { + configurable: true, + get: /* 当前访问器返回模拟的文件控件伪路径。 */ () => currentValue, + set: /* 当前访问器记录业务代码清空原生控件的操作。 */ value => { currentValue = String(value); }, + }); + // firstFile 保存首次选择时浏览器创建的文件快照。 + const firstFile = new File(['old-content'], 'products.xlsx', { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); + Object.defineProperty(input, 'files', { configurable: true, value: [firstFile] as unknown as FileList }); + expect(consumeSelectedFile(input)).toBe(firstFile); + expect(currentValue).toBe(''); + + // secondFile 保存同名文件修改后再次选择时浏览器创建的新快照。 + const secondFile = new File(['new-content'], 'products.xlsx', { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); + currentValue = 'C:\\fakepath\\products.xlsx'; + Object.defineProperty(input, 'files', { configurable: true, value: [secondFile] as unknown as FileList }); + expect(consumeSelectedFile(input)).toBe(secondFile); + expect(currentValue).toBe(''); + }); + }); diff --git a/frontend/app/features/items/fileInput.ts b/frontend/app/features/items/fileInput.ts new file mode 100644 index 0000000..628dcd0 --- /dev/null +++ b/frontend/app/features/items/fileInput.ts @@ -0,0 +1,11 @@ +/** + * consumeSelectedFile 读取文件控件当前选择的首个文件,并立即重置原生值。 + * input 是批量铺货表格控件;返回值是可由 React 状态持有的文件快照,重置让用户可再次选择相同路径的已修改文件。 + */ +export const consumeSelectedFile = (input: HTMLInputElement): File | null => { + // selectedFile 保存本次 change 事件携带的文件快照;无选择或用户取消时为 null。 + const selectedFile = input.files?.[0] || null; + // 原生控件必须在读取后清空,否则浏览器可能将相同路径的再次选择视为未变化而不触发 change。 + input.value = ''; + return selectedFile; +}; diff --git a/frontend/app/features/items/pages/ItemList.tsx b/frontend/app/features/items/pages/ItemList.tsx index 794c281..d186cb1 100644 --- a/frontend/app/features/items/pages/ItemList.tsx +++ b/frontend/app/features/items/pages/ItemList.tsx @@ -10,6 +10,7 @@ getShippingRules, } from '../api'; import { batchStatusClass,batchStatusText } from '../batchState'; import { BatchPhaseIndicator } from '../components/BatchPhaseIndicator'; +import { consumeSelectedFile } from '../fileInput'; import { useItemPublishBatch } from '../hooks'; import { useItemActions } from '../itemActions'; import type { ItemListProps } from '../types'; @@ -676,7 +677,7 @@ const ItemList: React.FC = ({ onConfigureDelivery }) => { className="hidden" type="file" accept=".xlsx,.csv,.tsv" - onChange={/* 当前回调处理用户交互或异步状态变化。 */ e => setBatchFile(e.target.files?.[0] || null)} + onChange={/* 当前回调读取本次文件快照并重置原生控件,以便同一路径文件修改后可再次选择。 */ event => setBatchFile(consumeSelectedFile(event.currentTarget))} />