-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feature(react-utilities): implements new slot methods (slot and assertSlots)
#28373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bsunderhus
merged 9 commits into
microsoft:master
from
bsunderhus:react-utilities/feature--implements-slot-method
Aug 4, 2023
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
95c371d
feature: implements new slot methods (slot and assertSlots)
bsunderhus b313bab
Update change/@fluentui-react-jsx-runtime-d2a505fc-6ffc-4679-aa63-5b2…
bsunderhus 7e6ae24
Update change/@fluentui-react-utilities-18c147c2-15a4-428f-89a7-04963…
bsunderhus 78e1884
chore: adds tests to slot, isSlot and assertSlots
bsunderhus e8f9e58
chore: updates tests & ensures it doesn't throw in production
bsunderhus 8c1cdb7
chore: removes metadata symbol
bsunderhus dfcc54b
chore: renames SlotComponent to SlotComponentType
bsunderhus a3aa2ea
chore: renames required to renderByDefault
bsunderhus b3e7f76
chore: converts slot to a namespace
bsunderhus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-jsx-runtime-d2a505fc-6ffc-4679-aa63-5b29dcc83f7b.json
This file contains hidden or 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,7 @@ | ||
| { | ||
| "type": "prerelease", | ||
| "comment": "chore: update createElement to support new slot methods", | ||
| "packageName": "@fluentui/react-jsx-runtime", | ||
| "email": "[email protected]", | ||
| "dependentChangeType": "patch" | ||
| } | ||
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-utilities-18c147c2-15a4-428f-89a7-049632ab27fe.json
This file contains hidden or 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,7 @@ | ||
| { | ||
| "type": "minor", | ||
| "comment": "feat: implement new slot methods (slot and assertSlots)", | ||
| "packageName": "@fluentui/react-utilities", | ||
| "email": "[email protected]", | ||
| "dependentChangeType": "patch" | ||
| } |
This file contains hidden or 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
69 changes: 46 additions & 23 deletions
69
packages/react-components/react-jsx-runtime/src/createElement.ts
This file contains hidden or 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,38 +1,61 @@ | ||
| import * as React from 'react'; | ||
| import { SlotRenderFunction, UnknownSlotProps, SLOT_RENDER_FUNCTION_SYMBOL } from '@fluentui/react-utilities'; | ||
|
|
||
| type WithMetadata<Props extends {}> = Props & { | ||
| [SLOT_RENDER_FUNCTION_SYMBOL]: SlotRenderFunction<Props>; | ||
| }; | ||
| import { | ||
| UnknownSlotProps, | ||
| isSlot, | ||
| SlotComponentType, | ||
| SLOT_ELEMENT_TYPE_SYMBOL, | ||
| SLOT_RENDER_FUNCTION_SYMBOL, | ||
| } from '@fluentui/react-utilities'; | ||
|
|
||
| export function createElement<P extends {}>( | ||
| type: React.ElementType<P>, | ||
| props?: P | null, | ||
| ...children: React.ReactNode[] | ||
| ): React.ReactElement<P> | null { | ||
| return hasRenderFunction(props) | ||
| ? createElementFromRenderFunction(type, props, children) | ||
| : React.createElement(type, props, ...children); | ||
| // TODO: | ||
| // this is for backwards compatibility with getSlotsNext | ||
| // it should be removed once getSlotsNext is obsolete | ||
| if (isSlot<P>(props)) { | ||
| return createElementFromSlotComponent( | ||
layershifter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { ...props, [SLOT_ELEMENT_TYPE_SYMBOL]: type } as SlotComponentType<P>, | ||
| children, | ||
| ); | ||
| } | ||
| if (isSlot<P>(type)) { | ||
| return createElementFromSlotComponent(type, children); | ||
| } | ||
| return React.createElement(type, props, ...children); | ||
| } | ||
|
|
||
| function createElementFromRenderFunction<P extends UnknownSlotProps>( | ||
| type: React.ElementType<P>, | ||
| props: WithMetadata<P>, | ||
| function createElementFromSlotComponent<Props extends UnknownSlotProps>( | ||
| type: SlotComponentType<Props>, | ||
| overrideChildren: React.ReactNode[], | ||
| ): React.ReactElement<P> | null { | ||
| const { [SLOT_RENDER_FUNCTION_SYMBOL]: renderFunction, ...renderProps } = props; | ||
| ): React.ReactElement<Props> | null { | ||
| const { | ||
| as, | ||
| [SLOT_ELEMENT_TYPE_SYMBOL]: baseElementType, | ||
| [SLOT_RENDER_FUNCTION_SYMBOL]: renderFunction, | ||
| ...propsWithoutMetadata | ||
| } = type; | ||
| const props = propsWithoutMetadata as UnknownSlotProps as Props; | ||
|
|
||
| if (overrideChildren.length > 0) { | ||
| renderProps.children = React.createElement(React.Fragment, {}, ...overrideChildren); | ||
| const elementType = typeof baseElementType === 'string' ? as ?? baseElementType : baseElementType; | ||
|
|
||
| if (typeof elementType !== 'string' && as) { | ||
| props.as = as; | ||
| } | ||
|
|
||
| return React.createElement( | ||
| React.Fragment, | ||
| {}, | ||
| renderFunction(type, renderProps as UnknownSlotProps as P), | ||
| ) as React.ReactElement<P>; | ||
| } | ||
| if (renderFunction) { | ||
| if (overrideChildren.length > 0) { | ||
| props.children = React.createElement(React.Fragment, {}, ...overrideChildren); | ||
| } | ||
|
|
||
| return React.createElement( | ||
| React.Fragment, | ||
| {}, | ||
| renderFunction(elementType as React.ElementType<Props>, props), | ||
| ) as React.ReactElement<Props>; | ||
| } | ||
|
|
||
| export function hasRenderFunction<Props extends {}>(props?: Props | null): props is WithMetadata<Props> { | ||
| return Boolean(props?.hasOwnProperty(SLOT_RENDER_FUNCTION_SYMBOL)); | ||
| return React.createElement(elementType, props, ...overrideChildren); | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.