refactor(TabsTitle): refactor with composition api

This commit is contained in:
chenjiahan 2020-09-21 17:16:49 +08:00
parent 8ca7219acc
commit 9279965cc1
2 changed files with 31 additions and 28 deletions

View File

@ -1,3 +1,4 @@
import { computed } from 'vue';
import { createNamespace, isDef } from '../utils'; import { createNamespace, isDef } from '../utils';
import Badge from '../badge'; import Badge from '../badge';
@ -16,20 +17,27 @@ export default createComponent({
activeColor: String, activeColor: String,
renderTitle: Function, renderTitle: Function,
inactiveColor: String, inactiveColor: String,
swipeThreshold: [Number, String],
}, },
computed: { setup(props) {
style() { const style = computed(() => {
const style = {}; const style: Record<string, string> = {};
const { color, isActive } = this; const {
const isCard = this.type === 'card'; type,
color,
disabled,
isActive,
activeColor,
inactiveColor,
} = props;
const isCard = type === 'card';
// card theme color // card theme color
if (color && isCard) { if (color && isCard) {
style.borderColor = color; style.borderColor = color;
if (!this.disabled) { if (!disabled) {
if (isActive) { if (isActive) {
style.backgroundColor = color; style.backgroundColor = color;
} else { } else {
@ -38,50 +46,46 @@ export default createComponent({
} }
} }
const titleColor = isActive ? this.activeColor : this.inactiveColor; const titleColor = isActive ? activeColor : inactiveColor;
if (titleColor) { if (titleColor) {
style.color = titleColor; style.color = titleColor;
} }
return style; return style;
}, });
},
methods: { const renderText = () => {
genText() {
const Text = ( const Text = (
<span class={bem('text', { ellipsis: !this.scrollable })}> <span class={bem('text', { ellipsis: !props.scrollable })}>
{this.renderTitle ? this.renderTitle() : this.title} {props.renderTitle ? props.renderTitle() : props.title}
</span> </span>
); );
if (this.dot || (isDef(this.badge) && this.badge !== '')) { if (props.dot || (isDef(props.badge) && props.badge !== '')) {
return ( return (
<span class={bem('text-wrapper')}> <span class={bem('text-wrapper')}>
{Text} {Text}
{<Badge dot={this.dot} badge={this.badge} />} {<Badge dot={props.dot} badge={props.badge} />}
</span> </span>
); );
} }
return Text; return Text;
}, };
},
render() { return () => (
return (
<div <div
role="tab" role="tab"
class={[ class={[
bem({ bem({
active: this.isActive, active: props.isActive,
disabled: this.disabled, disabled: props.disabled,
}), }),
]} ]}
style={this.style} style={style.value}
aria-selected={this.isActive} aria-selected={props.isActive}
> >
{this.genText()} {renderText()}
</div> </div>
); );
}, },

View File

@ -20,8 +20,8 @@ import { ParentMixin } from '../mixins/relation';
import { BindEventMixin } from '../mixins/bind-event'; import { BindEventMixin } from '../mixins/bind-event';
// Components // Components
import Title from './Title';
import Sticky from '../sticky'; import Sticky from '../sticky';
import TabsTitle from './TabsTitle';
import TabsContent from './TabsContent'; import TabsContent from './TabsContent';
const [createComponent, bem] = createNamespace('tabs'); const [createComponent, bem] = createNamespace('tabs');
@ -360,7 +360,7 @@ export default createComponent({
const Nav = this.children.map((item, index) => { const Nav = this.children.map((item, index) => {
return ( return (
<Title <TabsTitle
ref={(val) => { ref={(val) => {
this.titleRefs[index] = val; this.titleRefs[index] = val;
}} }}
@ -376,7 +376,6 @@ export default createComponent({
renderTitle={item.$slots.title} renderTitle={item.$slots.title}
activeColor={this.titleActiveColor} activeColor={this.titleActiveColor}
inactiveColor={this.titleInactiveColor} inactiveColor={this.titleInactiveColor}
swipeThreshold={this.swipeThreshold}
onClick={() => { onClick={() => {
this.onClick(item, index); this.onClick(item, index);
}} }}