chore(Sidebar): improve code

This commit is contained in:
chenjiahan 2020-08-25 15:20:05 +08:00
parent e686738fd5
commit d0a3726bbb
2 changed files with 8 additions and 13 deletions

View File

@ -1,4 +1,4 @@
import { ref, computed } from 'vue';
import { ref } from 'vue';
import { createNamespace } from '../utils';
import { route, routeProps } from '../utils/router';
import { useParent } from '../api/use-relation';
@ -21,10 +21,9 @@ export default createComponent({
setup(props, { emit }) {
const { parent, index } = useParent(SIDEBAR_KEY, ref());
const selected = computed(() => index.value === +parent.active());
return (vm) => {
const { dot, badge, title, disabled } = props;
const selected = index.value === parent.active();
const onClick = () => {
if (disabled) {
@ -33,7 +32,7 @@ export default createComponent({
emit('click', index.value);
parent.emit('update:modelValue', index.value);
parent.setIndex(index.value);
parent.setActive(index.value);
route(vm.$router, vm);
};

View File

@ -17,25 +17,21 @@ export default createComponent({
setup(props, { emit, slots }) {
const children = ref([]);
const index = ref(+props.modelValue);
const active = () => props.modelValue;
const active = () => +props.modelValue;
const setIndex = (value) => {
if (value !== index.value) {
index.value = value;
const setActive = (value) => {
if (value !== active()) {
emit('change', value);
}
};
watch(active, (value) => {
setIndex(+value);
});
watch(active, setActive);
provide(SIDEBAR_KEY, {
emit,
active,
children,
setIndex,
setActive,
});
return () => <div class={bem()}>{slots.default?.()}</div>;