1- import { useCallback , useMemo , useState } from "react"
1+ import { useCallback , useMemo , useState , useEffect } from "react"
22import { Box , Button , Heading , ModalBody , Switch , useStyleConfig } from "@chakra-ui/react"
33import { ContinueButton } from "../../components/ContinueButton"
44import { useRsi } from "../../hooks/useRsi"
@@ -22,20 +22,18 @@ 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 as ( Data < T > & Meta ) [ ] )
26+ useEffect ( ( ) => {
27+ addErrorsAndRunHooks < T > ( initialData , fields , rowHook , tableHook ) . then ( ( data ) => setData ( data ) )
28+ } , [ initialData , fields , rowHook , tableHook ] )
29+
3230 const [ selectedRows , setSelectedRows ] = useState < ReadonlySet < number | string > > ( new Set ( ) )
3331 const [ filterByErrors , setFilterByErrors ] = useState ( false )
3432 const [ showSubmitAlert , setShowSubmitAlert ] = useState ( false )
3533
3634 const updateData = useCallback (
37- ( rows : typeof data ) => {
38- setData ( addErrorsAndRunHooks < T > ( rows , fields , rowHook , tableHook ) )
35+ async ( rows : typeof data , indexes ?: number [ ] ) => {
36+ setData ( await addErrorsAndRunHooks < T > ( rows , fields , rowHook , tableHook , indexes ) )
3937 } ,
4038 [ setData , rowHook , tableHook , fields ] ,
4139 )
@@ -48,16 +46,17 @@ export const ValidationStep = <T extends string>({ initialData, file }: Props<T>
4846 }
4947 }
5048
51- const updateRow = useCallback (
49+ const updateRows = useCallback (
5250 ( rows : typeof data , changedData ?: RowsChangeData < ( typeof data ) [ number ] > ) => {
5351 const changes = changedData ?. indexes . reduce ( ( acc , index ) => {
5452 // when data is filtered val !== actual index in data
5553 const realIndex = data . findIndex ( ( value ) => value . __index === rows [ index ] . __index )
5654 acc [ realIndex ] = rows [ index ]
5755 return acc
5856 } , { } as Record < number , ( typeof data ) [ number ] > )
57+ const realIndexes = changes == null ? undefined : Object . keys ( changes ) . map ( ( index ) => Number ( index ) )
5958 const newData = Object . assign ( [ ] , data , changes )
60- updateData ( newData )
59+ updateData ( newData , realIndexes )
6160 } ,
6261 [ data , updateData ] ,
6362 )
@@ -136,7 +135,7 @@ export const ValidationStep = <T extends string>({ initialData, file }: Props<T>
136135 < Table
137136 rowKeyGetter = { rowKeyGetter }
138137 rows = { tableData }
139- onRowsChange = { updateRow }
138+ onRowsChange = { updateRows }
140139 columns = { columns }
141140 selectedRows = { selectedRows }
142141 onSelectedRowsChange = { setSelectedRows }
@@ -155,3 +154,5 @@ export const ValidationStep = <T extends string>({ initialData, file }: Props<T>
155154 </ >
156155 )
157156}
157+
158+
0 commit comments