Skip to content

Commit

Permalink
add-slots
Browse files Browse the repository at this point in the history
  • Loading branch information
sai6855 committed Nov 27, 2024
1 parent 418e888 commit 8c5161e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
29 changes: 28 additions & 1 deletion packages/mui-material/src/ListItemText/ListItemText.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,36 @@ import { SxProps } from '@mui/system';
import { InternalStandardProps as StandardProps, Theme } from '..';
import { TypographyProps } from '../Typography';
import { ListItemTextClasses } from './listItemTextClasses';
import { CreateSlotsAndSlotProps, SlotProps } from '../utils/types';

export interface ListItemTextSlots {
/**
* The component that renders the main content.
* @default span
*/
primary?: React.ElementType;
/**
* The component that renders the main content.
* @default p
*/
secondary?: React.ElementType;
}

export type ListItemTextSlotsAndSlotProps = CreateSlotsAndSlotProps<
ListItemTextSlots,
{
primary: SlotProps<React.ElementType<HTMLSpanElement>, {}, ListItemTextOwnerState>;
secondary: SlotProps<React.ElementType<HTMLParagraphElement>, {}, ListItemTextOwnerState>;
}
>;

export interface ListItemTextOwnerState extends ListItemTextProps {}

export interface ListItemTextProps<
PrimaryTypographyComponent extends React.ElementType = 'span',
SecondaryTypographyComponent extends React.ElementType = 'p',
> extends StandardProps<React.HTMLAttributes<HTMLDivElement>> {
> extends StandardProps<React.HTMLAttributes<HTMLDivElement>>,
ListItemTextSlotsAndSlotProps {
/**
* Alias for the `primary` prop.
*/
Expand Down Expand Up @@ -37,6 +62,7 @@ export interface ListItemTextProps<
/**
* These props will be forwarded to the primary typography component
* (as long as disableTypography is not `true`).
* @deprecated Use `slotProps.primary` instead. This prop will be removed in v7. See [Migrating from deprecated APIs](/material-ui/migration/migrating-from-deprecated-apis/) for more details.
*/
primaryTypographyProps?: TypographyProps<
PrimaryTypographyComponent,
Expand All @@ -49,6 +75,7 @@ export interface ListItemTextProps<
/**
* These props will be forwarded to the secondary typography component
* (as long as disableTypography is not `true`).
* @deprecated Use `slotProps.secondary` instead. This prop will be removed in v7. See [Migrating from deprecated APIs](/material-ui/migration/migrating-from-deprecated-apis/) for more details.
*/
secondaryTypographyProps?: TypographyProps<
SecondaryTypographyComponent,
Expand Down
37 changes: 30 additions & 7 deletions packages/mui-material/src/ListItemText/ListItemText.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ListContext from '../List/ListContext';
import { styled } from '../zero-styled';
import { useDefaultProps } from '../DefaultPropsProvider';
import listItemTextClasses, { getListItemTextUtilityClass } from './listItemTextClasses';
import useSlot from '../utils/useSlot';

const useUtilityClasses = (ownerState) => {
const { classes, inset, primary, secondary, dense } = ownerState;
Expand Down Expand Up @@ -75,6 +76,8 @@ const ListItemText = React.forwardRef(function ListItemText(inProps, ref) {
primaryTypographyProps,
secondary: secondaryProp,
secondaryTypographyProps,
slots = {},
slotProps = {},
...other
} = props;
const { dense } = React.useContext(ListContext);
Expand All @@ -93,29 +96,49 @@ const ListItemText = React.forwardRef(function ListItemText(inProps, ref) {

const classes = useUtilityClasses(ownerState);

const externalForwardedProps = {
slots,
slotProps: {
primary: primaryTypographyProps,
secondary: secondaryTypographyProps,
...slotProps,
},
};

const [PrimarySlot, primarySlotProps] = useSlot('primary', {
elementType: Typography,
externalForwardedProps,
ownerState,
});
const [SecondarySlot, secondarySlotProps] = useSlot('secondary', {
elementType: Typography,
externalForwardedProps,
ownerState,
});

if (primary != null && primary.type !== Typography && !disableTypography) {
primary = (
<Typography
<PrimarySlot
variant={dense ? 'body2' : 'body1'}
className={classes.primary}
component={primaryTypographyProps?.variant ? undefined : 'span'}
{...primaryTypographyProps}
component={primarySlotProps?.variant ? undefined : 'span'}
{...primarySlotProps}
>
{primary}
</Typography>
</PrimarySlot>
);
}

if (secondary != null && secondary.type !== Typography && !disableTypography) {
secondary = (
<Typography
<SecondarySlot
variant="body2"
className={classes.secondary}
color="textSecondary"
{...secondaryTypographyProps}
{...secondarySlotProps}
>
{secondary}
</Typography>
</SecondarySlot>
);
}

Expand Down

0 comments on commit 8c5161e

Please sign in to comment.