@@ -80,7 +80,6 @@ export const KeyValueTable = <K extends string>({
80
80
const { sortBy, sortOrder, handleSorting } = useStateFilters ( {
81
81
initialSortKey : isSortable ? firstHeaderKey : null ,
82
82
} )
83
- const inputRowRef = useRef < HTMLTextAreaElement > ( )
84
83
const keyTextAreaRef = useRef < Record < string , React . RefObject < HTMLTextAreaElement > > > ( )
85
84
const valueTextAreaRef = useRef < Record < string , React . RefObject < HTMLTextAreaElement > > > ( )
86
85
@@ -114,13 +113,15 @@ export const KeyValueTable = <K extends string>({
114
113
shouldTriggerCustomValidation : boolean = true ,
115
114
) => {
116
115
if ( shouldTriggerCustomValidation ) {
117
- if ( validateDuplicateKeys && key === firstHeaderKey && updatedRowsKeysFrequency [ value ] > 1 ) {
116
+ const trimmedValue = value . trim ( )
117
+
118
+ if ( validateDuplicateKeys && key === firstHeaderKey && updatedRowsKeysFrequency [ trimmedValue ] > 1 ) {
118
119
return false
119
120
}
120
121
121
- if ( validateEmptyKeys && key === firstHeaderKey && ! value ) {
122
+ if ( validateEmptyKeys && key === firstHeaderKey && ! trimmedValue ) {
122
123
const isValuePresentAtRow = updatedRows . some (
123
- ( { id, data } ) => id === rowId && data [ secondHeaderKey ] . value ,
124
+ ( { id, data } ) => id === rowId && data [ secondHeaderKey ] . value . trim ( ) ,
124
125
)
125
126
if ( isValuePresentAtRow ) {
126
127
return false
@@ -140,7 +141,8 @@ export const KeyValueTable = <K extends string>({
140
141
const { isAnyKeyDuplicated } = editedRows . reduce (
141
142
( acc , curr ) => {
142
143
const { keysFrequency } = acc
143
- const currentKey = curr . data [ firstHeaderKey ] . value
144
+ const currentKey = curr . data [ firstHeaderKey ] . value . trim ( )
145
+
144
146
if ( currentKey ) {
145
147
keysFrequency [ currentKey ] = ( keysFrequency [ currentKey ] || 0 ) + 1
146
148
}
@@ -160,7 +162,7 @@ export const KeyValueTable = <K extends string>({
160
162
161
163
if ( validateEmptyKeys ) {
162
164
const isEmptyKeyPresent = editedRows . some (
163
- ( row ) => ! row . data [ firstHeaderKey ] . value && row . data [ secondHeaderKey ] . value ,
165
+ ( row ) => ! row . data [ firstHeaderKey ] . value . trim ( ) && row . data [ secondHeaderKey ] . value . trim ( ) ,
164
166
)
165
167
166
168
if ( isEmptyKeyPresent ) {
@@ -292,28 +294,19 @@ export const KeyValueTable = <K extends string>({
292
294
293
295
const onRowDataEdit = ( row : KeyValueRow < K > , key : K ) => ( e : React . ChangeEvent < HTMLTextAreaElement > ) => {
294
296
const { value } = e . target
295
-
296
- if ( ! value && ! row . data [ key === firstHeaderKey ? secondHeaderKey : firstHeaderKey ] . value ) {
297
- onRowDelete ( row ) ( )
298
-
299
- if ( inputRowRef . current ) {
300
- inputRowRef . current . focus ( )
301
- }
302
- } else {
303
- const rowData = {
304
- ...row ,
305
- data : {
306
- ...row . data ,
307
- [ key ] : {
308
- ...row . data [ key ] ,
309
- value,
310
- } ,
297
+ const rowData = {
298
+ ...row ,
299
+ data : {
300
+ ...row . data ,
301
+ [ key ] : {
302
+ ...row . data [ key ] ,
303
+ value,
311
304
} ,
312
- }
313
- const editedRows = updatedRows . map ( ( _row ) => ( _row . id === row . id ? rowData : _row ) )
314
- onError ?.( ! checkAllRowsAreValid ( editedRows ) )
315
- setUpdatedRows ( editedRows )
305
+ } ,
316
306
}
307
+ const editedRows = updatedRows . map ( ( _row ) => ( _row . id === row . id ? rowData : _row ) )
308
+ onError ?.( ! checkAllRowsAreValid ( editedRows ) )
309
+ setUpdatedRows ( editedRows )
317
310
}
318
311
319
312
const onRowDataBlur = ( row : KeyValueRow < K > , key : K ) => ( e : React . FocusEvent < HTMLTextAreaElement > ) => {
@@ -333,7 +326,7 @@ export const KeyValueTable = <K extends string>({
333
326
{ isSortable ? (
334
327
< button
335
328
type = "button"
336
- className = "cn-9 fs-13 lh-20-imp fw-6 flexbox dc__align-items-center dc__gap-2"
329
+ className = "cn-7 fs-12 lh-20-imp fw-6 flexbox dc__align-items-center dc__gap-2"
337
330
onClick = { onSortBtnClick }
338
331
>
339
332
{ label }
@@ -346,7 +339,7 @@ export const KeyValueTable = <K extends string>({
346
339
</ button >
347
340
) : (
348
341
< div
349
- className = { `cn-9 fs-13 lh-20 fw-6 flexbox dc__align-items-center dc__content-space dc__gap-2 ${ hasRows ? 'dc__top-left-radius' : 'dc__left-radius-4' } ` }
342
+ 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' } ` }
350
343
>
351
344
{ label }
352
345
{ /* TODO: Test this */ }
0 commit comments