Skip to content

Commit 1d5c528

Browse files
anaisbergSimonClo
authored andcommitted
✨ add row to table
1 parent 3330cdc commit 1d5c528

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
import { Button } from '@openfun/cunningham-react';
2+
import { Dispatch, SetStateAction } from 'react';
23

3-
import { useColumns, useRows } from './hooks';
44
import { createNewRow } from './utils';
55

6-
export const AddRowButton = () => {
7-
const { setRowData } = useRows();
8-
const { colDefs } = useColumns();
9-
6+
export const AddRowButton = ({
7+
columns,
8+
setRowData,
9+
}: {
10+
columns: string[];
11+
setRowData: Dispatch<
12+
SetStateAction<Record<string, string | number | boolean>[] | undefined>
13+
>;
14+
}) => {
1015
const addRow = () => {
11-
const newRow = createNewRow('', colDefs);
16+
const newRow = createNewRow('', columns);
1217
setRowData((prev) => {
1318
if (prev === undefined) {
1419
return [newRow];
1520
}
16-
return [...prev, newRow];
21+
const updatedRows = [...prev];
22+
// Insert at the second-to-last position
23+
updatedRows.splice(updatedRows.length - 1, 0, newRow);
24+
return updatedRows;
1725
});
1826
};
1927

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ColDef, ColSpanParams } from 'ag-grid-community';
1+
import { ColDef, ColSpanParams, ICellRendererParams } from 'ag-grid-community';
22
import { AgGridReact } from 'ag-grid-react';
33
import { useEffect, useRef } from 'react';
44

@@ -80,15 +80,20 @@ export const DatabaseGrid = ({
8080
const columns: ColDef[] = columnNames.map((key) => ({
8181
field: key,
8282
colSpan: newRowColSpan,
83-
cellRendererSelector: addRowCellRenderer,
83+
cellRendererSelector: (
84+
params: ICellRendererParams<Record<string, string>>,
85+
) => addRowCellRenderer(params, columnNames, setRowData),
8486
}));
8587

8688
setColDefs(columns.concat(addColumnColDef));
8789
// eslint-disable-next-line react-hooks/exhaustive-deps
8890
}, [tableData]);
8991

9092
useEffect(() => {
91-
const addNewRow = createNewRow('+ new row', colDefs ?? []);
93+
const columnNames = (colDefs ?? [])
94+
.map((col) => col.field)
95+
.filter((col) => col !== undefined);
96+
const addNewRow = createNewRow('+ new row', columnNames);
9297
setRowData((prev) => [...(prev ? prev : []), addNewRow]);
9398
}, [colDefs, gridRef, setRowData]);
9499

@@ -97,7 +102,6 @@ export const DatabaseGrid = ({
97102
filter: true,
98103
editable: true,
99104
unSortIcon: true,
100-
spanRows: true,
101105
};
102106

103107
const addColumn = (columnName: string) => {

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { ColDef, ICellRendererParams } from 'ag-grid-community';
1+
import { ICellRendererParams } from 'ag-grid-community';
2+
import { Dispatch, SetStateAction } from 'react';
23

34
import { AddRowButton } from './AddRowButton';
45

56
export const createNewRow = (
67
value: string | undefined,
7-
columns: ColDef[] | undefined,
8+
columnNames: string[] | undefined,
89
) => {
910
const defaultValue = value ?? '';
10-
const columnNames = columns?.map((col) => col.field);
1111
const addNewRow: Record<string, string> = {};
1212
columnNames?.forEach((name) => {
1313
if (name !== undefined) {
@@ -20,10 +20,15 @@ export const createNewRow = (
2020

2121
export const addRowCellRenderer = (
2222
params: ICellRendererParams<Record<string, string>>,
23+
columns: string[] | undefined,
24+
setRowData: Dispatch<
25+
SetStateAction<Record<string, string | number | boolean>[] | undefined>
26+
>,
2327
) => {
2428
if (params.data) {
2529
const addRowButton = {
2630
component: AddRowButton,
31+
params: { columns, setRowData },
2732
};
2833
if (Object.values(params.data)[0]?.includes('new')) {
2934
return addRowButton;

0 commit comments

Comments
 (0)