Skip to content

Commit

Permalink
[DataGrid] Row editing
Browse files Browse the repository at this point in the history
  • Loading branch information
m4theushw committed Aug 9, 2021
1 parent c78db27 commit 25ed1c9
Show file tree
Hide file tree
Showing 36 changed files with 1,706 additions and 53 deletions.
6 changes: 5 additions & 1 deletion docs/pages/api-docs/data-grid/data-grid.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { DataGrid } from '@material-ui/data-grid';
| <span class="prop-name">disableExtendRowFullWidth</span> | <span class="prop-type">boolean</span> | false | If `true`, rows will not be extended to fill the full width of the grid container. |
| <span class="prop-name">disableSelectionOnClick</span> | <span class="prop-type">boolean</span> | false | If `true`, the selection on click on a row or cell is disabled. |
| <span class="prop-name">error</span> | <span class="prop-type">any</span> | | An error that will turn the grid into its error state and display the error component. |
| <span class="prop-name">editMode</span> | <span class="prop-type">`cell` \| `row`</span> | 'cell' | Controls whether to use the cell or row editing. |
| <span class="prop-name">editRowsModel</span> | <span class="prop-type">GridEditRowsModel</span> | undefined | Set the edit rows model of the grid. |
| <span class="prop-name">filterMode</span> | <span class="prop-type">GridFeatureMode</span> | 'client' | Filtering can be processed on the server or client-side. Set it to `server` if you would like to handle filtering on the server-side. |
| <span class="prop-name">filterModel</span> | <span class="prop-type">GridFilterModel</span> | | Set the filter model of the grid. |
Expand Down Expand Up @@ -74,9 +75,12 @@ import { DataGrid } from '@material-ui/data-grid';
| <span class="prop-name">onColumnVisibilityChange</span> | <span class="prop-type">(params: GridColumnVisibilityChangeParams, event: MuiEvent<{}>) => void</span> | | Callback fired when a column visibility changes. |
| <span class="prop-name">onError</span> | <span class="prop-type">(args: any) => void</span> | | Callback fired when an exception is thrown in the grid, or when the `showError` API method is called. |
| <span class="prop-name">onEditCellPropsChange</span> | <span class="prop-type">(params: GridEditCellPropsParams, event: MouseEvent<React.SyntheticEvent>) => void</span> | | Callback fired when the edit cell value changes. |
| <span class="prop-name">onCellEditCommit</span> | <span class="prop-type">(params: GridEditCellPropsParams, event: MouseEvent<React.SyntheticEvent>) => void</span> | | Callback fired when the cell changes are going to be committed. |
| <span class="prop-name">onCellEditCommit</span> | <span class="prop-type">(params: GridEditCellPropsParams, event: MouseEvent<React.SyntheticEvent>) => void</span> | | Callback fired when the cell changes are committed. |
| <span class="prop-name">onCellEditStart</span> | <span class="prop-type">(params: GridCellParams, event: React.SyntheticEvent) => void</span> | | Callback fired when the cell turns to edit mode. |
| <span class="prop-name">onCellEditStop</span> | <span class="prop-type">(params: GridCellParams, event: React.SyntheticEvent) => void</span> | | Callback fired when the cell turns to view mode. |
| <span class="prop-name">onRowEditCommit</span> | <span class="prop-type">(id: GridRowId, event: MouseEvent<React.SyntheticEvent>) => void</span> | | Callback fired when the row changes are committed. |
| <span class="prop-name">onRowEditStart</span> | <span class="prop-type">(params: GridRowParams, event: React.SyntheticEvent) => void</span> | | Callback fired when the row turns to edit mode. |
| <span class="prop-name">onRowEditStop</span> | <span class="prop-type">(params: GridRowParams, event: React.SyntheticEvent) => void</span> | | Callback fired when the row turns to view mode. |
| <span class="prop-name">onEditRowModelChange</span> | <span class="prop-type">(params: GridEditRowModelParams) => void</span> | | Callback fired when the EditRowModel changed. |
| <span class="prop-name">onFilterModelChange</span> | <span class="prop-type">(model: GridFilterModel) => void</span> | | Callback fired when the Filter model changes before the filters are applied. |
| <span class="prop-name">onPageChange</span> | <span class="prop-type">(page: number) => void</span> | | Callback fired when the current page has changed. |
Expand Down
5 changes: 4 additions & 1 deletion docs/pages/api-docs/data-grid/grid-api.md

Large diffs are not rendered by default.

56 changes: 56 additions & 0 deletions docs/pages/api-docs/data-grid/grid-edit-row-api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "GridEditRowApi",
"description": "The editing API interface that is available in the grid `apiRef`.",
"properties": [
{
"name": "commitCellChange",
"description": "Updates the field at the given id with the value stored in the edit row model.",
"type": "(params: GridCommitCellChangeParams, event?: any) => boolean"
},
{
"name": "commitRowChange",
"description": "Updates the row at the given id with the values stored in the edit row model.",
"type": "(id: GridRowId, event?: any) => boolean"
},
{
"name": "getCellMode",
"description": "Gets the mode of a cell.",
"type": "(id: GridRowId, field: string) => GridCellMode"
},
{
"name": "getEditRowsModel",
"description": "Gets the edit rows model of the grid.",
"type": "() => GridEditRowsModel"
},
{
"name": "getRowMode",
"description": "Gets the mode of a row.",
"type": "(id: GridRowId) => GridRowMode"
},
{
"name": "isCellEditable",
"description": "Controls if a cell is editable.",
"type": "(params: GridCellParams) => boolean"
},
{
"name": "setCellMode",
"description": "Sets the mode of a cell.",
"type": "(id: GridRowId, field: string, mode: GridCellMode) => void"
},
{
"name": "setEditCellValue",
"description": "Sets the value of the edit cell.\nCommonly used inside the edit cell component.",
"type": "(params: GridEditCellValueParams, event?: SyntheticEvent<Element, Event>) => void"
},
{
"name": "setEditRowsModel",
"description": "Set sthe edit rows model of the grid.",
"type": "(model: GridEditRowsModel) => void"
},
{
"name": "setRowMode",
"description": "Sets the mode of a row.",
"type": "(id: GridRowId, mode: GridRowMode) => void"
}
]
}
6 changes: 5 additions & 1 deletion docs/pages/api-docs/data-grid/x-grid.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { XGrid } from '@material-ui/x-grid';
| <span class="prop-name">disableMultipleSelection</span> | <span class="prop-type">boolean</span> | false | If `true`, multiple selection using the CTRL or CMD key is disabled. |
| <span class="prop-name">disableSelectionOnClick</span> | <span class="prop-type">boolean</span> | false | If `true`, the selection on click on a row or cell is disabled. |
| <span class="prop-name">error</span> | <span class="prop-type">any</span> | | An error that will turn the grid into its error state and display the error component. |
| <span class="prop-name">editMode</span> | <span class="prop-type">`cell` \| `row`</span> | 'cell' | Controls whether to use the cell or row editing. |
| <span class="prop-name">editRowsModel</span> | <span class="prop-type">GridEditRowsModel</span> | undefined | Set the edit rows model of the grid. |
| <span class="prop-name">filterMode</span> | <span class="prop-type">GridFeatureMode</span> | 'client' | Filtering can be processed on the server or client-side. Set it to `server` if you would like to handle filtering on the server-side. |
| <span class="prop-name">filterModel</span> | <span class="prop-type">GridFilterModel</span> | | Set the filter model of the grid. |
Expand Down Expand Up @@ -81,9 +82,12 @@ import { XGrid } from '@material-ui/x-grid';
| <span class="prop-name">onColumnVisibilityChange</span> | <span class="prop-type">(params: GridColumnVisibilityChangeParams, event: MuiEvent<{}>) => void</span> | | Callback fired when a column visibility changes. |
| <span class="prop-name">onError</span> | <span class="prop-type">(args: any) => void</span> | | Callback fired when an exception is thrown in the grid, or when the `showError` API method is called. |
| <span class="prop-name">onEditCellPropsChange</span> | <span class="prop-type">(params: GridEditCellPropsParams, event: MuiEvent<React.SyntheticEvent>) => void</span> | | Callback fired when the edit cell value changes. |
| <span class="prop-name">onCellEditCommit</span> | <span class="prop-type">(params: GridEditCellPropsParams, event: MuiEvent<React.SyntheticEvent>) => void</span> | | Callback fired when the cell changes are going to be committed. |
| <span class="prop-name">onCellEditCommit</span> | <span class="prop-type">(params: GridEditCellPropsParams, event: MuiEvent<React.SyntheticEvent>) => void</span> | | Callback fired when the cell changes are committed. |
| <span class="prop-name">onCellEditStart</span> | <span class="prop-type">(params: GridCellParams, event: React.SyntheticEvent) => void</span> | | Callback fired when the cell turns to edit mode. |
| <span class="prop-name">onCellEditStop</span> | <span class="prop-type">(params: GridCellParams, event: React.SyntheticEvent) => void</span> | | Callback fired when the cell turns to view mode. |
| <span class="prop-name">onRowEditCommit</span> | <span class="prop-type">(id: GridRowId, event: MouseEvent<React.SyntheticEvent>) => void</span> | | Callback fired when the row changes are committed. |
| <span class="prop-name">onRowEditStart</span> | <span class="prop-type">(params: GridRowParams, event: React.SyntheticEvent) => void</span> | | Callback fired when the row turns to edit mode. |
| <span class="prop-name">onRowEditStop</span> | <span class="prop-type">(params: GridRowParams, event: React.SyntheticEvent) => void</span> | | Callback fired when the row turns to view mode. |
| <span class="prop-name">onEditRowModelChange</span> | <span class="prop-type">(params: GridEditRowModelParams) => void</span> | | Callback fired when the EditRowModel changed. |
| <span class="prop-name">onFilterModelChange</span> | <span class="prop-type">(model: GridFilterModel) => void</span> | | Callback fired when the Filter model changes before the filters are applied. |
| <span class="prop-name">onPageChange</span> | <span class="prop-type">(page: number) => void</span> | | Callback fired when the current page has changed. |
Expand Down
1 change: 1 addition & 0 deletions docs/scripts/buildApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ function run(argv: { outputDirectory?: string }) {
'GridCsvExportApi',
'GridExportCsvOptions',
'GridVirtualizationApi',
'GridEditRowApi',
];

apisToGenerate.forEach((apiName) => {
Expand Down
73 changes: 73 additions & 0 deletions docs/src/pages/components/data-grid/editing/BasicRowEditingGrid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
import * as React from 'react';
import { DataGrid } from '@material-ui/data-grid';
import {
randomCreatedDate,
randomTraderName,
randomUpdatedDate,
} from '@material-ui/x-grid-data-generator';

export default function BasicRowEditingGrid() {
return (
<div style={{ height: 300, width: '100%' }}>
<DataGrid editMode="row" rows={rows} columns={columns} />
</div>
);
}

const columns = [
{ field: 'name', headerName: 'Name', width: 180, editable: true },
{ field: 'age', headerName: 'Age', type: 'number', editable: true },
{
field: 'dateCreated',
headerName: 'Date Created',
type: 'date',
width: 180,
editable: true,
},
{
field: 'lastLogin',
headerName: 'Last Login',
type: 'dateTime',
width: 220,
editable: true,
},
];

const rows = [
{
id: 1,
name: randomTraderName(),
age: 25,
dateCreated: randomCreatedDate(),
lastLogin: randomUpdatedDate(),
},
{
id: 2,
name: randomTraderName(),
age: 36,
dateCreated: randomCreatedDate(),
lastLogin: randomUpdatedDate(),
},
{
id: 3,
name: randomTraderName(),
age: 19,
dateCreated: randomCreatedDate(),
lastLogin: randomUpdatedDate(),
},
{
id: 4,
name: randomTraderName(),
age: 28,
dateCreated: randomCreatedDate(),
lastLogin: randomUpdatedDate(),
},
{
id: 5,
name: randomTraderName(),
age: 23,
dateCreated: randomCreatedDate(),
lastLogin: randomUpdatedDate(),
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
import * as React from 'react';
import { DataGrid, GridColumns, GridRowsProp } from '@material-ui/data-grid';
import {
randomCreatedDate,
randomTraderName,
randomUpdatedDate,
} from '@material-ui/x-grid-data-generator';

export default function BasicRowEditingGrid() {
return (
<div style={{ height: 300, width: '100%' }}>
<DataGrid editMode="row" rows={rows} columns={columns} />
</div>
);
}

const columns: GridColumns = [
{ field: 'name', headerName: 'Name', width: 180, editable: true },
{ field: 'age', headerName: 'Age', type: 'number', editable: true },
{
field: 'dateCreated',
headerName: 'Date Created',
type: 'date',
width: 180,
editable: true,
},
{
field: 'lastLogin',
headerName: 'Last Login',
type: 'dateTime',
width: 220,
editable: true,
},
];

const rows: GridRowsProp = [
{
id: 1,
name: randomTraderName(),
age: 25,
dateCreated: randomCreatedDate(),
lastLogin: randomUpdatedDate(),
},
{
id: 2,
name: randomTraderName(),
age: 36,
dateCreated: randomCreatedDate(),
lastLogin: randomUpdatedDate(),
},
{
id: 3,
name: randomTraderName(),
age: 19,
dateCreated: randomCreatedDate(),
lastLogin: randomUpdatedDate(),
},
{
id: 4,
name: randomTraderName(),
age: 28,
dateCreated: randomCreatedDate(),
lastLogin: randomUpdatedDate(),
},
{
id: 5,
name: randomTraderName(),
age: 23,
dateCreated: randomCreatedDate(),
lastLogin: randomUpdatedDate(),
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@material-ui/x-grid-data-generator';
import Alert from '@material-ui/lab/Alert';

export default function EditRowsModelControlGrid() {
export default function CellEditControlGrid() {
const [editRowsModel, setEditRowsModel] = React.useState({});

const handleEditRowsModelChange = React.useCallback((model) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '@material-ui/x-grid-data-generator';
import Alert from '@material-ui/lab/Alert';

export default function EditRowsModelControlGrid() {
export default function CellEditControlGrid() {
const [editRowsModel, setEditRowsModel] = React.useState({});

const handleEditRowsModelChange = React.useCallback((model: GridEditRowsModel) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import * as React from 'react';
import { createMuiTheme } from '@material-ui/core/styles';
import { makeStyles } from '@material-ui/styles';
import { DataGrid } from '@material-ui/data-grid';
import { randomPrice } from '@material-ui/x-grid-data-generator';

function getThemePaletteMode(palette) {
return palette.type || palette.mode;
}

const defaultTheme = createMuiTheme();

const useStyles = makeStyles(
(theme) => {
const isDark = getThemePaletteMode(theme.palette) === 'dark';

return {
root: {
'& .MuiDataGrid-cell--editing': {
backgroundColor: 'rgb(255,215,115, 0.19)',
color: '#1a3e72',
},
'& .Mui-error': {
backgroundColor: `rgb(126,10,15, ${isDark ? 0 : 0.1})`,
color: isDark ? '#ff4343' : '#750f0f',
},
},
};
},
{ defaultTheme },
);

const columns = [
{ field: 'expense', headerName: 'Expense', width: 160, editable: true },
{
field: 'price',
headerName: 'Price',
type: 'number',
width: 120,
editable: true,
},
{ field: 'dueAt', headerName: 'Due at', type: 'date', width: 160, editable: true },
{
field: 'isPaid',
headerName: 'Is paid?',
type: 'boolean',
width: 140,
editable: true,
},
{
field: 'paidAt',
headerName: 'Paid at',
type: 'date',
width: 160,
editable: true,
},
];

const rows = [
{
id: 1,
expense: 'Light bill',
price: randomPrice(0, 1000),
dueAt: new Date(2021, 6, 8),
isPaid: false,
},
{
id: 2,
expense: 'Rent',
price: randomPrice(0, 1000),
dueAt: new Date(2021, 7, 1),
isPaid: false,
},
{
id: 3,
expense: 'Car insurance',
price: randomPrice(0, 1000),
dueAt: new Date(2021, 7, 4),
isPaid: true,
paidAt: new Date(2021, 7, 2),
},
];

export default function ConditionalValidationGrid() {
const classes = useStyles();
const [editRowsModel, setEditRowsModel] = React.useState({});

const handleEditRowsModelChange = React.useCallback((newModel) => {
const updatedModel = { ...newModel };
Object.keys(updatedModel).forEach((id) => {
const hasError =
updatedModel[id].isPaid.value && !updatedModel[id].paidAt.value;
updatedModel[id].paidAt = { ...updatedModel[id].paidAt, error: hasError };
});
setEditRowsModel(updatedModel);
}, []);

return (
<div style={{ height: 400, width: '100%' }}>
<DataGrid
className={classes.root}
rows={rows}
columns={columns}
editMode="row"
editRowsModel={editRowsModel}
onEditRowsModelChange={handleEditRowsModelChange}
/>
</div>
);
}
Loading

0 comments on commit 25ed1c9

Please sign in to comment.