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'
3
3
import { DragDropContext , Droppable , Draggable } from 'react-beautiful-dnd'
4
4
import { WidgetProps } from '@ncwidgets/common-typings'
5
5
6
6
import { reorder , diff , extract } from './utils'
7
7
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
+
10
21
public state = {
11
22
data : [ ] ,
12
23
}
@@ -16,9 +27,7 @@ export const createControl = (ListElement: FC<{item: object}>) => {
16
27
17
28
const collection : string = field . get ( 'collection' )
18
29
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' )
22
31
const fieldsToBeExtracted = Array . from ( new Set ( [ fieldId , ...fieldDisplay . toJS ( ) ] ) )
23
32
24
33
const result = await query ( forID , collection , [ fieldId ] , '' )
@@ -66,7 +75,6 @@ export const createControl = (ListElement: FC<{item: object}>) => {
66
75
const { data } = this . state
67
76
68
77
const fieldId : string = field . get ( 'id_field' )
69
- const fieldDisplay : string [ ] = field . get ( 'display_fields' )
70
78
71
79
if ( data . length === 0 ) return < div > loading...</ div >
72
80
return (
@@ -103,8 +111,8 @@ export const createControl = (ListElement: FC<{item: object}>) => {
103
111
...provided . draggableProps . style ,
104
112
} }
105
113
>
106
- < ListElement item = { extract ( item , ... fieldDisplay ) } />
107
- </ div >
114
+ { renderListItem ( item ) }
115
+ </ div >
108
116
) }
109
117
</ Draggable >
110
118
) ) }
@@ -116,8 +124,4 @@ export const createControl = (ListElement: FC<{item: object}>) => {
116
124
)
117
125
}
118
126
}
119
- }
120
-
121
- export const Control = createControl ( ( { item } ) =>
122
- < div > { Object . keys ( item ) . map ( key => item [ key ] ) . join ( ' ' ) } </ div >
123
- )
127
+ }
0 commit comments