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 button props #15930

Merged
merged 15 commits into from
Jan 3, 2025
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import * as React from 'react';
import { IconButtonProps } from '@mui/material/IconButton';
import { GridSlotProps } from '@mui/x-data-grid';
import { useGridRootProps } from '../../hooks/utils/useGridRootProps';

interface GridHeaderFilterClearIconProps extends IconButtonProps {}
type BaseIconButtonProps = GridSlotProps['baseIconButton'];

const sx = { padding: '2px' };
// FIXME(v8:romgrk): Make parametric
interface GridHeaderFilterClearIconProps extends BaseIconButtonProps {}

const style = { padding: '2px' };

function GridHeaderFilterClearButton(props: GridHeaderFilterClearIconProps) {
const rootProps = useGridRootProps();
Expand All @@ -13,7 +16,7 @@ function GridHeaderFilterClearButton(props: GridHeaderFilterClearIconProps) {
tabIndex={-1}
aria-label="Clear filter"
size="small"
sx={sx}
style={style}
{...props}
{...rootProps.slotProps?.baseIconButton}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { refType, unstable_useId as useId } from '@mui/utils';
import { gridHeaderFilteringMenuSelector } from '@mui/x-data-grid/internals';
import { useGridRootProps } from '../../hooks/utils/useGridRootProps';

const sx = {
const style = {
width: 22,
height: 22,
margin: 'auto 0 10px 5px',
Expand Down Expand Up @@ -69,7 +69,7 @@ function GridHeaderFilterMenuContainer(props: {
tabIndex={-1}
size="small"
onClick={handleClick}
sx={sx}
style={style}
disabled={disabled}
{...rootProps.slotProps?.baseIconButton}
>
Expand Down
11 changes: 7 additions & 4 deletions packages/x-data-grid/src/components/cell/GridActionsCellItem.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import { IconButtonProps } from '@mui/material/IconButton';
import { MenuItemProps } from '@mui/material/MenuItem';
import { GridSlotProps } from '../../models/gridSlotsComponentsProps';
import { useGridRootProps } from '../../hooks/utils/useGridRootProps';

interface GridActionsCellItemCommonProps {
Expand All @@ -11,9 +10,13 @@ interface GridActionsCellItemCommonProps {
component?: React.ElementType;
}

// FIXME(v8:romgrk): Make parametric
export type GridActionsCellItemProps = GridActionsCellItemCommonProps &
(
| ({ showInMenu?: false; icon: React.ReactElement } & Omit<IconButtonProps, 'component'>)
| ({ showInMenu?: false; icon: React.ReactElement } & Omit<
GridSlotProps['baseIconButton'],
'component'
>)
| ({
showInMenu: true;
/**
Expand All @@ -22,7 +25,7 @@ export type GridActionsCellItemProps = GridActionsCellItemCommonProps &
*/
closeMenuOnClick?: boolean;
closeMenu?: () => void;
} & Omit<MenuItemProps, 'component'>)
} & Omit<GridSlotProps['baseMenuItem'], 'component'>)
);

const GridActionsCellItem = React.forwardRef<HTMLElement, GridActionsCellItemProps>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ function GridColumnHeaderFilterIconButton(props: ColumnHeaderFilterIconButtonPro
<rootProps.slots.baseIconButton
id={labelId}
onClick={toggleFilter}
color="default"
aria-label={apiRef.current.getLocaleText('columnHeaderFiltersLabel')}
size="small"
tabIndex={-1}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,15 @@ function GridColumnsManagement(props: GridColumnsManagementProps) {
<rootProps.slots.baseIconButton
aria-label={apiRef.current.getLocaleText('columnsManagementDeleteIconLabel')}
size="small"
sx={[
style={
searchValue
? {
visibility: 'visible',
}
: {
visibility: 'hidden',
},
]}
}
}
tabIndex={-1}
onClick={handleSearchReset}
{...rootProps.slotProps?.baseIconButton}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,15 @@ function GridToolbarQuickFilter(props: GridToolbarQuickFilterProps) {
aria-label={apiRef.current.getLocaleText('toolbarQuickFilterDeleteIconLabel')}
size="small"
edge="end"
sx={[
style={
searchValue
? {
visibility: 'visible',
}
: {
visibility: 'hidden',
},
]}
}
}
onClick={handleSearchReset}
{...rootProps.slotProps?.baseIconButton}
>
Expand Down
28 changes: 27 additions & 1 deletion packages/x-data-grid/src/models/gridBaseSlots.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
type Ref = React.RefCallback<HTMLElement> | React.RefObject<HTMLElement> | null;

export type BadgeProps = {
badgeContent?: React.ReactNode;
children: React.ReactNode;
color?: 'primary' | 'default' | 'error';
invisible?: boolean;
overlap?: 'circular';
variant?: 'dot';
invisible?: boolean;
style?: React.CSSProperties;
};

export type ButtonProps = {
ref?: Ref;
children?: React.ReactNode;
className?: string;
disabled?: boolean;
id?: string;
onClick?: React.MouseEventHandler<HTMLElement>;
onKeyDown?: React.KeyboardEventHandler<HTMLElement>;
role?: string;
size?: 'small' | 'medium' | 'large';
startIcon?: React.ReactNode;
style?: React.CSSProperties;
tabIndex?: number;
title?: string;
touchRippleRef?: any; // FIXME(v8:romgrk): find a way to remove
};

export type IconButtonProps = Omit<ButtonProps, 'startIcon'> & {
color?: 'default' | 'primary';
edge?: 'start' | 'end' | false;
Copy link
Member

Choose a reason for hiding this comment

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

There are quite a few props for buttons that don't make sense in a non-Material UI world, including this, but I think we can fine tune these later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean edge? Yeah I guess so, though if we remove it I'm sure it's going to cause a visual regression somewhere. I'm still wondering how we're going to add those little tunings while being design-system agnostic.

};

export type DividerProps = {};
Expand All @@ -20,4 +45,5 @@ export type MenuItemProps = {
iconEnd?: React.ReactNode;
selected?: boolean;
value?: number | string | readonly string[];
style?: React.CSSProperties;
};
14 changes: 11 additions & 3 deletions packages/x-data-grid/src/models/gridSlotsComponentsProps.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as React from 'react';
import type { BadgeProps as MUIBadgeProps } from '@mui/material/Badge';
import type { ButtonProps as MUIButtonProps } from '@mui/material/Button';
import type { CheckboxProps } from '@mui/material/Checkbox';
import type { MenuListProps } from '@mui/material/MenuList';
import type { MenuItemProps as MUIMenuItemProps } from '@mui/material/MenuItem';
import type { TextFieldProps } from '@mui/material/TextField';
import type { FormControlProps } from '@mui/material/FormControl';
import type { SelectProps } from '@mui/material/Select';
import type { SwitchProps } from '@mui/material/Switch';
import type { ButtonProps } from '@mui/material/Button';
import type { IconButtonProps } from '@mui/material/IconButton';
import type { IconButtonProps as MUIIconButtonProps } from '@mui/material/IconButton';
import type { InputAdornmentProps } from '@mui/material/InputAdornment';
import type { TooltipProps } from '@mui/material/Tooltip';
import type { InputLabelProps } from '@mui/material/InputLabel';
Expand All @@ -33,7 +33,13 @@ import type { GridColumnsManagementProps } from '../components/columnsManagement
import type { GridLoadingOverlayProps } from '../components/GridLoadingOverlay';
import type { GridRowCountProps } from '../components/GridRowCount';
import type { GridColumnHeaderSortIconProps } from '../components/columnHeaders/GridColumnHeaderSortIcon';
import type { BadgeProps, DividerProps, MenuItemProps } from './gridBaseSlots';
import type {
BadgeProps,
ButtonProps,
DividerProps,
IconButtonProps,
MenuItemProps,
} from './gridBaseSlots';

type RootProps = React.HTMLAttributes<HTMLDivElement> & Record<`data-${string}`, string>;
type MainProps = React.HTMLAttributes<HTMLDivElement> & Record<`data-${string}`, string>;
Expand Down Expand Up @@ -102,6 +108,8 @@ interface BaseSlotProps {

interface MaterialSlotProps {
baseBadge: MUIBadgeProps;
baseButton: MUIButtonProps;
baseIconButton: MUIIconButtonProps;
baseMenuItem: MUIMenuItemProps;
}

Expand Down
Loading