Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DataGrid] Add demo to the "Custom columns" page that does not use generator #13695

Merged
merged 25 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d78d61d
Add custom columns demo with the most common custom renderers
arminmeh Jul 1, 2024
1a609bd
Add header name for incoTerm column in the generator
arminmeh Jul 1, 2024
8af2630
Fix lint issues
arminmeh Jul 1, 2024
feb9820
Add renderers directly to the code example
arminmeh Jul 5, 2024
7666e0d
Fix lint issues
arminmeh Jul 5, 2024
8a93ea6
Avoid key prop spreading
arminmeh Jul 5, 2024
119f12e
Move renderers to separate files to enable easier copy of the relevan…
arminmeh Jul 8, 2024
40db12b
Add transpiled files
arminmeh Jul 15, 2024
3134539
Fix lint issues
arminmeh Jul 15, 2024
05132a9
Move custom cell renderers outside of the data grid docs folder
arminmeh Jul 22, 2024
f7114db
Ignore components folder in regression tests
arminmeh Jul 22, 2024
5e0c56e
Merge branch 'master' into docs-page-custom-columns
cherniavskii Aug 8, 2024
0eef1a8
Merge branch 'master' into docs-page-custom-columns
cherniavskii Aug 27, 2024
2385b24
lint
cherniavskii Aug 27, 2024
2a22719
lint
cherniavskii Aug 27, 2024
321f4ed
lint
cherniavskii Aug 27, 2024
b31716f
Merge branch 'master' into docs-page-custom-columns
cherniavskii Sep 9, 2024
daae123
docs:api
cherniavskii Sep 9, 2024
4866de8
link to the custom columns page
cherniavskii Sep 9, 2024
3862336
move cell renderers to shorten the file names
cherniavskii Sep 9, 2024
4202abd
fix regex
cherniavskii Sep 9, 2024
96c6579
only use random fns from the generator package
cherniavskii Sep 9, 2024
520d992
revert generator exports
cherniavskii Sep 9, 2024
aed5fd7
Merge remote-tracking branch 'mui/master' into docs-page-custom-columns
cherniavskii Sep 11, 2024
adf7ec9
fix demo in codesandbox embed
cherniavskii Sep 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions docs/data/data-grid/custom-columns/CustomColumnFullExample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import * as React from 'react';
import {
randomColor,
randomEmail,
randomInt,
randomName,
randomArrayItem,
random,
} from '@mui/x-data-grid-generator';
import { DataGrid, gridStringOrNumberComparator } from '@mui/x-data-grid';
import { renderAvatar } from './cell-renderers/avatar';
import { renderEmail } from './cell-renderers/email';
import { renderEditRating, renderRating } from './cell-renderers/rating';
import {
COUNTRY_ISO_OPTIONS,
renderCountry,
renderEditCountry,
} from './cell-renderers/country';
import { renderSparkline } from './cell-renderers/sparkline';
import { renderEditProgress, renderProgress } from './cell-renderers/progress';
import {
renderEditStatus,
renderStatus,
STATUS_OPTIONS,
} from './cell-renderers/status';
import {
INCOTERM_OPTIONS,
renderEditIncoterm,
renderIncoterm,
} from './cell-renderers/incoterm';

const columns = [
{
field: 'name',
headerName: 'Name',
width: 120,
editable: true,
},
{
field: 'avatar',
headerName: 'Avatar',
display: 'flex',
renderCell: renderAvatar,
valueGetter: (value, row) =>
row.name == null || row.avatar == null
? null
: { name: row.name, color: row.avatar },
sortable: false,
filterable: false,
},
{
field: 'email',
headerName: 'Email',
renderCell: renderEmail,
width: 150,
editable: true,
},
{
field: 'rating',
headerName: 'Rating',
display: 'flex',
renderCell: renderRating,
renderEditCell: renderEditRating,
width: 180,
type: 'number',
editable: true,
availableAggregationFunctions: ['avg', 'min', 'max', 'size'],
},
{
field: 'country',
headerName: 'Country',
type: 'singleSelect',
valueOptions: COUNTRY_ISO_OPTIONS,
valueFormatter: (value) => value?.label,
renderCell: renderCountry,
renderEditCell: renderEditCountry,
sortComparator: (v1, v2, param1, param2) =>
gridStringOrNumberComparator(v1.label, v2.label, param1, param2),
width: 150,
editable: true,
},
{
field: 'salary',
headerName: 'Salary',
type: 'number',
valueFormatter: (value) => {
if (!value || typeof value !== 'number') {
return value;
}
return `$${value.toLocaleString()}`;
},
editable: true,
},
{
field: 'monthlyActivity',
headerName: 'Monthly activity',
type: 'custom',
resizable: false,
filterable: false,
sortable: false,
editable: false,
groupable: false,
display: 'flex',
renderCell: renderSparkline,
width: 150,
valueGetter: (value, row) => row.monthlyActivity,
},
{
field: 'budget',
headerName: 'Budget left',
renderCell: renderProgress,
renderEditCell: renderEditProgress,
availableAggregationFunctions: ['min', 'max', 'avg', 'size'],
type: 'number',
width: 120,
editable: true,
},
{
field: 'status',
headerName: 'Status',
renderCell: renderStatus,
renderEditCell: renderEditStatus,
type: 'singleSelect',
valueOptions: STATUS_OPTIONS,
width: 150,
editable: true,
},
{
field: 'incoTerm',
headerName: 'Incoterm',
renderCell: renderIncoterm,
renderEditCell: renderEditIncoterm,
type: 'singleSelect',
valueOptions: INCOTERM_OPTIONS,
editable: true,
},
];

const rows = Array.from({ length: 10 }, (_, index) => ({
id: index,
name: randomName({}, {}),
avatar: randomColor(),
email: randomEmail(),
rating: randomInt(1, 5),
country: randomArrayItem(COUNTRY_ISO_OPTIONS),
salary: randomInt(35000, 80000),
monthlyActivity: Array.from({ length: 30 }, () => randomInt(1, 25)),
budget: random(0, 1).toPrecision(),
status: randomArrayItem(STATUS_OPTIONS),
incoTerm: randomArrayItem(INCOTERM_OPTIONS),
}));

export default function CustomColumnFullExample() {
return (
<div style={{ height: 400, width: '100%' }}>
<DataGrid rows={rows} columns={columns} />
</div>
);
}
164 changes: 164 additions & 0 deletions docs/data/data-grid/custom-columns/CustomColumnFullExample.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import * as React from 'react';
import {
randomColor,
randomEmail,
randomInt,
randomName,
randomArrayItem,
random,
} from '@mui/x-data-grid-generator';
import {
DataGrid,
GridColDef,
gridStringOrNumberComparator,
} from '@mui/x-data-grid';
import { renderAvatar } from './cell-renderers/avatar';
import { renderEmail } from './cell-renderers/email';
import { renderEditRating, renderRating } from './cell-renderers/rating';
import {
COUNTRY_ISO_OPTIONS,
CountryIsoOption,
renderCountry,
renderEditCountry,
} from './cell-renderers/country';
import { renderSparkline } from './cell-renderers/sparkline';
import { renderEditProgress, renderProgress } from './cell-renderers/progress';
import {
renderEditStatus,
renderStatus,
STATUS_OPTIONS,
} from './cell-renderers/status';
import {
INCOTERM_OPTIONS,
renderEditIncoterm,
renderIncoterm,
} from './cell-renderers/incoterm';

const columns: GridColDef<(typeof rows)[number]>[] = [
{
field: 'name',
headerName: 'Name',
width: 120,
editable: true,
},
{
field: 'avatar',
headerName: 'Avatar',
display: 'flex',
renderCell: renderAvatar,
valueGetter: (value, row) =>
row.name == null || row.avatar == null
? null
: { name: row.name, color: row.avatar },
sortable: false,
filterable: false,
} as GridColDef<any, { color: string; name: string }>,
{
field: 'email',
headerName: 'Email',
renderCell: renderEmail,
width: 150,
editable: true,
},
{
field: 'rating',
headerName: 'Rating',
display: 'flex',
renderCell: renderRating,
renderEditCell: renderEditRating,
width: 180,
type: 'number',
editable: true,
availableAggregationFunctions: ['avg', 'min', 'max', 'size'],
},
{
field: 'country',
headerName: 'Country',
type: 'singleSelect',
valueOptions: COUNTRY_ISO_OPTIONS,
valueFormatter: (value: CountryIsoOption) => value?.label,
renderCell: renderCountry,
renderEditCell: renderEditCountry,
sortComparator: (v1, v2, param1, param2) =>
gridStringOrNumberComparator(v1.label, v2.label, param1, param2),
width: 150,
editable: true,
} as GridColDef<any, CountryIsoOption, string>,
{
field: 'salary',
headerName: 'Salary',
type: 'number',
valueFormatter: (value?: number) => {
if (!value || typeof value !== 'number') {
return value;
}
return `$${value.toLocaleString()}`;
},
editable: true,
},
{
field: 'monthlyActivity',
headerName: 'Monthly activity',
type: 'custom',
resizable: false,
filterable: false,
sortable: false,
editable: false,
groupable: false,
display: 'flex',
renderCell: renderSparkline,
width: 150,
valueGetter: (value, row) => row.monthlyActivity,
},
{
field: 'budget',
headerName: 'Budget left',
renderCell: renderProgress,
renderEditCell: renderEditProgress,
availableAggregationFunctions: ['min', 'max', 'avg', 'size'],
type: 'number',
width: 120,
editable: true,
},
{
field: 'status',
headerName: 'Status',
renderCell: renderStatus,
renderEditCell: renderEditStatus,
type: 'singleSelect',
valueOptions: STATUS_OPTIONS,
width: 150,
editable: true,
},
{
field: 'incoTerm',
headerName: 'Incoterm',
renderCell: renderIncoterm,
renderEditCell: renderEditIncoterm,
type: 'singleSelect',
valueOptions: INCOTERM_OPTIONS,
editable: true,
},
];

const rows = Array.from({ length: 10 }, (_, index) => ({
id: index,
name: randomName({}, {}),
avatar: randomColor(),
email: randomEmail(),
rating: randomInt(1, 5),
country: randomArrayItem(COUNTRY_ISO_OPTIONS),
salary: randomInt(35000, 80000),
monthlyActivity: Array.from({ length: 30 }, () => randomInt(1, 25)),
budget: random(0, 1).toPrecision(),
status: randomArrayItem(STATUS_OPTIONS),
incoTerm: randomArrayItem(INCOTERM_OPTIONS),
}));

export default function CustomColumnFullExample() {
return (
<div style={{ height: 400, width: '100%' }}>
<DataGrid rows={rows} columns={columns} />
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<DataGrid rows={rows} columns={columns} />
14 changes: 14 additions & 0 deletions docs/data/data-grid/custom-columns/cell-renderers/avatar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as React from 'react';
import Avatar from '@mui/material/Avatar';

export function renderAvatar(params) {
if (params.value == null) {
return '';
}

return (
<Avatar style={{ backgroundColor: params.value.color }}>
{params.value.name.toUpperCase().substring(0, 1)}
</Avatar>
);
}
17 changes: 17 additions & 0 deletions docs/data/data-grid/custom-columns/cell-renderers/avatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as React from 'react';
import Avatar from '@mui/material/Avatar';
import { GridRenderCellParams } from '@mui/x-data-grid';

export function renderAvatar(
params: GridRenderCellParams<{ name: string; color: string }, any, any>,
) {
if (params.value == null) {
return '';
}

return (
<Avatar style={{ backgroundColor: params.value.color }}>
{params.value.name.toUpperCase().substring(0, 1)}
</Avatar>
);
}
Loading
Loading