@@ -4,6 +4,7 @@ import Icon from '@uiw/react-icon';
4
4
import Thead from './Thead' ;
5
5
import { getLevelItems , getAllColumnsKeys } from './util' ;
6
6
import ExpandableComponent from './Expandable' ;
7
+ import TableTr from './TableTr' ;
7
8
import './style/index.less' ;
8
9
9
10
// 展开配置
@@ -16,14 +17,18 @@ export interface ExpandableType<T> {
16
17
rowExpandable ?: ( record : T ) => boolean ;
17
18
// 初始时,是否展开所有行
18
19
defaultExpandAllRows ?: boolean ;
19
- // 默认展开的行 rowKey数组
20
+ // 默认展开的行 rowKey数组
20
21
defaultExpandedRowKeys ?: Array < T [ keyof T ] | number > ;
21
- // 控制展开的行 rowKey数组
22
+ // 控制展开的行 rowKey数组
22
23
expandedRowKeys ?: Array < T [ keyof T ] | number > ;
23
24
// 展开的行变化触发
24
25
onExpandedRowsChange ?: ( expandedRows : Array < T [ keyof T ] | number > ) => void ;
25
26
// 点击展开图标触发
26
27
onExpand ?: ( expanded : boolean , record : T , index : number ) => void ;
28
+ // 控制树形结构每一层的缩进宽度
29
+ indentSize ?: number ;
30
+ // 指定树形结构的列名
31
+ childrenColumnName ?: string ;
27
32
}
28
33
29
34
export type TableColumns < T = any > = {
@@ -127,54 +132,62 @@ export default function Table<T extends { [key: string]: V }, V>(props: TablePro
127
132
) ;
128
133
} ;
129
134
} , [ expandable , expandIndex ] ) ;
130
- let keys = getAllColumnsKeys < T > ( columns ) ;
131
- let selfColumns : TableColumns < T > [ ] = [ ] ;
132
- if ( expandable ?. expandedRowRender ) {
133
- keys = [ 'uiw-expanded' , ...keys ] ;
134
- selfColumns = [
135
- {
136
- title : '' ,
137
- key : 'uiw-expanded' ,
138
- width : 50 ,
139
- align : 'center' ,
140
- render : ( text , key , record , index ) => {
141
- return (
142
- < ExpandableComponent
143
- defaultExpand = {
144
- expandable . defaultExpandAllRows === undefined
145
- ? ! ! expandable . defaultExpandedRowKeys ?. includes ( rowKey ? record [ rowKey ] : index )
146
- : ! ! expandable . defaultExpandAllRows
147
- }
148
- onClick = { ( expand ) => {
149
- expandable . onExpand ?.( expand , record , index ) ;
150
- if ( expand ) {
151
- const result = expandIndex . filter ( ( it ) => ( rowKey ? it !== record [ rowKey ] : it !== index ) ) ;
152
- expandable . onExpandedRowsChange ? expandable . onExpandedRowsChange ( result ) : setExpandIndex ( result ) ;
153
- } else {
154
- const result = [ ...expandIndex , rowKey ? record [ rowKey ] : index ] ;
155
- expandable . onExpandedRowsChange ? expandable . onExpandedRowsChange ( result ) : setExpandIndex ( result ) ;
156
- }
157
- } }
158
- expandIcon = { ( expand ) => {
159
- if ( expandable . rowExpandable && ! expandable . rowExpandable ?.( record ) ) {
160
- return null ;
161
- }
162
- if ( expandable . expandIcon ) {
163
- return expandable . expandIcon ( expand , record , index ) ;
135
+
136
+ const self = useMemo ( ( ) => {
137
+ let keys = getAllColumnsKeys < T > ( columns ) ;
138
+ let selfColumns : TableColumns < T > [ ] = [ ] ;
139
+ if ( expandable ?. expandedRowRender ) {
140
+ keys = [ 'uiw-expanded' , ...keys ] ;
141
+ selfColumns = [
142
+ {
143
+ title : '' ,
144
+ key : 'uiw-expanded' ,
145
+ width : 50 ,
146
+ align : 'center' ,
147
+ render : ( text , key , record , index ) => {
148
+ return (
149
+ < ExpandableComponent
150
+ defaultExpand = {
151
+ expandable . defaultExpandAllRows === undefined
152
+ ? ! ! expandable . defaultExpandedRowKeys ?. includes ( rowKey ? record [ rowKey ] : index )
153
+ : ! ! expandable . defaultExpandAllRows
164
154
}
165
- return expand ? < Icon type = "minus-square-o" /> : < Icon type = "plus-square-o" /> ;
166
- } }
167
- />
168
- ) ;
155
+ onClick = { ( expand ) => {
156
+ expandable . onExpand ?.( expand , record , index ) ;
157
+ if ( expand ) {
158
+ const result = expandIndex . filter ( ( it ) => ( rowKey ? it !== record [ rowKey ] : it !== index ) ) ;
159
+ expandable . onExpandedRowsChange ? expandable . onExpandedRowsChange ( result ) : setExpandIndex ( result ) ;
160
+ } else {
161
+ const result = [ ...expandIndex , rowKey ? record [ rowKey ] : index ] ;
162
+ expandable . onExpandedRowsChange ? expandable . onExpandedRowsChange ( result ) : setExpandIndex ( result ) ;
163
+ }
164
+ } }
165
+ expandIcon = { ( expand ) => {
166
+ if ( expandable . rowExpandable && ! expandable . rowExpandable ?.( record ) ) {
167
+ return null ;
168
+ }
169
+ if ( expandable . expandIcon ) {
170
+ return expandable . expandIcon ( expand , record , index ) ;
171
+ }
172
+ return expand ? < Icon type = "minus-square-o" /> : < Icon type = "plus-square-o" /> ;
173
+ } }
174
+ />
175
+ ) ;
176
+ } ,
169
177
} ,
170
- } ,
171
- ...columns ,
172
- ] ;
173
- } else {
174
- selfColumns = [ ...columns ] ;
175
- }
178
+ ...columns ,
179
+ ] ;
180
+ } else {
181
+ selfColumns = [ ...columns ] ;
182
+ }
183
+ return {
184
+ keys,
185
+ selfColumns,
186
+ } ;
187
+ } , [ columns , expandIndex ] ) ;
188
+
176
189
const cls = [ prefixCls , className , bordered ? `${ prefixCls } -bordered` : null ] . filter ( Boolean ) . join ( ' ' ) . trim ( ) ;
177
- const { header, render, ellipsis } = getLevelItems ( selfColumns ) ;
190
+ const { header, render, ellipsis } = getLevelItems ( self . selfColumns ) ;
178
191
179
192
return (
180
193
< div >
@@ -184,44 +197,19 @@ export default function Table<T extends { [key: string]: V }, V>(props: TablePro
184
197
{ columns && columns . length > 0 && < Thead onCellHead = { onCellHead } data = { header } /> }
185
198
{ data && data . length > 0 && (
186
199
< tbody >
187
- { data . map ( ( trData , rowNum ) => {
188
- return (
189
- < React . Fragment key = { rowNum } >
190
- < tr key = { rowKey ? trData [ rowKey ] + '' : rowNum } >
191
- { keys . map ( ( keyName , colNum ) => {
192
- let objs : React . TdHTMLAttributes < HTMLTableDataCellElement > = {
193
- children : trData [ keyName ] ,
194
- } ;
195
- if ( render [ keyName ] ) {
196
- const child = render [ keyName ] ( trData [ keyName ] , keyName , trData , rowNum , colNum ) ;
197
- if ( React . isValidElement ( child ) ) {
198
- objs . children = child ;
199
- } else {
200
- if ( child . props ) {
201
- objs = { ...child . props , children : objs . children } ;
202
- if ( child . props . rowSpan === 0 || child . props . colSpan === 0 ) return null ;
203
- }
204
- if ( child . children ) {
205
- objs . children = child . children ;
206
- }
207
- }
208
- }
209
- if ( ellipsis && ellipsis [ keyName ] ) {
210
- objs . className = `${ prefixCls } -ellipsis` ;
211
- }
212
- return (
213
- < td
214
- { ...objs }
215
- key = { colNum }
216
- onClick = { ( evn ) => onCell ( trData , { rowNum, colNum, keyName } , evn ) }
217
- />
218
- ) ;
219
- } ) }
220
- </ tr >
221
- { isExpandedDom ( trData , rowNum ) }
222
- </ React . Fragment >
223
- ) ;
224
- } ) }
200
+ < TableTr
201
+ rowKey = { rowKey }
202
+ data = { data }
203
+ keys = { self . keys }
204
+ render = { render }
205
+ ellipsis = { ellipsis }
206
+ prefixCls = { prefixCls }
207
+ onCell = { onCell }
208
+ hierarchy = { 0 }
209
+ isExpandedDom = { isExpandedDom }
210
+ indentSize = { expandable ?. indentSize || 16 }
211
+ childrenColumnName = { expandable ?. childrenColumnName || 'children' }
212
+ />
225
213
</ tbody >
226
214
) }
227
215
{ data && data . length === 0 && empty && (
0 commit comments