1
1
import React , { useState , useMemo } from 'react'
2
- import { observer , useFieldSchema , useField , Schema } from '@formily/react'
2
+ import {
3
+ observer ,
4
+ useFieldSchema ,
5
+ useField ,
6
+ Schema ,
7
+ RecursionField ,
8
+ } from '@formily/react'
3
9
import cls from 'classnames'
10
+ import { GeneralField , FieldDisplayTypes } from '@formily/core'
4
11
import { isArr , isBool , isFn } from '@formily/shared'
5
12
import { Search , Table } from '@alifd/next'
6
13
import { TableProps , ColumnProps } from '@alifd/next/types/table'
@@ -13,6 +20,14 @@ import { useCheckSlackly, getIndeterminate } from './useCheckSlackly'
13
20
import { getUISelected , getOutputData } from './utils'
14
21
import { usePrefixCls } from '../__builtins__'
15
22
23
+ interface ObservableColumnSource {
24
+ field : GeneralField
25
+ columnProps : ColumnProps
26
+ schema : Schema
27
+ display : FieldDisplayTypes
28
+ name : string
29
+ }
30
+
16
31
type IFilterOption = boolean | ( ( option : any , keyword : string ) => boolean )
17
32
18
33
type IFilterSort = ( optionA : any , optionB : any ) => number
@@ -50,24 +65,69 @@ const isColumnComponent = (schema: Schema) => {
50
65
return schema [ 'x-component' ] ?. indexOf ( 'Column' ) > - 1
51
66
}
52
67
53
- const useColumns = ( ) => {
68
+ const useSources = ( ) => {
69
+ const arrayField = useField ( )
54
70
const schema = useFieldSchema ( )
55
- const columns : ISelectTableColumnProps [ ] = [ ]
71
+ const parseSources = ( schema : Schema ) : ObservableColumnSource [ ] => {
72
+ if ( isColumnComponent ( schema ) ) {
73
+ if ( ! schema [ 'x-component-props' ] ?. [ 'dataIndex' ] && ! schema [ 'name' ] )
74
+ return [ ]
75
+ const name = schema [ 'x-component-props' ] ?. [ 'dataIndex' ] || schema [ 'name' ]
76
+ const field = arrayField . query ( arrayField . address . concat ( name ) ) . take ( )
77
+ const columnProps =
78
+ field ?. component ?. [ 1 ] || schema [ 'x-component-props' ] || { }
79
+ const display = field ?. display || schema [ 'x-display' ]
80
+ return [
81
+ {
82
+ name,
83
+ display,
84
+ field,
85
+ schema,
86
+ columnProps : {
87
+ title : field ?. title || columnProps . title ,
88
+ ...columnProps ,
89
+ } ,
90
+ } ,
91
+ ]
92
+ } else if ( schema . properties ) {
93
+ return schema . reduceProperties ( ( buf , schema ) => {
94
+ return buf . concat ( parseSources ( schema ) )
95
+ } , [ ] )
96
+ }
97
+ }
98
+
99
+ const parseArrayItems = ( schema : Schema [ 'items' ] ) => {
100
+ if ( ! schema ) return [ ]
101
+ const sources : ObservableColumnSource [ ] = [ ]
102
+ const items = isArr ( schema ) ? schema : [ schema ]
103
+ return items . reduce ( ( columns , schema ) => {
104
+ const item = parseSources ( schema )
105
+ if ( item ) {
106
+ return columns . concat ( item )
107
+ }
108
+ return columns
109
+ } , sources )
110
+ }
111
+
56
112
const validSchema = (
57
113
schema ?. type === 'array' && schema ?. items ? schema . items : schema
58
114
) as Schema
59
115
60
- validSchema ?. mapProperties ( ( schema , name ) => {
61
- if ( isColumnComponent ( schema ) ) {
62
- const props = schema ?. [ 'x-component-props' ]
63
- columns . push ( {
64
- ...props ,
65
- title : props ?. title || schema ?. title ,
66
- dataIndex : props ?. dataIndex || name ,
67
- } )
68
- }
69
- } )
70
- return columns
116
+ return parseArrayItems ( validSchema )
117
+ }
118
+
119
+ const useColumns = (
120
+ sources : ObservableColumnSource [ ]
121
+ ) : TableProps [ 'columns' ] => {
122
+ return sources . reduce ( ( buf , { name, columnProps, schema, display } , key ) => {
123
+ if ( display !== 'visible' ) return buf
124
+ if ( ! isColumnComponent ( schema ) ) return buf
125
+ return buf . concat ( {
126
+ ...columnProps ,
127
+ key,
128
+ dataIndex : name ,
129
+ } )
130
+ } , [ ] )
71
131
}
72
132
73
133
const addPrimaryKey = ( dataSource , rowKey , primaryKey ) =>
@@ -113,7 +173,8 @@ export const SelectTable: ComposedSelectTable = observer((props) => {
113
173
props ?. size
114
174
)
115
175
const primaryKey = isFn ( rowKey ) ? '__formily_key__' : rowKey
116
- const columns = useColumns ( )
176
+ const sources = useSources ( )
177
+ const columns = useColumns ( sources )
117
178
118
179
// dataSource
119
180
let dataSource = isArr ( propsDataSource ) ? propsDataSource : field . dataSource
@@ -313,12 +374,23 @@ export const SelectTable: ComposedSelectTable = observer((props) => {
313
374
>
314
375
{ '' }
315
376
</ Table >
377
+ { sources . map ( ( column , key ) => {
378
+ //专门用来承接对Column的状态管理
379
+ if ( ! isColumnComponent ( column . schema ) ) return
380
+ return React . createElement ( RecursionField , {
381
+ name : column . name ,
382
+ schema : column . schema ,
383
+ onlyRenderSelf : true ,
384
+ key,
385
+ } )
386
+ } ) }
316
387
</ div >
317
388
)
318
389
} )
319
390
320
- const TableColumn : React . FC < React . PropsWithChildren < ISelectTableColumnProps > > =
321
- ( ) => < > </ >
391
+ const TableColumn : React . FC <
392
+ React . PropsWithChildren < ISelectTableColumnProps >
393
+ > = ( ) => < > </ >
322
394
323
395
SelectTable . Column = TableColumn
324
396
0 commit comments