From 8c5161e897272bc6c687c76c4c72b8c47c6ca9d6 Mon Sep 17 00:00:00 2001 From: sai6855 Date: Wed, 27 Nov 2024 15:55:16 +0530 Subject: [PATCH] add-slots --- .../src/ListItemText/ListItemText.d.ts | 29 ++++++++++++++- .../src/ListItemText/ListItemText.js | 37 +++++++++++++++---- 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/packages/mui-material/src/ListItemText/ListItemText.d.ts b/packages/mui-material/src/ListItemText/ListItemText.d.ts index f99f467ec6b74c..e58a1443de4e57 100644 --- a/packages/mui-material/src/ListItemText/ListItemText.d.ts +++ b/packages/mui-material/src/ListItemText/ListItemText.d.ts @@ -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, {}, ListItemTextOwnerState>; + secondary: SlotProps, {}, ListItemTextOwnerState>; + } +>; + +export interface ListItemTextOwnerState extends ListItemTextProps {} export interface ListItemTextProps< PrimaryTypographyComponent extends React.ElementType = 'span', SecondaryTypographyComponent extends React.ElementType = 'p', -> extends StandardProps> { +> extends StandardProps>, + ListItemTextSlotsAndSlotProps { /** * Alias for the `primary` prop. */ @@ -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, @@ -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, diff --git a/packages/mui-material/src/ListItemText/ListItemText.js b/packages/mui-material/src/ListItemText/ListItemText.js index 08358c5877ad8e..69520cb666e1c0 100644 --- a/packages/mui-material/src/ListItemText/ListItemText.js +++ b/packages/mui-material/src/ListItemText/ListItemText.js @@ -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; @@ -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); @@ -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 = ( - {primary} - + ); } if (secondary != null && secondary.type !== Typography && !disableTypography) { secondary = ( - {secondary} - + ); }