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

[material-ui][Checkbox] Add slots and slotProps #44974

Open
wants to merge 3 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
22 changes: 16 additions & 6 deletions docs/pages/material-ui/api/checkbox.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
},
"default": "'medium'"
},
"slotProps": {
"type": { "name": "shape", "description": "{ root?: func<br>&#124;&nbsp;object }" },
"default": "{}"
},
"slots": {
"type": { "name": "shape", "description": "{ root?: elementType }" },
"default": "{}"
},
"sx": {
"type": {
"name": "union",
Expand All @@ -48,6 +56,14 @@
"import Checkbox from '@mui/material/Checkbox';",
"import { Checkbox } from '@mui/material';"
],
"slots": [
{
"name": "root",
"description": "The component that renders the root slot.",
"default": "SwitchBase",
"class": "MuiCheckbox-root"
}
],
"classes": [
{
"key": "checked",
Expand Down Expand Up @@ -79,12 +95,6 @@
"description": "State class applied to the root element if `indeterminate={true}`.",
"isGlobal": false
},
{
"key": "root",
"className": "MuiCheckbox-root",
"description": "Class name applied to the root element.",
"isGlobal": false
},
{
"key": "sizeMedium",
"className": "MuiCheckbox-sizeMedium",
Expand Down
6 changes: 4 additions & 2 deletions docs/translations/api-docs/checkbox/checkbox.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
"size": {
"description": "The size of the component. <code>small</code> is equivalent to the dense checkbox styling."
},
"slotProps": { "description": "The props used for each slot inside." },
"slots": { "description": "The components used for each slot inside." },
"sx": {
"description": "The system prop that allows defining system overrides as well as additional CSS styles."
},
Expand Down Expand Up @@ -69,7 +71,6 @@
"nodeName": "the root element",
"conditions": "<code>indeterminate={true}</code>"
},
"root": { "description": "Class name applied to the root element." },
"sizeMedium": {
"description": "State class applied to {{nodeName}} if {{conditions}}.",
"nodeName": "the root element",
Expand All @@ -80,5 +81,6 @@
"nodeName": "the root element",
"conditions": "<code>size=\"small\"</code>"
}
}
},
"slotDescriptions": { "root": "The component that renders the root slot." }
}
37 changes: 35 additions & 2 deletions packages/mui-material/src/Checkbox/Checkbox.d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,49 @@
import * as React from 'react';
import { SxProps } from '@mui/system';
import { OverridableStringUnion } from '@mui/types';
import { InternalStandardProps as StandardProps, Theme } from '..';
import {
CreateSlotsAndSlotProps,
SlotProps,
InternalStandardProps as StandardProps,
Theme,
} from '..';
import { SwitchBaseProps } from '../internal/SwitchBase';
import { CheckboxClasses } from './checkboxClasses';

export interface CheckboxPropsSizeOverrides {}

export interface CheckboxPropsColorOverrides {}

export interface CheckboxRootSlotPropsOverrides {}

export interface CheckboxSlots {
/**
* The component that renders the root slot.
* @default SwitchBase
*/
root: React.ElementType;
}
Comment on lines +19 to +25
Copy link
Member

Choose a reason for hiding this comment

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

The checkbox should have slots.input too. If you track back to SwitchBase, it has SwitchBaseRoot and SwitchBaseInput.

User should be able to do this:

<Checkbox slotProps={{
  root: { className: '...' },
  input: { className: '...' },
}} />

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@siriwatknp I'm bit confused on how to handle slots.input here, since input slot is not passed to SwitchBase from Checkbox but only inputProps are passed.


export type CheckboxSlotsAndSlotProps = CreateSlotsAndSlotProps<
CheckboxSlots,
{
/**
* Props forwarded to the root slot.
* By default, the available props are based on the SwitchBase.
*/
root: SlotProps<
React.ElementType<SwitchBaseProps>,
CheckboxRootSlotPropsOverrides,
CheckboxOwnerState
>;
}
>;

export interface CheckboxOwnerState extends CheckboxProps {}

export interface CheckboxProps
extends StandardProps<SwitchBaseProps, 'checkedIcon' | 'color' | 'icon' | 'type'> {
extends StandardProps<SwitchBaseProps, 'checkedIcon' | 'color' | 'icon' | 'type'>,
CheckboxSlotsAndSlotProps {
/**
* If `true`, the component is checked.
*/
Expand Down
38 changes: 33 additions & 5 deletions packages/mui-material/src/Checkbox/Checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import memoTheme from '../utils/memoTheme';
import createSimplePaletteValueFilter from '../utils/createSimplePaletteValueFilter';

import { useDefaultProps } from '../DefaultPropsProvider';
import useSlot from '../utils/useSlot';

const useUtilityClasses = (ownerState) => {
const { classes, indeterminate, color, size } = ownerState;
Expand Down Expand Up @@ -123,6 +124,8 @@ const Checkbox = React.forwardRef(function Checkbox(inProps, ref) {
size = 'medium',
disableRipple = false,
className,
slots = {},
slotProps = {},
...other
} = props;

Expand All @@ -139,8 +142,22 @@ const Checkbox = React.forwardRef(function Checkbox(inProps, ref) {

const classes = useUtilityClasses(ownerState);

const externalForwardedProps = {
...other,
slots,
slotProps,
};

const [RootSlot, rootProps] = useSlot('root', {
Copy link
Member

Choose a reason for hiding this comment

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

What's the runtime cost of this for the component?

elementType: CheckboxRoot,
ref,
className: clsx(classes.root, className),
ownerState,
externalForwardedProps,
});

return (
<CheckboxRoot
<RootSlot
Copy link
Member

@siriwatknp siriwatknp Jan 9, 2025

Choose a reason for hiding this comment

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

Does it make sense to deprecate inputProps in favor of slotProps.input? cc @DiegoAndai

I mean deprecate the inputProps of Checkbox, but still passing slotProps.input to <RootSlot inputProps={slotPropss.input} /> because SwitchBase is an internal component, no need to update it.

type="checkbox"
inputProps={{
'data-indeterminate': indeterminate,
Expand All @@ -152,11 +169,8 @@ const Checkbox = React.forwardRef(function Checkbox(inProps, ref) {
checkedIcon={React.cloneElement(indeterminateIcon, {
fontSize: indeterminateIcon.props.fontSize ?? size,
})}
ownerState={ownerState}
ref={ref}
className={clsx(classes.root, className)}
disableRipple={disableRipple}
{...other}
{...rootProps}
classes={classes}
/>
);
Expand Down Expand Up @@ -259,6 +273,20 @@ Checkbox.propTypes /* remove-proptypes */ = {
PropTypes.oneOf(['medium', 'small']),
PropTypes.string,
]),
/**
* The props used for each slot inside.
* @default {}
*/
slotProps: PropTypes.shape({
root: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
}),
/**
* The components used for each slot inside.
* @default {}
*/
slots: PropTypes.shape({
root: PropTypes.elementType,
}),
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
Expand Down
6 changes: 6 additions & 0 deletions packages/mui-material/src/Checkbox/Checkbox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ describe('<Checkbox />', () => {
testStateOverrides: { prop: 'color', value: 'secondary', styleKey: 'colorSecondary' },
refInstanceof: window.HTMLSpanElement,
skip: ['componentProp', 'componentsProp', 'rootClass'],
slots: {
root: {
testWithElement: null,
expectedClassName: classes.root,
},
},
}));

it('should have the classes required for Checkbox', () => {
Expand Down
Loading