Skip to content

Commit

Permalink
Allow pinning DataGrid columns using the column config (#2452)
Browse files Browse the repository at this point in the history
When using `DataGridPro` or `DataGridPremium` with the
`usePersistentColumnState` hook.

```tsx
const columns: GridColDef[] = [
    {
        field: "title",
        pinned: "left",
    },
    // ... other columns
    {
        field: "actions",
        pinned: "right",
    },
];
```
  • Loading branch information
jamesricky authored Aug 22, 2024
1 parent b7646db commit 0fb8d9a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
19 changes: 19 additions & 0 deletions .changeset/cold-hats-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
"@comet/admin": minor
---

Allow pinning DataGrid columns using the column config when using `DataGridPro` or `DataGridPremium` with the `usePersistentColumnState` hook

```tsx
const columns: GridColDef[] = [
{
field: "title",
pinned: "left",
},
// ... other columns
{
field: "actions",
pinned: "right",
},
];
```
1 change: 1 addition & 0 deletions demo/admin/src/products/ProductsGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ export function ProductsGrid() {
sortable: false,
filterable: false,
width: 106,
pinned: "right",
renderCell: (params) => {
return (
<>
Expand Down
6 changes: 6 additions & 0 deletions packages/admin/admin/src/dataGrid/GridColDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
GridValidRowModel,
} from "@mui/x-data-grid";

import { GridPinnedColumns } from "./usePersistentColumnState";

export interface GridColDef<R extends GridValidRowModel = any, V = any, F = V> extends MuiGridColDef<R, V, F> {
/**
* Media query to define when the column is visible.
Expand All @@ -14,4 +16,8 @@ export interface GridColDef<R extends GridValidRowModel = any, V = any, F = V> e
* Requires DataGridPro or DataGridPremium.
*/
sortBy?: string | string[];
/**
* Requires DataGridPro or DataGridPremium.
*/
pinned?: keyof GridPinnedColumns;
}
23 changes: 21 additions & 2 deletions packages/admin/admin/src/dataGrid/usePersistentColumnState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import * as React from "react";
import { useStoredState } from "../hooks/useStoredState";
import { GridColDef } from "./GridColDef";

export type GridPinnedColumns = {
left?: string[];
right?: string[];
};

const useGridColumns = (apiRef: ReturnType<typeof useGridApiRef>) => {
const [columns, setColumns] = React.useState<GridColDef[] | undefined>();

Expand Down Expand Up @@ -75,14 +80,28 @@ export function usePersistentColumnState(stateKey: string): GridProps {
[mediaQueryColumnVisibilityModel, setStoredColumnVisibilityModel],
);

const [pinnedColumns, setPinnedColumns] = useStoredState<GridColumnVisibilityModel>(`${stateKey}PinnedColumns`, {});
const [pinnedColumns, setPinnedColumns] = useStoredState<GridPinnedColumns>(`${stateKey}PinnedColumns`, {});
const handlePinnedColumnsChange = React.useCallback(
(newModel: GridColumnVisibilityModel) => {
(newModel: GridPinnedColumns) => {
setPinnedColumns(newModel);
},
[setPinnedColumns],
);

React.useEffect(() => {
// During the first render, `columns` is `undefined`, so we cannot set this as the initial values of the `pinnedColumns` state.
// We have to wait until the columns are loaded from the `useGridColumns` hook.
const pinnedColumnsHaveNotYetBeenSet = Object.keys(pinnedColumns).length === 0;
const columnsHaveBeenLoaded = Boolean(columns);

if (pinnedColumnsHaveNotYetBeenSet && columnsHaveBeenLoaded) {
setPinnedColumns({
left: columns?.filter((column) => column.pinned === "left")?.map((column) => column.field),
right: columns?.filter((column) => column.pinned === "right")?.map((column) => column.field),
});
}
}, [columns, setPinnedColumns, pinnedColumns]);

//no API for column dimensions as controlled state, export on change instead
const columnDimensionsKey = `${stateKey}ColumnDimensions`;
const initialColumnDimensions = React.useMemo(() => {
Expand Down

0 comments on commit 0fb8d9a

Please sign in to comment.