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

[website] Update main data grid demo on X landing page #37001

Merged
merged 5 commits into from
Apr 25, 2023
Merged
Changes from all commits
Commits
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
151 changes: 46 additions & 105 deletions docs/src/components/productX/XHero.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,117 +18,60 @@ import { alpha } from '@mui/material/styles';
import {
DataGridPremium,
useGridApiRef,
GridRowGroupingModel,
GridAggregationModel,
GridColDef,
GridColumnVisibilityModel,
useKeepGroupedColumnsHidden,
} from '@mui/x-data-grid-premium';
import {
renderEditProgress,
renderEditStatus,
renderProgress,
renderStatus,
renderTotalPrice,
useDemoData,
} from '@mui/x-data-grid-generator';
import { STATUS_OPTIONS } from '@mui/x-data-grid-generator/services/static-data';
import { useDemoData } from '@mui/x-data-grid-generator';

const startDate = new Date();
startDate.setDate(10);
const endDate = new Date();
endDate.setDate(endDate.getDate() + 28);

export default function XHero() {
const columns: GridColDef[] = [
{
field: 'commodity',
headerName: 'Commodity',
width: 180,
},
{
field: 'unitPrice',
headerName: 'Unit Price',
type: 'number',

valueParser: (value: number) => Number(value),
},
{
field: 'feeRate',
headerName: 'Fee Rate',
type: 'number',
width: 80,
const visibleFields = [
'commodity',
'unitPrice',
'feeRate',
'quantity',
'filledQuantity',
'isFilled',
'traderName',
'status',
'totalPrice',
];

valueParser: (value) => Number(value),
},
{
field: 'quantity',
headerName: 'Quantity',
type: 'number',
width: 140,
valueParser: (value: string) => Number(value),
},
{
field: 'filledQuantity',
headerName: 'Filled Quantity',
renderCell: renderProgress,
renderEditCell: renderEditProgress,
availableAggregationFunctions: ['min', 'max', 'avg', 'size'],
type: 'number',
width: 120,
},
{
field: 'isFilled',
headerName: 'Is Filled',
align: 'center',
type: 'boolean',
width: 80,
},
{
field: 'traderName',
headerName: 'Trader Name',
width: 120,
},
{
field: 'status',
headerName: 'Status',
renderCell: renderStatus,
renderEditCell: renderEditStatus,
type: 'singleSelect',
valueOptions: STATUS_OPTIONS,
width: 150,
},
{
field: 'totalPrice',
headerName: 'Total in USD',
valueGetter: ({ row }) =>
row.feeRate == null || row.quantity == null || row.unitPrice == null
? null
: row.feeRate + row.quantity * row.unitPrice,
renderCell: renderTotalPrice,
type: 'number',
width: 160,
},
];
export default function XHero() {
const { loading, data } = useDemoData({
dataSet: 'Commodity',
rowLength: 10000,
maxColumns: 40,
editable: true,
visibleFields,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

visibleFields allows to pick specific columns. This allows to remove the duplicated column definitions.

});
const [columnVisibilityModel, setColumnVisibilityModel] =
React.useState<GridColumnVisibilityModel>({
commodity: false,
const apiRef = useGridApiRef();

const sortedColumns = React.useMemo(() => {
return [...data.columns].sort((a, b) => {
return visibleFields.indexOf(a.field) - visibleFields.indexOf(b.field);
});
const [rowGroupingModel, setRowGroupingModel] = React.useState<GridRowGroupingModel>([
'commodity',
]);
const [aggregationModel, setAggregationModel] = React.useState<GridAggregationModel>({
quantity: 'sum',
unitPrice: 'avg',
feeRate: 'min',
totalPrice: 'max',
}, [data.columns]);

const initialState = useKeepGroupedColumnsHidden({
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useKeepGroupedColumnsHidden fixes the missing Commodities column when you stop grouping by it

apiRef,
initialState: {
...data.initialState,
rowGrouping: {
model: ['commodity'],
},
aggregation: {
model: {
quantity: 'sum',
unitPrice: 'avg',
feeRate: 'min',
totalPrice: 'max',
},
},
},
});
const apiRef = useGridApiRef();

let rowGroupingCounter = 0;
return (
<HeroContainer
Expand Down Expand Up @@ -278,12 +221,16 @@ export default function XHero() {
>
<DataGridPremium
{...data}
columns={columns}
columns={sortedColumns}
apiRef={apiRef}
initialState={initialState}
disableRowSelectionOnClick
groupingColDef={{
renderHeader: (params) => {
return <Box sx={{ pl: 5, fontWeight: 500 }}>{params.colDef.headerName}</Box>;
headerClassName: 'grouping-column-header',
}}
sx={{
'& .grouping-column-header': {
pl: 6,
},
}}
hideFooter
Expand All @@ -292,12 +239,6 @@ export default function XHero() {
rowGroupingCounter += 1;
return rowGroupingCounter === 3;
}}
rowGroupingModel={rowGroupingModel}
onRowGroupingModelChange={(model) => setRowGroupingModel(model)}
aggregationModel={aggregationModel}
onAggregationModelChange={(newModel) => setAggregationModel(newModel)}
columnVisibilityModel={columnVisibilityModel}
onColumnVisibilityModelChange={(newModel) => setColumnVisibilityModel(newModel)}
Comment on lines -295 to -300
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to use controlled models for this, initialState should be enough here

/>
</Box>
</Paper>
Expand Down
Loading