Skip to content

Commit c3f0910

Browse files
anaisbergSimonClo
authored andcommitted
✨ new row button
1 parent a0ec6ca commit c3f0910

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Button } from '@openfun/cunningham-react';
2+
import { CustomCellRendererProps } from 'ag-grid-react';
3+
4+
export const AddRowButton = (props: CustomCellRendererProps) => {
5+
console.log('AddRowButton props', props);
6+
return (
7+
<Button
8+
color="tertiary-text"
9+
icon={<span className="material-icons">add</span>}
10+
>
11+
new row
12+
</Button>
13+
);
14+
};

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { ColDef, ColSpanParams, themeQuartz } from 'ag-grid-community';
1+
import { ColDef, ColSpanParams, ICellRendererParams } from 'ag-grid-community';
22
import { AgGridReact } from 'ag-grid-react';
3-
import { useEffect, useMemo, useRef, useState } from 'react';
3+
import { useEffect, useRef, useState } from 'react';
44

55
import { Box } from '@/components';
66
import {
@@ -10,6 +10,7 @@ import {
1010
} from '@/features/grist';
1111

1212
import { AddButtonComponent } from './AddColumnButton';
13+
import { AddRowButton } from './AddRowButton';
1314

1415
export const DatabaseGrid = ({
1516
documentId,
@@ -84,6 +85,23 @@ export const DatabaseGrid = ({
8485
const columns: ColDef[] = columnNames.map((key) => ({
8586
field: key,
8687
colSpan: newRowColSpan,
88+
cellRendererSelector: (
89+
params: ICellRendererParams<Record<string, string>>,
90+
) => {
91+
if (params.data) {
92+
const addRowButton = {
93+
component: AddRowButton,
94+
params: { values: params.data },
95+
};
96+
97+
if (Object.values(params.data)[0].includes('new')) {
98+
return addRowButton;
99+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
100+
}
101+
return undefined;
102+
}
103+
return undefined;
104+
},
87105
}));
88106

89107
setColDefs(columns.concat(addColumnColDef));
@@ -98,24 +116,16 @@ export const DatabaseGrid = ({
98116
spanRows: true,
99117
};
100118

101-
const theme = useMemo(() => {
102-
return themeQuartz.withParams({
103-
pinnedRowBorder: {
104-
width: 2,
105-
},
106-
});
107-
}, []);
108-
109119
const addColumn = (columnName: string) => {
110120
const newColDef: ColDef = {
111121
field: columnName,
112122
};
113123

114124
setColDefs((prev) => {
115-
const addColumn = prev.pop();
125+
const addColumn = prev?.pop();
116126

117127
return [
118-
...prev,
128+
...(prev ?? []),
119129
newColDef,
120130
...(addColumn !== undefined ? [addColumn] : []),
121131
];
@@ -141,7 +151,6 @@ export const DatabaseGrid = ({
141151
defaultColDef={defaultColDef}
142152
domLayout="autoHeight"
143153
enableCellSpan={true}
144-
theme={theme}
145154
/>
146155
</Box>
147156
);

0 commit comments

Comments
 (0)