@@ -5,63 +5,66 @@ import { MaterialReactTable, MRT_Cell } from 'material-react-table';
55import * as _ from 'lodash'
66
77import { useAppSelector , useAppDispatch } from 'state' ;
8- import { selectData , setCellData , selectEditorState , setRowSelection , Person } from 'state' ;
8+ import { selectData , setCellData , selectEditorState , setRowSelection , Row } from 'state' ;
99
10- import example_jsonSchema from './example-jsonschema.json ' ;
10+ import { selectJsonSchema } from 'project-state ' ;
1111import { schema2columns } from './schema2columns' ;
1212
1313export const Table = ( ) => {
1414 const tableInstanceRef = useRef ( null ) ;
15- //console.log(tableInstanceRef)
1615
1716 const tableData = useAppSelector ( selectData ) ;
1817 const tableState = useAppSelector ( selectEditorState ) ;
1918 const dispatch = useAppDispatch ( ) ;
2019
20+ const jsonSchema = useAppSelector ( selectJsonSchema ) ;
2121 const columns = useMemo (
22- ( ) => schema2columns ( example_jsonSchema ) ,
23- [ ] ,
22+ ( ) => jsonSchema && schema2columns ( jsonSchema ) ,
23+ [ jsonSchema ] ,
2424 ) ;
2525
26- const handleSaveCell = ( cell : MRT_Cell < Person > , value : any ) => {
26+ const handleSaveCell = ( cell : MRT_Cell < Row > , value : any ) => {
2727 dispatch ( setCellData ( [ cell . row . index , cell . column . id , value ] ) )
2828 } ;
2929
30- return < MaterialReactTable
31- tableInstanceRef = { tableInstanceRef }
30+ console . log ( { tableData, tableState} )
31+ return ! columns ? < p > Loading Columns from Schema…</ p > : (
32+ < MaterialReactTable
33+ tableInstanceRef = { tableInstanceRef }
3234
33- //@ts -ignore
34- columns = { columns }
35- data = { tableData }
36- state = { tableState }
35+ //@ts -ignore
36+ columns = { columns }
37+ data = { tableData }
38+ state = { tableState }
3739
38- enableColumnResizing
39- enablePinning
40+ enableColumnResizing
41+ enablePinning
4042
41- enableEditing
42- editingMode = "cell"
43- muiTableBodyCellEditTextFieldProps = { ( { cell } ) => ( {
44- onBlur : ( event ) => {
45- //@ts -ignore
46- handleSaveCell ( cell , event . target . value ) ;
47- } ,
48- } ) }
43+ enableEditing
44+ editingMode = "cell"
45+ muiTableBodyCellEditTextFieldProps = { ( { cell } ) => ( {
46+ onBlur : ( event ) => {
47+ //@ts -ignore
48+ handleSaveCell ( cell , event . target . value ) ;
49+ } ,
50+ } ) }
4951
50- muiTableBodyCellProps = { ( { table, column, cell } ) => ( {
51- onMouseDown : ( _event ) => {
52- dispatch ( setRowSelection ( [ cell . row . index ] ) ) ;
53- } ,
54- onClick : ( _event ) => {
55- /** like handleDoubleClick of MRT_TableBodyCell **/
56- table . setEditingCell ( cell ) ;
57- queueMicrotask ( ( ) => {
58- const textField = table . refs . editInputRefs . current [ column . id ] ;
59- if ( textField ) {
60- textField . focus ( ) ;
61- textField . select ?.( ) ;
62- }
63- } ) ;
64- }
65- } ) }
66- /> ;
52+ muiTableBodyCellProps = { ( { table, column, cell } ) => ( {
53+ onMouseDown : ( _event ) => {
54+ dispatch ( setRowSelection ( [ cell . row . index ] ) ) ;
55+ } ,
56+ onClick : ( _event ) => {
57+ /** like handleDoubleClick of MRT_TableBodyCell **/
58+ table . setEditingCell ( cell ) ;
59+ queueMicrotask ( ( ) => {
60+ const textField = table . refs . editInputRefs . current [ column . id ] ;
61+ if ( textField ) {
62+ textField . focus ( ) ;
63+ textField . select ?.( ) ;
64+ }
65+ } ) ;
66+ }
67+ } ) }
68+ />
69+ ) ;
6770} ;
0 commit comments