-
Notifications
You must be signed in to change notification settings - Fork 2.9k
bugfix(react-jsx-runtime): static children problem on render function #29162
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 1 commit into
microsoft:master
from
bsunderhus:react-jsx-runtime/fix--29144
Sep 20, 2023
Merged
Changes from all commits
Commits
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-6b5ad738-3061-4164-9bd3-1433b8b6a54e.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": "patch", | ||
| "comment": "fix: static children problem on render function", | ||
| "packageName": "@fluentui/react-jsx-runtime", | ||
| "email": "bernardo.sunderhus@gmail.com", | ||
| "dependentChangeType": "patch" | ||
| } |
5 changes: 3 additions & 2 deletions
5
packages/react-components/react-jsx-runtime/src/jsx-dev-runtime.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,6 +1,7 @@ | ||
| import { DevRuntime } from './utils/DevRuntime'; | ||
| import { createJSX } from './jsx/createJSX'; | ||
| import { jsxDEVSlot } from './jsx/jsxDEVSlot'; | ||
| import { DevRuntime } from './utils/DevRuntime'; | ||
|
|
||
| export { Fragment } from 'react'; | ||
|
|
||
| export const jsxDEV = createJSX(DevRuntime.jsxDEV); | ||
| export const jsxDEV = createJSX(DevRuntime.jsxDEV, jsxDEVSlot); |
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 |
|---|---|---|
|
|
@@ -266,20 +266,29 @@ describe('createElement with assertSlots', () => { | |
| }); | ||
|
|
||
| it('keeps children from a render template in a render callback', () => { | ||
| type TestComponentSlots = { outer: NonNullable<Slot<'div'>>; inner: NonNullable<Slot<'div'>> }; | ||
| const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => { | ||
| /* noop */ | ||
| }); | ||
| type TestComponentSlots = { | ||
| outer: NonNullable<Slot<'div'>>; | ||
| inner1: NonNullable<Slot<'div'>>; | ||
| inner2: NonNullable<Slot<'div'>>; | ||
| }; | ||
| type TestComponentState = ComponentState<TestComponentSlots>; | ||
| type TestComponentProps = ComponentProps<Partial<TestComponentSlots>>; | ||
|
|
||
| const TestComponent = (props: TestComponentProps) => { | ||
| const state: TestComponentState = { | ||
| components: { outer: 'div', inner: 'div' }, | ||
| inner: slot.always(props.inner, { defaultProps: { id: 'inner' }, elementType: 'div' }), | ||
| components: { outer: 'div', inner1: 'div', inner2: 'div' }, | ||
| inner1: slot.always(props.inner1, { defaultProps: { id: 'inner-1' }, elementType: 'div' }), | ||
| inner2: slot.always(props.inner2, { defaultProps: { id: 'inner-2' }, elementType: 'div' }), | ||
| outer: slot.always(props.outer, { defaultProps: { id: 'outer' }, elementType: 'div' }), | ||
| }; | ||
| assertSlots<TestComponentSlots>(state); | ||
| return ( | ||
| <state.outer> | ||
| <state.inner /> | ||
| <state.inner1 /> | ||
| <state.inner2 /> | ||
| </state.outer> | ||
| ); | ||
| }; | ||
|
|
@@ -289,17 +298,33 @@ describe('createElement with assertSlots', () => { | |
| <Component {...props} /> | ||
| </div> | ||
| )); | ||
| const result = render(<TestComponent outer={{ children }} inner={{ children: 'Inner children' }} />); | ||
| const result = render( | ||
| <TestComponent | ||
| outer={{ children }} | ||
| inner1={{ children: 'Inner children 1' }} | ||
| inner2={{ children: 'Inner children 2' }} | ||
| />, | ||
| ); | ||
|
|
||
| // your test code here | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cruft? |
||
| expect(errorSpy).not.toHaveBeenCalled(); | ||
| errorSpy.mockRestore(); | ||
| expect(children).toHaveBeenCalledTimes(1); | ||
| expect(children.mock.calls[0][0]).toBe('div'); | ||
| expect(children.mock.calls[0][1].id).toBe('outer'); | ||
| expect(children.mock.calls[0][1].children).toMatchInlineSnapshot(` | ||
| <div | ||
| id="inner" | ||
| > | ||
| Inner children | ||
| </div> | ||
| <React.Fragment> | ||
| <div | ||
| id="inner-1" | ||
| > | ||
| Inner children 1 | ||
| </div> | ||
| <div | ||
| id="inner-2" | ||
| > | ||
| Inner children 2 | ||
| </div> | ||
| </React.Fragment> | ||
| `); | ||
|
|
||
| expect(result.container.firstChild).toMatchInlineSnapshot(` | ||
|
|
@@ -310,9 +335,14 @@ describe('createElement with assertSlots', () => { | |
| id="outer" | ||
| > | ||
| <div | ||
| id="inner" | ||
| id="inner-1" | ||
| > | ||
| Inner children | ||
| Inner children 1 | ||
| </div> | ||
| <div | ||
| id="inner-2" | ||
| > | ||
| Inner children 2 | ||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
||
8 changes: 5 additions & 3 deletions
8
packages/react-components/react-jsx-runtime/src/jsx-runtime.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,7 +1,9 @@ | ||
| import { Runtime } from './utils/Runtime'; | ||
| import { createJSX } from './jsx/createJSX'; | ||
| import { jsxSlot } from './jsx/jsxSlot'; | ||
| import { jsxsSlot } from './jsx/jsxsSlot'; | ||
| import { Runtime } from './utils/Runtime'; | ||
|
|
||
| export { Fragment } from 'react'; | ||
|
|
||
| export const jsx = createJSX(Runtime.jsx); | ||
| export const jsxs = createJSX(Runtime.jsxs); | ||
| export const jsx = createJSX(Runtime.jsx, jsxSlot); | ||
| export const jsxs = createJSX(Runtime.jsxs, jsxsSlot); |
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
48 changes: 48 additions & 0 deletions
48
packages/react-components/react-jsx-runtime/src/jsx/jsxDEVSlot.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 |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import * as React from 'react'; | ||
| import { SlotComponentType, UnknownSlotProps } from '@fluentui/react-utilities'; | ||
| import { getMetadataFromSlotComponent } from '../utils/getMetadataFromSlotComponent'; | ||
| import { DevRuntime } from '../utils/DevRuntime'; | ||
|
|
||
| export const jsxDEVSlot = <Props extends UnknownSlotProps>( | ||
| type: SlotComponentType<Props>, | ||
| overrideProps: Props | null, | ||
| key?: React.Key, | ||
| source?: unknown, | ||
|
bsunderhus marked this conversation as resolved.
|
||
| self?: unknown, | ||
| ): React.ReactElement<Props> => { | ||
| const { elementType, renderFunction, props: slotProps } = getMetadataFromSlotComponent(type); | ||
|
|
||
| const props: Props = { ...slotProps, ...overrideProps }; | ||
|
|
||
| if (renderFunction) { | ||
| // if runtime is static | ||
| if (source === true) { | ||
| return DevRuntime.jsxDEV( | ||
| React.Fragment, | ||
| { | ||
| children: renderFunction(elementType, { | ||
| ...props, | ||
| /** | ||
| * If the runtime is static then children is an array and this array won't be keyed. | ||
| * Then we should wrap children by a static fragment | ||
| * as there's no way to know if renderFunction will render statically or dynamically | ||
| */ | ||
| children: DevRuntime.jsxDEV(React.Fragment, { children: props.children }, undefined, true, self), | ||
| }), | ||
| }, | ||
| key, | ||
| false, // by marking source as false we're declaring that this render is dynamic | ||
| self, | ||
| ) as React.ReactElement<Props>; | ||
| } | ||
| // if runtime is dynamic (source = false) things are simpler | ||
| return DevRuntime.jsxDEV( | ||
| React.Fragment, | ||
| { children: renderFunction(elementType, props) }, | ||
| key, | ||
| source, | ||
| self, | ||
| ) as React.ReactElement<Props>; | ||
| } | ||
| return DevRuntime.jsxDEV(elementType, props, key, source, self); | ||
| }; | ||
23 changes: 23 additions & 0 deletions
23
packages/react-components/react-jsx-runtime/src/jsx/jsxSlot.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 |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import * as React from 'react'; | ||
| import { SlotComponentType, UnknownSlotProps } from '@fluentui/react-utilities'; | ||
| import { getMetadataFromSlotComponent } from '../utils/getMetadataFromSlotComponent'; | ||
| import { Runtime } from '../utils/Runtime'; | ||
|
|
||
| export const jsxSlot = <Props extends UnknownSlotProps>( | ||
| type: SlotComponentType<Props>, | ||
| overrideProps: Props | null, | ||
| key?: React.Key, | ||
| ): React.ReactElement<Props> => { | ||
| const { elementType, renderFunction, props: slotProps } = getMetadataFromSlotComponent(type); | ||
|
|
||
| const props: Props = { ...slotProps, ...overrideProps }; | ||
|
|
||
| if (renderFunction) { | ||
| return Runtime.jsx( | ||
| React.Fragment, | ||
| { children: renderFunction(elementType, props) }, | ||
| key, | ||
| ) as React.ReactElement<Props>; | ||
| } | ||
| return Runtime.jsx(elementType, props, key); | ||
| }; |
33 changes: 33 additions & 0 deletions
33
packages/react-components/react-jsx-runtime/src/jsx/jsxsSlot.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 |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import * as React from 'react'; | ||
| import { SlotComponentType, UnknownSlotProps } from '@fluentui/react-utilities'; | ||
| import { getMetadataFromSlotComponent } from '../utils/getMetadataFromSlotComponent'; | ||
| import { Runtime } from '../utils/Runtime'; | ||
|
|
||
| export const jsxsSlot = <Props extends UnknownSlotProps>( | ||
| type: SlotComponentType<Props>, | ||
| overrideProps: Props | null, | ||
| key?: React.Key, | ||
| ): React.ReactElement<Props> => { | ||
| const { elementType, renderFunction, props: slotProps } = getMetadataFromSlotComponent(type); | ||
|
|
||
| const props: Props = { ...slotProps, ...overrideProps }; | ||
|
|
||
| if (renderFunction) { | ||
| /** | ||
| * In static runtime then children is an array and this array won't be keyed. | ||
| * We should wrap children by a static fragment | ||
| * as there's no way to know if renderFunction will render statically or dynamically | ||
| */ | ||
| return Runtime.jsx( | ||
| React.Fragment, | ||
| { | ||
| children: renderFunction(elementType, { | ||
| ...props, | ||
| children: Runtime.jsxs(React.Fragment, { children: props.children }, undefined), | ||
| }), | ||
| }, | ||
| key, | ||
| ) as React.ReactElement<Props>; | ||
| } | ||
| return Runtime.jsxs(elementType, props, key); | ||
| }; |
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
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.