index.tsx
5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { useMemo } from 'react';
import { UCard, UTable, UText } from '@uj/ui';
import type { TableProps } from 'antd';
import { buildPolicyColumns } from './columns';
import type { StoreImportRowDTO } from '@/api/modules/store';
import { mockPolicyRows, convertPreviewRows } from '../../tools/policyTableData';
import type { PolicyConfirmRow } from '../../tools/policyTableData';
export interface PolicyConfirmTableProps {
dataSource?: PolicyConfirmRow[];
previewRows?: StoreImportRowDTO[];
/** 通过行的业务场景数(如 2) */
ruleSceneCount?: number;
/** 通过行的产品分类数(如 6) */
ruleCategoryCount?: number;
/** 通过行数(标题红框显示的数,如 11) */
ruleTotalRows?: number;
}
export function PolicyConfirmTable({
dataSource,
previewRows,
ruleSceneCount,
ruleCategoryCount,
ruleTotalRows,
}: PolicyConfirmTableProps) {
/**
* 数据源优先级: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 文字:优先用外部传入的通过行统计,
* 否则从 previewRows 派生,最后兜底 2 场景 × 6 分类 = 12 行。
*/
const footerText = useMemo(() => {
// 内部派生兜底(外部没传时用)
let s = ruleSceneCount;
let c = ruleCategoryCount;
let t = ruleTotalRows;
if (
(typeof s !== 'number' || typeof c !== 'number' || typeof t !== 'number') &&
Array.isArray(previewRows) &&
previewRows.length > 0
) {
s = s ?? new Set(previewRows.map((r) => r.businessScene).filter(Boolean)).size;
c = c ?? new Set(previewRows.map((r) => r.productCategory).filter(Boolean)).size;
t = t ?? previewRows.length;
}
const scene = s ?? 2;
const category = c ?? 6;
const total = t ?? scene * category;
return (
`表格唯一项 = 门店 + 业务场景 + 产品分类。同一门店的多个业务场景各占独立行` +
`(本批次 ${scene} 个业务场景 × ${category} 个产品分类 = ${total} 行),` +
`门店名称重复出现、不合并;「业务场景」若一家门店有多个,须拆分为独立行导入。` +
`「产品分类」多级分类可用「-」区分。「装修、设备总投入」「当月产生费用」按产品分类维度合并填写,` +
`「人工成本」按人力成本维度合并填写 —— 同一门店的多行需保持一致,系统将校验一致性。`
);
}, [ruleSceneCount, ruleCategoryCount, ruleTotalRows, previewRows]);
// columns 预设宽度累加,保证横向滚动条出现
const scrollX = useMemo(() => {
const colWidths: number[] = [];
const walk = (arr: any[] | undefined) => {
if (!arr) return;
for (const c of arr) {
if (c.children && c.children.length > 0) walk(c.children);
else if (typeof c.width === 'number') colWidths.push(c.width);
else colWidths.push(160);
}
};
walk(columns);
return colWidths.reduce((a, b) => a + b, 0) + 40; // 加 40px 冗余
}, [columns]);
// 显式标注 TableProps:否则 pagination:false 会被推断成 boolean,赋给 UTable 时类型不兼容
const tableProps: TableProps<PolicyConfirmRow> = {
rowKey: (r: PolicyConfirmRow) => `${r.storeMerchantNo}-${r.businessScene}-${r.productCategory}`,
columns: columns as TableProps<PolicyConfirmRow>['columns'],
dataSource: rows,
size: 'middle',
// tableLayout='fixed' 强制按每列 width 渲染,避免 ant 自动压缩到父容器宽度
tableLayout: 'fixed',
// 明确给 scroll.x,让 antd Table 内部生成横向滚动容器(内层 table width=x)
scroll: { x: scrollX },
pagination: false,
bordered: true,
};
return (
<UCard
styles={{ body: { padding: '16px 16px 12px' } }}
style={{
border: '1px solid #E5E7EB',
marginBottom: 20,
borderRadius: 10,
overflow: 'hidden',
}}
>
{/* 外层专属 wrapper:覆盖全局 index.css 关 ant Table 横向滚动的 !important 规则 */}
<div className="policy-confirm-table-wrap" style={{ width: '100%' }}>
<UTable<PolicyConfirmRow> {...tableProps} />
</div>
<div
style={{
marginTop: 12,
padding: '10px 12px',
background: '#F9FAFB',
borderRadius: 6,
}}
>
<UText type="secondary" style={{ fontSize: 12, lineHeight: 1.7 }}>
{footerText}
</UText>
</div>
{/* 写在组件末尾的 <style>:针对当前 wrapper 下的 ant 容器恢复横向滚动 */}
<style>{`
.policy-confirm-table-wrap .ant-table,
.policy-confirm-table-wrap .ant-table-container,
.policy-confirm-table-wrap .ant-table-content,
.policy-confirm-table-wrap .ant-table-body {
overflow-x: auto !important;
}
`}</style>
</UCard>
);
}