@@ -12,6 +12,7 @@ import type {
12
12
ColumnGroupType ,
13
13
} from '../interface' ;
14
14
import { INTERNAL_COL_DEFINE } from '../utils/legacyUtil' ;
15
+ import { EXPAND_COLUMN } from '../constant' ;
15
16
16
17
export function convertChildrenToColumns < RecordType > (
17
18
children : React . ReactNode ,
@@ -144,11 +145,41 @@ function useColumns<RecordType>(
144
145
[ columns , children ] ,
145
146
) ;
146
147
147
- // Add expand column
148
+ // ========================== Expand ==========================
148
149
const withExpandColumns = React . useMemo < ColumnsType < RecordType > > ( ( ) => {
149
150
if ( expandable ) {
150
- const expandColIndex = expandIconColumnIndex || 0 ;
151
- const prevColumn = baseColumns [ expandColIndex ] ;
151
+ let cloneColumns = baseColumns . slice ( ) ;
152
+
153
+ // >>> Warning if use `expandIconColumnIndex`
154
+ if ( process . env . NODE_ENV !== 'production' && expandIconColumnIndex >= 0 ) {
155
+ warning (
156
+ false ,
157
+ '`expandIconColumnIndex` is deprecated. Please use `Table.EXPAND_COLUMN` in `columns` instead.' ,
158
+ ) ;
159
+ }
160
+
161
+ // >>> Insert expand column if not exist
162
+ if ( ! cloneColumns . includes ( EXPAND_COLUMN ) ) {
163
+ const expandColIndex = expandIconColumnIndex || 0 ;
164
+ if ( expandColIndex >= 0 ) {
165
+ cloneColumns . splice ( expandColIndex , 0 , EXPAND_COLUMN ) ;
166
+ }
167
+ }
168
+
169
+ // >>> Deduplicate additional expand column
170
+ if (
171
+ process . env . NODE_ENV !== 'production' &&
172
+ cloneColumns . filter ( c => c === EXPAND_COLUMN ) . length > 1
173
+ ) {
174
+ warning ( false , 'There exist more than one `EXPAND_COLUMN` in `columns`.' ) ;
175
+ }
176
+ const expandColumnIndex = cloneColumns . indexOf ( EXPAND_COLUMN ) ;
177
+ cloneColumns = cloneColumns . filter (
178
+ ( column , index ) => column !== EXPAND_COLUMN || index === expandColumnIndex ,
179
+ ) ;
180
+
181
+ // >>> Check if expand column need to fixed
182
+ const prevColumn = baseColumns [ expandColumnIndex ] ;
152
183
153
184
let fixedColumn : FixedType | null ;
154
185
if ( ( fixed === 'left' || fixed ) && ! expandIconColumnIndex ) {
@@ -159,6 +190,7 @@ function useColumns<RecordType>(
159
190
fixedColumn = prevColumn ? prevColumn . fixed : null ;
160
191
}
161
192
193
+ // >>> Create expandable column
162
194
const expandColumn = {
163
195
[ INTERNAL_COL_DEFINE ] : {
164
196
className : `${ prefixCls } -expand-icon-col` ,
@@ -187,16 +219,12 @@ function useColumns<RecordType>(
187
219
} ,
188
220
} ;
189
221
190
- // Insert expand column in the target position
191
- const cloneColumns = baseColumns . slice ( ) ;
192
- if ( expandColIndex >= 0 ) {
193
- cloneColumns . splice ( expandColIndex , 0 , expandColumn ) ;
194
- }
195
- return cloneColumns ;
222
+ return cloneColumns . map ( col => ( col === EXPAND_COLUMN ? expandColumn : col ) ) ;
196
223
}
197
224
return baseColumns ;
198
225
} , [ expandable , baseColumns , getRowKey , expandedKeys , expandIcon , direction ] ) ;
199
226
227
+ // ========================= Transform ========================
200
228
const mergedColumns = React . useMemo ( ( ) => {
201
229
let finalColumns = withExpandColumns ;
202
230
if ( transformColumns ) {
@@ -214,6 +242,7 @@ function useColumns<RecordType>(
214
242
return finalColumns ;
215
243
} , [ transformColumns , withExpandColumns , direction ] ) ;
216
244
245
+ // ========================== Flatten =========================
217
246
const flattenColumns = React . useMemo ( ( ) => {
218
247
if ( direction === 'rtl' ) {
219
248
return revertForRtl ( flatColumns ( mergedColumns ) ) ;
0 commit comments