-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[material-ui] Add
mergeSlotProps
for extending components (#44809)
- Loading branch information
1 parent
4bcf3cc
commit ba35dfc
Showing
6 changed files
with
251 additions
and
0 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,64 @@ | ||
import * as React from 'react'; | ||
import { expectType } from '@mui/types'; | ||
import Box from '@mui/material/Box'; | ||
import Tooltip, { TooltipProps } from '@mui/material/Tooltip'; | ||
import { mergeSlotProps, SlotComponentProps } from '@mui/material/utils'; | ||
|
||
// without explicit type | ||
const slotProps = mergeSlotProps(undefined, { className: 'foo', 'aria-label': 'bar' }); | ||
expectType<SlotComponentProps<React.ElementType, {}, {}>, typeof slotProps>(slotProps); | ||
|
||
// explicit external slot props type | ||
const defaultProps = mergeSlotProps<{ foo: string }>(undefined, { foo: 'bar' }); | ||
expectType<{ foo: string }, typeof defaultProps>(defaultProps); | ||
|
||
// explicit slot props type with function | ||
const externalSlotProps = mergeSlotProps< | ||
(ownerState: { foo: string }) => { foo: string }, | ||
{ foo: string } | ||
>(() => ({ foo: 'external' }), { foo: 'default' })({ foo: '' }); | ||
expectType<{ foo: string }, typeof externalSlotProps>(externalSlotProps); | ||
|
||
export const CustomTooltip = (props: TooltipProps) => { | ||
const { children, title } = props; | ||
|
||
return ( | ||
<Tooltip | ||
{...props} | ||
title={<Box sx={{ p: 4 }}>{title}</Box>} | ||
slotProps={{ | ||
...props.slotProps, | ||
popper: mergeSlotProps(props.slotProps?.popper, { | ||
className: 'custom-tooltip', | ||
disablePortal: true, | ||
placement: 'top', | ||
}), | ||
}} | ||
> | ||
{children} | ||
</Tooltip> | ||
); | ||
}; | ||
|
||
export const CustomTooltip2 = (props: TooltipProps) => { | ||
const { children, title } = props; | ||
|
||
// to ensure that the return type of `mergeSlotProps` is correctly inferred | ||
const popperProps = mergeSlotProps(props.slotProps?.popper, { | ||
className: 'custom-tooltip', | ||
disablePortal: true, | ||
placement: 'top', | ||
}); | ||
return ( | ||
<Tooltip | ||
{...props} | ||
title={<Box sx={{ p: 4 }}>{title}</Box>} | ||
slotProps={{ | ||
...props.slotProps, | ||
popper: popperProps, | ||
}} | ||
> | ||
{children} | ||
</Tooltip> | ||
); | ||
}; |
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,99 @@ | ||
import { expect } from 'chai'; | ||
|
||
import mergeSlotProps from './mergeSlotProps'; | ||
|
||
type OwnerState = { | ||
className: string; | ||
'aria-label'?: string; | ||
}; | ||
|
||
describe('utils/index.js', () => { | ||
describe('mergeSlotProps', () => { | ||
it('external slot props is undefined', () => { | ||
expect( | ||
mergeSlotProps<OwnerState>(undefined, { | ||
className: 'default', | ||
'aria-label': 'foo', | ||
}), | ||
).to.deep.equal({ | ||
className: 'default', | ||
'aria-label': 'foo', | ||
}); | ||
}); | ||
|
||
it('external slot props should override', () => { | ||
expect( | ||
mergeSlotProps<OwnerState>( | ||
{ className: 'external', 'aria-label': 'bar' }, | ||
{ className: 'default', 'aria-label': 'foo' }, | ||
), | ||
).to.deep.equal({ | ||
className: 'default external', | ||
'aria-label': 'bar', | ||
}); | ||
}); | ||
|
||
it('external slot props is a function', () => { | ||
expect( | ||
mergeSlotProps<(ownerState: OwnerState) => OwnerState, OwnerState>( | ||
() => ({ | ||
className: 'external', | ||
}), | ||
{ className: 'default', 'aria-label': 'foo' }, | ||
)({ className: '' }), | ||
).to.deep.equal({ | ||
className: 'default external', | ||
'aria-label': 'foo', | ||
}); | ||
}); | ||
|
||
it('default slot props is a function', () => { | ||
expect( | ||
mergeSlotProps<OwnerState, (ownerState: OwnerState) => OwnerState>( | ||
{ | ||
className: 'external', | ||
}, | ||
() => ({ className: 'default', 'aria-label': 'foo' }), | ||
)({ className: 'base' }), | ||
).to.deep.equal({ | ||
className: 'base default external', | ||
'aria-label': 'foo', | ||
}); | ||
}); | ||
|
||
it('both slot props are functions', () => { | ||
expect( | ||
mergeSlotProps<(ownerState: OwnerState) => OwnerState>( | ||
() => ({ | ||
className: 'external', | ||
}), | ||
() => ({ | ||
className: 'default', | ||
'aria-label': 'foo', | ||
}), | ||
)({ className: 'base' }), | ||
).to.deep.equal({ | ||
className: 'base default external', | ||
'aria-label': 'foo', | ||
}); | ||
}); | ||
|
||
it('external callback should be called with default slot props', () => { | ||
expect( | ||
mergeSlotProps<(ownerState: OwnerState) => OwnerState>( | ||
({ 'aria-label': ariaLabel }) => ({ | ||
className: 'external', | ||
'aria-label': ariaLabel === 'foo' ? 'bar' : 'baz', | ||
}), | ||
() => ({ | ||
className: 'default', | ||
'aria-label': 'foo', | ||
}), | ||
)({ className: 'base', 'aria-label': 'unknown' }), | ||
).to.deep.equal({ | ||
className: 'base default external', | ||
'aria-label': 'bar', | ||
}); | ||
}); | ||
}); | ||
}); |
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,43 @@ | ||
import { SlotComponentProps } from '@mui/utils'; | ||
import clsx from 'clsx'; | ||
|
||
export default function mergeSlotProps< | ||
T extends SlotComponentProps<React.ElementType, {}, {}>, | ||
K = T, | ||
// infer external slot props first to provide autocomplete for default slot props | ||
U = T extends Function ? T : K extends Function ? K : T extends undefined ? K : T, | ||
>(externalSlotProps: T | undefined, defaultSlotProps: K): U { | ||
if (!externalSlotProps) { | ||
return defaultSlotProps as unknown as U; | ||
} | ||
if (typeof externalSlotProps === 'function' || typeof defaultSlotProps === 'function') { | ||
return ((ownerState: Record<string, any>) => { | ||
const defaultSlotPropsValue = | ||
typeof defaultSlotProps === 'function' ? defaultSlotProps(ownerState) : defaultSlotProps; | ||
const externalSlotPropsValue = | ||
typeof externalSlotProps === 'function' | ||
? externalSlotProps({ ...ownerState, ...defaultSlotPropsValue }) | ||
: externalSlotProps; | ||
|
||
const className = clsx( | ||
ownerState?.className, | ||
defaultSlotPropsValue?.className, | ||
externalSlotPropsValue?.className, | ||
); | ||
return { | ||
...defaultSlotPropsValue, | ||
...externalSlotPropsValue, | ||
...(!!className && { className }), | ||
}; | ||
}) as U; | ||
} | ||
const className = clsx( | ||
(defaultSlotProps as Record<string, any>)?.className, | ||
externalSlotProps?.className, | ||
); | ||
return { | ||
...defaultSlotProps, | ||
...externalSlotProps, | ||
...(!!className && { className }), | ||
} as U; | ||
} |