From 02736ee1ebf85dc4877535a150948bf4a1e1935f Mon Sep 17 00:00:00 2001 From: Levent Deniz <32421538+leventdeniz@users.noreply.github.com> Date: Wed, 23 Nov 2022 16:01:57 +0100 Subject: [PATCH] [Base] Allow useSlotProps to receive undefined elementType (#35192) --- packages/mui-base/src/utils/appendOwnerState.test.ts | 9 +++++++++ packages/mui-base/src/utils/appendOwnerState.ts | 6 +++--- packages/mui-base/src/utils/useSlotProps.ts | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/mui-base/src/utils/appendOwnerState.test.ts b/packages/mui-base/src/utils/appendOwnerState.test.ts index f775075b503010..1b5ba88ec0170e 100644 --- a/packages/mui-base/src/utils/appendOwnerState.test.ts +++ b/packages/mui-base/src/utils/appendOwnerState.test.ts @@ -11,6 +11,15 @@ function CustomComponent() { } describe('appendOwnerState', () => { + describe('when the provided elementType is undefined', () => { + it('returns the provided existingProps without modification ', () => { + const existingProps = { className: 'foo' }; + const actual = appendOwnerState(undefined, existingProps, ownerState); + + expect(actual).to.equal(existingProps); + }); + }); + describe('when a DOM element is provided as elementType', () => { it('returns the provided existingProps without modification ', () => { const existingProps = { className: 'foo' }; diff --git a/packages/mui-base/src/utils/appendOwnerState.ts b/packages/mui-base/src/utils/appendOwnerState.ts index 5ecbd67e6ff624..6731ada3d47cbf 100644 --- a/packages/mui-base/src/utils/appendOwnerState.ts +++ b/packages/mui-base/src/utils/appendOwnerState.ts @@ -29,7 +29,7 @@ export type AppendOwnerStateReturnType< /** * Appends the ownerState object to the props, merging with the existing one if necessary. * - * @param elementType Type of the element that owns the `existingProps`. If the element is a DOM node, `ownerState` is not applied. + * @param elementType Type of the element that owns the `existingProps`. If the element is a DOM node or undefined, `ownerState` is not applied. * @param otherProps Props of the element. * @param ownerState */ @@ -38,11 +38,11 @@ export default function appendOwnerState< OtherProps extends Record, OwnerState, >( - elementType: ElementType, + elementType: ElementType | undefined, otherProps: OtherProps, ownerState: OwnerState, ): AppendOwnerStateReturnType { - if (isHostComponent(elementType)) { + if (elementType === undefined || isHostComponent(elementType)) { return otherProps as AppendOwnerStateReturnType; } diff --git a/packages/mui-base/src/utils/useSlotProps.ts b/packages/mui-base/src/utils/useSlotProps.ts index 9fae5f82c75954..7ec2e93501d47e 100644 --- a/packages/mui-base/src/utils/useSlotProps.ts +++ b/packages/mui-base/src/utils/useSlotProps.ts @@ -22,7 +22,7 @@ export type UseSlotPropsParameters< /** * The type of the component used in the slot. */ - elementType: ElementType; + elementType: ElementType | undefined; /** * The `slotProps.*` of the unstyled component. */