Skip to content

Commit c25953d

Browse files
authored
Merge pull request #487 from devtron-labs/fix/dynamic-data-table-ref
fix: DynamicDataTable - cellRef check update, rowIds memoised
2 parents ea983fc + 9250489 commit c25953d

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "1.3.11",
3+
"version": "1.3.11-beta-1",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Shared/Components/DynamicDataTable/DynamicDataTableRow.tsx

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
import { createElement, createRef, Fragment, ReactElement, RefObject, useEffect, useRef, useState } from 'react'
1+
import {
2+
createElement,
3+
createRef,
4+
Fragment,
5+
ReactElement,
6+
RefObject,
7+
useEffect,
8+
useMemo,
9+
useRef,
10+
useState,
11+
} from 'react'
212
// eslint-disable-next-line import/no-extraneous-dependencies
313
import { followCursor } from 'tippy.js'
414

@@ -74,10 +84,10 @@ export const DynamicDataTableRow = <K extends string, CustomStateType = Record<s
7484
{},
7585
)
7686
}
87+
const rowIds = useMemo(() => rows.map(({ id }) => id), [rows])
7788

7889
useEffect(() => {
7990
setIsRowAdded(rows.length > 0 && Object.keys(cellRef.current).length < rows.length)
80-
const rowIds = rows.map(({ id }) => id)
8191

8292
const updatedCellRef = rowIds.reduce((acc, curr) => {
8393
if (cellRef.current[curr]) {
@@ -89,14 +99,16 @@ export const DynamicDataTableRow = <K extends string, CustomStateType = Record<s
8999
}, {})
90100

91101
cellRef.current = updatedCellRef
92-
}, [rows.length])
102+
}, [JSON.stringify(rowIds)])
93103

94104
useEffect(() => {
95-
// Using the below logic to ensure the cell is focused after row addition.
96-
const cell = cellRef.current[rows[0].id][focusableFieldKey || headers[0].key].current
97-
if (isRowAdded && cell) {
98-
cell.focus()
99-
setIsRowAdded(false)
105+
if (isRowAdded) {
106+
// Using the below logic to ensure the cell is focused after row addition.
107+
const cell = cellRef.current[rows[0].id][focusableFieldKey || headers[0].key].current
108+
if (cell) {
109+
cell.focus()
110+
setIsRowAdded(false)
111+
}
100112
}
101113
}, [isRowAdded])
102114

0 commit comments

Comments
 (0)