vant/src/sidebar-item/SidebarItem.tsx
2021-04-12 20:57:24 +08:00

65 lines
1.5 KiB
TypeScript

import { defineComponent } from 'vue';
// Utils
import { createNamespace, extend } from '../utils';
import { SIDEBAR_KEY, SidebarProvide } from '../sidebar/Sidebar';
// Composables
import { useParent } from '@vant/use';
import { useRoute, routeProps } from '../composables/use-route';
// Components
import { Badge } from '../badge';
const [name, bem] = createNamespace('sidebar-item');
export default defineComponent({
name,
props: extend({}, routeProps, {
dot: Boolean,
title: String,
badge: [Number, String],
disabled: Boolean,
}),
emits: ['click'],
setup(props, { emit, slots }) {
const route = useRoute();
const { parent, index } = useParent<SidebarProvide>(SIDEBAR_KEY);
if (!parent) {
if (process.env.NODE_ENV !== 'production') {
console.error(
'[Vant] <SidebarItem> must be a child component of <Sidebar>.'
);
}
return;
}
const onClick = () => {
if (props.disabled) {
return;
}
emit('click', index.value);
parent.setActive(index.value);
route();
};
return () => {
const { dot, badge, title, disabled } = props;
const selected = index.value === parent.getActive();
return (
<a class={bem({ select: selected, disabled })} onClick={onClick}>
<Badge dot={dot} content={badge} class={bem('text')}>
{slots.title ? slots.title() : title}
</Badge>
</a>
);
};
},
});