1
- import React , { useMemo , useState } from 'react' ;
1
+ import React , { useMemo , useState , useEffect } from 'react' ;
2
2
import Icon from '@uiw/react-icon' ;
3
3
import { TableProps } from './' ;
4
4
import './style/index.less' ;
@@ -7,7 +7,7 @@ import { noop } from '@uiw/utils';
7
7
interface TableTrProps < T > {
8
8
rowKey ?: keyof T ;
9
9
data : T [ ] ;
10
- keys : string [ ] ;
10
+ keys : TableProps [ 'columns' ] ;
11
11
render : { [ key : string ] : any } ;
12
12
ellipsis ?: Record < string , boolean > ;
13
13
prefixCls : string ;
@@ -34,16 +34,25 @@ export default function TableTr<T extends { [key: string]: any }>(props: TableTr
34
34
indentSize,
35
35
childrenColumnName,
36
36
} = props ;
37
-
37
+ const [ isOpacity , setIsOpacity ] = useState ( false ) ;
38
38
const [ expandIndex , setExpandIndex ] = useState < Array < T [ keyof T ] | number > > ( [ ] ) ;
39
+ useEffect ( ( ) => {
40
+ setIsOpacity ( ! ! data ?. find ( ( it ) => it [ childrenColumnName ] ) ) ;
41
+ } , [ data ] ) ;
39
42
40
43
const IconDom = useMemo ( ( ) => {
41
44
return ( key : T [ keyof T ] | number , isOpacity : boolean ) => {
42
45
const flag = expandIndex . includes ( key ) ;
43
46
return (
44
47
< Icon
45
48
type = { flag ? 'minus-square-o' : 'plus-square-o' }
46
- style = { { marginRight : 10 , opacity : isOpacity ? 1 : 0 , marginLeft : hierarchy * indentSize } }
49
+ style = { {
50
+ marginRight : 10 ,
51
+ opacity : isOpacity ? 1 : 0 ,
52
+ marginLeft : hierarchy * indentSize ,
53
+ float : 'left' ,
54
+ marginTop : 3.24 ,
55
+ } }
47
56
onClick = { ( ) => {
48
57
setExpandIndex ( flag ? expandIndex . filter ( ( it ) => it !== key ) : [ ...expandIndex , key ] ) ;
49
58
} }
@@ -61,12 +70,12 @@ export default function TableTr<T extends { [key: string]: any }>(props: TableTr
61
70
return (
62
71
< React . Fragment key = { rowNum } >
63
72
< tr key = { key } >
64
- { keys . map ( ( keyName , colNum ) => {
73
+ { keys ! . map ( ( keyName , colNum ) => {
65
74
let objs : React . TdHTMLAttributes < HTMLTableDataCellElement > = {
66
- children : trData [ keyName ] ,
75
+ children : trData [ keyName . key ! ] ,
67
76
} ;
68
- if ( render [ keyName ] ) {
69
- const child = render [ keyName ] ( trData [ keyName ] , keyName , trData , rowNum , colNum ) ;
77
+ if ( render [ keyName . key ! ] ) {
78
+ const child = render [ keyName . key ! ] ( trData [ keyName . key ! ] , keyName . key ! , trData , rowNum , colNum ) ;
70
79
if ( React . isValidElement ( child ) ) {
71
80
objs . children = child ;
72
81
} else {
@@ -79,21 +88,29 @@ export default function TableTr<T extends { [key: string]: any }>(props: TableTr
79
88
}
80
89
}
81
90
}
82
- if ( ellipsis && ellipsis [ keyName ] ) {
91
+ if ( ellipsis && ellipsis [ keyName . key ! ] ) {
83
92
objs . className = `${ prefixCls } -ellipsis` ;
84
93
}
85
94
const isHasChildren = Array . isArray ( trData [ childrenColumnName ] ) ;
86
- if ( colNum === 0 && ( hierarchy || isHasChildren ) ) {
87
- objs . className = `${ objs . className } ${ prefixCls } -has-children` ;
95
+ if ( colNum === 0 && ( isOpacity || hierarchy || isHasChildren ) ) {
88
96
objs . children = (
89
97
< >
90
98
{ IconDom ( key , isHasChildren ) }
99
+ < span style = { { paddingLeft : hierarchy * indentSize } } > </ span >
91
100
{ objs . children }
92
101
</ >
93
102
) ;
94
103
}
95
104
return (
96
- < td { ...objs } key = { colNum } onClick = { ( evn ) => onCell ( trData , { rowNum, colNum, keyName } , evn ) } />
105
+ < td
106
+ { ...objs }
107
+ key = { colNum }
108
+ // style={keyName?.style}
109
+ className = { `${ objs . className } ${ prefixCls } -tr-children-${ keyName ?. align || 'left' } ${
110
+ keyName . className || ''
111
+ } `}
112
+ onClick = { ( evn ) => onCell ( trData , { rowNum, colNum, keyName : keyName . key ! } , evn ) }
113
+ />
97
114
) ;
98
115
} ) }
99
116
</ tr >
0 commit comments