16
16
17
17
import { useEffect , useMemo , useState } from 'react'
18
18
19
+ import { useEffectAfterMount } from '@Common/Helper'
19
20
import { useStateFilters } from '@Common/Hooks'
20
21
21
22
import { DynamicDataTable } from '../DynamicDataTable'
22
- import { KeyValueTableDataType , KeyValueTableInternalProps , KeyValueTableProps } from './KeyValueTable.types'
23
+ import {
24
+ KeyValueTableDataType ,
25
+ KeyValueTableInternalProps ,
26
+ KeyValueTableProps ,
27
+ KeyValueTableRowType ,
28
+ } from './KeyValueTable.types'
23
29
import {
24
30
getEmptyRow ,
25
31
getKeyValueHeaders ,
@@ -49,6 +55,7 @@ export const KeyValueTable = ({
49
55
} : KeyValueTableProps ) => {
50
56
// STATES
51
57
const [ cellError , setCellError ] = useState < KeyValueTableInternalProps [ 'cellError' ] > ( { } )
58
+ const [ sortedRows , setSortedRows ] = useState < KeyValueTableInternalProps [ 'rows' ] > ( [ ] )
52
59
53
60
// HOOKS
54
61
const { sortBy, sortOrder, handleSorting } = useStateFilters < KeyValueTableDataType > ( {
@@ -61,8 +68,21 @@ export const KeyValueTable = ({
61
68
[ initialRows , placeholder , maskValue , isSortable , sortOrder , sortBy ] ,
62
69
)
63
70
64
- // Set cell error on mount
71
+ /** Function to update the sorted rows based on the current sorting configuration */
72
+ const updateSortedRows = ( ) => {
73
+ if ( isSortable ) {
74
+ setSortedRows (
75
+ getKeyValueTableSortedRows ( {
76
+ rows,
77
+ sortBy,
78
+ sortOrder,
79
+ } ) ,
80
+ )
81
+ }
82
+ }
83
+
65
84
useEffect ( ( ) => {
85
+ // Set cell error on mount
66
86
const { isValid, updatedCellError } = getKeyValueTableCellError ( {
67
87
rows,
68
88
validateDuplicateKeys,
@@ -72,8 +92,46 @@ export const KeyValueTable = ({
72
92
73
93
setCellError ( updatedCellError )
74
94
onError ?.( ! isValid )
95
+
96
+ // Set sorted rows on mount
97
+ updateSortedRows ( )
75
98
} , [ ] )
76
99
100
+ // Sort rows for display purposes only. \
101
+ // The `sortedRows` state is used internally to render the data, while the original `rows` prop remains unaltered during sorting.
102
+ useEffectAfterMount ( ( ) => {
103
+ if ( isSortable ) {
104
+ // Create a map of rows using their IDs for quick lookup
105
+ const rowMap = rows . reduce < Record < KeyValueTableRowType [ 'id' ] , KeyValueTableInternalProps [ 'rows' ] [ number ] > > (
106
+ ( acc , row ) => {
107
+ acc [ row . id ] = row
108
+ return acc
109
+ } ,
110
+ { } ,
111
+ )
112
+
113
+ // Create a set of IDs from the currently sorted rows for efficient lookup
114
+ const sortedRowSet = new Set ( sortedRows . map ( ( { id } ) => id ) )
115
+
116
+ // Update the sorted rows by filtering out rows that no longer exist and mapping them to the latest data
117
+ const updatedSortedRows = sortedRows . filter ( ( { id } ) => rowMap [ id ] ) . map ( ( { id } ) => rowMap [ id ] )
118
+
119
+ // Identify rows that are not part of the current sorted set (new or unsorted rows)
120
+ const unsortedRows = rows . filter ( ( { id } ) => ! sortedRowSet . has ( id ) )
121
+
122
+ // Combine unsorted rows with updated sorted rows and set them as the new sorted rows
123
+ setSortedRows ( [ ...unsortedRows , ...updatedSortedRows ] )
124
+ } else {
125
+ // If sorting is disabled, directly set the rows as the sorted rows
126
+ setSortedRows ( rows )
127
+ }
128
+ } , [ rows ] )
129
+
130
+ // Update the sorted rows whenever the sorting configuration changes
131
+ useEffectAfterMount ( ( ) => {
132
+ updateSortedRows ( )
133
+ } , [ sortBy , sortOrder ] )
134
+
77
135
// METHODS
78
136
const setUpdatedRows = ( updatedRows : typeof rows ) => {
79
137
const { isValid, updatedCellError } = getKeyValueTableCellError ( {
@@ -124,7 +182,7 @@ export const KeyValueTable = ({
124
182
return (
125
183
< DynamicDataTable
126
184
headers = { getKeyValueHeaders ( { headerLabel, isSortable } ) }
127
- rows = { getKeyValueTableSortedRows ( { isSortable , rows , sortBy , sortOrder } ) }
185
+ rows = { sortedRows }
128
186
cellError = { showError ? cellError : { } }
129
187
onRowAdd = { onRowAdd }
130
188
onRowDelete = { onRowDelete }
0 commit comments