import { createTable, filterPlugin, paginationPlugin, selectionPlugin, sortPlugin, } from '../dist/index.js'; const users = [ { id: 1, name: 'Charlie', age: 35, city: 'Shenzhen' }, { id: 2, name: 'Alice', age: 28, city: 'Beijing' }, { id: 3, name: 'Bob', age: 31, city: 'Shanghai' }, { id: 4, name: 'Dave', age: 22, city: 'Beijing' }, { id: 5, name: 'Eve', age: 29, city: 'Shanghai' }, ]; const table = createTable({ data: users, columns: [ { key: 'name', header: 'Name', sortable: true }, { key: 'age', header: 'Age', sortable: true }, { key: 'city', header: 'City' }, ], rowKey: 'id', plugins: [filterPlugin(), sortPlugin(), paginationPlugin(), selectionPlugin()], initialState: { pagination: { page: 1, pageSize: 2 } }, }); /*** 断言工具:失败即退出 ***/ const assert = (cond, msg) => { if (!cond) { console.error('FAIL:', msg); process.exit(1); } console.log('ok -', msg); }; table.dispatch({ type: 'toggleSort', key: 'name' }); // 按名称升序 assert( JSON.stringify(table.getVisibleRows().rows.map((r) => r.name)) === JSON.stringify(['Alice', 'Bob']), 'sort by name asc + page 1 of size 2', ); table.dispatch({ type: 'setFilter', key: 'city', value: 'shang' }); // 筛选上海 const filtered = table.getVisibleRows(); assert( JSON.stringify(filtered.rows.map((r) => r.name)) === JSON.stringify(['Bob', 'Eve']) && filtered.total === 2, 'filter city~sh keeps Shanghai rows, total=2', ); assert(table.getPageInfo().totalPages === 1, 'totalPages computed from filtered total'); table.dispatch({ type: 'clearFilters' }); // 清筛选回到全量 table.dispatch({ type: 'setPage', page: 2 }); assert( JSON.stringify(table.getVisibleRows().rows.map((r) => r.name)) === JSON.stringify(['Charlie', 'Dave']), 'page 2 after clearing filter', ); assert(table.getPageInfo().page === 2 && table.getPageInfo().totalPages === 3, 'page info total=3 pages'); table.dispatch({ type: 'toggleSelect', key: 3 }); // 勾选行 3 assert(JSON.stringify(table.getState().selection) === JSON.stringify([3]), 'toggleSelect adds rowKey'); table.selectAllVisible(); // 全选当前页(替换语义) assert( JSON.stringify(table.getState().selection) === JSON.stringify([1, 4]), 'selectAllVisible replaces selection with visible rowKeys', ); table.dispatch({ type: 'setPage', page: 99 }); // 越界页码 const clamped = table.getVisibleRows(); assert( table.getPageInfo().page === 3 && clamped.rows.length === 1, 'page clamped to totalPages', ); const before = table.getVisibleRows(); // memo 基线 table.dispatch({ type: 'clearSelection' }); // 无关状态变更 assert(table.getVisibleRows() === before, 'derive stages memoized across unrelated state changes'); console.log('SMOKE OK');