mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-06-01 11:19:16 +08:00
chore(Sidebar): improve code
This commit is contained in:
parent
e686738fd5
commit
d0a3726bbb
@ -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);
|
||||
};
|
||||
|
||||
|
@ -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>;
|
||||
|
Loading…
x
Reference in New Issue
Block a user