Skip to content

Commit c2b9c2f

Browse files
anaisbergSimonClo
authored andcommitted
♻️ extract get columns names
1 parent b86b876 commit c2b9c2f

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ import {
1212
import { AddButtonComponent } from './AddColumnButton';
1313
import { useColumns, useRows } from './hooks';
1414
import { DatabaseRow } from './types';
15-
import { addRowCellRenderer, createNewRow, newRowColSpan } from './utils';
15+
import {
16+
addRowCellRenderer,
17+
createNewRow,
18+
getColumnNames,
19+
newRowColSpan,
20+
} from './utils';
1621

1722
export const DatabaseGrid = ({
1823
documentId,
@@ -66,9 +71,7 @@ export const DatabaseGrid = ({
6671
}, [tableData]);
6772

6873
useEffect(() => {
69-
const columnNames = (colDefs ?? [])
70-
.map((col) => col.field)
71-
.filter((col) => col !== undefined);
74+
const columnNames = getColumnNames(colDefs);
7275
const addNewRow = createNewRow({ value: '+ new row', columnNames });
7376
setRowData((prev) => [...(prev ? prev : []), addNewRow]);
7477
}, [colDefs, setRowData]);
@@ -82,8 +85,13 @@ export const DatabaseGrid = ({
8285
};
8386

8487
const addColumn = (columnName: string) => {
88+
const columnNames = getColumnNames(colDefs);
8589
const newColDef: ColDef = {
8690
field: columnName,
91+
colSpan: newRowColSpan,
92+
cellRendererSelector: (
93+
params: ICellRendererParams<Record<string, string>>,
94+
) => addRowCellRenderer(params, columnNames, setRowData),
8795
};
8896

8997
setColDefs((prev) => {

src/frontend/apps/impress/src/features/docs/doc-editor/components/custom-blocks/DatabaseBlock/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
ColDef,
23
ColSpanParams,
34
ICellRendererParams,
45
SizeColumnsToContentStrategy,
@@ -58,3 +59,6 @@ export const newRowColSpan = (
5859

5960
return 1;
6061
};
62+
63+
export const getColumnNames = (colDefs: ColDef[] | undefined) =>
64+
(colDefs ?? []).map((col) => col.field).filter((col) => col !== undefined);

0 commit comments

Comments
 (0)