11import 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'
39import cls from 'classnames'
10+ import { GeneralField , FieldDisplayTypes } from '@formily/core'
411import { isArr , isBool , isFn } from '@formily/shared'
512import { Search , Table } from '@alifd/next'
613import { TableProps , ColumnProps } from '@alifd/next/types/table'
@@ -13,6 +20,14 @@ import { useCheckSlackly, getIndeterminate } from './useCheckSlackly'
1320import { getUISelected , getOutputData } from './utils'
1421import { usePrefixCls } from '../__builtins__'
1522
23+ interface ObservableColumnSource {
24+ field : GeneralField
25+ columnProps : ColumnProps
26+ schema : Schema
27+ display : FieldDisplayTypes
28+ name : string
29+ }
30+
1631type IFilterOption = boolean | ( ( option : any , keyword : string ) => boolean )
1732
1833type IFilterSort = ( optionA : any , optionB : any ) => number
@@ -50,24 +65,69 @@ const isColumnComponent = (schema: Schema) => {
5065 return schema [ 'x-component' ] ?. indexOf ( 'Column' ) > - 1
5166}
5267
53- const useColumns = ( ) => {
68+ const useSources = ( ) => {
69+ const arrayField = useField ( )
5470 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+
56112 const validSchema = (
57113 schema ?. type === 'array' && schema ?. items ? schema . items : schema
58114 ) as Schema
59115
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+ } , [ ] )
71131}
72132
73133const addPrimaryKey = ( dataSource , rowKey , primaryKey ) =>
@@ -113,7 +173,8 @@ export const SelectTable: ComposedSelectTable = observer((props) => {
113173 props ?. size
114174 )
115175 const primaryKey = isFn ( rowKey ) ? '__formily_key__' : rowKey
116- const columns = useColumns ( )
176+ const sources = useSources ( )
177+ const columns = useColumns ( sources )
117178
118179 // dataSource
119180 let dataSource = isArr ( propsDataSource ) ? propsDataSource : field . dataSource
@@ -313,12 +374,23 @@ export const SelectTable: ComposedSelectTable = observer((props) => {
313374 >
314375 { '' }
315376 </ 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+ } ) }
316387 </ div >
317388 )
318389} )
319390
320- const TableColumn : React . FC < React . PropsWithChildren < ISelectTableColumnProps > > =
321- ( ) => < > </ >
391+ const TableColumn : React . FC <
392+ React . PropsWithChildren < ISelectTableColumnProps >
393+ > = ( ) => < > </ >
322394
323395SelectTable . Column = TableColumn
324396
0 commit comments