forked from alibaba/ali-react-table
/
columnHover.tsx
49 lines (43 loc) · 1.34 KB
/
columnHover.tsx
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
import React from 'react'
import { isLeafNode } from '../../common-utils'
import { TableTransform } from '../interfaces'
import { transformColumn } from '../utils'
export interface ColumnHoverOptions {
hoverColor?: string
hoverColIndex: number
onChangeHoverColIndex(nextColIndex: number): void
}
export function columnHover({
hoverColor = '#f5f5f5',
hoverColIndex,
onChangeHoverColIndex,
}: ColumnHoverOptions): TableTransform {
return transformColumn((col, { range }) => {
const colIndexMatched = range.start <= hoverColIndex && hoverColIndex < range.end
if (!isLeafNode(col)) {
return col
}
const prevGetCellProps = col.getCellProps
return {
...col,
getCellProps(value: any, record: any, rowIndex: number): React.TdHTMLAttributes<HTMLTableDataCellElement> {
const prevCellProps = prevGetCellProps?.(value, record, rowIndex) ?? {}
return {
...prevCellProps,
style: {
...prevCellProps.style,
backgroundColor: colIndexMatched ? hoverColor : undefined,
},
onMouseEnter(e) {
prevCellProps.onMouseEnter?.(e)
onChangeHoverColIndex(range.start)
},
onMouseLeave(e) {
prevCellProps.onMouseLeave?.(e)
onChangeHoverColIndex(-1)
},
}
},
}
})
}