forked from alibaba/ali-react-table
/
autoRowSpan.ts
62 lines (53 loc) · 1.61 KB
/
autoRowSpan.ts
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
import { safeGetValue } from '../../common-utils/internals'
import isLeafNode from '../../common-utils/isLeafNode'
import { SpanRect } from '../../interfaces'
import { TableTransform } from '../interfaces'
import { transformColumn } from '../utils'
function isIdentity(x: any, y: any) {
return x === y
}
export function autoRowSpan(): TableTransform {
return transformColumn((col, { dataSource, range }) => {
if (!col.features?.autoRowSpan) {
return col
}
if (!isLeafNode(col)) {
return col
}
const isFunc = typeof col.features.autoRowSpan === 'function'
const shouldMergeCell = isFunc ? col.features.autoRowSpan : isIdentity
const spanRects: SpanRect[] = []
let lastBottom = 0
let prevValue: any = null
for (let rowIndex = 0; rowIndex < dataSource.length; rowIndex++) {
const value = safeGetValue(col, dataSource[rowIndex], rowIndex)
if (rowIndex === 0 || !shouldMergeCell(prevValue, value)) {
const spanRect: SpanRect = {
top: lastBottom,
bottom: rowIndex,
left: range.start,
right: range.end,
}
for (let i = lastBottom; i < rowIndex; i++) {
spanRects.push(spanRect)
}
lastBottom = rowIndex
}
prevValue = value
}
for (let i = lastBottom; i < dataSource.length; i++) {
spanRects.push({
top: lastBottom,
bottom: dataSource.length,
left: range.start,
right: range.end,
})
}
return {
...col,
getSpanRect(value: any, row: any, rowIndex: number) {
return spanRects[rowIndex]
},
}
})
}