Skip to content

Commit f1160a0

Browse files
committed
fix: Trim key and value before validating in KeyValueTable.component.tsx
1 parent 88970f6 commit f1160a0

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed

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

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ export const KeyValueTable = <K extends string>({
8080
const { sortBy, sortOrder, handleSorting } = useStateFilters({
8181
initialSortKey: isSortable ? firstHeaderKey : null,
8282
})
83-
const inputRowRef = useRef<HTMLTextAreaElement>()
8483
const keyTextAreaRef = useRef<Record<string, React.RefObject<HTMLTextAreaElement>>>()
8584
const valueTextAreaRef = useRef<Record<string, React.RefObject<HTMLTextAreaElement>>>()
8685

@@ -114,13 +113,15 @@ export const KeyValueTable = <K extends string>({
114113
shouldTriggerCustomValidation: boolean = true,
115114
) => {
116115
if (shouldTriggerCustomValidation) {
117-
if (validateDuplicateKeys && key === firstHeaderKey && updatedRowsKeysFrequency[value] > 1) {
116+
const trimmedValue = value.trim()
117+
118+
if (validateDuplicateKeys && key === firstHeaderKey && updatedRowsKeysFrequency[trimmedValue] > 1) {
118119
return false
119120
}
120121

121-
if (validateEmptyKeys && key === firstHeaderKey && !value) {
122+
if (validateEmptyKeys && key === firstHeaderKey && !trimmedValue) {
122123
const isValuePresentAtRow = updatedRows.some(
123-
({ id, data }) => id === rowId && data[secondHeaderKey].value,
124+
({ id, data }) => id === rowId && data[secondHeaderKey].value.trim(),
124125
)
125126
if (isValuePresentAtRow) {
126127
return false
@@ -140,7 +141,8 @@ export const KeyValueTable = <K extends string>({
140141
const { isAnyKeyDuplicated } = editedRows.reduce(
141142
(acc, curr) => {
142143
const { keysFrequency } = acc
143-
const currentKey = curr.data[firstHeaderKey].value
144+
const currentKey = curr.data[firstHeaderKey].value.trim()
145+
144146
if (currentKey) {
145147
keysFrequency[currentKey] = (keysFrequency[currentKey] || 0) + 1
146148
}
@@ -160,7 +162,7 @@ export const KeyValueTable = <K extends string>({
160162

161163
if (validateEmptyKeys) {
162164
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(),
164166
)
165167

166168
if (isEmptyKeyPresent) {
@@ -292,28 +294,19 @@ export const KeyValueTable = <K extends string>({
292294

293295
const onRowDataEdit = (row: KeyValueRow<K>, key: K) => (e: React.ChangeEvent<HTMLTextAreaElement>) => {
294296
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,
311304
},
312-
}
313-
const editedRows = updatedRows.map((_row) => (_row.id === row.id ? rowData : _row))
314-
onError?.(!checkAllRowsAreValid(editedRows))
315-
setUpdatedRows(editedRows)
305+
},
316306
}
307+
const editedRows = updatedRows.map((_row) => (_row.id === row.id ? rowData : _row))
308+
onError?.(!checkAllRowsAreValid(editedRows))
309+
setUpdatedRows(editedRows)
317310
}
318311

319312
const onRowDataBlur = (row: KeyValueRow<K>, key: K) => (e: React.FocusEvent<HTMLTextAreaElement>) => {
@@ -333,7 +326,7 @@ export const KeyValueTable = <K extends string>({
333326
{isSortable ? (
334327
<button
335328
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"
337330
onClick={onSortBtnClick}
338331
>
339332
{label}
@@ -346,7 +339,7 @@ export const KeyValueTable = <K extends string>({
346339
</button>
347340
) : (
348341
<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'}`}
350343
>
351344
{label}
352345
{/* TODO: Test this */}

0 commit comments

Comments
 (0)