Skip to content
This repository was archived by the owner on Apr 7, 2024. It is now read-only.

Commit f5e0c84

Browse files
committed
fix: 当表头分组第一列就在组内时,树表格和带详情的功能失效
1 parent 76fadfc commit f5e0c84

4 files changed

Lines changed: 159 additions & 147 deletions

File tree

packages/ali-react-table/src/pipeline/features/rowDetail.tsx

Lines changed: 70 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React, { ReactNode } from 'react'
33
import { ExpansionCell, icons, InlineFlexCell } from '../../common-views'
44
import { ArtColumn } from '../../interfaces'
55
import { internals } from '../../internals'
6-
import { collectNodes, mergeCellProps } from '../../utils'
6+
import { collectNodes, mergeCellProps, upDataFirstCol } from '../../utils'
77
import { always, flatMap } from '../../utils/others'
88
import { TablePipeline } from '../pipeline'
99

@@ -100,7 +100,7 @@ export function rowDetail(opts: RowDetailFeatureOptions = {}) {
100100
onChangeOpenKeys([...openKeys, rowKey], rowKey, 'expand')
101101
}
102102
}
103-
103+
104104
// 还原primaryKey
105105
const resetRow = (row:any)=>{
106106
let key = row[originKeySymbol];
@@ -132,87 +132,86 @@ export function rowDetail(opts: RowDetailFeatureOptions = {}) {
132132
return columns
133133
}
134134
const columnFlatCount = collectNodes(columns, 'leaf-only').length
135-
const [firstCol, ...others] = columns
136-
137-
const render = (value: any, row: any, rowIndex: number) => {
138-
if (row[rowDetailMetaKey]) {
139-
return renderDetail(resetRow(row), rowIndex)
140-
}
141-
142-
const content = internals.safeRender(firstCol, row, rowIndex)
143135

144-
if (!hasDetail(row, rowIndex)) {
145-
return <InlineFlexCell style={{ marginLeft: textOffset }}>{content}</InlineFlexCell>
146-
}
136+
return upDataFirstCol(columns, (firstCol) => {
147137

148-
const rowKey = row[primaryKey]
149-
const expanded = openKeySet.has(rowKey)
150-
const onClick = (e: React.MouseEvent) => {
151-
if (opts.stopClickEventPropagation) {
152-
e.stopPropagation()
138+
const render = (value: any, row: any, rowIndex: number) => {
139+
if (row[rowDetailMetaKey]) {
140+
return renderDetail(resetRow(row), rowIndex)
153141
}
154-
toggle(rowKey)
155-
}
156-
157-
const expandCls = expanded ? 'expanded' : 'collapsed'
158-
return (
159-
<ExpansionCell
160-
className={cx('expansion-cell', expandCls)}
161-
style={{ cursor: clickArea === 'content' ? 'pointer' : undefined }}
162-
onClick={clickArea === 'content' ? onClick : undefined}
163-
>
164-
<icons.CaretRight
165-
style={{
166-
cursor: clickArea === 'icon' ? 'pointer' : undefined,
167-
marginLeft: indents.iconIndent,
168-
marginRight: indents.iconGap,
169-
}}
170-
className={cx('expansion-icon', expandCls)}
171-
onClick={clickArea === 'icon' ? onClick : undefined}
172-
/>
173-
{content}
174-
</ExpansionCell>
175-
)
176-
}
177-
178-
const getCellProps = (value: any, row: any, rowIndex: number) => {
179-
if (row[rowDetailMetaKey]) {
180-
return {
181-
style: {
182-
'--cell-padding': '0',
183-
overflow: 'hidden',
184-
...opts.detailCellStyle,
185-
} as any,
142+
143+
const content = internals.safeRender(firstCol, row, rowIndex)
144+
145+
if (!hasDetail(row, rowIndex)) {
146+
return <InlineFlexCell style={{ marginLeft: textOffset }}>{content}</InlineFlexCell>
186147
}
187-
}
188-
189-
const prevProps = firstCol.getCellProps?.(value, row, rowIndex)
190-
191-
if (!hasDetail(row, rowIndex)) {
192-
return prevProps
193-
}
194-
195-
return mergeCellProps(prevProps, {
196-
onClick(e) {
148+
149+
const rowKey = row[primaryKey]
150+
const expanded = openKeySet.has(rowKey)
151+
const onClick = (e: React.MouseEvent) => {
197152
if (opts.stopClickEventPropagation) {
198153
e.stopPropagation()
199154
}
200-
toggle(row[primaryKey])
201-
},
202-
style: { cursor: 'pointer' },
203-
})
204-
}
155+
toggle(rowKey)
156+
}
157+
158+
const expandCls = expanded ? 'expanded' : 'collapsed'
159+
return (
160+
<ExpansionCell
161+
className={cx('expansion-cell', expandCls)}
162+
style={{ cursor: clickArea === 'content' ? 'pointer' : undefined }}
163+
onClick={clickArea === 'content' ? onClick : undefined}
164+
>
165+
<icons.CaretRight
166+
style={{
167+
cursor: clickArea === 'icon' ? 'pointer' : undefined,
168+
marginLeft: indents.iconIndent,
169+
marginRight: indents.iconGap,
170+
}}
171+
className={cx('expansion-icon', expandCls)}
172+
onClick={clickArea === 'icon' ? onClick : undefined}
173+
/>
174+
{content}
175+
</ExpansionCell>
176+
)
177+
}
178+
179+
const getCellProps = (value: any, row: any, rowIndex: number) => {
180+
if (row[rowDetailMetaKey]) {
181+
return {
182+
style: {
183+
'--cell-padding': '0',
184+
overflow: 'hidden',
185+
...opts.detailCellStyle,
186+
} as any,
187+
}
188+
}
189+
190+
const prevProps = firstCol.getCellProps?.(value, row, rowIndex)
191+
192+
if (!hasDetail(row, rowIndex)) {
193+
return prevProps
194+
}
195+
196+
return mergeCellProps(prevProps, {
197+
onClick(e) {
198+
if (opts.stopClickEventPropagation) {
199+
e.stopPropagation()
200+
}
201+
toggle(row[primaryKey])
202+
},
203+
style: { cursor: 'pointer' },
204+
})
205+
}
205206

206-
return [
207-
{
207+
return {
208208
...firstCol,
209209
title: (
210210
<div style={{ display: 'inline-block', marginLeft: textOffset }}>
211211
{internals.safeRenderHeader(firstCol)}
212212
</div>
213213
),
214214
render,
215-
// getCellProps: clickArea === 'cell' ? getCellProps : firstCol.getCellProps,
216215
getCellProps: (value: any, row: any, rowIndex: number)=>{
217216
if (row[rowDetailMetaKey] || clickArea === 'cell') {
218217
return getCellProps(value, row, rowIndex);
@@ -225,10 +224,9 @@ export function rowDetail(opts: RowDetailFeatureOptions = {}) {
225224
// detail 总是成一行
226225
return { top: rowIndex, bottom: rowIndex + 1, left: 0, right: columnFlatCount }
227226
}
228-
},
229-
},
230-
...others,
231-
]
227+
}
228+
} as ArtColumn
229+
})
232230
}
233231
}
234232
}

packages/ali-react-table/src/pipeline/features/treeMode.tsx

Lines changed: 72 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React from 'react'
33
import { ExpansionCell, icons, InlineFlexCell } from '../../common-views'
44
import { ArtColumn } from '../../interfaces'
55
import { internals } from '../../internals'
6-
import { isLeafNode as standardIsLeafNode, mergeCellProps } from '../../utils'
6+
import { isLeafNode as standardIsLeafNode, mergeCellProps, upDataFirstCol } from '../../utils'
77
import { TablePipeline } from '../pipeline'
88

99
export const treeMetaSymbol = Symbol('treeMetaSymbol')
@@ -110,91 +110,88 @@ export function treeMode(opts: TreeModeFeatureOptions = {}) {
110110
if (columns.length === 0) {
111111
return columns
112112
}
113-
const [firstCol, ...others] = columns
114-
115-
const render = (value: any, record: any, recordIndex: number) => {
116-
const content = internals.safeRender(firstCol, record, recordIndex)
117-
if (record[treeMetaKey] == null) {
118-
// 没有 treeMeta 信息的话,就返回原先的渲染结果
119-
return content
120-
}
121-
122-
const { rowKey, depth, isLeaf, expanded } = record[treeMetaKey]
123-
124-
const indent = iconIndent + depth * indentSize
125-
126-
if (isLeaf) {
127-
return (
128-
<InlineFlexCell className="expansion-cell leaf">
129-
<span style={{ marginLeft: indent + iconWidth + iconGap }}>{content}</span>
130-
</InlineFlexCell>
131-
)
132-
}
133-
134-
const onClick = (e: React.MouseEvent) => {
135-
if (stopClickEventPropagation) {
136-
e.stopPropagation()
113+
// const [firstCol, ...others] = columns
114+
return upDataFirstCol(columns, firstCol => {
115+
const render = (value: any, record: any, recordIndex: number) => {
116+
const content = internals.safeRender(firstCol, record, recordIndex)
117+
if (record[treeMetaKey] == null) {
118+
// 没有 treeMeta 信息的话,就返回原先的渲染结果
119+
return content
137120
}
138-
toggle(rowKey)
139-
}
140-
141-
const expandCls = expanded ? 'expanded' : 'collapsed'
142-
return (
143-
<ExpansionCell
144-
className={cx('expansion-cell', expandCls)}
145-
style={{
146-
cursor: clickArea === 'content' ? 'pointer' : undefined,
147-
}}
148-
onClick={clickArea === 'content' ? onClick : undefined}
149-
>
150-
<icons.CaretRight
151-
className={cx('expansion-icon', expandCls)}
152-
style={{
153-
cursor: clickArea === 'icon' ? 'pointer' : undefined,
154-
marginLeft: indent,
155-
marginRight: iconGap,
156-
}}
157-
onClick={clickArea === 'icon' ? onClick : undefined}
158-
/>
159-
{content}
160-
</ExpansionCell>
161-
)
162-
}
163-
164-
const getCellProps = (value: any, record: any, rowIndex: number) => {
165-
const prevProps = internals.safeGetCellProps(firstCol, record, rowIndex)
166-
if (record[treeMetaKey] == null) {
167-
// 没有 treeMeta 信息的话,就返回原先的 cellProps
168-
return prevProps
169-
}
170-
171-
const { isLeaf, rowKey } = record[treeMetaKey]
172-
if (isLeaf) {
173-
return prevProps
174-
}
175-
176-
return mergeCellProps(prevProps, {
177-
onClick(e) {
121+
122+
const { rowKey, depth, isLeaf, expanded } = record[treeMetaKey]
123+
124+
const indent = iconIndent + depth * indentSize
125+
126+
if (isLeaf) {
127+
return (
128+
<InlineFlexCell className="expansion-cell leaf">
129+
<span style={{ marginLeft: indent + iconWidth + iconGap }}>{content}</span>
130+
</InlineFlexCell>
131+
)
132+
}
133+
134+
const onClick = (e: React.MouseEvent) => {
178135
if (stopClickEventPropagation) {
179136
e.stopPropagation()
180137
}
181138
toggle(rowKey)
182-
},
183-
style: { cursor: 'pointer' },
184-
})
185-
}
186-
187-
return [
188-
{
139+
}
140+
141+
const expandCls = expanded ? 'expanded' : 'collapsed'
142+
return (
143+
<ExpansionCell
144+
className={cx('expansion-cell', expandCls)}
145+
style={{
146+
cursor: clickArea === 'content' ? 'pointer' : undefined,
147+
}}
148+
onClick={clickArea === 'content' ? onClick : undefined}
149+
>
150+
<icons.CaretRight
151+
className={cx('expansion-icon', expandCls)}
152+
style={{
153+
cursor: clickArea === 'icon' ? 'pointer' : undefined,
154+
marginLeft: indent,
155+
marginRight: iconGap,
156+
}}
157+
onClick={clickArea === 'icon' ? onClick : undefined}
158+
/>
159+
{content}
160+
</ExpansionCell>
161+
)
162+
}
163+
164+
const getCellProps = (value: any, record: any, rowIndex: number) => {
165+
const prevProps = internals.safeGetCellProps(firstCol, record, rowIndex)
166+
if (record[treeMetaKey] == null) {
167+
// 没有 treeMeta 信息的话,就返回原先的 cellProps
168+
return prevProps
169+
}
170+
171+
const { isLeaf, rowKey } = record[treeMetaKey]
172+
if (isLeaf) {
173+
return prevProps
174+
}
175+
176+
return mergeCellProps(prevProps, {
177+
onClick(e) {
178+
if (stopClickEventPropagation) {
179+
e.stopPropagation()
180+
}
181+
toggle(rowKey)
182+
},
183+
style: { cursor: 'pointer' },
184+
})
185+
}
186+
return {
189187
...firstCol,
190188
title: (
191189
<span style={{ marginLeft: iconIndent + iconWidth + iconGap }}>{internals.safeRenderHeader(firstCol)}</span>
192190
),
193191
render,
194192
getCellProps: clickArea === 'cell' ? getCellProps : firstCol.getCellProps,
195-
},
196-
...others,
197-
]
193+
}
194+
})
198195
}
199196
}
200197
}

packages/ali-react-table/src/utils/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export { default as proto } from './proto'
1212
export { default as makeRecursiveMapper } from './makeRecursiveMapper'
1313
export { default as smartCompare } from './smartCompare'
1414
export { default as traverseColumn } from './traverseColumn'
15+
export { default as upDataFirstCol } from './upDataFirstCol'
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { ArtColumn } from '../interfaces'
2+
/**
3+
* 更新第一个叶子节点列配置
4+
*/
5+
export default function upDataFirstCol(columns: ArtColumn[], upFn:(firstCol:ArtColumn)=>ArtColumn):ArtColumn[]{
6+
const [firstCol, ...others] = columns;
7+
if(firstCol.children && firstCol.children.length){
8+
firstCol.children = upDataFirstCol(firstCol.children, upFn)
9+
return [...columns];
10+
}else{
11+
return [
12+
upFn(firstCol),
13+
...others
14+
]
15+
}
16+
}

0 commit comments

Comments
 (0)