@@ -38,13 +38,15 @@ const EditableCell = ({
38
38
} ) => {
39
39
// We need to keep and update the state of the cell normally
40
40
const [ value , setValue ] = React . useState ( initialValue ) ;
41
+ const [ selected , setSelected ] = React . useState ( false ) ;
41
42
42
43
const onChange = ( e ) => {
43
44
setValue ( e . target . value ) ;
44
45
} ;
45
46
46
47
// We'll only update the external data when the input is blurred
47
48
const onBlur = ( ) => {
49
+ setSelected ( false ) ;
48
50
updateMyData ( index , id , value ) ;
49
51
} ;
50
52
@@ -53,7 +55,19 @@ const EditableCell = ({
53
55
setValue ( initialValue ) ;
54
56
} , [ initialValue ] ) ;
55
57
56
- return < input value = { value } onChange = { onChange } onBlur = { onBlur } /> ;
58
+ return selected ? (
59
+ // eslint-disable-next-line jsx-a11y/no-autofocus
60
+ < input autoFocus value = { value } onChange = { onChange } onBlur = { onBlur } />
61
+ ) : (
62
+ < span
63
+ role = "button"
64
+ tabIndex = { 0 }
65
+ onClick = { ( ) => setSelected ( true ) }
66
+ onKeyDown = { ( ) => setSelected ( true ) }
67
+ >
68
+ { value }
69
+ </ span >
70
+ ) ;
57
71
} ;
58
72
59
73
const defaultColumn = {
@@ -156,6 +170,7 @@ function Table({ columns, data, updateMyData, skipPageReset }) {
156
170
style = { { width : '100px' } }
157
171
/>
158
172
</ span > { ' ' }
173
+ { /* eslint-disable-next-line jsx-a11y/no-onchange */ }
159
174
< select
160
175
value = { pageSize }
161
176
onChange = { ( e ) => {
@@ -173,14 +188,30 @@ function Table({ columns, data, updateMyData, skipPageReset }) {
173
188
) ;
174
189
}
175
190
176
- function ReactDataTableWidget ( props ) {
191
+ const ReactDataTableWidget = ( props ) => {
177
192
// Set our editable cell renderer as the default Cell renderer
178
- let { columns , items , csvexport, csvimport } = props ;
193
+ let { schema , value , onChange , id , csvexport, csvimport } = props ;
179
194
180
195
const intl = useIntl ( ) ;
181
- const tablecolumns = React . useMemo ( ( ) => columns , [ columns ] ) ;
196
+ const header_columns = schema . fieldsets [ 0 ] . fields . map ( ( field ) => {
197
+ return { Header : schema . properties [ field ] . title , accessor : field } ;
198
+ } ) ;
199
+
200
+ // Set our editable cell renderer as the default Cell renderer
201
+ const tablecolumns = React . useMemo (
202
+ ( ) => [
203
+ {
204
+ Header : 'React table header' ,
205
+ columns : header_columns ,
206
+ } ,
207
+ ] ,
208
+ // eslint-disable-next-line react-hooks/exhaustive-deps
209
+ [ ] ,
210
+ ) ;
182
211
183
- const [ data , setData ] = React . useState ( ( ) => items ) ;
212
+ // const tablecolumns = React.useMemo(() => columns, [columns]);
213
+
214
+ const [ data , setData ] = React . useState ( ( ) => value ) ;
184
215
const [ originalData ] = React . useState ( data ) ;
185
216
const [ skipPageReset , setSkipPageReset ] = React . useState ( false ) ;
186
217
@@ -190,31 +221,24 @@ function ReactDataTableWidget(props) {
190
221
// When our cell renderer calls updateMyData, we'll use
191
222
// the rowIndex, columnId and new value to update the
192
223
// original data
193
- const updateMyData = ( rowIndex , columnId , value ) => {
224
+ const updateMyData = ( rowIndex , columnId , updateValue ) => {
194
225
// We also turn on the flag to not reset the page
195
226
setSkipPageReset ( true ) ;
196
227
setData ( ( old ) =>
197
228
old . map ( ( row , index ) => {
198
229
if ( index === rowIndex ) {
199
230
return {
200
231
...old [ rowIndex ] ,
201
- [ columnId ] : value ,
232
+ [ columnId ] : updateValue ,
202
233
} ;
203
234
}
204
235
return row ;
205
236
} ) ,
206
237
) ;
207
-
208
- // return items back to the component
209
- items = items . map ( ( row , index ) => {
210
- if ( index === rowIndex ) {
211
- return {
212
- ...items [ rowIndex ] ,
213
- [ columnId ] : value ,
214
- } ;
215
- }
216
- return row ;
217
- } ) ;
238
+ const newvalue = value . map ( ( v , i ) =>
239
+ i !== rowIndex ? v : { ...v , [ columnId ] : updateValue } ,
240
+ ) ;
241
+ onChange ( id , newvalue ) ;
218
242
} ;
219
243
220
244
// After data chagnes, we turn the flag back off
@@ -278,21 +302,21 @@ function ReactDataTableWidget(props) {
278
302
let modifiedcount = newdata . length - newdatacount ;
279
303
280
304
setData ( newdata ) ;
281
- props . value . items = newdata ;
305
+ onChange ( id , newdata ) ;
282
306
toast . success (
283
307
< Toast
284
308
success
285
309
autoClose = { 5000 }
286
- content = {
287
- ( intl . formatMessage ( messages . csv_file_imported_correctly ) +
288
- ' ' ,
289
- + intl . formatMessage ( messages . import_new_imported_item_count , {
310
+ content = { `${ intl . formatMessage (
311
+ messages . csv_file_imported_correctly ,
312
+ ) } ${ intl . formatMessage (
313
+ messages . import_new_imported_item_count ,
314
+ {
290
315
count : newdatacount ,
291
- } ) + ' ' ,
292
- + intl . formatMessage ( messages . import_modified_item_count , {
293
- count : modifiedcount ,
294
- } ) )
295
- }
316
+ } ,
317
+ ) } ${ intl . formatMessage ( messages . import_modified_item_count , {
318
+ count : modifiedcount ,
319
+ } ) } `}
296
320
/> ,
297
321
) ;
298
322
} }
@@ -307,7 +331,7 @@ function ReactDataTableWidget(props) {
307
331
< >
308
332
< div >
309
333
< button type = "button" { ...getRootProps ( ) } >
310
- intl.formatMessage(messages.import_csv_file)
334
+ { intl . formatMessage ( messages . import_csv_file ) }
311
335
</ button >
312
336
< div > { acceptedFile && acceptedFile . name } </ div >
313
337
< button { ...getRemoveFileProps ( ) } > Remove</ button >
@@ -326,6 +350,6 @@ function ReactDataTableWidget(props) {
326
350
/>
327
351
</ >
328
352
) ;
329
- }
353
+ } ;
330
354
331
355
export default ReactDataTableWidget ;
0 commit comments