|
| 1 | +import * as VTable from '../../src'; |
| 2 | +import { bindDebugTool } from '../../src/scenegraph/debug-tool'; |
| 3 | +const ListTable = VTable.ListTable; |
| 4 | +const CONTAINER_ID = 'vTable'; |
| 5 | + |
| 6 | +/** |
| 7 | + * 验证场景:列宽/行高调整过程中调用 setRecords 或 updateOption 时, |
| 8 | + * 调整指示线应被正确清除,不会残留在表格上。 |
| 9 | + * 对应 issue: https://github.com/VisActor/VTable/issues/4120 |
| 10 | + * |
| 11 | + * 复现步骤: |
| 12 | + * 1. 点击"自动定时刷新"按钮开启每3秒自动 setRecords |
| 13 | + * 2. 拖动列头边界开始调整列宽(鼠标按住不放) |
| 14 | + * 3. 等待自动刷新触发(或手动点击按钮) |
| 15 | + * 4. 修复前:指示线会卡在表格上无法消失 |
| 16 | + * 5. 修复后:指示线在 setRecords/updateOption 调用时被正确清除 |
| 17 | + */ |
| 18 | +export function createTable() { |
| 19 | + const generatePersons = (count: number) => { |
| 20 | + return Array.from(new Array(count)).map((_, i) => ({ |
| 21 | + id: i + 1, |
| 22 | + name: `员工${i + 1}`, |
| 23 | + email: `user${i + 1}@example.com`, |
| 24 | + department: ['研发部', '市场部', '设计部', '产品部', '运营部'][i % 5], |
| 25 | + salary: Math.round(Math.random() * 10000 + 5000), |
| 26 | + city: ['北京', '上海', '广州', '深圳', '杭州'][i % 5] |
| 27 | + })); |
| 28 | + }; |
| 29 | + |
| 30 | + let records = generatePersons(100); |
| 31 | + |
| 32 | + const columns: VTable.ColumnsDefine = [ |
| 33 | + { field: 'id', title: 'ID', width: 80 }, |
| 34 | + { field: 'name', title: '姓名', width: 120 }, |
| 35 | + { field: 'email', title: '邮箱', width: 220 }, |
| 36 | + { field: 'department', title: '部门', width: 120 }, |
| 37 | + { field: 'salary', title: '薪资', width: 120 }, |
| 38 | + { field: 'city', title: '城市', width: 120 } |
| 39 | + ]; |
| 40 | + |
| 41 | + const option: VTable.ListTableConstructorOptions = { |
| 42 | + container: document.getElementById(CONTAINER_ID), |
| 43 | + records, |
| 44 | + columns, |
| 45 | + widthMode: 'standard', |
| 46 | + defaultRowHeight: 40, |
| 47 | + defaultHeaderRowHeight: 50, |
| 48 | + theme: VTable.themes.ARCO |
| 49 | + }; |
| 50 | + |
| 51 | + const btnContainer = document.createElement('div'); |
| 52 | + btnContainer.style.cssText = 'padding: 10px 0; display: flex; gap: 10px; align-items: center; flex-wrap: wrap;'; |
| 53 | + |
| 54 | + const tip = document.createElement('span'); |
| 55 | + tip.style.cssText = 'color: #666; font-size: 13px;'; |
| 56 | + tip.textContent = '操作:先拖动列边界调整列宽,拖动过程中点击下方按钮'; |
| 57 | + btnContainer.appendChild(tip); |
| 58 | + |
| 59 | + const btnSetRecords = document.createElement('button'); |
| 60 | + btnSetRecords.textContent = 'setRecords (刷新数据)'; |
| 61 | + btnSetRecords.style.cssText = |
| 62 | + 'padding: 6px 16px; cursor: pointer; background: #416EFF; color: #fff; border: none; border-radius: 4px;'; |
| 63 | + btnSetRecords.addEventListener('click', () => { |
| 64 | + records = generatePersons(100); |
| 65 | + instance.setRecords(records); |
| 66 | + console.log('setRecords called'); |
| 67 | + }); |
| 68 | + btnContainer.appendChild(btnSetRecords); |
| 69 | + |
| 70 | + const btnUpdateOption = document.createElement('button'); |
| 71 | + btnUpdateOption.textContent = 'updateOption (更新配置)'; |
| 72 | + btnUpdateOption.style.cssText = |
| 73 | + 'padding: 6px 16px; cursor: pointer; background: #52C41A; color: #fff; border: none; border-radius: 4px;'; |
| 74 | + btnUpdateOption.addEventListener('click', () => { |
| 75 | + records = generatePersons(100); |
| 76 | + instance.updateOption({ |
| 77 | + ...option, |
| 78 | + records |
| 79 | + }); |
| 80 | + console.log('updateOption called'); |
| 81 | + }); |
| 82 | + btnContainer.appendChild(btnUpdateOption); |
| 83 | + |
| 84 | + let timer: any = null; |
| 85 | + const btnAutoTest = document.createElement('button'); |
| 86 | + btnAutoTest.textContent = '自动定时刷新 (每3秒)'; |
| 87 | + btnAutoTest.style.cssText = |
| 88 | + 'padding: 6px 16px; cursor: pointer; background: #FA8C16; color: #fff; border: none; border-radius: 4px;'; |
| 89 | + btnAutoTest.addEventListener('click', () => { |
| 90 | + if (timer) { |
| 91 | + clearInterval(timer); |
| 92 | + timer = null; |
| 93 | + btnAutoTest.textContent = '自动定时刷新 (每3秒)'; |
| 94 | + btnAutoTest.style.background = '#FA8C16'; |
| 95 | + } else { |
| 96 | + timer = setInterval(() => { |
| 97 | + records = generatePersons(100); |
| 98 | + instance.setRecords(records); |
| 99 | + console.log('auto setRecords triggered'); |
| 100 | + }, 3000); |
| 101 | + btnAutoTest.textContent = '停止自动刷新'; |
| 102 | + btnAutoTest.style.background = '#FF4D4F'; |
| 103 | + } |
| 104 | + }); |
| 105 | + btnContainer.appendChild(btnAutoTest); |
| 106 | + |
| 107 | + document.getElementById(CONTAINER_ID)?.before(btnContainer); |
| 108 | + |
| 109 | + const instance = new ListTable(option); |
| 110 | + |
| 111 | + bindDebugTool(instance.scenegraph.stage as any, { |
| 112 | + customGrapicKeys: ['role', '_updateTag'] |
| 113 | + }); |
| 114 | + |
| 115 | + const originalRelease = instance.release.bind(instance); |
| 116 | + instance.release = () => { |
| 117 | + if (timer) { |
| 118 | + clearInterval(timer); |
| 119 | + timer = null; |
| 120 | + } |
| 121 | + btnContainer.remove(); |
| 122 | + originalRelease(); |
| 123 | + }; |
| 124 | + |
| 125 | + (window as any).tableInstance = instance; |
| 126 | +} |
0 commit comments