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

[DataGridPro] Implement header filter height #12666

Merged
merged 9 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/pages/x/api/data-grid/data-grid-premium.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"type": { "name": "shape", "description": "{ warnIfFocusStateIsNotSynced?: bool }" }
},
"filterDebounceMs": { "type": { "name": "number" }, "default": "150" },
"headerFilterHeight": { "type": { "name": "number" } },
"filterMode": {
"type": { "name": "enum", "description": "'client'<br>&#124;&nbsp;'server'" },
"default": "\"client\""
Expand Down
1 change: 1 addition & 0 deletions docs/pages/x/api/data-grid/data-grid-pro.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"type": { "name": "shape", "description": "{ warnIfFocusStateIsNotSynced?: bool }" }
},
"filterDebounceMs": { "type": { "name": "number" }, "default": "150" },
"headerFilterHeight": { "type": { "name": "number" } },
"filterMode": {
"type": { "name": "enum", "description": "'client'<br>&#124;&nbsp;'server'" },
"default": "\"client\""
Expand Down
1 change: 1 addition & 0 deletions docs/pages/x/api/data-grid/data-grid.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"type": { "name": "shape", "description": "{ warnIfFocusStateIsNotSynced?: bool }" }
},
"filterDebounceMs": { "type": { "name": "number" }, "default": "150" },
"headerFilterHeight": { "type": { "name": "number" } },
"filterMode": {
"type": { "name": "enum", "description": "'client'<br>&#124;&nbsp;'server'" },
"default": "\"client\""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
"filterDebounceMs": {
"description": "The milliseconds delay to wait after a keystroke before triggering filtering."
},
"headerFilterHeight": { "description": "Override the height/width of the header filters." },
"filterMode": {
"description": "Filtering can be processed on the server or client-side. Set it to &#39;server&#39; if you would like to handle filtering on the server-side."
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"filterDebounceMs": {
"description": "The milliseconds delay to wait after a keystroke before triggering filtering."
},
"headerFilterHeight": { "description": "Override the height/width of the header filters." },
"filterMode": {
"description": "Filtering can be processed on the server or client-side. Set it to &#39;server&#39; if you would like to handle filtering on the server-side."
},
Expand Down
romgrk marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"filterDebounceMs": {
"description": "The milliseconds delay to wait after a keystroke before triggering filtering."
},
"headerFilterHeight": { "description": "Override the height/width of the header filters." },
"filterMode": {
"description": "Filtering can be processed on the server or client-side. Set it to &#39;server&#39; if you would like to handle filtering on the server-side."
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ DataGridPremiumRaw.propTypes = {
* @default 150
*/
filterDebounceMs: PropTypes.number,
/**
* Override the height of the header filters.
*/
headerFilterHeight: PropTypes.number,
/**
* Filtering can be processed on the server or client-side.
* Set it to 'server' if you would like to handle filtering on the server-side.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const useGridCellSelection = (
const autoScrollRAF = React.useRef<number | null>();
const sortedRowIds = useGridSelector(apiRef, gridSortedRowIdsSelector);
const dimensions = useGridSelector(apiRef, gridDimensionsSelector);
const totalHeaderHeight = getTotalHeaderHeight(apiRef, props.columnHeaderHeight);
const totalHeaderHeight = getTotalHeaderHeight(apiRef, props);

const ignoreValueFormatterProp = props.ignoreValueFormatterDuringExport;
const ignoreValueFormatter =
Expand Down
4 changes: 4 additions & 0 deletions packages/x-data-grid-pro/src/DataGridPro/DataGridPro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ DataGridProRaw.propTypes = {
* @default 150
*/
filterDebounceMs: PropTypes.number,
/**
* Override the height of the header filters.
*/
headerFilterHeight: PropTypes.number,
/**
* Filtering can be processed on the server or client-side.
* Set it to 'server' if you would like to handle filtering on the server-side.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export const useGridColumnHeaders = (props: UseGridColumnHeadersProps) => {
<rootProps.slots.headerFilterCell
colIndex={columnIndex}
key={`${colDef.field}-filter`}
height={dimensions.headerHeight}
height={dimensions.headerFilterHeight}
width={colDef.computedWidth}
colDef={colDef}
hasFocus={hasFocus}
Expand Down
8 changes: 5 additions & 3 deletions packages/x-data-grid-pro/src/models/dataGridProProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
DataGridPropsWithComplexDefaultValueBeforeProcessing,
GridPinnedColumnFields,
DataGridProSharedPropsWithDefaultValue,
DataGridProSharedPropsWithoutDefaultValue,
} from '@mui/x-data-grid/internals';
import type { GridPinnedRowsProp } from '../hooks/features/rowPinning';
import { GridApiPro } from './gridApiPro';
Expand Down Expand Up @@ -138,9 +139,10 @@ export interface DataGridProPropsWithDefaultValue<R extends GridValidRowModel =

export interface DataGridProPropsWithoutDefaultValue<R extends GridValidRowModel = any>
extends Omit<
DataGridPropsWithoutDefaultValue<R>,
'initialState' | 'componentsProps' | 'slotProps'
> {
DataGridPropsWithoutDefaultValue<R>,
'initialState' | 'componentsProps' | 'slotProps'
>,
DataGridProSharedPropsWithoutDefaultValue {
/**
* The ref object that allows grid manipulation. Can be instantiated with `useGridApiRef()`.
*/
Expand Down
16 changes: 16 additions & 0 deletions packages/x-data-grid-pro/src/tests/layout.DataGridPro.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,22 @@ describe('<DataGridPro /> - Layout', () => {
});
});

it('should work with headerFilterHeight', () => {
romgrk marked this conversation as resolved.
Show resolved Hide resolved
render(
<div style={{ width: 300, height: 300 }}>
<DataGridPro
{...baselineProps}
autoHeight
headerFilters
columnHeaderHeight={20}
headerFilterHeight={32}
rowHeight={20}
/>
</div>,
);
expect(grid('main')!.clientHeight).to.equal(baselineProps.rows.length * 20 + 20 + 32);
});

it('should support translations in the theme', () => {
render(
<ThemeProvider theme={createTheme({}, ptBR)}>
Expand Down
4 changes: 4 additions & 0 deletions packages/x-data-grid/src/DataGrid/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ DataGridRaw.propTypes = {
* @default 150
*/
filterDebounceMs: PropTypes.number,
/**
* Override the height of the header filters.
*/
headerFilterHeight: PropTypes.number,
/**
* Filtering can be processed on the server or client-side.
* Set it to 'server' if you would like to handle filtering on the server-side.
Expand Down
1 change: 1 addition & 0 deletions packages/x-data-grid/src/components/GridRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ GridRow.propTypes = {
}).isRequired,
hasScrollX: PropTypes.bool.isRequired,
hasScrollY: PropTypes.bool.isRequired,
headerFilterHeight: PropTypes.number.isRequired,
headerHeight: PropTypes.number.isRequired,
headersTotalHeight: PropTypes.number.isRequired,
isReady: PropTypes.bool.isRequired,
Expand Down
2 changes: 1 addition & 1 deletion packages/x-data-grid/src/components/GridScrollArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function GridScrollAreaRaw(props: ScrollAreaProps) {
const rootProps = useGridRootProps();
const ownerState = { ...rootProps, scrollDirection };
const classes = useUtilityClasses(ownerState);
const totalHeaderHeight = getTotalHeaderHeight(apiRef, rootProps.columnHeaderHeight);
const totalHeaderHeight = getTotalHeaderHeight(apiRef, rootProps);
const headerHeight = Math.floor(rootProps.columnHeaderHeight * densityFactor);

const style: React.CSSProperties = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ export const GridColumnHeaderRow = styled('div', {
overridesResolver: (_, styles) => styles.columnHeaderRow,
})<{ ownerState: OwnerState }>({
display: 'flex',
height: 'var(--DataGrid-headerHeight)',
});

export const useGridColumnHeaders = (props: UseGridColumnHeadersProps) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
GRID_STRING_COL_DEF,
getGridDefaultColumnTypes,
} from '../../../colDef';
import { DataGridProcessedProps } from '../../../models/props/DataGridProps';
import { GridApiCommunity } from '../../../models/api/gridApiCommunity';
import { GridColDef, GridStateColDef } from '../../../models/colDef/gridColDef';
import { gridColumnsStateSelector, gridColumnVisibilityModelSelector } from './gridColumnsSelector';
Expand Down Expand Up @@ -431,11 +432,16 @@ export function getFirstNonSpannedColumnToRender({

export function getTotalHeaderHeight(
apiRef: React.MutableRefObject<GridApiCommunity>,
headerHeight: number,
props: Pick<DataGridProcessedProps, 'columnHeaderHeight' | 'headerFilterHeight'>,
) {
const densityFactor = gridDensityFactorSelector(apiRef);
const maxDepth = gridColumnGroupsHeaderMaxDepthSelector(apiRef);
const isHeaderFilteringEnabled = gridHeaderFilteringEnabledSelector(apiRef);
const multiplicationFactor = isHeaderFilteringEnabled ? 2 : 1;
return Math.floor(headerHeight * densityFactor) * ((maxDepth ?? 0) + multiplicationFactor);

const columnHeadersHeight = Math.floor(props.columnHeaderHeight * densityFactor);
const filterHeadersHeight = isHeaderFilteringEnabled
? Math.floor((props.headerFilterHeight ?? props.columnHeaderHeight) * densityFactor)
: 0;

return columnHeadersHeight * (1 + (maxDepth ?? 0)) + filterHeadersHeight;
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,13 @@ export interface GridDimensions {
*/
rightPinnedWidth: number;
/**
* Height of one headers.
* Height of one column header.
*/
headerHeight: number;
/**
* Height of header filters.
*/
headerFilterHeight: number;
/**
* Height of all the column headers.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type RootProps = Pick<
| 'rowHeight'
| 'resizeThrottleMs'
| 'columnHeaderHeight'
| 'headerFilterHeight'
>;

export type GridDimensionsState = GridDimensions;
Expand All @@ -58,6 +59,7 @@ const EMPTY_DIMENSIONS: GridDimensions = {
hasScrollY: false,
scrollbarSize: 0,
headerHeight: 0,
headerFilterHeight: 0,
rowWidth: 0,
rowHeight: 0,
columnsTotalWidth: 0,
Expand Down Expand Up @@ -89,8 +91,11 @@ export function useGridDimensions(
const densityFactor = useGridSelector(apiRef, gridDensityFactorSelector);
const rowHeight = Math.floor(props.rowHeight * densityFactor);
const headerHeight = Math.floor(props.columnHeaderHeight * densityFactor);
const headerFilterHeight = Math.floor(
(props.headerFilterHeight ?? props.columnHeaderHeight) * densityFactor,
);
const columnsTotalWidth = roundToDecimalPlaces(gridColumnsTotalWidthSelector(apiRef), 6);
const headersTotalHeight = getTotalHeaderHeight(apiRef, props.columnHeaderHeight);
const headersTotalHeight = getTotalHeaderHeight(apiRef, props);

const leftPinnedWidth = pinnedColumns.left.reduce((w, col) => w + col.computedWidth, 0);
const rightPinnedWidth = pinnedColumns.right.reduce((w, col) => w + col.computedWidth, 0);
Expand Down Expand Up @@ -244,6 +249,7 @@ export function useGridDimensions(
hasScrollY,
scrollbarSize,
headerHeight,
headerFilterHeight,
rowWidth,
rowHeight,
columnsTotalWidth,
Expand Down Expand Up @@ -273,6 +279,7 @@ export function useGridDimensions(
rowsMeta.currentPageTotalHeight,
rowHeight,
headerHeight,
headerFilterHeight,
columnsTotalWidth,
headersTotalHeight,
leftPinnedWidth,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function buildPrintWindow(title?: string): HTMLIFrameElement {
*/
export const useGridPrintExport = (
apiRef: React.MutableRefObject<GridPrivateApiCommunity>,
props: Pick<DataGridProcessedProps, 'pagination' | 'columnHeaderHeight'>,
props: Pick<DataGridProcessedProps, 'pagination' | 'columnHeaderHeight' | 'headerFilterHeight'>,
): void => {
const logger = useGridLogger(apiRef, 'useGridPrintExport');
const doc = React.useRef<Document | null>(null);
Expand Down Expand Up @@ -160,7 +160,7 @@ export const useGridPrintExport = (
// Expand container height to accommodate all rows
const computedTotalHeight =
rowsMeta.currentPageTotalHeight +
getTotalHeaderHeight(apiRef, props.columnHeaderHeight) +
getTotalHeaderHeight(apiRef, props) +
gridToolbarElementHeight +
gridFooterElementHeight;
gridClone.style.height = `${computedTotalHeight}px`;
Expand Down Expand Up @@ -256,7 +256,7 @@ export const useGridPrintExport = (
});
}
},
[apiRef, doc, props.columnHeaderHeight],
[apiRef, doc, props],
);

const handlePrintWindowAfterPrint = React.useCallback(
Expand Down
10 changes: 1 addition & 9 deletions packages/x-data-grid/src/internals/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,7 @@ export { useGridVisibleRows, getVisibleRows } from '../hooks/utils/useGridVisibl
export { useGridInitializeState } from '../hooks/utils/useGridInitializeState';
export type { GridStateInitializer } from '../hooks/utils/useGridInitializeState';

export type {
DataGridProSharedPropsWithDefaultValue,
DataGridPremiumSharedPropsWithDefaultValue,
GridExperimentalFeatures,
DataGridPropsWithoutDefaultValue,
DataGridPropsWithDefaultValues,
DataGridPropsWithComplexDefaultValueAfterProcessing,
DataGridPropsWithComplexDefaultValueBeforeProcessing,
} from '../models/props/DataGridProps';
export type * from '../models/props/DataGridProps';

export { getColumnsToExport, defaultGetRowsToExport } from '../hooks/features/export/utils';
export * from '../utils/createControllablePromise';
Expand Down
8 changes: 8 additions & 0 deletions packages/x-data-grid/src/models/props/DataGridProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,13 @@ export interface DataGridProSharedPropsWithDefaultValue {
headerFilters: boolean;
}

export interface DataGridProSharedPropsWithoutDefaultValue {
/**
* Override the height of the header filters.
*/
headerFilterHeight?: number;
}

export interface DataGridPremiumSharedPropsWithDefaultValue {
/**
* If `true`, the cell selection mode is enabled.
Expand All @@ -812,5 +819,6 @@ export interface DataGridPremiumSharedPropsWithDefaultValue {
*/
export interface DataGridProcessedPropsWithShared
extends DataGridProcessedProps,
DataGridProSharedPropsWithoutDefaultValue,
Partial<DataGridProSharedPropsWithDefaultValue>,
Partial<DataGridPremiumSharedPropsWithDefaultValue> {}