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] Add new option hideDescendantCount to Tree Data #3368

Merged
merged 4 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion docs/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// <reference types="next" />
/// <reference types="next/types/global" />
Copy link
Member

Choose a reason for hiding this comment

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

Do you know what caused this modification?

Copy link
Member Author

Choose a reason for hiding this comment

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

#3343

Probably this one
I checked the NextJS example and they have this line

Copy link
Member Author

Choose a reason for hiding this comment

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

/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ const useUtilityClasses = (ownerState: OwnerState) => {
return composeClasses(slots, getDataGridUtilityClass, classes);
};

const GridTreeDataGroupingCell = (props: GridRenderCellParams) => {
const { id, field, rowNode } = props;
interface GridTreeDataGroupingCellProps extends GridRenderCellParams {
hideDescendantCount?: boolean;
}

const GridTreeDataGroupingCell = (props: GridTreeDataGroupingCellProps) => {
const { id, field, rowNode, hideDescendantCount } = props;

const rootProps = useGridRootProps();
const apiRef = useGridApiContext();
Expand Down Expand Up @@ -80,7 +84,7 @@ const GridTreeDataGroupingCell = (props: GridRenderCellParams) => {
</div>
<span>
{rowNode.groupingKey}
{filteredDescendantCount > 0 ? ` (${filteredDescendantCount})` : ''}
{!hideDescendantCount && filteredDescendantCount > 0 ? ` (${filteredDescendantCount})` : ''}
</span>
</Box>
);
Expand Down Expand Up @@ -122,6 +126,7 @@ GridTreeDataGroupingCell.propTypes = {
* If true, the cell is the active element.
*/
hasFocus: PropTypes.bool.isRequired,
hideDescendantCount: PropTypes.bool,
/**
* The grid row id.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import * as React from 'react';
import { GridColDef } from '../../../models/colDef/gridColDef';
import { GridTreeDataGroupingCell } from '../../../components/cell/GridTreeDataGroupingCell';
import { GRID_STRING_COL_DEF } from '../../../models/colDef/gridStringColDef';
import { GridValueGetterFullParams } from '../../../models';

Expand All @@ -17,10 +15,12 @@ export const GRID_TREE_DATA_GROUP_COL_DEF: Omit<GridColDef, 'field' | 'editable'
align: 'left',
width: 200,
valueGetter: (params) => (params as GridValueGetterFullParams).rowNode.groupingKey,
renderCell: (params) => <GridTreeDataGroupingCell {...params} />,
};

export const GRID_TREE_DATA_GROUP_COL_DEF_FORCED_FIELDS: Pick<GridColDef, 'field' | 'editable'> = {
export const GRID_TREE_DATA_GROUP_COL_DEF_FORCED_PROPERTIES: Pick<
GridColDef,
'field' | 'editable'
> = {
field: '__tree_data_group__',
editable: false,
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@ import { GridApiRef } from '../../../models/api/gridApiRef';
import { GridComponentProps } from '../../../GridComponentProps';
import {
GRID_TREE_DATA_GROUP_COL_DEF,
GRID_TREE_DATA_GROUP_COL_DEF_FORCED_FIELDS,
GRID_TREE_DATA_GROUP_COL_DEF_FORCED_PROPERTIES,
} from './gridTreeDataGroupColDef';
import { useGridApiEventHandler } from '../../utils/useGridApiEventHandler';
import { GridEvents, GridEventListener } from '../../../models/events';
import { GridColDef, GridGroupingColDefOverrideParams } from '../../../models';
import {
GridColDef,
GridGroupingColDefOverride,
GridGroupingColDefOverrideParams,
} from '../../../models';
import { isSpaceKey } from '../../../utils/keyboardUtils';
import { useFirstRender } from '../../utils/useFirstRender';
import { buildRowTree, BuildRowTreeGroupingCriteria } from '../../../utils/tree/buildRowTree';
import { GridRowGroupingPreProcessing } from '../../core/rowGroupsPerProcessing';
import { gridFilteredDescendantCountLookupSelector } from '../filter';
import { GridPreProcessingGroup, useGridRegisterPreProcessor } from '../../core/preProcessing';
import { GridColumnsRawState } from '../columns/gridColumnsState';
import { GridTreeDataGroupingCell } from '../../../components';

const TREE_DATA_GROUPING_NAME = 'tree-data';

Expand All @@ -33,12 +38,7 @@ export const useGridTreeData = (
const groupingColDef = React.useMemo<GridColDef>(() => {
const propGroupingColDef = props.groupingColDef;

const baseColDef: GridColDef = {
...GRID_TREE_DATA_GROUP_COL_DEF,
headerName: apiRef.current.getLocaleText('treeDataGroupingHeaderName'),
...GRID_TREE_DATA_GROUP_COL_DEF_FORCED_FIELDS,
};
let colDefOverride: Partial<GridColDef> | null | undefined;
let colDefOverride: GridGroupingColDefOverride | null | undefined;

if (typeof propGroupingColDef === 'function') {
const params: GridGroupingColDefOverrideParams = {
Expand All @@ -51,9 +51,20 @@ export const useGridTreeData = (
colDefOverride = propGroupingColDef;
}

const { hideDescendantCount, ...colDefOverrideProperties } = colDefOverride ?? {};

const commonProperties: Omit<GridColDef, 'field' | 'editable'> = {
...GRID_TREE_DATA_GROUP_COL_DEF,
renderCell: (params) => (
Copy link
Member

Choose a reason for hiding this comment

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

Why do you not hideDescendantCount to the colDef and use this parameter in the <GridTreeDataGroupingCell /> component like colDef.valueOptions for <GridEditSingleSelectCell />?

Copy link
Member Author

Choose a reason for hiding this comment

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

I wanted to avoid adding properties on the colDef that are only useful for the grouping column.
But if the team feels like it's better to pass it to the colDef then I just have to move hideDescendantCount from GridGroupingColDefOverride to GridColDef and access it through the colDefprop in the cell component.

Copy link
Member

Choose a reason for hiding this comment

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

Make sense, but when defining groupingColDef developers will probably expect their props to be in the colDef

Copy link
Member Author

Choose a reason for hiding this comment

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

For column grouping I have other properties only usefull for the grouping column.
Putting everything on the colDef is not great I think.

<GridTreeDataGroupingCell {...params} hideDescendantCount={hideDescendantCount} />
),
headerName: apiRef.current.getLocaleText('treeDataGroupingHeaderName'),
};

return {
...baseColDef,
...colDefOverride,
...commonProperties,
...colDefOverrideProperties,
...GRID_TREE_DATA_GROUP_COL_DEF_FORCED_PROPERTIES,
};
}, [apiRef, props.groupingColDef]);

Expand Down
8 changes: 7 additions & 1 deletion packages/grid/_modules_/grid/models/colDef/gridColDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,13 @@ export interface GridGroupingColDefOverride
extends Omit<
GridColDef,
'editable' | 'valueSetter' | 'field' | 'preProcessEditCellProps' | 'renderEditCell'
> {}
> {
/**
* If `true`, the grouping cells will not render the amount of descendants.
* @default: false
*/
hideDescendantCount?: boolean;
}

export interface GridGroupingColDefOverrideParams {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,30 @@ describe('<DataGridPro /> - Tree Data', () => {
render(<Test groupingColDef={{ headerName: 'Custom header name' }} />);
expect(getColumnHeadersTextContent()).to.deep.equal(['Custom header name', 'name']);
});

it('should render descendant count when hideDescendantCount = false', () => {
render(
<Test groupingColDef={{ hideDescendantCount: false }} defaultGroupingExpansionDepth={-1} />,
);
expect(getColumnValues(0)).to.deep.equal([
'A (2)',
'A',
'B',
'B (4)',
'A',
'B (2)',
'A (1)',
'A',
'C',
]);
});

it('should not render descendant count when hideDescendantCount = true', () => {
render(
<Test groupingColDef={{ hideDescendantCount: true }} defaultGroupingExpansionDepth={-1} />,
);
expect(getColumnValues(0)).to.deep.equal(['A', 'A', 'B', 'B', 'A', 'B', 'A', 'A', 'C']);
});
});

describe('row grouping column', () => {
Expand Down