@@ -71,10 +71,12 @@ export const KeyValueTable = <K extends string>({
71
71
/** State to trigger useEffect to trigger autoFocus */
72
72
const [ newRowAdded , setNewRowAdded ] = useState ( false )
73
73
74
+ const isActionDisabled = readOnly || isAdditionNotAllowed
75
+
74
76
/** Boolean determining if table has rows. */
75
77
const hasRows = ( ! readOnly && ! isAdditionNotAllowed ) || ! ! updatedRows . length
76
- // TODO: See null checks
77
78
const isFirstRowEmpty = ! updatedRows [ 0 ] ?. data [ firstHeaderKey ] . value && ! updatedRows [ 0 ] ?. data [ secondHeaderKey ] . value
79
+ const disableDeleteRow = updatedRows . length === 1 && isFirstRowEmpty
78
80
79
81
// HOOKS
80
82
const { sortBy, sortOrder, handleSorting } = useStateFilters ( {
@@ -218,15 +220,15 @@ export const KeyValueTable = <K extends string>({
218
220
}
219
221
220
222
useEffect ( ( ) => {
221
- if ( ! readOnly && ! isAdditionNotAllowed && ! updatedRows . length ) {
223
+ if ( ! isActionDisabled && ! updatedRows . length ) {
222
224
handleAddNewRow ( )
223
225
}
224
226
} , [ ] )
225
227
226
228
useEffect ( ( ) => {
227
229
if ( isSortable ) {
228
230
setUpdatedRows ( ( prevRows ) => {
229
- const sortedRows = [ ... prevRows ]
231
+ const sortedRows = structuredClone ( prevRows )
230
232
sortedRows . sort ( ( a , b ) =>
231
233
stringComparatorBySortOrder ( a . data [ sortBy ] . value , b . data [ sortBy ] . value , sortOrder ) ,
232
234
)
@@ -236,23 +238,17 @@ export const KeyValueTable = <K extends string>({
236
238
} , [ sortOrder ] )
237
239
238
240
useEffect ( ( ) => {
239
- const firstRow = updatedRows ?. [ 0 ]
241
+ const firstRow = updatedRows [ 0 ]
240
242
241
243
if ( firstRow && newRowAdded ) {
242
244
setNewRowAdded ( false )
245
+ const areKeyAndValueTextAreaRefsPresent =
246
+ keyTextAreaRef . current [ firstRow . id ] . current && valueTextAreaRef . current [ firstRow . id ] . current
243
247
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 ) {
249
249
valueTextAreaRef . current [ firstRow . id ] . current . focus ( )
250
250
}
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 ) {
256
252
keyTextAreaRef . current [ firstRow . id ] . current . focus ( )
257
253
}
258
254
}
@@ -320,8 +316,8 @@ export const KeyValueTable = <K extends string>({
320
316
321
317
const renderFirstHeader = ( key : K , label : string , className : string ) => (
322
318
< 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 || '' } ` }
325
321
>
326
322
{ isSortable ? (
327
323
< button
@@ -342,12 +338,16 @@ export const KeyValueTable = <K extends string>({
342
338
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' } ` }
343
339
>
344
340
{ label }
345
- { /* TODO: Test this */ }
346
341
{ ! ! headerComponent && headerComponent }
347
342
</ div >
348
343
) }
349
344
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
+ >
351
351
< ICAdd className = "icon-dim-12 fcb-5 dc__no-shrink" />
352
352
< span className = "cb-5 fs-12 fw-6 lh-20" > Add</ span >
353
353
</ button >
@@ -395,7 +395,6 @@ export const KeyValueTable = <K extends string>({
395
395
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 || '' } ` }
396
396
>
397
397
{ label }
398
- { /* TODO: Test this */ }
399
398
{ ! ! headerComponent && headerComponent }
400
399
</ div >
401
400
) ,
@@ -461,9 +460,9 @@ export const KeyValueTable = <K extends string>({
461
460
{ ! readOnly && (
462
461
< button
463
462
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' : '' } ` }
465
464
onClick = { onRowDelete ( row ) }
466
- disabled = { updatedRows . length === 1 && isFirstRowEmpty }
465
+ disabled = { disableDeleteRow }
467
466
>
468
467
< ICCross
469
468
aria-label = "delete-row"
0 commit comments