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

[DataGrid] Refactor: create base Popper #16362

Merged
merged 24 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from 9 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
2 changes: 1 addition & 1 deletion docs/data/data-grid/filtering/CustomFilterPanelPosition.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default function CustomFilterPanelPosition() {
slots={{ toolbar: CustomToolbar }}
slotProps={{
panel: {
anchorEl: filterButtonEl,
target: filterButtonEl,
},
toolbar: { setFilterButtonEl },
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function CustomFilterPanelPosition() {
slots={{ toolbar: CustomToolbar }}
slotProps={{
panel: {
anchorEl: filterButtonEl,
target: filterButtonEl,
},
toolbar: { setFilterButtonEl },
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
slots={{ toolbar: CustomToolbar }}
slotProps={{
panel: {
anchorEl: filterButtonEl,
target: filterButtonEl,
},
toolbar: { setFilterButtonEl },
}}
Expand Down
119 changes: 82 additions & 37 deletions packages/x-data-grid/src/components/menu/GridMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import ClickAwayListener, { ClickAwayListenerProps } from '@mui/material/ClickAwayListener';
import {
unstable_composeClasses as composeClasses,
unstable_useEnhancedEffect as useEnhancedEffect,
HTMLElementType,
} from '@mui/utils';
import Grow, { GrowProps } from '@mui/material/Grow';
import Paper from '@mui/material/Paper';
import Popper, { PopperProps } from '@mui/material/Popper';
import { styled } from '@mui/material/styles';
import { PopperProps } from '../../models/gridBaseSlots';
import { GridBaseSlots } from '../../models/gridSlotsComponent';
import { getDataGridUtilityClass, gridClasses } from '../../constants/gridClasses';
import type { DataGridProcessedProps } from '../../models/props/DataGridProps';
import { useGridRootProps } from '../../hooks/utils/useGridRootProps';
Expand Down Expand Up @@ -43,10 +41,9 @@ const useUtilityClasses = (ownerState: OwnerState) => {
return composeClasses(slots, getDataGridUtilityClass, classes);
};

const GridMenuRoot = styled(Popper, {
const GridMenuRoot = styled('div' as unknown as GridBaseSlots['basePopper'], {
romgrk marked this conversation as resolved.
Show resolved Hide resolved
name: 'MuiDataGrid',
slot: 'Menu',
overridesResolver: (_, styles) => styles.menu,
})<{ ownerState: OwnerState }>(({ theme }) => ({
zIndex: theme.zIndex.modal,
[`& .${gridClasses.menuList}`]: {
Expand All @@ -59,15 +56,9 @@ export interface GridMenuProps extends Omit<PopperProps, 'onKeyDown' | 'children
target: HTMLElement | null;
onClose: (event?: Event) => void;
position?: MenuPosition;
onExited?: GrowProps['onExited'];
children: React.ReactNode;
}

const transformOrigin = {
'bottom-start': 'top left',
'bottom-end': 'top right',
};

function GridMenu(props: GridMenuProps) {
const { open, target, onClose, children, position, className, onExited, ...other } = props;
const apiRef = useGridApiContext();
Expand All @@ -91,17 +82,7 @@ function GridMenu(props: GridMenuProps) {
apiRef.current.publishEvent(eventName, { target });
}, [apiRef, open, target]);

const handleExited = (popperOnExited: (() => void) | undefined) => (node: HTMLElement) => {
if (popperOnExited) {
popperOnExited();
}

if (onExited) {
onExited(node);
}
};

const handleClickAway: ClickAwayListenerProps['onClickAway'] = (event) => {
const handleClickAway = (event: MouseEvent) => {
if (event.target && (target === event.target || target?.contains(event.target as Node))) {
return;
}
Expand All @@ -114,23 +95,16 @@ function GridMenu(props: GridMenuProps) {
className={clsx(classes.root, className)}
ownerState={rootProps}
open={open}
anchorEl={target as any}
target={target as any}
transition
placement={position}
onClickAway={handleClickAway}
onExited={onExited}
clickAwayMouseEvent="onMouseDown"
{...other}
{...rootProps.slotProps?.basePopper}
>
{({ TransitionProps, placement }) => (
<ClickAwayListener onClickAway={handleClickAway} mouseEvent="onMouseDown">
<Grow
{...TransitionProps}
style={{ transformOrigin: transformOrigin[placement as keyof typeof transformOrigin] }}
onExited={handleExited(TransitionProps?.onExited)}
>
<Paper>{children}</Paper>
</Grow>
</ClickAwayListener>
)}
{children}
</GridMenuRoot>
);
}
Expand All @@ -141,12 +115,82 @@ GridMenu.propTypes = {
// | To update them edit the TypeScript types and run "pnpm proptypes" |
// ----------------------------------------------------------------------
children: PropTypes.node,
className: PropTypes.string,
clickAwayMouseEvent: PropTypes.oneOf([
'onClick',
'onMouseDown',
'onMouseUp',
'onPointerDown',
'onPointerUp',
false,
]),
clickAwayTouchEvent: PropTypes.oneOf(['onTouchEnd', 'onTouchStart', false]),
flip: PropTypes.bool,
focusTrap: PropTypes.bool,
focusTrapEnabled: PropTypes.bool,
id: PropTypes.string,
onClickAway: PropTypes.shape({
'__@hasInstance@654': PropTypes.func.isRequired,
'__@metadata@656': PropTypes.any,
apply: PropTypes.func.isRequired,
arguments: PropTypes.any.isRequired,
bind: PropTypes.func.isRequired,
call: PropTypes.func.isRequired,
caller: PropTypes.object.isRequired,
length: PropTypes.number.isRequired,
name: PropTypes.string.isRequired,
prototype: PropTypes.any.isRequired,
toString: PropTypes.func.isRequired,
}),
onClose: PropTypes.func.isRequired,
onDidMount: PropTypes.shape({
'__@hasInstance@654': PropTypes.func.isRequired,
'__@metadata@656': PropTypes.any,
apply: PropTypes.func.isRequired,
arguments: PropTypes.any.isRequired,
bind: PropTypes.func.isRequired,
call: PropTypes.func.isRequired,
caller: PropTypes.object.isRequired,
length: PropTypes.number.isRequired,
name: PropTypes.string.isRequired,
prototype: PropTypes.any.isRequired,
toString: PropTypes.func.isRequired,
}),
onDidUnmount: PropTypes.shape({
'__@hasInstance@654': PropTypes.func.isRequired,
'__@metadata@656': PropTypes.any,
apply: PropTypes.func.isRequired,
arguments: PropTypes.any.isRequired,
bind: PropTypes.func.isRequired,
call: PropTypes.func.isRequired,
caller: PropTypes.object.isRequired,
length: PropTypes.number.isRequired,
name: PropTypes.string.isRequired,
prototype: PropTypes.any.isRequired,
toString: PropTypes.func.isRequired,
}),
onExited: PropTypes.func,
open: PropTypes.bool.isRequired,
/**
* If `true`, the component is shown.
* @default 'bottom'
*/
open: PropTypes.bool.isRequired,
placement: PropTypes.oneOf([
'auto-end',
'auto-start',
'auto',
'bottom-end',
'bottom-start',
'bottom',
'left-end',
'left-start',
'left',
'right-end',
'right-start',
'right',
'top-end',
'top-start',
'top',
]),
position: PropTypes.oneOf([
'bottom-end',
'bottom-start',
Expand All @@ -162,6 +206,7 @@ GridMenu.propTypes = {
'top',
]),
target: HTMLElementType,
transition: PropTypes.bool,
} as any;

export { GridMenu };
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import { GridPanelWrapper, GridPanelWrapperProps } from './GridPanelWrapper';
import { useGridRootProps } from '../../hooks/utils/useGridRootProps';

Expand All @@ -14,12 +13,4 @@ function GridColumnsPanel(props: GridColumnsPanelProps) {
);
}

GridColumnsPanel.propTypes = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the TypeScript types and run "pnpm proptypes" |
// ----------------------------------------------------------------------
slotProps: PropTypes.object,
} as any;

export { GridColumnsPanel };
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { GridPanel } from '@mui/x-data-grid';
function MyPanel() {
return (
<div>
<GridPanel classes={{ paper: 'paper' }} open modifiers={[{ name: 'flip', enabled: false }]} />
<GridPanel classes={{ paper: 'paper' }} open flip={false} />
{/* @ts-expect-error foo classes doesn't exist */}
<GridPanel classes={{ foo: 'foo' }} />
</div>
Expand Down
romgrk marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('<GridPanel />', () => {
);
}

describeConformance(<GridPanel disablePortal open />, () => ({
describeConformance(<GridPanel open />, () => ({
classes: classes as any,
inheritComponent: Popper,
muiName: 'MuiGridPanel',
Expand Down
Loading
Loading