chore(Tab): optimize implementation of title slot (#4974)

This commit is contained in:
neverland 2019-11-10 11:25:05 +08:00 committed by GitHub
parent 945f6a1781
commit 76cdb03962
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 36 deletions

View File

@ -41,20 +41,10 @@ export default createComponent({
} }
}, },
mounted() {
if (this.slots('title')) {
this.parent.renderTitle(this.$refs.title, this.index);
}
},
render(h) { render(h) {
const { slots, isActive } = this; const { slots, isActive } = this;
const shouldRender = this.inited || !this.parent.lazyRender; const shouldRender = this.inited || !this.parent.lazyRender;
const Content = [shouldRender ? slots() : h()]; const Content = shouldRender ? slots() : h();
if (slots('title')) {
Content.push(<div ref="title">{slots('title')}</div>);
}
if (this.parent.animated) { if (this.parent.animated) {
return ( return (

View File

@ -194,10 +194,10 @@ exports[`renders demo correctly 1`] = `
<div class="van-tabs van-tabs--line"> <div class="van-tabs van-tabs--line">
<div class="van-tabs__wrap van-hairline--top-bottom"> <div class="van-tabs__wrap van-hairline--top-bottom">
<div role="tablist" class="van-tabs__nav van-tabs__nav--line"> <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-ellipsis"><div><i class="van-icon van-icon-more-o"><!----></i>标签 <div role="tab" aria-selected="true" class="van-tab van-tab--active"><span class="van-ellipsis"><i class="van-icon van-icon-more-o"><!----></i>标签
</div></span></div> </span></div>
<div role="tab" class="van-tab"><span class="van-ellipsis"><div><i class="van-icon van-icon-more-o"><!----></i>标签 <div role="tab" class="van-tab"><span class="van-ellipsis"><i class="van-icon van-icon-more-o"><!----></i>标签
</div></span></div> </span></div>
<div class="van-tabs__line" style="width: 0px; transform: translateX(0px) translateX(-50%);"></div> <div class="van-tabs__line" style="width: 0px; transform: translateX(0px) translateX(-50%);"></div>
</div> </div>
</div> </div>

View File

@ -1,8 +1,8 @@
import { createNamespace } from '../utils'; import { createNamespace } from '../utils';
const bem = createNamespace('tab')[1]; const [createComponent, bem] = createNamespace('tab');
export default { export default createComponent({
props: { props: {
type: String, type: String,
color: String, color: String,
@ -51,12 +51,6 @@ export default {
methods: { methods: {
onClick() { onClick() {
this.$emit('click'); this.$emit('click');
},
renderTitle(el) {
const { title } = this.$refs;
title.innerHTML = '';
title.appendChild(el);
} }
}, },
@ -73,10 +67,10 @@ export default {
style={this.style} style={this.style}
onClick={this.onClick} onClick={this.onClick}
> >
<span ref="title" class={{ 'van-ellipsis': this.ellipsis }}> <span class={{ 'van-ellipsis': this.ellipsis }}>
{this.title} {this.slots() || this.title}
</span> </span>
</div> </div>
); );
} }
}; });

View File

@ -203,7 +203,11 @@ export default createComponent({
this.$emit('input', this.currentName); this.$emit('input', this.currentName);
if (shouldEmitChange) { if (shouldEmitChange) {
this.$emit('change', this.currentName, this.children[currentIndex].title); this.$emit(
'change',
this.currentName,
this.children[currentIndex].title
);
} }
} }
}, },
@ -246,13 +250,6 @@ export default createComponent({
scrollLeftTo(nav, to, immediate ? 0 : this.duration); scrollLeftTo(nav, to, immediate ? 0 : this.duration);
}, },
// render title slot of child tab
renderTitle(el, index) {
this.$nextTick(() => {
this.$refs.titles[index].renderTitle(el);
});
},
onScroll(params) { onScroll(params) {
this.stickyFixed = params.isFixed; this.stickyFixed = params.isFixed;
this.$emit('scroll', params); this.$emit('scroll', params);
@ -276,6 +273,9 @@ export default createComponent({
activeColor={this.titleActiveColor} activeColor={this.titleActiveColor}
inactiveColor={this.titleInactiveColor} inactiveColor={this.titleInactiveColor}
swipeThreshold={this.swipeThreshold} swipeThreshold={this.swipeThreshold}
scopedSlots={{
default: () => item.slots('title')
}}
onClick={() => { onClick={() => {
this.onClick(index); this.onClick(index);
route(item.$router, item); route(item.$router, item);
@ -291,10 +291,17 @@ export default createComponent({
{ [BORDER_TOP_BOTTOM]: type === 'line' && this.border } { [BORDER_TOP_BOTTOM]: type === 'line' && this.border }
]} ]}
> >
<div ref="nav" role="tablist" class={bem('nav', [type])} style={this.navStyle}> <div
ref="nav"
role="tablist"
class={bem('nav', [type])}
style={this.navStyle}
>
{this.slots('nav-left')} {this.slots('nav-left')}
{Nav} {Nav}
{type === 'line' && <div class={bem('line')} style={this.lineStyle} />} {type === 'line' && (
<div class={bem('line')} style={this.lineStyle} />
)}
{this.slots('nav-right')} {this.slots('nav-right')}
</div> </div>
</div> </div>