@@ -87,7 +87,7 @@ export const KeyValueTable = <K extends string>({
87
87
valueTextAreaRef . current [ firstRow . id ] . current . focus ( )
88
88
}
89
89
}
90
- } , [ updatedRows , newRowAdded ] )
90
+ } , [ newRowAdded ] )
91
91
92
92
// METHODS
93
93
const onSortBtnClick = ( ) => handleSorting ( sortBy )
@@ -122,15 +122,21 @@ export const KeyValueTable = <K extends string>({
122
122
}
123
123
}
124
124
125
+ const onRowDelete = ( row : KeyValueRow < K > ) => ( ) => {
126
+ const remainingRows = updatedRows . filter ( ( { id } ) => id !== row . id )
127
+ setUpdatedRows ( remainingRows )
128
+
129
+ delete keyTextAreaRef . current [ row . id ]
130
+ delete valueTextAreaRef . current [ row . id ]
131
+
132
+ onDelete ?.( row . id )
133
+ }
134
+
125
135
const onRowDataEdit = ( row : KeyValueRow < K > , key : K ) => ( e : React . ChangeEvent < HTMLTextAreaElement > ) => {
126
136
const { value } = e . target
127
137
128
- let editedRows = [ ]
129
138
if ( ! value && ! row . data [ key === firstHeaderKey ? secondHeaderKey : firstHeaderKey ] . value ) {
130
- editedRows = updatedRows . filter ( ( { id } ) => id !== row . id )
131
-
132
- delete keyTextAreaRef . current [ row . id ]
133
- delete valueTextAreaRef . current [ row . id ]
139
+ onRowDelete ( row ) ( )
134
140
135
141
if ( inputRowRef . current ) {
136
142
inputRowRef . current . focus ( )
@@ -146,21 +152,17 @@ export const KeyValueTable = <K extends string>({
146
152
} ,
147
153
} ,
148
154
}
149
- editedRows = updatedRows . map ( ( _row ) => ( _row . id === row . id ? rowData : _row ) )
155
+ const editedRows = updatedRows . map ( ( _row ) => ( _row . id === row . id ? rowData : _row ) )
156
+ setUpdatedRows ( editedRows )
150
157
}
151
-
152
- setUpdatedRows ( editedRows )
153
- onChange ?.( row . id , key , value )
154
158
}
155
159
156
- const onRowDelete = ( row : KeyValueRow < K > ) => ( ) => {
157
- const remainingRows = updatedRows . filter ( ( { id } ) => id !== row . id )
158
- setUpdatedRows ( remainingRows )
159
-
160
- delete keyTextAreaRef . current [ row . id ]
161
- delete valueTextAreaRef . current [ row . id ]
160
+ const onRowDataBlur = ( row : KeyValueRow < K > , key : K ) => ( e : React . FocusEvent < HTMLTextAreaElement > ) => {
161
+ const { value } = e . target
162
162
163
- onDelete ?.( row . id )
163
+ if ( value || row . data [ key === firstHeaderKey ? secondHeaderKey : firstHeaderKey ] . value ) {
164
+ onChange ?.( row . id , key , value )
165
+ }
164
166
}
165
167
166
168
return (
@@ -238,6 +240,7 @@ export const KeyValueTable = <K extends string>({
238
240
value = { row . data [ key ] . value }
239
241
placeholder = { placeholder [ key ] }
240
242
onChange = { onRowDataEdit ( row , key ) }
243
+ onBlur = { onRowDataBlur ( row , key ) }
241
244
refVar = {
242
245
key === firstHeaderKey
243
246
? keyTextAreaRef . current ?. [ row . id ]
@@ -250,7 +253,7 @@ export const KeyValueTable = <K extends string>({
250
253
}
251
254
disableOnBlurResizeToMinHeight
252
255
/>
253
- { row . data [ key ] . showAsterisk && (
256
+ { row . data [ key ] . required && (
254
257
< span className = "cr-5 fs-16 dc__align-self-start px-6" > *</ span >
255
258
) }
256
259
</ >
0 commit comments