Skip to content

Commit

Permalink
[core] Support toggling initial visibility of columns (#3490)
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot authored May 7, 2024
1 parent 7ee9540 commit 354a28b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/toolpad-studio-components/src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ export interface SerializableGridColumn
dateFormat?: DateFormat;
dateTimeFormat?: DateFormat;
codeComponent?: string;
visible?: boolean;
}

export type SerializableGridColumns = SerializableGridColumn[];
Expand Down Expand Up @@ -1281,6 +1282,10 @@ const DataGridComponent = React.forwardRef(function DataGridComponent(
const appHost = useAppHost();
const isProPlan = appHost.plan === 'pro';

const columnVisibilityModel = Object.fromEntries(
(columnsProp ?? []).map((column) => [column.field, column.visible ?? true]),
);

return (
<LicenseInfoProvider info={LICENSE_INFO}>
<Box ref={ref} sx={{ ...sx, width: '100%', height: '100%', position: 'relative' }}>
Expand Down Expand Up @@ -1308,7 +1313,10 @@ const DataGridComponent = React.forwardRef(function DataGridComponent(
getRowId={getRowId}
onRowSelectionModelChange={onSelectionModelChange}
rowSelectionModel={selectionModel}
initialState={{ pinnedColumns: { right: [ACTIONS_COLUMN_FIELD] } }}
initialState={{
columns: { columnVisibilityModel },
pinnedColumns: { right: [ACTIONS_COLUMN_FIELD] },
}}
disableAggregation={!isProPlan}
disableRowGrouping={!isProPlan}
{...props}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,24 @@ function GridColumnEditor({
) : null}
</Box>

<Tooltip title="Initial visibility of this column.">
<FormControlLabel
control={
<Checkbox
checked={editedColumn.visible ?? true}
disabled={disabled}
onChange={(event) =>
handleColumnChange({
...editedColumn,
visible: event.target.checked,
})
}
/>
}
label="Visible"
/>
</Tooltip>

<FormControlLabel
control={
<Checkbox
Expand Down

0 comments on commit 354a28b

Please sign in to comment.