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 Input #16443

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions docs/pages/x/api/data-grid/data-grid-premium.json
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,12 @@
"default": "IconButton",
"class": null
},
{
"name": "baseInput",
"description": "The custom Input component used in the grid.",
"default": "Input",
"class": null
},
{
"name": "baseTooltip",
"description": "The custom Tooltip component used in the grid.",
Expand Down
6 changes: 6 additions & 0 deletions docs/pages/x/api/data-grid/data-grid-pro.json
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,12 @@
"default": "IconButton",
"class": null
},
{
"name": "baseInput",
"description": "The custom Input component used in the grid.",
"default": "Input",
"class": null
},
{
"name": "baseTooltip",
"description": "The custom Tooltip component used in the grid.",
Expand Down
6 changes: 6 additions & 0 deletions docs/pages/x/api/data-grid/data-grid.json
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,12 @@
"default": "IconButton",
"class": null
},
{
"name": "baseInput",
"description": "The custom Input component used in the grid.",
"default": "Input",
"class": null
},
{
"name": "baseTooltip",
"description": "The custom Tooltip component used in the grid.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,7 @@
"baseDivider": "The custom Divider component used in the grid.",
"baseFormControl": "The custom FormControl component used in the grid.",
"baseIconButton": "The custom IconButton component used in the grid.",
"baseInput": "The custom Input component used in the grid.",
"baseInputAdornment": "The custom InputAdornment component used in the grid.",
"baseInputLabel": "The custom InputLabel component used in the grid.",
"baseLinearProgress": "The custom LinearProgress component used in the grid.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,7 @@
"baseDivider": "The custom Divider component used in the grid.",
"baseFormControl": "The custom FormControl component used in the grid.",
"baseIconButton": "The custom IconButton component used in the grid.",
"baseInput": "The custom Input component used in the grid.",
"baseInputAdornment": "The custom InputAdornment component used in the grid.",
"baseInputLabel": "The custom InputLabel component used in the grid.",
"baseLinearProgress": "The custom LinearProgress component used in the grid.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,7 @@
"baseDivider": "The custom Divider component used in the grid.",
"baseFormControl": "The custom FormControl component used in the grid.",
"baseIconButton": "The custom IconButton component used in the grid.",
"baseInput": "The custom Input component used in the grid.",
"baseInputAdornment": "The custom InputAdornment component used in the grid.",
"baseInputLabel": "The custom InputLabel component used in the grid.",
"baseLinearProgress": "The custom LinearProgress component used in the grid.",
Expand Down
26 changes: 17 additions & 9 deletions packages/x-data-grid/src/components/cell/GridEditDateCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import {
unstable_composeClasses as composeClasses,
unstable_useEnhancedEffect as useEnhancedEffect,
} from '@mui/utils';
import InputBase, { InputBaseProps } from '@mui/material/InputBase';
import { styled } from '@mui/material/styles';
import { NotRendered } from '../../utils/assert';
import { GridSlotProps } from '../../models/gridSlotsComponent';
import { GridRenderEditCellParams } from '../../models/params/gridCellParams';
import { getDataGridUtilityClass } from '../../constants/gridClasses';
import { useGridRootProps } from '../../hooks/utils/useGridRootProps';
Expand All @@ -14,7 +15,7 @@ import { useGridApiContext } from '../../hooks/utils/useGridApiContext';

type OwnerState = { classes: DataGridProcessedProps['classes'] };

const StyledInputBase = styled(InputBase)({
const StyledInputBase = styled(NotRendered<GridSlotProps['baseInput']>)({
fontSize: 'inherit',
});

Expand All @@ -28,9 +29,7 @@ const useUtilityClasses = (ownerState: OwnerState) => {
return composeClasses(slots, getDataGridUtilityClass, classes);
};

export interface GridEditDateCellProps
extends GridRenderEditCellParams,
Omit<InputBaseProps, 'id' | 'value' | 'tabIndex'> {
export interface GridEditDateCellProps extends GridRenderEditCellParams {
/**
* Callback called when the value is changed by the user.
* @param {React.ChangeEvent<HTMLInputElement>} event The event source of the callback.
Expand All @@ -41,6 +40,9 @@ export interface GridEditDateCellProps
event: React.ChangeEvent<HTMLInputElement>,
newValue: Date | null,
) => Promise<void> | void;
slotProps?: {
root?: Partial<GridSlotProps['baseInput']>;
};
}

function GridEditDateCell(props: GridEditDateCellProps) {
Expand All @@ -61,6 +63,7 @@ function GridEditDateCell(props: GridEditDateCellProps) {
isValidating,
isProcessingProps,
onValueChange,
slotProps,
...other
} = props;

Expand Down Expand Up @@ -152,17 +155,21 @@ function GridEditDateCell(props: GridEditDateCellProps) {
}, [hasFocus]);
return (
<StyledInputBase
as={rootProps.slots.baseInput}
inputRef={inputRef}
fullWidth
className={classes.root}
type={isDateTime ? 'datetime-local' : 'date'}
inputProps={{
max: isDateTime ? '9999-12-31T23:59' : '9999-12-31',
...inputProps,
}}
value={valueState.formatted}
onChange={handleChange}
{...other}
{...slotProps?.root}
slotProps={{
htmlInput: {
max: isDateTime ? '9999-12-31T23:59' : '9999-12-31',
...slotProps?.root?.slotProps?.htmlInput,
},
}}
/>
);
}
Expand Down Expand Up @@ -222,6 +229,7 @@ GridEditDateCell.propTypes = {
* The node of the row that the current cell belongs to.
*/
rowNode: PropTypes.object.isRequired,
slotProps: PropTypes.object,
/**
* the tabIndex value.
*/
Expand Down
17 changes: 11 additions & 6 deletions packages/x-data-grid/src/components/cell/GridEditInputCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import {
unstable_useEnhancedEffect as useEnhancedEffect,
} from '@mui/utils';
import { styled } from '@mui/material/styles';
import InputBase, { InputBaseProps } from '@mui/material/InputBase';
import { forwardRef } from '@mui/x-internals/forwardRef';
import { NotRendered } from '../../utils/assert';
import { GridSlotProps } from '../../models/gridSlotsComponent';
import { GridRenderEditCellParams } from '../../models/params/gridCellParams';
import { getDataGridUtilityClass } from '../../constants/gridClasses';
import { useGridRootProps } from '../../hooks/utils/useGridRootProps';
Expand All @@ -25,10 +26,9 @@ const useUtilityClasses = (ownerState: OwnerState) => {
return composeClasses(slots, getDataGridUtilityClass, classes);
};

const GridEditInputCellRoot = styled(InputBase, {
const GridEditInputCellRoot = styled(NotRendered<GridSlotProps['baseInput']>, {
name: 'MuiDataGrid',
slot: 'EditInputCell',
overridesResolver: (props, styles) => styles.editInputCell,
})<{ ownerState: OwnerState }>(({ theme }) => ({
...theme.typography.body2,
padding: '1px 0',
Expand All @@ -38,9 +38,7 @@ const GridEditInputCellRoot = styled(InputBase, {
},
}));

export interface GridEditInputCellProps
extends GridRenderEditCellParams,
Omit<InputBaseProps, 'id' | 'value' | 'tabIndex' | 'ref'> {
export interface GridEditInputCellProps extends GridRenderEditCellParams {
debounceMs?: number;
/**
* Callback called when the value is changed by the user.
Expand All @@ -52,6 +50,9 @@ export interface GridEditInputCellProps
event: React.ChangeEvent<HTMLInputElement>,
newValue: string,
) => Promise<void> | void;
slotProps?: {
root?: Partial<GridSlotProps['baseInput']>;
};
}

const GridEditInputCell = forwardRef<HTMLInputElement, GridEditInputCellProps>((props, ref) => {
Expand All @@ -74,6 +75,7 @@ const GridEditInputCell = forwardRef<HTMLInputElement, GridEditInputCellProps>((
debounceMs = 200,
isProcessingProps,
onValueChange,
slotProps,
...other
} = props;

Expand Down Expand Up @@ -122,6 +124,7 @@ const GridEditInputCell = forwardRef<HTMLInputElement, GridEditInputCellProps>((

return (
<GridEditInputCellRoot
as={rootProps.slots.baseInput}
inputRef={inputRef}
className={classes.root}
ownerState={rootProps}
Expand All @@ -133,6 +136,7 @@ const GridEditInputCell = forwardRef<HTMLInputElement, GridEditInputCellProps>((
isProcessingProps ? <rootProps.slots.loadIcon fontSize="small" color="action" /> : undefined
}
{...other}
{...slotProps?.root}
ref={ref}
/>
);
Expand Down Expand Up @@ -194,6 +198,7 @@ GridEditInputCell.propTypes = {
* The node of the row that the current cell belongs to.
*/
rowNode: PropTypes.object.isRequired,
slotProps: PropTypes.object,
/**
* the tabIndex value.
*/
Expand Down
7 changes: 7 additions & 0 deletions packages/x-data-grid/src/material/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import MUICheckbox from '@mui/material/Checkbox';
import MUIChip from '@mui/material/Chip';
import MUICircularProgress from '@mui/material/CircularProgress';
import MUIDivider from '@mui/material/Divider';
import MUIInputBase from '@mui/material/InputBase';
import MUILinearProgress from '@mui/material/LinearProgress';
import MUIListItemIcon from '@mui/material/ListItemIcon';
import MUIListItemText from '@mui/material/ListItemText';
Expand Down Expand Up @@ -96,6 +97,7 @@ const baseSlots: GridBaseSlots = {
baseCheckbox: MUICheckbox,
baseCircularProgress: MUICircularProgress,
baseDivider: MUIDivider,
baseInput: BaseInput,
baseLinearProgress: MUILinearProgress,
baseMenuList: MUIMenuList,
baseMenuItem: BaseMenuItem,
Expand Down Expand Up @@ -149,3 +151,8 @@ function BaseTextField(props: GridSlotProps['baseTextField']) {
/>
);
}

function BaseInput(props: GridSlotProps['baseInput']) {
const { slotProps, ...rest } = props;
return <MUIInputBase {...rest} inputProps={slotProps?.htmlInput} />;
}
16 changes: 16 additions & 0 deletions packages/x-data-grid/src/models/gridBaseSlots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ export type CircularProgressProps = {

export type LinearProgressProps = {};

export type InputProps = {
ref?: React.Ref<HTMLInputElement>;
inputRef?: React.Ref<HTMLInputElement>;
className?: string;
fullWidth?: boolean;
type?: React.HTMLInputTypeAttribute;
value?: string;
onChange: React.ChangeEventHandler;
disabled?: boolean;
endAdornment?: React.ReactNode;
startAdornment?: React.ReactNode;
slotProps?: {
htmlInput?: React.InputHTMLAttributes<HTMLInputElement>;
};
};

export type SkeletonProps = {
variant?: 'circular' | 'text';
width?: number | string;
Expand Down
5 changes: 5 additions & 0 deletions packages/x-data-grid/src/models/gridSlotsComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ export interface GridBaseSlots {
* @default IconButton
*/
baseIconButton: React.JSXElementConstructor<GridSlotProps['baseIconButton']>;
/**
* The custom Input component used in the grid.
* @default Input
*/
baseInput: React.JSXElementConstructor<GridSlotProps['baseInput']>;
/**
* The custom Tooltip component used in the grid.
* @default Tooltip
Expand Down
3 changes: 3 additions & 0 deletions packages/x-data-grid/src/models/gridSlotsComponentsProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import type {
CircularProgressProps,
DividerProps,
IconButtonProps,
InputProps,
LinearProgressProps,
MenuItemProps,
SkeletonProps,
Expand Down Expand Up @@ -67,6 +68,7 @@ export interface BaseIconButtonPropsOverrides {}
export interface BaseInputAdornmentPropsOverrides {}
export interface BaseTooltipPropsOverrides {}
export interface BasePopperPropsOverrides {}
export interface BaseInputPropsOverrides {}
export interface BaseInputLabelPropsOverrides {}
export interface BaseSelectOptionPropsOverrides {}
export interface BaseSkeletonPropsOverrides {}
Expand Down Expand Up @@ -108,6 +110,7 @@ interface BaseSlotProps {
baseIconButton: IconButtonProps & BaseIconButtonPropsOverrides;
basePopper: PopperProps & BasePopperPropsOverrides;
baseTooltip: TooltipProps & BaseTooltipPropsOverrides;
baseInput: InputProps & BaseInputPropsOverrides;
baseInputLabel: InputLabelProps & BaseInputLabelPropsOverrides;
baseInputAdornment: InputAdornmentProps & BaseInputAdornmentPropsOverrides;
baseSelectOption: {
Expand Down
1 change: 1 addition & 0 deletions scripts/x-data-grid-premium.exports.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{ "name": "BaseIconButtonPropsOverrides", "kind": "Interface" },
{ "name": "BaseInputAdornmentPropsOverrides", "kind": "Interface" },
{ "name": "BaseInputLabelPropsOverrides", "kind": "Interface" },
{ "name": "BaseInputPropsOverrides", "kind": "Interface" },
{ "name": "BaseLinearProgressPropsOverrides", "kind": "Interface" },
{ "name": "BaseMenuItemPropsOverrides", "kind": "Interface" },
{ "name": "BaseMenuListPropsOverrides", "kind": "Interface" },
Expand Down
1 change: 1 addition & 0 deletions scripts/x-data-grid-pro.exports.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{ "name": "BaseIconButtonPropsOverrides", "kind": "Interface" },
{ "name": "BaseInputAdornmentPropsOverrides", "kind": "Interface" },
{ "name": "BaseInputLabelPropsOverrides", "kind": "Interface" },
{ "name": "BaseInputPropsOverrides", "kind": "Interface" },
{ "name": "BaseLinearProgressPropsOverrides", "kind": "Interface" },
{ "name": "BaseMenuItemPropsOverrides", "kind": "Interface" },
{ "name": "BaseMenuListPropsOverrides", "kind": "Interface" },
Expand Down
1 change: 1 addition & 0 deletions scripts/x-data-grid.exports.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{ "name": "BaseIconButtonPropsOverrides", "kind": "Interface" },
{ "name": "BaseInputAdornmentPropsOverrides", "kind": "Interface" },
{ "name": "BaseInputLabelPropsOverrides", "kind": "Interface" },
{ "name": "BaseInputPropsOverrides", "kind": "Interface" },
{ "name": "BaseLinearProgressPropsOverrides", "kind": "Interface" },
{ "name": "BaseMenuItemPropsOverrides", "kind": "Interface" },
{ "name": "BaseMenuListPropsOverrides", "kind": "Interface" },
Expand Down