11import React , { useCallback , useMemo , useState } from "react"
2- import { Box , Button , Heading , ModalBody , Switch , Text , useStyleConfig } from "@chakra-ui/react"
2+ import { Box , Button , Heading , ModalBody , Switch , useStyleConfig } from "@chakra-ui/react"
33import { ContinueButton } from "../../components/ContinueButton"
44import { useRsi } from "../../hooks/useRsi"
55import type { Meta } from "./types"
6- import { addErrorsAndRunHooks , addIndexes } from "./utils/dataMutations"
6+ import { addErrorsAndRunHooks } from "./utils/dataMutations"
77import { generateColumns } from "./components/columns"
88import { Table } from "../../components/Table"
99import { SubmitDataAlert } from "../../components/Alerts/SubmitDataAlert"
@@ -20,7 +20,7 @@ export const ValidationStep = <T extends string>({ initialData }: Props<T>) => {
2020 const styles = useStyleConfig ( "ValidationStep" ) as typeof themeOverrides [ "components" ] [ "ValidationStep" ] [ "baseStyle" ]
2121
2222 const [ data , setData ] = useState < ( Data < T > & Meta ) [ ] > (
23- useMemo ( ( ) => addErrorsAndRunHooks < T > ( addIndexes < T > ( initialHook ( initialData ) ) , fields , rowHook , tableHook ) , [ ] ) ,
23+ useMemo ( ( ) => addErrorsAndRunHooks < T > ( initialHook ( initialData ) , fields , rowHook , tableHook ) , [ ] ) ,
2424 )
2525 const [ selectedRows , setSelectedRows ] = useState < ReadonlySet < number | string > > ( new Set ( ) )
2626 const [ filterByErrors , setFilterByErrors ] = useState ( false )
@@ -30,7 +30,7 @@ export const ValidationStep = <T extends string>({ initialData }: Props<T>) => {
3030 ( rows : typeof data ) => {
3131 setData ( addErrorsAndRunHooks < T > ( rows , fields , rowHook , tableHook ) )
3232 } ,
33- [ setData , addErrorsAndRunHooks , rowHook , tableHook ] ,
33+ [ setData , addErrorsAndRunHooks , rowHook , tableHook , fields ] ,
3434 )
3535
3636 const deleteSelectedRows = ( ) => {
@@ -41,24 +41,25 @@ export const ValidationStep = <T extends string>({ initialData }: Props<T>) => {
4141 }
4242 }
4343
44- const updateRowTable = useCallback (
44+ const updateRow = useCallback (
4545 ( rows : typeof data , changedData ?: RowsChangeData < typeof data [ number ] > ) => {
46- const changes = changedData ?. indexes . reduce ( ( acc , val ) => {
47- const realIndex = rows [ val ] . __index
48- acc [ realIndex ] = rows [ val ]
46+ const changes = changedData ?. indexes . reduce ( ( acc , index ) => {
47+ // when data is filtered val !== actual index in data
48+ const realIndex = data . findIndex ( ( value ) => value . __index === rows [ index ] . __index )
49+ acc [ realIndex ] = rows [ index ]
4950 return acc
5051 } , { } as Record < number , typeof data [ number ] > )
5152 const newData = Object . assign ( [ ] , data , changes )
5253 updateData ( newData )
5354 } ,
54- [ data , setData , addErrorsAndRunHooks , rowHook , tableHook ] ,
55+ [ data , updateData ] ,
5556 )
5657
5758 const columns = useMemo ( ( ) => generateColumns ( fields ) , [ fields , generateColumns ] )
5859
5960 const tableData = useMemo ( ( ) => {
6061 if ( filterByErrors ) {
61- return data . filter ( ( value , index ) => {
62+ return data . filter ( ( value ) => {
6263 if ( value ?. __errors ) {
6364 return Object . values ( value . __errors ) ?. filter ( ( err ) => err . level === "error" ) . length
6465 }
@@ -127,7 +128,7 @@ export const ValidationStep = <T extends string>({ initialData }: Props<T>) => {
127128 < Table
128129 rowKeyGetter = { rowKeyGetter }
129130 rows = { tableData }
130- onRowsChange = { updateRowTable }
131+ onRowsChange = { updateRow }
131132 columns = { columns }
132133 selectedRows = { selectedRows }
133134 onSelectedRowsChange = { setSelectedRows }
0 commit comments