@@ -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}
1818
@@ -22,22 +22,21 @@ export const ValidationStep = <T extends string>({ initialData, file }: Props<T>
2222 "ValidationStep" ,
2323 ) as ( typeof themeOverrides ) [ "components" ] [ "ValidationStep" ] [ "baseStyle" ]
2424
25- const [ data , setData ] = useState < ( Data < T > & Meta ) [ ] > (
26- useMemo (
27- ( ) => addErrorsAndRunHooks < T > ( initialData , fields , rowHook , tableHook ) ,
28- // eslint-disable-next-line react-hooks/exhaustive-deps
29- [ ] ,
30- ) ,
31- )
25+ const [ data , setData ] = useState < ( Data < T > & Meta ) [ ] > ( initialData )
26+
3227 const [ selectedRows , setSelectedRows ] = useState < ReadonlySet < number | string > > ( new Set ( ) )
3328 const [ filterByErrors , setFilterByErrors ] = useState ( false )
3429 const [ showSubmitAlert , setShowSubmitAlert ] = useState ( false )
3530
3631 const updateData = useCallback (
37- ( rows : typeof data ) => {
38- setData ( addErrorsAndRunHooks < T > ( rows , fields , rowHook , tableHook ) )
32+ async ( rows : typeof data , indexes ?: number [ ] ) => {
33+ // Check if hooks are async - if they are we want to apply changes optimistically for better UX
34+ if ( rowHook ?. constructor . name === "AsyncFunction" || tableHook ?. constructor . name === "AsyncFunction" ) {
35+ setData ( rows )
36+ }
37+ addErrorsAndRunHooks < T > ( rows , fields , rowHook , tableHook , indexes ) . then ( ( data ) => setData ( data ) )
3938 } ,
40- [ setData , rowHook , tableHook , fields ] ,
39+ [ rowHook , tableHook , fields ] ,
4140 )
4241
4342 const deleteSelectedRows = ( ) => {
@@ -48,16 +47,17 @@ export const ValidationStep = <T extends string>({ initialData, file }: Props<T>
4847 }
4948 }
5049
51- const updateRow = useCallback (
50+ const updateRows = useCallback (
5251 ( rows : typeof data , changedData ?: RowsChangeData < ( typeof data ) [ number ] > ) => {
5352 const changes = changedData ?. indexes . reduce ( ( acc , index ) => {
5453 // when data is filtered val !== actual index in data
5554 const realIndex = data . findIndex ( ( value ) => value . __index === rows [ index ] . __index )
5655 acc [ realIndex ] = rows [ index ]
5756 return acc
5857 } , { } as Record < number , ( typeof data ) [ number ] > )
58+ const realIndexes = changes && Object . keys ( changes ) . map ( ( index ) => Number ( index ) )
5959 const newData = Object . assign ( [ ] , data , changes )
60- updateData ( newData )
60+ updateData ( newData , realIndexes )
6161 } ,
6262 [ data , updateData ] ,
6363 )
@@ -136,7 +136,7 @@ export const ValidationStep = <T extends string>({ initialData, file }: Props<T>
136136 < Table
137137 rowKeyGetter = { rowKeyGetter }
138138 rows = { tableData }
139- onRowsChange = { updateRow }
139+ onRowsChange = { updateRows }
140140 columns = { columns }
141141 selectedRows = { selectedRows }
142142 onSelectedRowsChange = { setSelectedRows }
0 commit comments