1- import React , { FunctionComponent as FC } from 'react'
2- import { fromJS } from 'immutable'
1+ import * as React from 'react'
2+ import { fromJS , List } from 'immutable'
33import { DragDropContext , Droppable , Draggable } from 'react-beautiful-dnd'
44import { WidgetProps } from '@ncwidgets/common-typings'
55
66import { reorder , diff , extract } from './utils'
77
8- export const createControl = ( ListElement : FC < { item : object } > ) => {
9- return class extends React . Component < WidgetProps > {
8+ const defaultListItem = item => Object . values ( item ) . join ( ' ' )
9+
10+ export interface CreateControlOptions {
11+ renderListItem ?: ( item : object ) => React . ComponentType < { item : Record < string , any > } > ;
12+ }
13+
14+ type CreateControl = ( options ?: CreateControlOptions ) => React . ComponentClass < WidgetProps >
15+
16+ export const createControl : CreateControl = ( options = { } ) => {
17+ const renderListItem = options . renderListItem || defaultListItem
18+
19+ return class Control extends React . Component < WidgetProps > {
20+
1021 public state = {
1122 data : [ ] ,
1223 }
@@ -16,9 +27,7 @@ export const createControl = (ListElement: FC<{item: object}>) => {
1627
1728 const collection : string = field . get ( 'collection' )
1829 const fieldId : string = field . get ( 'id_field' )
19- const fieldDisplay : string [ ] = field . get ( 'display_fields' )
20-
21- // @ts -ignore
30+ const fieldDisplay : List < string > = field . get ( 'display_fields' )
2231 const fieldsToBeExtracted = Array . from ( new Set ( [ fieldId , ...fieldDisplay . toJS ( ) ] ) )
2332
2433 const result = await query ( forID , collection , [ fieldId ] , '' )
@@ -66,7 +75,6 @@ export const createControl = (ListElement: FC<{item: object}>) => {
6675 const { data } = this . state
6776
6877 const fieldId : string = field . get ( 'id_field' )
69- const fieldDisplay : string [ ] = field . get ( 'display_fields' )
7078
7179 if ( data . length === 0 ) return < div > loading...</ div >
7280 return (
@@ -103,8 +111,8 @@ export const createControl = (ListElement: FC<{item: object}>) => {
103111 ...provided . draggableProps . style ,
104112 } }
105113 >
106- < ListElement item = { extract ( item , ... fieldDisplay ) } />
107- </ div >
114+ { renderListItem ( item ) }
115+ </ div >
108116 ) }
109117 </ Draggable >
110118 ) ) }
@@ -116,8 +124,4 @@ export const createControl = (ListElement: FC<{item: object}>) => {
116124 )
117125 }
118126 }
119- }
120-
121- export const Control = createControl ( ( { item } ) =>
122- < div > { Object . keys ( item ) . map ( key => item [ key ] ) . join ( ' ' ) } </ div >
123- )
127+ }
0 commit comments