Skip to content

Commit ab4c4ab

Browse files
committed
chore: Refactor KeyValueTable component to improve readability and maintainability
1 parent 7b64752 commit ab4c4ab

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

src/Shared/Components/KeyValueTable/KeyValueTable.component.tsx

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,12 @@ export const KeyValueTable = <K extends string>({
7171
/** State to trigger useEffect to trigger autoFocus */
7272
const [newRowAdded, setNewRowAdded] = useState(false)
7373

74+
const isActionDisabled = readOnly || isAdditionNotAllowed
75+
7476
/** Boolean determining if table has rows. */
7577
const hasRows = (!readOnly && !isAdditionNotAllowed) || !!updatedRows.length
76-
// TODO: See null checks
7778
const isFirstRowEmpty = !updatedRows[0]?.data[firstHeaderKey].value && !updatedRows[0]?.data[secondHeaderKey].value
79+
const disableDeleteRow = updatedRows.length === 1 && isFirstRowEmpty
7880

7981
// HOOKS
8082
const { sortBy, sortOrder, handleSorting } = useStateFilters({
@@ -218,15 +220,15 @@ export const KeyValueTable = <K extends string>({
218220
}
219221

220222
useEffect(() => {
221-
if (!readOnly && !isAdditionNotAllowed && !updatedRows.length) {
223+
if (!isActionDisabled && !updatedRows.length) {
222224
handleAddNewRow()
223225
}
224226
}, [])
225227

226228
useEffect(() => {
227229
if (isSortable) {
228230
setUpdatedRows((prevRows) => {
229-
const sortedRows = [...prevRows]
231+
const sortedRows = structuredClone(prevRows)
230232
sortedRows.sort((a, b) =>
231233
stringComparatorBySortOrder(a.data[sortBy].value, b.data[sortBy].value, sortOrder),
232234
)
@@ -236,23 +238,17 @@ export const KeyValueTable = <K extends string>({
236238
}, [sortOrder])
237239

238240
useEffect(() => {
239-
const firstRow = updatedRows?.[0]
241+
const firstRow = updatedRows[0]
240242

241243
if (firstRow && newRowAdded) {
242244
setNewRowAdded(false)
245+
const areKeyAndValueTextAreaRefsPresent =
246+
keyTextAreaRef.current[firstRow.id].current && valueTextAreaRef.current[firstRow.id].current
243247

244-
if (
245-
!firstRow.data[firstHeaderKey].value &&
246-
keyTextAreaRef.current[firstRow.id].current &&
247-
valueTextAreaRef.current[firstRow.id].current
248-
) {
248+
if (!firstRow.data[firstHeaderKey].value && areKeyAndValueTextAreaRefsPresent) {
249249
valueTextAreaRef.current[firstRow.id].current.focus()
250250
}
251-
if (
252-
!firstRow.data[secondHeaderKey].value &&
253-
keyTextAreaRef.current[firstRow.id].current &&
254-
valueTextAreaRef.current[firstRow.id].current
255-
) {
251+
if (!firstRow.data[secondHeaderKey].value && areKeyAndValueTextAreaRefsPresent) {
256252
keyTextAreaRef.current[firstRow.id].current.focus()
257253
}
258254
}
@@ -320,8 +316,8 @@ export const KeyValueTable = <K extends string>({
320316

321317
const renderFirstHeader = (key: K, label: string, className: string) => (
322318
<div
323-
key={key}
324-
className={`bcn-50 py-8 px-12 flexbox dc__content-space dc__align-items-center ${updatedRows.length || (!readOnly && !isAdditionNotAllowed) ? 'dc__top-left-radius' : 'dc__left-radius-4'} ${className || ''}`}
319+
key={`${key}-header`}
320+
className={`bcn-50 py-8 px-12 flexbox dc__content-space dc__align-items-center ${updatedRows.length || !isActionDisabled ? 'dc__top-left-radius' : 'dc__left-radius-4'} ${className || ''}`}
325321
>
326322
{isSortable ? (
327323
<button
@@ -342,12 +338,16 @@ export const KeyValueTable = <K extends string>({
342338
className={`cn-7 fs-12 lh-20 fw-6 flexbox dc__align-items-center dc__content-space dc__gap-2 ${hasRows ? 'dc__top-left-radius' : 'dc__left-radius-4'}`}
343339
>
344340
{label}
345-
{/* TODO: Test this */}
346341
{!!headerComponent && headerComponent}
347342
</div>
348343
)}
349344

350-
<button type="button" className="dc__transparent p-0 flex dc__gap-4" onClick={handleAddNewRow}>
345+
<button
346+
type="button"
347+
className={`dc__transparent p-0 flex dc__gap-4 ${isActionDisabled ? 'dc__disabled' : ''}`}
348+
disabled={isActionDisabled}
349+
onClick={handleAddNewRow}
350+
>
351351
<ICAdd className="icon-dim-12 fcb-5 dc__no-shrink" />
352352
<span className="cb-5 fs-12 fw-6 lh-20">Add</span>
353353
</button>
@@ -395,7 +395,6 @@ export const KeyValueTable = <K extends string>({
395395
className={`bcn-50 cn-9 fs-13 lh-20 py-8 px-12 fw-6 flexbox dc__align-items-center dc__content-space dc__gap-2 ${key === firstHeaderKey ? `${hasRows ? 'dc__top-left-radius' : 'dc__left-radius-4'}` : `${hasRows ? 'dc__top-right-radius' : 'dc__right-radius-4'}`} ${className || ''}`}
396396
>
397397
{label}
398-
{/* TODO: Test this */}
399398
{!!headerComponent && headerComponent}
400399
</div>
401400
),
@@ -461,9 +460,9 @@ export const KeyValueTable = <K extends string>({
461460
{!readOnly && (
462461
<button
463462
type="button"
464-
className={`key-value-table__row-delete-btn dc__unset-button-styles dc__align-self-stretch dc__no-shrink flex py-10 px-8 bcn-0 dc__hover-n50 dc__tab-focus ${updatedRows.length === 1 && isFirstRowEmpty ? 'dc__disabled' : ''}`}
463+
className={`key-value-table__row-delete-btn dc__unset-button-styles dc__align-self-stretch dc__no-shrink flex py-10 px-8 bcn-0 dc__hover-n50 dc__tab-focus ${disableDeleteRow ? 'dc__disabled' : ''}`}
465464
onClick={onRowDelete(row)}
466-
disabled={updatedRows.length === 1 && isFirstRowEmpty}
465+
disabled={disableDeleteRow}
467466
>
468467
<ICCross
469468
aria-label="delete-row"

0 commit comments

Comments
 (0)