@@ -12,7 +12,7 @@ import type { themeOverrides } from "../../theme"
1212import type { RowsChangeData } from "react-data-grid"
1313
1414type Props < T extends string > = {
15- initialData : Data < T > [ ]
15+ initialData : ( Data < T > & Meta ) [ ]
1616 file : File
1717 onBack ?: ( ) => void
1818}
@@ -23,22 +23,21 @@ export const ValidationStep = <T extends string>({ initialData, file, onBack }:
2323 "ValidationStep" ,
2424 ) as ( typeof themeOverrides ) [ "components" ] [ "ValidationStep" ] [ "baseStyle" ]
2525
26- const [ data , setData ] = useState < ( Data < T > & Meta ) [ ] > (
27- useMemo (
28- ( ) => addErrorsAndRunHooks < T > ( initialData , fields , rowHook , tableHook ) ,
29- // eslint-disable-next-line react-hooks/exhaustive-deps
30- [ ] ,
31- ) ,
32- )
26+ const [ data , setData ] = useState < ( Data < T > & Meta ) [ ] > ( initialData )
27+
3328 const [ selectedRows , setSelectedRows ] = useState < ReadonlySet < number | string > > ( new Set ( ) )
3429 const [ filterByErrors , setFilterByErrors ] = useState ( false )
3530 const [ showSubmitAlert , setShowSubmitAlert ] = useState ( false )
3631
3732 const updateData = useCallback (
38- ( rows : typeof data ) => {
39- setData ( addErrorsAndRunHooks < T > ( rows , fields , rowHook , tableHook ) )
33+ async ( rows : typeof data , indexes ?: number [ ] ) => {
34+ // Check if hooks are async - if they are we want to apply changes optimistically for better UX
35+ if ( rowHook ?. constructor . name === "AsyncFunction" || tableHook ?. constructor . name === "AsyncFunction" ) {
36+ setData ( rows )
37+ }
38+ addErrorsAndRunHooks < T > ( rows , fields , rowHook , tableHook , indexes ) . then ( ( data ) => setData ( data ) )
4039 } ,
41- [ setData , rowHook , tableHook , fields ] ,
40+ [ rowHook , tableHook , fields ] ,
4241 )
4342
4443 const deleteSelectedRows = ( ) => {
@@ -49,16 +48,17 @@ export const ValidationStep = <T extends string>({ initialData, file, onBack }:
4948 }
5049 }
5150
52- const updateRow = useCallback (
51+ const updateRows = useCallback (
5352 ( rows : typeof data , changedData ?: RowsChangeData < ( typeof data ) [ number ] > ) => {
5453 const changes = changedData ?. indexes . reduce ( ( acc , index ) => {
5554 // when data is filtered val !== actual index in data
5655 const realIndex = data . findIndex ( ( value ) => value . __index === rows [ index ] . __index )
5756 acc [ realIndex ] = rows [ index ]
5857 return acc
5958 } , { } as Record < number , ( typeof data ) [ number ] > )
59+ const realIndexes = changes && Object . keys ( changes ) . map ( ( index ) => Number ( index ) )
6060 const newData = Object . assign ( [ ] , data , changes )
61- updateData ( newData )
61+ updateData ( newData , realIndexes )
6262 } ,
6363 [ data , updateData ] ,
6464 )
@@ -137,7 +137,7 @@ export const ValidationStep = <T extends string>({ initialData, file, onBack }:
137137 < Table
138138 rowKeyGetter = { rowKeyGetter }
139139 rows = { tableData }
140- onRowsChange = { updateRow }
140+ onRowsChange = { updateRows }
141141 columns = { columns }
142142 selectedRows = { selectedRows }
143143 onSelectedRowsChange = { setSelectedRows }
0 commit comments