Skip to content

Commit da58d52

Browse files
RohitRaj011AbhishekA1509
authored andcommitted
refactor: KeyValueTable - onChange, onEdit - logic refactor, types rename
1 parent f031076 commit da58d52

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export const KeyValueTable = <K extends string>({
8787
valueTextAreaRef.current[firstRow.id].current.focus()
8888
}
8989
}
90-
}, [updatedRows, newRowAdded])
90+
}, [newRowAdded])
9191

9292
// METHODS
9393
const onSortBtnClick = () => handleSorting(sortBy)
@@ -122,15 +122,21 @@ export const KeyValueTable = <K extends string>({
122122
}
123123
}
124124

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+
125135
const onRowDataEdit = (row: KeyValueRow<K>, key: K) => (e: React.ChangeEvent<HTMLTextAreaElement>) => {
126136
const { value } = e.target
127137

128-
let editedRows = []
129138
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)()
134140

135141
if (inputRowRef.current) {
136142
inputRowRef.current.focus()
@@ -146,21 +152,17 @@ export const KeyValueTable = <K extends string>({
146152
},
147153
},
148154
}
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)
150157
}
151-
152-
setUpdatedRows(editedRows)
153-
onChange?.(row.id, key, value)
154158
}
155159

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
162162

163-
onDelete?.(row.id)
163+
if (value || row.data[key === firstHeaderKey ? secondHeaderKey : firstHeaderKey].value) {
164+
onChange?.(row.id, key, value)
165+
}
164166
}
165167

166168
return (
@@ -238,6 +240,7 @@ export const KeyValueTable = <K extends string>({
238240
value={row.data[key].value}
239241
placeholder={placeholder[key]}
240242
onChange={onRowDataEdit(row, key)}
243+
onBlur={onRowDataBlur(row, key)}
241244
refVar={
242245
key === firstHeaderKey
243246
? keyTextAreaRef.current?.[row.id]
@@ -250,7 +253,7 @@ export const KeyValueTable = <K extends string>({
250253
}
251254
disableOnBlurResizeToMinHeight
252255
/>
253-
{row.data[key].showAsterisk && (
256+
{row.data[key].required && (
254257
<span className="cr-5 fs-16 dc__align-self-start px-6">*</span>
255258
)}
256259
</>

src/Shared/Components/KeyValueTable/KeyValueTable.types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export type KeyValueRow<K extends string> = {
3939
data: {
4040
[key in K]: Pick<ResizableTagTextAreaProps, 'value' | 'dataTestId' | 'disabled' | 'tabIndex'> & {
4141
/** An optional boolean indicating if an asterisk should be shown. */
42-
showAsterisk?: boolean
42+
required?: boolean
4343
}
4444
}
4545
id: string | number
@@ -65,7 +65,7 @@ export type KeyValueMask<K extends string> = {
6565
}
6666

6767
export type KeyValuePlaceholder<K extends string> = {
68-
[key in K]: string
68+
[key in K]?: string
6969
}
7070

7171
/**

0 commit comments

Comments
 (0)