Skip to content

Commit

Permalink
[DataGrid] Allow passing readonly arrays to columns and `sortingOrd…
Browse files Browse the repository at this point in the history
…er` props (#10686)
  • Loading branch information
pcorpet authored Oct 19, 2023
1 parent 7d2ecd9 commit 391351c
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function GridColumnHeaderItem(props: GridColumnHeaderItemProps) {
/>
);

const sortingOrder: GridSortDirection[] = colDef.sortingOrder ?? rootProps.sortingOrder;
const sortingOrder: readonly GridSortDirection[] = colDef.sortingOrder ?? rootProps.sortingOrder;

const columnTitleIconButtons = (
<React.Fragment>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { GridIconButtonContainer } from './GridIconButtonContainer';
export interface GridColumnHeaderSortIconProps {
direction: GridSortDirection;
index: number | undefined;
sortingOrder: GridSortDirection[];
sortingOrder: readonly GridSortDirection[];
}

type OwnerState = GridColumnHeaderSortIconProps & {
Expand All @@ -34,7 +34,7 @@ function getIcon(
icons: UncapitalizedGridSlotsComponent,
direction: GridSortDirection,
className: string,
sortingOrder: GridSortDirection[],
sortingOrder: readonly GridSortDirection[],
) {
let Icon;
const iconProps: any = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function GridColumnMenuSortItem(props: GridColumnMenuItemProps) {
return sortItem?.sort;
}, [colDef, sortModel]);

const sortingOrder: GridSortDirection[] = colDef.sortingOrder ?? rootProps.sortingOrder;
const sortingOrder: readonly GridSortDirection[] = colDef.sortingOrder ?? rootProps.sortingOrder;

const onSortMenuItemClick = React.useCallback(
(event: React.MouseEvent<HTMLElement>) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export const createColumnsState = ({
columnVisibilityModel = gridColumnVisibilityModelSelector(apiRef),
keepOnlyColumnsToUpsert = false,
}: {
columnsToUpsert: GridColDef[];
columnsToUpsert: readonly GridColDef[];
initialState: GridColumnsInitialState | undefined;
columnTypes: GridColumnTypesRecord;
columnVisibilityModel?: GridColumnVisibilityModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export const buildAggregatedSortingApplier = (
};

export const getNextGridSortDirection = (
sortingOrder: GridSortDirection[],
sortingOrder: readonly GridSortDirection[],
current?: GridSortDirection,
) => {
const currentIdx = sortingOrder.indexOf(current);
Expand Down
4 changes: 2 additions & 2 deletions packages/grid/x-data-grid/src/models/props/DataGridProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ export interface DataGridPropsWithDefaultValues {
* The order of the sorting sequence.
* @default ['asc', 'desc', null]
*/
sortingOrder: GridSortDirection[];
sortingOrder: readonly GridSortDirection[];
/**
* Sorting can be processed on the server or client-side.
* Set it to 'client' if you would like to handle sorting on the client-side.
Expand Down Expand Up @@ -705,7 +705,7 @@ export interface DataGridPropsWithoutDefaultValue<R extends GridValidRowModel =
/**
* Set of columns of type [[GridColDef[]]].
*/
columns: GridColDef<R>[];
columns: readonly GridColDef<R>[];
/**
* Return the id of a given [[GridRowModel]].
*/
Expand Down
7 changes: 7 additions & 0 deletions packages/grid/x-data-grid/src/tests/columns.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,10 @@ function CellParamsFormattedValue() {
/>
);
}

const constBrandColumns = [{ field: 'brand' }] as const;
const constEmptyRows = [] as const;

function ConstProps() {
return <DataGrid rows={constEmptyRows} columns={constBrandColumns} />;
}

0 comments on commit 391351c

Please sign in to comment.