@gmfe/table-x 2.14.30-beta.4 → 2.14.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,165 +0,0 @@
1
- import {
2
- TABLE_X_SELECT_ID,
3
- TABLE_X_DIY_ID,
4
- TABLE_X_EXPAND_ID
5
- } from '../../util'
6
-
7
- /**
8
- * 检查一级表头下的子列 fixed 状态是否一致
9
- */
10
- function validateSubColumnsFixed(column) {
11
- if (!column.subColumns || column.subColumns.length === 0) {
12
- return { valid: true }
13
- }
14
-
15
- const subColumns = column.subColumns
16
- // 重要:二级分类的 fixed 应该与一级分类保持一致
17
- // 所以验证时,所有子列应该使用一级表头的 fixed(column.fixed)
18
- const expectedFixed = column.fixed !== undefined ? column.fixed : undefined
19
- const fixedStates = subColumns.map(subCol => {
20
- // 子列的 fixed 应该与一级表头一致,如果有不一致的,记录实际的 fixed 用于检查
21
- return subCol.fixed !== undefined ? subCol.fixed : expectedFixed
22
- })
23
-
24
- const allSame = fixedStates.every(fixed => fixed === expectedFixed)
25
-
26
- if (!allSame) {
27
- return {
28
- valid: false,
29
- error:
30
- `一级表头 "${column.Header || column.id}" 下的子列 fixed 状态不一致。` +
31
- `为了避免滚动时表头宽度错位,请确保同一一级表头下的所有子列要么全部固定,要么全部不固定。`
32
- }
33
- }
34
-
35
- return { valid: true }
36
- }
37
-
38
- /**
39
- * 将带有 subColumns 的 columns 转换为 react-table 可以理解的嵌套结构
40
- * 输入:[{ Header: '菜品信息', subColumns: [...] }]
41
- * 输出:[{ Header: '菜品信息', columns: [...] }] (react-table 格式)
42
- */
43
- export function transformColumnsForTwoLevel(columns) {
44
- const transformedColumns = []
45
- const firstLevelHeaders = []
46
- const specialColumnIds = [
47
- TABLE_X_SELECT_ID,
48
- TABLE_X_DIY_ID,
49
- TABLE_X_EXPAND_ID
50
- ]
51
-
52
- // 分离普通列和特殊列
53
- const normalColumns = []
54
- const leftFixedSpecialColumns = [] // fixed: 'left' 的特殊列(如 diy)
55
- const otherSpecialColumns = [] // 其他特殊列(如 select、expand)
56
-
57
- columns.forEach(column => {
58
- if (specialColumnIds.includes(column.id)) {
59
- if (column.fixed === 'left') {
60
- leftFixedSpecialColumns.push(column)
61
- } else {
62
- otherSpecialColumns.push(column)
63
- }
64
- } else {
65
- normalColumns.push(column)
66
- }
67
- })
68
-
69
- // 先处理普通列
70
- normalColumns.forEach((column, index) => {
71
- if (column.subColumns && column.subColumns.length > 0) {
72
- // 过滤可见的子列
73
- const visibleSubCols = column.subColumns.filter(
74
- subCol => subCol.show !== false
75
- )
76
-
77
- // 如果所有子列都被隐藏,跳过这个一级表头
78
- if (visibleSubCols.length === 0) {
79
- return
80
- }
81
-
82
- // 验证 fixed 状态(使用可见的子列)
83
- const validation = validateSubColumnsFixed({
84
- ...column,
85
- subColumns: visibleSubCols
86
- })
87
- if (!validation.valid) {
88
- console.error(
89
- `[TwoLevelTableX] 配置错误 (第 ${index + 1} 个列):`,
90
- validation.error
91
- )
92
- if (process.env.NODE_ENV === 'development') {
93
- throw new Error(validation.error)
94
- }
95
- }
96
-
97
- firstLevelHeaders.push({
98
- ...column,
99
- hasSubColumns: true,
100
- subColumnCount: visibleSubCols.length // 使用可见子列的数量
101
- })
102
-
103
- // 确定统一的 fixed 状态
104
- // 重要:只使用一级表头的 fixed 属性,二级分类的 fixed 应该与一级分类保持一致
105
- const unifiedFixed = column.fixed !== undefined ? column.fixed : undefined
106
-
107
- // 转换为 react-table 的嵌套结构(只使用可见的子列)
108
- transformedColumns.push({
109
- Header: column.Header,
110
- id: column.id || `group_${transformedColumns.length}`,
111
- fixed: unifiedFixed,
112
- columns: visibleSubCols.map(subCol => {
113
- // 移除子列自己的 fixed 属性,强制使用一级表头的 fixed
114
- const { fixed: _subColFixed, ...subColWithoutFixed } = subCol
115
- return {
116
- ...subColWithoutFixed,
117
- fixed: unifiedFixed // 二级分类的 fixed 与一级分类保持一致
118
- }
119
- })
120
- })
121
- } else {
122
- // 没有 subColumns,直接添加(会占两行)
123
- firstLevelHeaders.push({
124
- ...column,
125
- hasSubColumns: false,
126
- subColumnCount: 1
127
- })
128
-
129
- transformedColumns.push(column)
130
- }
131
- })
132
-
133
- // 处理 fixed: 'left' 的特殊列(如 diy),放在最前面
134
- leftFixedSpecialColumns.forEach(column => {
135
- transformedColumns.unshift(column)
136
- firstLevelHeaders.unshift({
137
- ...column,
138
- hasSubColumns: false,
139
- subColumnCount: 1,
140
- isSpecialColumn: true
141
- })
142
- })
143
-
144
- // 最后处理其他特殊列(select、expand)放在最后
145
- // 注意:移除特殊列的 fixed 属性,让它们按数组顺序显示在最后(不固定)
146
- // 如果用户需要特殊列固定在右边,可以手动设置 fixed: 'right'
147
- otherSpecialColumns.forEach(column => {
148
- const { fixed, ...columnWithoutFixed } = column
149
- const adjustedColumn = {
150
- ...columnWithoutFixed,
151
- // 特殊列应该在最后,移除 fixed: 'left',让它们按数组顺序显示
152
- // 如果需要固定在右边,可以改为 fixed: 'right'
153
- fixed: fixed === 'left' ? undefined : fixed
154
- }
155
- transformedColumns.push(adjustedColumn)
156
- firstLevelHeaders.push({
157
- ...adjustedColumn,
158
- hasSubColumns: false,
159
- subColumnCount: 1,
160
- isSpecialColumn: true
161
- })
162
- })
163
-
164
- return { transformedColumns, firstLevelHeaders }
165
- }
@@ -1,36 +0,0 @@
1
- import React, { useMemo } from 'react'
2
- import PropTypes from 'prop-types'
3
- import { flattenColumnsForSelectAndDiy } from './two_level_header_table_x/flatten_helper'
4
-
5
- /**
6
- * 两级表头 HOC
7
- * 功能:将带 subColumns 的 columns 扁平化,传递给内部 Component 处理
8
- *
9
- * 使用方式:
10
- * const TwoLevelTable = twoLevelHeaderTableXHOC(
11
- * selectTableXHOC(
12
- * diyTableXHOC(TwoLevelTableX)
13
- * )
14
- * )
15
- *
16
- * 注意:当存在一级表头相同的列时,在column配置中加id标识区分,避免混乱
17
- */
18
- function twoLevelHeaderTableXHOC(Component) {
19
- const TwoLevelHeaderTableXWrapper = props => {
20
- const { columns: originalColumns, ...rest } = props
21
-
22
- const flattenedColumns = useMemo(() => {
23
- return flattenColumnsForSelectAndDiy(originalColumns)
24
- }, [originalColumns])
25
-
26
- return <Component {...rest} columns={flattenedColumns} />
27
- }
28
-
29
- TwoLevelHeaderTableXWrapper.propTypes = {
30
- columns: PropTypes.array.isRequired
31
- }
32
-
33
- return TwoLevelHeaderTableXWrapper
34
- }
35
-
36
- export default twoLevelHeaderTableXHOC