Skip to content

Commit 623112d

Browse files
committed
fix: DynamicDataTable - handled case where cell was getting autofocused on mount
1 parent c026c39 commit 623112d

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

src/Shared/Components/DynamicDataTable/DynamicDataTable.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { useMemo } from 'react'
17+
import { useMemo, useState } from 'react'
1818

1919
import { DynamicDataTableHeader } from './DynamicDataTableHeader'
2020
import { DynamicDataTableRow } from './DynamicDataTableRow'
@@ -24,14 +24,30 @@ import './styles.scss'
2424

2525
export const DynamicDataTable = <K extends string, CustomStateType = Record<string, unknown>>({
2626
headers,
27+
onRowAdd,
2728
...props
2829
}: DynamicDataTableProps<K, CustomStateType>) => {
30+
// STATES
31+
const [isAddRowButtonClicked, setIsAddRowButtonClicked] = useState(false)
32+
33+
// CONSTANTS
2934
const filteredHeaders = useMemo(() => headers.filter(({ isHidden }) => !isHidden), [headers])
3035

36+
// HANDLERS
37+
const handleRowAdd = () => {
38+
setIsAddRowButtonClicked(true)
39+
onRowAdd()
40+
}
41+
3142
return (
3243
<div className="w-100">
33-
<DynamicDataTableHeader headers={filteredHeaders} {...props} />
34-
<DynamicDataTableRow headers={filteredHeaders} {...props} />
44+
<DynamicDataTableHeader headers={filteredHeaders} onRowAdd={handleRowAdd} {...props} />
45+
<DynamicDataTableRow
46+
headers={filteredHeaders}
47+
isAddRowButtonClicked={isAddRowButtonClicked}
48+
setIsAddRowButtonClicked={setIsAddRowButtonClicked}
49+
{...props}
50+
/>
3551
</div>
3652
)
3753
}

src/Shared/Components/DynamicDataTable/DynamicDataTableRow.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ export const DynamicDataTableRow = <K extends string, CustomStateType = Record<s
7373
trailingCellIcon,
7474
buttonCellWrapComponent,
7575
focusableFieldKey,
76+
isAddRowButtonClicked,
77+
setIsAddRowButtonClicked,
7678
}: DynamicDataTableRowProps<K, CustomStateType>) => {
7779
// CONSTANTS
7880
const isFirstRowEmpty = headers.every(({ key }) => !rows[0]?.data[key].value)
@@ -105,6 +107,8 @@ export const DynamicDataTableRow = <K extends string, CustomStateType = Record<s
105107
useEffect(() => {
106108
setIsRowAdded(rows.length > 0 && Object.keys(cellRef.current).length < rows.length)
107109

110+
// When a new row is added, we create references for its cells.
111+
// This logic ensures that references are created only for the newly added row, while retaining the existing references.
108112
const updatedCellRef = rowIds.reduce((acc, curr) => {
109113
if (cellRef.current[curr]) {
110114
acc[curr] = cellRef.current[curr]
@@ -118,15 +122,16 @@ export const DynamicDataTableRow = <K extends string, CustomStateType = Record<s
118122
}, [JSON.stringify(rowIds)])
119123

120124
useEffect(() => {
121-
if (isRowAdded) {
125+
if (isAddRowButtonClicked && isRowAdded) {
122126
// Using the below logic to ensure the cell is focused after row addition.
123127
const cell = cellRef.current[rows[0].id][focusableFieldKey || headers[0].key].current
124128
if (cell) {
125129
cell.focus()
126130
setIsRowAdded(false)
131+
setIsAddRowButtonClicked(false)
127132
}
128133
}
129-
}, [isRowAdded])
134+
}, [isRowAdded, isAddRowButtonClicked])
130135

131136
// METHODS
132137
const onChange =

src/Shared/Components/DynamicDataTable/types.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { DetailedHTMLProps, ReactElement, ReactNode } from 'react'
17+
import { DetailedHTMLProps, Dispatch, ReactElement, ReactNode, SetStateAction } from 'react'
1818

1919
import { ResizableTagTextAreaProps } from '@Common/CustomTagSelector'
2020
import { UseStateFiltersReturnType } from '@Common/Hooks'
@@ -261,4 +261,7 @@ export interface DynamicDataTableRowProps<K extends string, CustomStateType = Re
261261
| 'trailingCellIcon'
262262
| 'buttonCellWrapComponent'
263263
| 'focusableFieldKey'
264-
> {}
264+
> {
265+
isAddRowButtonClicked: boolean
266+
setIsAddRowButtonClicked: Dispatch<SetStateAction<boolean>>
267+
}

0 commit comments

Comments
 (0)