forked from mui/material-ui
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Base] Improve the return type of useSlotProps (mui#33279)
- Loading branch information
1 parent
06422c2
commit df9a1d3
Showing
7 changed files
with
100 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import * as React from 'react'; | ||
import appendOwnerState from './appendOwnerState'; | ||
|
||
const divProps = appendOwnerState('div', { otherProp: true }, { ownerStateProps: true }); | ||
|
||
// ownerState is not available on a host component | ||
// @ts-expect-error | ||
const test1 = divProps.ownerState.ownerStateProps; | ||
// @ts-expect-error | ||
const test2 = divProps.ownerState?.ownerStateProps; | ||
|
||
const componentProps = appendOwnerState( | ||
() => <div />, | ||
{ otherProp: true }, | ||
{ ownerStateProps: true }, | ||
); | ||
|
||
// ownerState is present on a custom component | ||
const test3: boolean = componentProps.ownerState.ownerStateProps; | ||
|
||
function test(element: React.ElementType) { | ||
const props = appendOwnerState(element, { otherProp: true }, { ownerStateProps: true }); | ||
|
||
// ownerState may be present on a provided element type (it depends on its exact type) | ||
// @ts-expect-error | ||
const test4 = props.ownerState.ownerStateProps; | ||
const test5: boolean | undefined = props.ownerState?.ownerStateProps; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,53 @@ | ||
import { Simplify } from '@mui/types'; | ||
import React from 'react'; | ||
import isHostComponent from './isHostComponent'; | ||
|
||
/** | ||
* Type of the ownerState based on the type of an element it applies to. | ||
* This resolves to the provided OwnerState for React components and `undefined` for host components. | ||
* Falls back to `OwnerState | undefined` when the exact type can't be determined in development time. | ||
*/ | ||
type OwnerStateWhenApplicable< | ||
ElementType extends React.ElementType, | ||
OwnerState, | ||
> = ElementType extends React.ComponentType<any> | ||
? OwnerState | ||
: ElementType extends keyof JSX.IntrinsicElements | ||
? undefined | ||
: OwnerState | undefined; | ||
|
||
export type AppendOwnerStateReturnType< | ||
ElementType extends React.ElementType, | ||
OtherProps, | ||
OwnerState, | ||
> = Simplify< | ||
OtherProps & { | ||
ownerState: OwnerStateWhenApplicable<ElementType, OwnerState>; | ||
} | ||
>; | ||
|
||
/** | ||
* 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` are not applied. | ||
* @param existingProps Props of the element. | ||
* @param elementType Type of the element that owns the `existingProps`. If the element is a DOM node, `ownerState` is not applied. | ||
* @param otherProps Props of the element. | ||
* @param ownerState | ||
*/ | ||
export default function appendOwnerState< | ||
TExistingProps extends Record<string, any>, | ||
TOwnerState extends {}, | ||
ElementType extends React.ElementType, | ||
OtherProps extends Record<string, any>, | ||
OwnerState, | ||
>( | ||
elementType: React.ElementType, | ||
existingProps: TExistingProps = {} as TExistingProps, | ||
ownerState: TOwnerState, | ||
): TExistingProps & { ownerState?: TOwnerState } { | ||
elementType: ElementType, | ||
otherProps: OtherProps = {} as OtherProps, | ||
ownerState: OwnerState, | ||
): AppendOwnerStateReturnType<ElementType, OtherProps, OwnerState> { | ||
if (isHostComponent(elementType)) { | ||
return existingProps; | ||
return otherProps as AppendOwnerStateReturnType<ElementType, OtherProps, OwnerState>; | ||
} | ||
|
||
return { | ||
...existingProps, | ||
ownerState: { ...existingProps.ownerState, ...ownerState }, | ||
}; | ||
...otherProps, | ||
ownerState: { ...otherProps.ownerState, ...ownerState }, | ||
} as AppendOwnerStateReturnType<ElementType, OtherProps, OwnerState>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters