Skip to content

Commit 823d8f0

Browse files
anaisbergSimonClo
authored andcommitted
♻️ create new row util
1 parent 4b57d46 commit 823d8f0

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/frontend/apps/impress/src/features/docs/doc-editor/components/custom-blocks/DatabaseBlock/DatabaseGrid.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111

1212
import { AddButtonComponent } from './AddColumnButton';
1313
import { AddRowButton } from './AddRowButton';
14+
import { createNewRow } from './utils';
1415

1516
export const DatabaseGrid = ({
1617
documentId,
@@ -73,14 +74,11 @@ export const DatabaseGrid = ({
7374
rowData1.push(row);
7475
}
7576

76-
const addNewRow: Record<string, string> = {};
77-
for (const [key] of filteredEntries) {
78-
addNewRow[key] = '+ new row';
79-
}
80-
rowData1.push(addNewRow);
81-
8277
setRowData(rowData1);
8378

79+
const addNewRow = createNewRow('+ new row', colDefs ?? []);
80+
rowData1.push(addNewRow);
81+
8482
const columnNames = Object.keys(Object.fromEntries(filteredEntries));
8583
const columns: ColDef[] = columnNames.map((key) => ({
8684
field: key,
@@ -94,9 +92,8 @@ export const DatabaseGrid = ({
9492
params: { values: params.data },
9593
};
9694

97-
if (Object.values(params.data)[0].includes('new')) {
95+
if (Object.values(params.data)[0]?.includes('new')) {
9896
return addRowButton;
99-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
10097
}
10198
return undefined;
10299
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { ColDef } from 'ag-grid-community';
2+
3+
export const createNewRow = (
4+
value: string | undefined,
5+
columns: ColDef[] | undefined,
6+
) => {
7+
const defaultValue = value ?? '';
8+
const columnNames = columns?.map((col) => col.field);
9+
const addNewRow: Record<string, string> = {};
10+
columnNames?.forEach((name) => {
11+
if (name !== undefined) {
12+
addNewRow[name] = defaultValue;
13+
}
14+
});
15+
16+
return addNewRow;
17+
};

0 commit comments

Comments
 (0)