Commit c69fa6f8 by 李伟康

feat(PolicyConfirmTable): 实现表格自动行合并并优化样式

重构组件代码结构,将数据源计算提前至列定义前
新增基于门店和业务场景的自动行合并逻辑
调整表头背景色、文字色与分类标签样式
更新buildPolicyColumns函数以接收rows参数
1 parent cff6b99e
......@@ -3,8 +3,8 @@ import type { TableProps } from 'antd';
import type { PolicyConfirmRow } from '../../tools/policyTableData';
const storeInfoHeaderStyle: React.CSSProperties = {
background: '#EEF2FF',
color: '#3730A3',
background: '#EFF4FF',
color: '#2B5CE6',
fontWeight: 600,
fontSize: 13,
};
......@@ -51,7 +51,7 @@ function SceneTag({ text }: { text: string }) {
function CategoryTag({ text }: { text: string }) {
return (
<UTag
color="success"
color="processing"
bordered={false}
title={text}
style={{
......@@ -77,7 +77,27 @@ function FeeValueCell({ value }: { value: string }) {
return <span style={{ fontWeight: 600, color: '#1F2937' }}>{value}</span>;
}
export function buildPolicyColumns(): TableProps<PolicyConfirmRow>['columns'] {
export function buildPolicyColumns(
rows: PolicyConfirmRow[] = [],
): TableProps<PolicyConfirmRow>['columns'] {
/**
* 计算当前行某列的 rowSpan(同门店 / 同业务场景合并单元格):
* - keyFn:对每行提取分组 key(如门店名、或「门店名|业务场景」复合键)
* - 当前行与上一行 key 相同 → rowSpan = 0(被合并)
* - 否则向下统计连续相同 key 的行数作为 rowSpan
*/
const getRowSpan = (index: number, keyFn: (r: PolicyConfirmRow) => string): number => {
if (index < 0 || index >= rows.length) return 1;
const cur = keyFn(rows[index]);
if (index > 0 && keyFn(rows[index - 1]) === cur) return 0;
let span = 1;
for (let i = index + 1; i < rows.length; i++) {
if (keyFn(rows[i]) === cur) span++;
else break;
}
return span;
};
return [
// ------------- 第一组:门店信息(4 列) -------------
{
......@@ -90,7 +110,10 @@ export function buildPolicyColumns(): TableProps<PolicyConfirmRow>['columns'] {
key: 'storeName',
width: 200,
onHeaderCell: () => ({ style: storeInfoHeaderStyle }),
onCell: () => ({ style: cellStyle }),
onCell: (_r, index) => ({
rowSpan: getRowSpan(index ?? 0, (r) => r.storeName),
style: cellStyle,
}),
render: (v) => <span style={{ color: '#1A202C', fontWeight: 500 }}>{v}</span>,
},
{
......@@ -100,7 +123,11 @@ export function buildPolicyColumns(): TableProps<PolicyConfirmRow>['columns'] {
width: 130,
align: 'center',
onHeaderCell: () => ({ style: storeInfoHeaderStyle }),
onCell: () => ({ style: cellStyle }),
onCell: (_r, index) => ({
// 同门店的商户号必然相同,按「门店名 + 商户号」复合键合并
rowSpan: getRowSpan(index ?? 0, (r) => `${r.storeName}|${r.storeMerchantNo}`),
style: cellStyle,
}),
render: (v) => <a style={{ color: '#2563EB', fontSize: 12.5 }}>{v}</a>,
},
{
......@@ -110,7 +137,11 @@ export function buildPolicyColumns(): TableProps<PolicyConfirmRow>['columns'] {
width: 130,
align: 'center',
onHeaderCell: () => ({ style: storeInfoHeaderStyle }),
onCell: () => ({ style: cellStyle }),
onCell: (_r, index) => ({
// 同门店下相同业务场景才合并(不同门店同名场景不合并)
rowSpan: getRowSpan(index ?? 0, (r) => `${r.storeName}|${r.businessScene}`),
style: cellStyle,
}),
render: (v: string) => <SceneTag text={v} />,
},
{
......@@ -203,7 +234,11 @@ export function buildPolicyColumns(): TableProps<PolicyConfirmRow>['columns'] {
width: 170,
align: 'center',
onHeaderCell: () => ({ style: bizPolicyHeaderStyle }),
onCell: () => ({ style: cellStyle }),
onCell: (_r, index) => ({
// 同门店同业务场景合并(与「业务场景」列合并粒度一致)
rowSpan: getRowSpan(index ?? 0, (r) => `${r.storeName}|${r.businessScene}`),
style: cellStyle,
}),
},
{
title: '一线阳光计算模式',
......@@ -241,7 +276,11 @@ export function buildPolicyColumns(): TableProps<PolicyConfirmRow>['columns'] {
width: 180,
align: 'center',
onHeaderCell: () => ({ style: bizPolicyHeaderStyle }),
onCell: () => ({ style: cellStyle }),
onCell: (_r, index) => ({
// 同门店同业务场景合并(与「业务场景」列合并粒度一致)
rowSpan: getRowSpan(index ?? 0, (r) => `${r.storeName}|${r.businessScene}`),
style: cellStyle,
}),
},
{
title: '一线阳光2计算模式',
......@@ -285,7 +324,11 @@ export function buildPolicyColumns(): TableProps<PolicyConfirmRow>['columns'] {
width: 160,
align: 'center',
onHeaderCell: () => ({ style: costHeaderStyle }),
onCell: () => ({ style: cellStyle }),
onCell: (_r, index) => ({
// 运营成本按「门店 + 业务场景」维度合并(同一门店下不同业务场景的运营成本不同)
rowSpan: getRowSpan(index ?? 0, (r) => `${r.storeName}|${r.businessScene}`),
style: cellStyle,
}),
render: (v) => <MoneyCell value={v as string} />,
},
{
......@@ -295,7 +338,10 @@ export function buildPolicyColumns(): TableProps<PolicyConfirmRow>['columns'] {
width: 220,
align: 'center',
onHeaderCell: () => ({ style: costHeaderStyle }),
onCell: () => ({ style: cellStyle }),
onCell: (_r, index) => ({
rowSpan: getRowSpan(index ?? 0, (r) => `${r.storeName}|${r.businessScene}`),
style: cellStyle,
}),
render: (v) => <MoneyCell value={v as string} />,
},
{
......@@ -305,7 +351,10 @@ export function buildPolicyColumns(): TableProps<PolicyConfirmRow>['columns'] {
width: 140,
align: 'center',
onHeaderCell: () => ({ style: costHeaderStyle }),
onCell: () => ({ style: cellStyle }),
onCell: (_r, index) => ({
rowSpan: getRowSpan(index ?? 0, (r) => `${r.storeName}|${r.businessScene}`),
style: cellStyle,
}),
render: (v) => <MoneyCell value={v as string} />,
},
],
......
......@@ -24,7 +24,21 @@ export function PolicyConfirmTable({
ruleCategoryCount,
ruleTotalRows,
}: PolicyConfirmTableProps) {
const columns = useMemo(() => buildPolicyColumns() as any[], []);
/**
* 数据源优先级:previewRows(接口真实数据) > dataSource > mock 兜底
* 放在 columns 之前,因为 buildPolicyColumns 需要 rows 来计算同门店 / 同场景的合并单元格 rowSpan
*/
const rows: PolicyConfirmRow[] = useMemo(() => {
if (Array.isArray(previewRows) && previewRows.length > 0) {
return convertPreviewRows(previewRows);
}
if (Array.isArray(dataSource) && dataSource.length > 0) {
return dataSource;
}
return mockPolicyRows;
}, [dataSource, previewRows]);
const columns = useMemo(() => buildPolicyColumns(rows) as any[], [rows]);
/**
* 动态拼接 footer 文字:优先用外部传入的通过行统计,
......@@ -71,17 +85,6 @@ export function PolicyConfirmTable({
return colWidths.reduce((a, b) => a + b, 0) + 40; // 加 40px 冗余
}, [columns]);
const rows: PolicyConfirmRow[] = useMemo(() => {
// 优先级:previewRows(接口真实数据) > dataSource > mock 兜底
if (Array.isArray(previewRows) && previewRows.length > 0) {
return convertPreviewRows(previewRows);
}
if (Array.isArray(dataSource) && dataSource.length > 0) {
return dataSource;
}
return mockPolicyRows;
}, [dataSource, previewRows]);
// 显式标注 TableProps:否则 pagination:false 会被推断成 boolean,赋给 UTable 时类型不兼容
const tableProps: TableProps<PolicyConfirmRow> = {
rowKey: (r: PolicyConfirmRow) => `${r.storeMerchantNo}-${r.businessScene}-${r.productCategory}`,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!