mirror of
https://github.com/youzan/vant.git
synced 2025-10-22 03:44:48 +00:00
[improvement] Tabbar: jsx (#2663)
This commit is contained in:
54
packages/tabbar-item/index.js
Normal file
54
packages/tabbar-item/index.js
Normal file
@@ -0,0 +1,54 @@
|
||||
import { use, useSlots } from '../utils';
|
||||
import Icon from '../icon';
|
||||
import Info from '../info';
|
||||
import RouterLink from '../mixins/router-link';
|
||||
|
||||
const [sfc, bem] = use('tabbar-item');
|
||||
|
||||
export default sfc({
|
||||
mixins: [RouterLink],
|
||||
|
||||
props: {
|
||||
icon: String,
|
||||
dot: Boolean,
|
||||
info: [String, Number]
|
||||
},
|
||||
|
||||
data() {
|
||||
return {
|
||||
active: false
|
||||
};
|
||||
},
|
||||
|
||||
beforeCreate() {
|
||||
this.$parent.items.push(this);
|
||||
},
|
||||
|
||||
destroyed() {
|
||||
this.$parent.items.splice(this.$parent.items.indexOf(this), 1);
|
||||
},
|
||||
|
||||
methods: {
|
||||
onClick(event) {
|
||||
this.$parent.onChange(this.$parent.items.indexOf(this));
|
||||
this.$emit('click', event);
|
||||
this.routerLink();
|
||||
}
|
||||
},
|
||||
|
||||
render(h) {
|
||||
const { icon, active } = this;
|
||||
const slots = useSlots(this);
|
||||
const style = active ? { color: this.$parent.activeColor } : null;
|
||||
|
||||
return (
|
||||
<div class={bem({ active })} style={style} onClick={this.onClick}>
|
||||
<div class={bem('icon', { dot: this.dot })}>
|
||||
{slots('icon', { active }) || (icon && <Icon name={icon} />)}
|
||||
<Info info={this.info} />
|
||||
</div>
|
||||
<div class={bem('text')}>{slots('default', { active })}</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
});
|
@@ -1,73 +0,0 @@
|
||||
<template>
|
||||
<div
|
||||
:class="b({ active })"
|
||||
:style="style"
|
||||
@click="onClick"
|
||||
>
|
||||
<div :class="b('icon', { dot })">
|
||||
<slot
|
||||
name="icon"
|
||||
:active="active"
|
||||
>
|
||||
<icon
|
||||
v-if="icon"
|
||||
:name="icon"
|
||||
/>
|
||||
</slot>
|
||||
<van-info :info="info" />
|
||||
</div>
|
||||
<div :class="b('text')">
|
||||
<slot :active="active" />
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import Info from '../info';
|
||||
import create from '../utils/create';
|
||||
import RouterLink from '../mixins/router-link';
|
||||
|
||||
export default create({
|
||||
name: 'tabbar-item',
|
||||
|
||||
components: {
|
||||
[Info.name]: Info
|
||||
},
|
||||
|
||||
mixins: [RouterLink],
|
||||
|
||||
props: {
|
||||
icon: String,
|
||||
dot: Boolean,
|
||||
info: [String, Number]
|
||||
},
|
||||
|
||||
data() {
|
||||
return {
|
||||
active: false
|
||||
};
|
||||
},
|
||||
|
||||
computed: {
|
||||
style() {
|
||||
return this.active ? { color: this.$parent.activeColor } : null;
|
||||
}
|
||||
},
|
||||
|
||||
beforeCreate() {
|
||||
this.$parent.items.push(this);
|
||||
},
|
||||
|
||||
destroyed() {
|
||||
this.$parent.items.splice(this.$parent.items.indexOf(this), 1);
|
||||
},
|
||||
|
||||
methods: {
|
||||
onClick(event) {
|
||||
this.$parent.onChange(this.$parent.items.indexOf(this));
|
||||
this.$emit('click', event);
|
||||
this.routerLink();
|
||||
}
|
||||
}
|
||||
});
|
||||
</script>
|
Reference in New Issue
Block a user