perf(Tab): skip rendering empty pane (#7238)

This commit is contained in:
neverland 2020-09-23 18:00:26 +08:00 committed by GitHub
parent 17099d3ac1
commit 68d47fe16a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 2 deletions

View File

@ -55,9 +55,15 @@ export default createComponent({
render(h) { render(h) {
const { slots, parent, isActive } = this; const { slots, parent, isActive } = this;
const shouldRender = this.inited || parent.scrollspy || !parent.lazyRender; const slotContent = slots();
if (!slotContent) {
return;
}
const show = parent.scrollspy || isActive; const show = parent.scrollspy || isActive;
const Content = shouldRender ? slots() : h(); const shouldRender = this.inited || parent.scrollspy || !parent.lazyRender;
const Content = shouldRender ? slotContent : h();
if (parent.animated) { if (parent.animated) {
return ( return (

View File

@ -202,6 +202,22 @@ exports[`name prop 1`] = `
</div> </div>
`; `;
exports[`render empty tab 1`] = `
<div class="van-tabs van-tabs--line">
<div class="van-tabs__wrap">
<div role="tablist" class="van-tabs__nav van-tabs__nav--line">
<div role="tab" aria-selected="true" class="van-tab van-tab--active"><span class="van-tab__text van-tab__text--ellipsis">title1</span></div>
<div role="tab" class="van-tab"><span class="van-tab__text van-tab__text--ellipsis">title2</span></div>
<div class="van-tabs__line"></div>
</div>
</div>
<div class="van-tabs__content">
<!---->
<!---->
</div>
</div>
`;
exports[`render nav-left & nav-right slot 1`] = ` exports[`render nav-left & nav-right slot 1`] = `
<div class="van-tabs van-tabs--line"> <div class="van-tabs van-tabs--line">
<div> <div>

View File

@ -410,3 +410,16 @@ test('before-change prop', async () => {
expect(onChange).toHaveBeenCalledTimes(2); expect(onChange).toHaveBeenCalledTimes(2);
expect(onChange).toHaveBeenLastCalledWith(4, 'title5'); expect(onChange).toHaveBeenLastCalledWith(4, 'title5');
}); });
test('render empty tab', async () => {
const wrapper = mount({
template: `
<van-tabs>
<van-tab title="title1" />
<van-tab title="title2" />
</van-tabs>
`,
});
expect(wrapper).toMatchSnapshot();
});