Skip to content

Commit

Permalink
[material-ui][SCheckbox] Add slots and slotProps
Browse files Browse the repository at this point in the history
  • Loading branch information
sai6855 committed Jan 8, 2025
1 parent 0221d17 commit 1dcf867
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 15 deletions.
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;
}

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', {
elementType: CheckboxRoot,
ref,
className: clsx(classes.root, className),
ownerState,
externalForwardedProps,
});

return (
<CheckboxRoot
<RootSlot
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

0 comments on commit 1dcf867

Please sign in to comment.