refactor(Sidebar): refactor with composition api

This commit is contained in:
chenjiahan 2020-08-25 15:04:04 +08:00
parent a847313477
commit c371301648
2 changed files with 56 additions and 58 deletions

View File

@ -1,53 +1,50 @@
import { ref, computed } from 'vue';
import { createNamespace } from '../utils'; import { createNamespace } from '../utils';
import { ChildrenMixin } from '../mixins/relation';
import { route, routeProps } from '../utils/router'; import { route, routeProps } from '../utils/router';
import { useParent } from '../api/use-relation';
import { SIDEBAR_KEY } from '../sidebar';
import Badge from '../badge'; import Badge from '../badge';
const [createComponent, bem] = createNamespace('sidebar-item'); const [createComponent, bem] = createNamespace('sidebar-item');
export default createComponent({ export default createComponent({
mixins: [ChildrenMixin('vanSidebar')],
emits: ['click'],
props: { props: {
...routeProps, ...routeProps,
dot: Boolean, dot: Boolean,
badge: [Number, String],
title: String, title: String,
badge: [Number, String],
disabled: Boolean, disabled: Boolean,
}, },
computed: { emits: ['click'],
select() {
return this.index === +this.parent.modelValue;
},
},
methods: { setup(props, { emit }) {
onClick() { const { parent, index } = useParent(SIDEBAR_KEY, ref());
if (this.disabled) {
const selected = computed(() => index.value === +parent.active.value);
return (vm) => {
const { dot, badge, title, disabled } = props;
const onClick = () => {
if (disabled) {
return; return;
} }
this.$emit('click', this.index); emit('click', index.value);
this.parent.$emit('update:modelValue', this.index); parent.emit('update:modelValue', index.value);
this.parent.setIndex(this.index); parent.setIndex(index.value);
route(this.$router, this); route(vm.$router, vm);
}, };
},
render() {
return ( return (
<a <a class={bem({ select: selected.value, disabled })} onClick={onClick}>
class={bem({ select: this.select, disabled: this.disabled })}
onClick={this.onClick}
>
<div class={bem('text')}> <div class={bem('text')}>
{this.title} {title}
<Badge dot={this.dot} badge={this.badge} class={bem('badge')} /> <Badge dot={dot} badge={badge} class={bem('badge')} />
</div> </div>
</a> </a>
); );
};
}, },
}); });

View File

@ -1,13 +1,11 @@
import { ref, watch, provide, computed } from 'vue';
import { createNamespace } from '../utils'; import { createNamespace } from '../utils';
import { ParentMixin } from '../mixins/relation';
const [createComponent, bem] = createNamespace('sidebar'); const [createComponent, bem] = createNamespace('sidebar');
export const SIDEBAR_KEY = 'vanSidebar';
export default createComponent({ export default createComponent({
mixins: [ParentMixin('vanSidebar')],
emits: ['change', 'update:modelValue'],
props: { props: {
modelValue: { modelValue: {
type: [Number, String], type: [Number, String],
@ -15,28 +13,31 @@ export default createComponent({
}, },
}, },
data() { emits: ['change', 'update:modelValue'],
return {
index: +this.modelValue,
};
},
watch: { setup(props, { emit, slots }) {
modelValue() { const children = ref([]);
this.setIndex(+this.modelValue); const index = ref(+props.modelValue);
}, const active = computed(() => props.modelValue);
},
methods: { const setIndex = (value) => {
setIndex(index) { if (value !== index.value) {
if (index !== this.index) { index.value = value;
this.index = index; emit('change', value);
this.$emit('change', index);
} }
}, };
},
render() { watch(active, (val) => {
return <div class={bem()}>{this.$slots.default?.()}</div>; setIndex(+val);
});
provide(SIDEBAR_KEY, {
emit,
active,
children,
setIndex,
});
return () => <div class={bem()}>{slots.default?.()}</div>;
}, },
}); });