-
-
Notifications
You must be signed in to change notification settings - Fork 92
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
Add support for mixed rendering #50
Open
Saegrov
wants to merge
4
commits into
preactjs:main
Choose a base branch
from
Saegrov:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -48,12 +48,27 @@ renderToString.render = renderToString; | |
let shallowRender = (vnode, context) => renderToString(vnode, context, SHALLOW); | ||
|
||
|
||
/** | ||
* Only render elements, leaving Components inline as `<ComponentName ... />` | ||
* except those specified in fullyRenderedComponents. | ||
* This method is just a convenience alias for `render(vnode, context, { shallow:true, alwaysRenderedComponented: [] })` | ||
* @param {VNode} vnode JSX VNode to render. | ||
* @param {Array} alwaysRenderedComponents List of components that should be rendered with shallow rendering | ||
* @param {Object} [context={}] Optionally pass an initial context object through the render path. | ||
*/ | ||
let mixedRender = (vnode, alwaysRenderedComponents = [], context) => { | ||
const opts = Object.assign({ alwaysRenderedComponents }, SHALLOW); | ||
return renderToString(vnode, context, opts); | ||
}; | ||
|
||
|
||
/** The default export is an alias of `render()`. */ | ||
export default function renderToString(vnode, context, opts, inner, isSvgMode) { | ||
let { nodeName, attributes, children } = vnode || EMPTY, | ||
isComponent = false; | ||
context = context || {}; | ||
opts = opts || {}; | ||
opts.alwaysRenderedComponents = opts.alwaysRenderedComponents || []; | ||
|
||
let pretty = opts.pretty, | ||
indentChar = typeof pretty==='string' ? pretty : '\t'; | ||
|
@@ -69,9 +84,12 @@ export default function renderToString(vnode, context, opts, inner, isSvgMode) { | |
|
||
// components | ||
if (typeof nodeName==='function') { | ||
let componentName = getComponentName(nodeName); | ||
isComponent = true; | ||
if (opts.shallow && (inner || opts.renderRootComponent===false)) { | ||
nodeName = getComponentName(nodeName); | ||
if (opts.shallow && | ||
(inner || opts.renderRootComponent===false) && | ||
!opts.alwaysRenderedComponents.includes(componentName)) { | ||
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. Needs to use |
||
nodeName = componentName; | ||
} | ||
else { | ||
let props = getNodeProps(vnode), | ||
|
@@ -230,10 +248,12 @@ function getFallbackComponentName(component) { | |
return name; | ||
} | ||
renderToString.shallowRender = shallowRender; | ||
renderToString.mixedRender = mixedRender; | ||
|
||
|
||
export { | ||
renderToString as render, | ||
renderToString, | ||
shallowRender | ||
shallowRender, | ||
mixedRender | ||
}; |
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,84 @@ | ||
import { mixedRender } from '../src'; | ||
import { h, Component } from 'preact'; | ||
import chai, { expect } from 'chai'; | ||
import { spy, match } from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
chai.use(sinonChai); | ||
|
||
describe('mixedRender()', () => { | ||
it('should not render nested components when not white listed', () => { | ||
let Test = spy( ({ foo, children }) => <div bar={foo}><b>test child</b>{ children }</div> ); | ||
Test.displayName = 'Test'; | ||
|
||
let rendered = mixedRender( | ||
<section> | ||
<Test foo={1}><span>asdf</span></Test> | ||
</section> | ||
); | ||
|
||
expect(rendered).to.equal(`<section><Test foo="1"><span>asdf</span></Test></section>`); | ||
expect(Test).not.to.have.been.called; | ||
}); | ||
|
||
it('should always render root component', () => { | ||
let Test = spy( ({ foo, children }) => <div bar={foo}><b>test child</b>{ children }</div> ); | ||
Test.displayName = 'Test'; | ||
|
||
let rendered = mixedRender( | ||
<Test foo={1}> | ||
<span>asdf</span> | ||
</Test> | ||
); | ||
|
||
expect(rendered).to.equal(`<div bar="1"><b>test child</b><span>asdf</span></div>`); | ||
expect(Test).to.have.been.calledOnce; | ||
}); | ||
|
||
it('should render nested components when they are white listed', () => { | ||
let Test = spy( ({ foo, children }) => <div bar={foo}><b>test child</b>{ children }</div> ); | ||
Test.displayName = 'Test'; | ||
|
||
let rendered = mixedRender( | ||
<section> | ||
<Test foo={1}><span>asdf</span></Test> | ||
</section> | ||
, ['Test']); | ||
|
||
expect(rendered).to.equal(`<section><div bar="1"><b>test child</b><span>asdf</span></div></section>`); | ||
expect(Test).to.have.been.called; | ||
}); | ||
|
||
it('should not render nested components inside a whitelisted component', () => { | ||
let Test = spy( ({ foo, children }) => <div bar={foo}><b>test child</b>{ children }</div> ); | ||
let Ignored = spy( ({ title, children }) => <h2>This {title} should not be rendered</h2> ); | ||
Test.displayName = 'Test'; | ||
Ignored.displayName = 'Ignored'; | ||
|
||
let rendered = mixedRender( | ||
<section> | ||
<Test foo={1}><Ignored title={'FooBarTitle'}/></Test> | ||
</section> | ||
, ['Test']); | ||
|
||
expect(rendered).to.equal(`<section><div bar="1"><b>test child</b><Ignored title="FooBarTitle"></Ignored></div></section>`); | ||
expect(Test).to.have.been.called; | ||
expect(Ignored).to.not.have.been.called; | ||
}); | ||
|
||
it('should render deeply nested components when all are white listed', () => { | ||
let Test = spy( ({ foo, children }) => <div bar={foo}><b>test child</b>{ children }</div> ); | ||
let Ignored = spy( ({ title, children }) => <h2>This {title} should be rendered</h2> ); | ||
Test.displayName = 'Test'; | ||
Ignored.displayName = 'Ignored'; | ||
|
||
let rendered = mixedRender( | ||
<section> | ||
<Test foo={1}><Ignored title={'FooBarTitle'}/></Test> | ||
</section> | ||
, ['Test', 'Ignored']); | ||
|
||
expect(rendered).to.equal(`<section><div bar="1"><b>test child</b><h2>This FooBarTitle should be rendered</h2></div></section>`); | ||
expect(Test).to.have.been.called; | ||
expect(Ignored).to.have.been.called; | ||
}); | ||
}); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is size-constrained - would it be alright to add the
alwaysRenderedComponents
option, but not export another method (mixedRender
)? That would keep things reasonably minimal.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! I'll update the PR.