@@ -28,8 +28,8 @@ import { stringComparatorBySortOrder } from '@Shared/Helpers'
28
28
import { DEFAULT_SECRET_PLACEHOLDER } from '@Shared/constants'
29
29
30
30
import { KeyValueRow , KeyValueTableProps } from './KeyValueTable.types'
31
+ import { DUPLICATE_KEYS_VALIDATION_MESSAGE , EMPTY_KEY_VALIDATION_MESSAGE } from './constants'
31
32
import './KeyValueTable.scss'
32
- import { DUPLICATE_KEYS_VALIDATION_MESSAGE } from './constants'
33
33
34
34
const renderWithReadOnlyTippy = ( children : ReactElement ) => (
35
35
< Tippy
@@ -59,6 +59,7 @@ export const KeyValueTable = <K extends string>({
59
59
errorMessages : parentErrorMessages = [ ] ,
60
60
onError,
61
61
validateDuplicateKeys = false ,
62
+ validateEmptyKeys = false ,
62
63
} : KeyValueTableProps < K > ) => {
63
64
// CONSTANTS
64
65
const { headers, rows } = config
@@ -106,13 +107,20 @@ export const KeyValueTable = <K extends string>({
106
107
[ updatedRows ] ,
107
108
)
108
109
109
- const validationSchema : typeof parentValidationSchema = ( value , key ) => {
110
+ const validationSchema : typeof parentValidationSchema = ( value , key , rowId ) => {
110
111
if ( validateDuplicateKeys && key === firstHeaderKey && updatedRowsKeysFrequency [ value ] > 1 ) {
111
112
return false
112
113
}
113
114
115
+ if ( validateEmptyKeys && key === firstHeaderKey && ! value ) {
116
+ const isValuePresentAtRow = updatedRows . some ( ( { id } ) => id === rowId && value )
117
+ if ( isValuePresentAtRow ) {
118
+ return true
119
+ }
120
+ }
121
+
114
122
if ( parentValidationSchema ) {
115
- return parentValidationSchema ( value , key )
123
+ return parentValidationSchema ( value , key , rowId )
116
124
}
117
125
118
126
return true
@@ -141,10 +149,20 @@ export const KeyValueTable = <K extends string>({
141
149
}
142
150
}
143
151
152
+ if ( validateEmptyKeys ) {
153
+ const isEmptyKeyPresent = editedRows . some (
154
+ ( row ) => ! row . data [ firstHeaderKey ] . value && row . data [ secondHeaderKey ] . value ,
155
+ )
156
+
157
+ if ( isEmptyKeyPresent ) {
158
+ return false
159
+ }
160
+ }
161
+
144
162
const isValid = editedRows . every (
145
- ( { data : _data } ) =>
146
- validationSchema ( _data [ firstHeaderKey ] . value , firstHeaderKey ) &&
147
- validationSchema ( _data [ secondHeaderKey ] . value , secondHeaderKey ) ,
163
+ ( { data : _data , id } ) =>
164
+ validationSchema ( _data [ firstHeaderKey ] . value , firstHeaderKey , id ) &&
165
+ validationSchema ( _data [ secondHeaderKey ] . value , secondHeaderKey , id ) ,
148
166
)
149
167
150
168
return isValid
@@ -341,15 +359,17 @@ export const KeyValueTable = <K extends string>({
341
359
const renderErrorMessages = (
342
360
value : Parameters < typeof validationSchema > [ 0 ] ,
343
361
key : Parameters < typeof validationSchema > [ 1 ] ,
362
+ rowId : KeyValueRow < K > [ 'id' ] ,
344
363
) => {
345
- const showErrorMessages = showError && ! validationSchema ( value , key )
364
+ const showErrorMessages = showError && ! validationSchema ( value , key , rowId )
346
365
if ( ! showErrorMessages ) {
347
366
return null
348
367
}
349
368
350
369
return (
351
370
< div className = "key-value-table__error bcn-0 dc__border br-4 py-7 px-8 flexbox-col dc__gap-4" >
352
371
{ validateDuplicateKeys && renderErrorMessage ( DUPLICATE_KEYS_VALIDATION_MESSAGE ) }
372
+ { validateEmptyKeys && renderErrorMessage ( EMPTY_KEY_VALIDATION_MESSAGE ) }
353
373
{ parentErrorMessages . map ( ( error ) => renderErrorMessage ( error ) ) }
354
374
</ div >
355
375
)
@@ -391,7 +411,7 @@ export const KeyValueTable = <K extends string>({
391
411
< Fragment key = { key } >
392
412
< ConditionalWrap wrap = { renderWithReadOnlyTippy } condition = { readOnly } >
393
413
< div
394
- className = { `key-value-table__cell bcn-0 flexbox dc__align-items-center dc__gap-4 dc__position-rel ${ readOnly || row . data [ key ] . disabled ? 'cursor-not-allowed no-hover' : '' } ${ showError && ! validationSchema ( row . data [ key ] . value , key ) ? 'key-value-table__cell--error no-hover' : '' } ` }
414
+ className = { `key-value-table__cell bcn-0 flexbox dc__align-items-center dc__gap-4 dc__position-rel ${ readOnly || row . data [ key ] . disabled ? 'cursor-not-allowed no-hover' : '' } ${ showError && ! validationSchema ( row . data [ key ] . value , key , row . id ) ? 'key-value-table__cell--error no-hover' : '' } ` }
395
415
>
396
416
{ maskValue ?. [ key ] && row . data [ key ] . value ? (
397
417
< div className = "py-8 px-12 h-36 flex" >
@@ -426,7 +446,7 @@ export const KeyValueTable = <K extends string>({
426
446
*
427
447
</ span >
428
448
) }
429
- { renderErrorMessages ( row . data [ key ] . value , key ) }
449
+ { renderErrorMessages ( row . data [ key ] . value , key , row . id ) }
430
450
</ >
431
451
) }
432
452
</ div >
0 commit comments