-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
15 changed files
with
254 additions
and
279 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
168 changes: 168 additions & 0 deletions
168
packages/react-components/react-jsx-runtime/src/createElement.test.tsx
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,168 @@ | ||
/* eslint-disable jsdoc/check-tag-names */ | ||
/** @jsxRuntime classic */ | ||
/** @jsxFrag Fragment */ | ||
/** @jsx createElement */ | ||
/* eslint-enable jsdoc/check-tag-names */ | ||
|
||
import { render } from '@testing-library/react'; | ||
import { ComponentProps, ComponentState, Slot, getSlotsNext, resolveShorthand } from '@fluentui/react-utilities'; | ||
import { createElement } from './createElement'; | ||
|
||
describe('createElement', () => { | ||
describe('general behavior tests', () => { | ||
it('handles a string', () => { | ||
const result = render(<div>Hello world</div>); | ||
|
||
expect(result.container.firstChild).toMatchInlineSnapshot(` | ||
<div> | ||
Hello world | ||
</div> | ||
`); | ||
}); | ||
|
||
it('handles an array', () => { | ||
const result = render( | ||
<div> | ||
{Array.from({ length: 3 }, (_, i) => ( | ||
<div key={i}>{i}</div> | ||
))} | ||
</div>, | ||
); | ||
|
||
expect(result.container.firstChild).toMatchInlineSnapshot(` | ||
<div> | ||
<div> | ||
0 | ||
</div> | ||
<div> | ||
1 | ||
</div> | ||
<div> | ||
2 | ||
</div> | ||
</div> | ||
`); | ||
}); | ||
|
||
it('handles an array of children', () => { | ||
const result = render( | ||
<div> | ||
<div>1</div> | ||
<div>2</div> | ||
</div>, | ||
); | ||
|
||
expect(result.container.firstChild).toMatchInlineSnapshot(` | ||
<div> | ||
<div> | ||
1 | ||
</div> | ||
<div> | ||
2 | ||
</div> | ||
</div> | ||
`); | ||
}); | ||
}); | ||
|
||
describe('custom behavior tests', () => { | ||
it('keeps children from "defaultProps" in a render callback', () => { | ||
type TestComponentSlots = { slot: Slot<'div'> }; | ||
type TestComponentState = ComponentState<TestComponentSlots>; | ||
type TestComponentProps = ComponentProps<Partial<TestComponentSlots>>; | ||
|
||
const TestComponent = (props: TestComponentProps) => { | ||
const state: TestComponentState = { | ||
components: { slot: 'div' }, | ||
|
||
slot: resolveShorthand(props.slot, { | ||
defaultProps: { children: 'Default Children', id: 'slot' }, | ||
}), | ||
}; | ||
const { slots, slotProps } = getSlotsNext<TestComponentSlots>(state); | ||
|
||
return <slots.slot {...slotProps.slot} />; | ||
}; | ||
|
||
const children = jest.fn().mockImplementation((Component, props) => ( | ||
<div id="render-fn"> | ||
<Component {...props} /> | ||
</div> | ||
)); | ||
const result = render(<TestComponent slot={{ children }} />); | ||
|
||
expect(children).toHaveBeenCalledTimes(1); | ||
expect(children).toHaveBeenCalledWith('div', { children: 'Default Children', id: 'slot' }); | ||
|
||
expect(result.container.firstChild).toMatchInlineSnapshot(` | ||
<div | ||
id="render-fn" | ||
> | ||
<div | ||
id="slot" | ||
> | ||
Default Children | ||
</div> | ||
</div> | ||
`); | ||
}); | ||
|
||
it('keeps children from a render template in a render callback', () => { | ||
type TestComponentSlots = { outer: Slot<'div'>; inner: Slot<'div'> }; | ||
type TestComponentState = ComponentState<TestComponentSlots>; | ||
type TestComponentProps = ComponentProps<Partial<TestComponentSlots>>; | ||
|
||
const TestComponent = (props: TestComponentProps) => { | ||
const state: TestComponentState = { | ||
components: { inner: 'div', outer: 'div' }, | ||
|
||
inner: resolveShorthand(props.inner, { defaultProps: { id: 'inner' } }), | ||
outer: resolveShorthand(props.outer, { defaultProps: { id: 'outer' } }), | ||
}; | ||
const { slots, slotProps } = getSlotsNext<TestComponentSlots>(state); | ||
|
||
return ( | ||
<slots.outer {...slotProps.outer}> | ||
<slots.inner {...slotProps.inner} /> | ||
</slots.outer> | ||
); | ||
}; | ||
|
||
const children = jest.fn().mockImplementation((Component, props) => ( | ||
<div id="render-fn"> | ||
<Component {...props} /> | ||
</div> | ||
)); | ||
const result = render(<TestComponent outer={{ children }} inner={{ children: 'Inner children' }} />); | ||
|
||
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(` | ||
<React.Fragment> | ||
<div | ||
id="inner" | ||
> | ||
Inner children | ||
</div> | ||
</React.Fragment> | ||
`); | ||
|
||
expect(result.container.firstChild).toMatchInlineSnapshot(` | ||
<div | ||
id="render-fn" | ||
> | ||
<div | ||
id="outer" | ||
> | ||
<div | ||
id="inner" | ||
> | ||
Inner children | ||
</div> | ||
</div> | ||
</div> | ||
`); | ||
}); | ||
}); | ||
}); |
60 changes: 60 additions & 0 deletions
60
packages/react-components/react-jsx-runtime/src/createElement.ts
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,60 @@ | ||
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>; | ||
}; | ||
|
||
export function createElement<P extends {}>( | ||
type: React.ElementType<P>, | ||
props?: P | null, | ||
...children: React.ReactNode[] | ||
): React.ReactElement<P> | null { | ||
if (isSlotComponent(props)) { | ||
const result = normalizeElementArguments(props, children); | ||
|
||
if (result.renderFn) { | ||
return React.createElement( | ||
React.Fragment, | ||
{}, | ||
result.renderFn(type, { ...result.props, children: result.children }), | ||
) as React.ReactElement<P>; | ||
} | ||
} | ||
|
||
return React.createElement(type, props, ...children); | ||
} | ||
|
||
function normalizeElementArguments<Props extends UnknownSlotProps>( | ||
propsWithMetadata: WithMetadata<Props>, | ||
overrideChildren?: React.ReactNode[], | ||
): { | ||
props: Props; | ||
children: React.ReactNode; | ||
renderFn?: SlotRenderFunction<Props>; | ||
} { | ||
const { [SLOT_RENDER_FUNCTION_SYMBOL]: renderFn, ...props } = propsWithMetadata; | ||
if (overrideChildren?.length === 0) { | ||
overrideChildren = undefined; | ||
} | ||
const children: React.ReactNode[] | React.ReactNode = overrideChildren ?? propsWithMetadata.children; | ||
const normalizedChildren: React.ReactNode = Array.isArray(children) | ||
? React.createElement(React.Fragment, {}, ...children) | ||
: children; | ||
if (renderFn) { | ||
const { children: _, ...propsWithoutChildren } = props as UnknownSlotProps; | ||
return { | ||
renderFn, | ||
children: normalizedChildren, | ||
props: propsWithoutChildren as Props, | ||
}; | ||
} | ||
return { | ||
children: normalizedChildren, | ||
props: props as UnknownSlotProps as Props, | ||
}; | ||
} | ||
|
||
export function isSlotComponent<Props extends {}>(props?: Props | null): props is WithMetadata<Props> { | ||
return Boolean(props?.hasOwnProperty(SLOT_RENDER_FUNCTION_SYMBOL)); | ||
} |
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,2 +1,2 @@ | ||
export { jsx } from './jsx'; | ||
export { createElement } from './createElement'; | ||
export { Fragment } from 'react'; |
18 changes: 0 additions & 18 deletions
18
packages/react-components/react-jsx-runtime/src/jsx-dev-runtime.ts
This file was deleted.
Oops, something went wrong.
24 changes: 0 additions & 24 deletions
24
packages/react-components/react-jsx-runtime/src/jsx-runtime.ts
This file was deleted.
Oops, something went wrong.
84 changes: 0 additions & 84 deletions
84
packages/react-components/react-jsx-runtime/src/jsx.test.tsx
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.