{
return randomArrayItem(roles);
};
-const initialRows: GridRowsProp = [
+const rows: GridRowsProp = [
{
id: randomId(),
name: randomTraderName(),
@@ -80,15 +81,12 @@ const columns: GridColDef[] = [
},
];
-interface EditActionProps extends Pick
{
- onSave: (id: string, rowUpdates: GridRowModel) => void;
-}
-
-function EditAction(props: EditActionProps) {
- const { row, onSave } = props;
+function EditAction(props: Pick) {
+ const { row } = props;
const [editing, setEditing] = React.useState(false);
const [name, setName] = React.useState(row.name);
const [position, setPosition] = React.useState(row.position);
+ const apiRef = useGridApiContext();
const handleEdit = (event: React.MouseEvent) => {
event.stopPropagation();
@@ -101,7 +99,7 @@ function EditAction(props: EditActionProps) {
const handleSave = (event: React.FormEvent) => {
event.preventDefault();
- onSave(row.id, { name, position });
+ apiRef.current.updateRows([{ id: row.id, name, position }]);
handleClose();
};
@@ -167,11 +165,21 @@ function EditAction(props: EditActionProps) {
);
}
-interface ListViewCellProps extends GridRenderCellParams {
- onSave: (id: string, rowUpdates: GridRowModel) => void;
+function DeleteAction(props: Pick) {
+ const { row } = props;
+ const apiRef = useGridApiContext();
+
+ return (
+ apiRef.current.updateRows([{ id: row.id, _action: 'delete' }])}
+ >
+
+
+ );
}
-function ListViewCell(props: ListViewCellProps) {
+function ListViewCell(props: GridRenderCellParams) {
const { row } = props;
return (
@@ -192,28 +200,20 @@ function ListViewCell(props: ListViewCellProps) {
{row.position}
-
+
+
+
+
);
}
-export default function ListViewEdit() {
- const [rows, setRows] = React.useState(initialRows);
-
- const updateRow = React.useCallback((id: string, rowUpdates: GridRowModel) => {
- setRows((prevRows) =>
- prevRows.map((row) => (row.id === id ? { ...row, ...rowUpdates } : row)),
- );
- }, []);
-
- const listColDef: GridListColDef = React.useMemo(
- () => ({
- field: 'listColumn',
- renderCell: (params) => ,
- }),
- [updateRow],
- );
+const listColDef: GridListColDef = {
+ field: 'listColumn',
+ renderCell: (params) => ,
+};
+export default function ListViewEdit() {
return (