style(frontend): 全项目表格统一百分比布局并居中

This commit is contained in:
yml2213
2026-10-24 14:49:23 +08:00
parent a96229a106
commit ce8efa6bad
52 changed files with 346 additions and 145 deletions
@@ -0,0 +1,77 @@
import assert from 'node:assert/strict'
import fs from 'node:fs'
import path from 'node:path'
import test from 'node:test'
import type { TableColumnsType } from 'antd'
import { normalizePercentColumns } from '../src/utils/percent-table-layout.ts'
test('百分比表格:按原列宽权重归一化到 100%', () => {
const columns: TableColumnsType<Record<string, unknown>> = [
{ title: 'A', dataIndex: 'a', width: 100 },
{ title: 'B', dataIndex: 'b', width: 300 },
]
const result = normalizePercentColumns(columns)
assert.deepEqual(
result.map((column) => column.width),
['25.0000%', '75.0000%'],
)
assert.ok(result.every((column) => column.align === 'center'))
})
test('百分比表格:没有列宽时平均分配', () => {
const columns: TableColumnsType<Record<string, unknown>> = [
{ title: 'A', dataIndex: 'a' },
{ title: 'B', dataIndex: 'b' },
]
assert.deepEqual(
normalizePercentColumns(columns).map((column) => column.width),
['50.0000%', '50.0000%'],
)
})
test('百分比表格:选择列预留 4%', () => {
const columns: TableColumnsType<Record<string, unknown>> = [
{ title: 'A', dataIndex: 'a', width: 100 },
{ title: 'B', dataIndex: 'b', width: 100 },
]
const result = normalizePercentColumns(columns, { reservedPercent: 4 })
assert.deepEqual(
result.map((column) => column.width),
['48.0000%', '48.0000%'],
)
})
test('百分比表格:分组列宽等于子列之和', () => {
const columns: TableColumnsType<Record<string, unknown>> = [
{
title: '分组',
children: [
{ title: 'A', dataIndex: 'a', width: 100 },
{ title: 'B', dataIndex: 'b', width: 100 },
],
},
]
const result = normalizePercentColumns(columns)
assert.equal(result[0]?.width, '100.0000%')
})
test('全项目表格必须统一使用 PercentTable', () => {
const offenders: string[] = []
for (const file of listTsxFiles(path.join(process.cwd(), 'src'))) {
const source = fs.readFileSync(file, 'utf8')
if (/<Table\b/.test(source)) {
offenders.push(path.relative(process.cwd(), file))
}
}
assert.deepEqual(offenders, [], `仍有表格直接使用 AntD Table:${offenders.join(', ')}`)
})
function listTsxFiles(dir: string): string[] {
return fs.readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
const fullPath = path.join(dir, entry.name)
if (entry.isDirectory()) return listTsxFiles(fullPath)
return entry.isFile() && entry.name.endsWith('.tsx') ? [fullPath] : []
})
}