mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-23 18:00:27 +08:00
fix(Tabbar): incorrect active tab when name is zero (#8125)
This commit is contained in:
parent
0b68d3a141
commit
6b28f12e7e
@ -33,7 +33,7 @@ export const cellProps = {
|
|||||||
default: true,
|
default: true,
|
||||||
},
|
},
|
||||||
clickable: {
|
clickable: {
|
||||||
type: Boolean,
|
type: Boolean as PropType<boolean | null>,
|
||||||
default: null,
|
default: null,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -26,7 +26,7 @@ export default createComponent({
|
|||||||
wrapable: Boolean,
|
wrapable: Boolean,
|
||||||
background: String,
|
background: String,
|
||||||
scrollable: {
|
scrollable: {
|
||||||
type: Boolean,
|
type: Boolean as PropType<boolean | null>,
|
||||||
default: null,
|
default: null,
|
||||||
},
|
},
|
||||||
delay: {
|
delay: {
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { computed, getCurrentInstance } from 'vue';
|
import { computed, getCurrentInstance } from 'vue';
|
||||||
import { TABBAR_KEY } from '../tabbar';
|
import { TABBAR_KEY, TabbarProvide } from '../tabbar';
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
import { createNamespace, isObject, isDef } from '../utils';
|
import { createNamespace, isObject } from '../utils';
|
||||||
|
|
||||||
// Composition
|
// Composition
|
||||||
import { useParent } from '@vant/use';
|
import { useParent } from '@vant/use';
|
||||||
@ -28,8 +28,15 @@ export default createComponent({
|
|||||||
|
|
||||||
setup(props, { emit, slots }) {
|
setup(props, { emit, slots }) {
|
||||||
const route = useRoute();
|
const route = useRoute();
|
||||||
const vm = getCurrentInstance().proxy;
|
const vm = getCurrentInstance()!.proxy!;
|
||||||
const { parent, index } = useParent(TABBAR_KEY);
|
const { parent, index } = useParent<TabbarProvide>(TABBAR_KEY);
|
||||||
|
|
||||||
|
if (!parent) {
|
||||||
|
if (process.env.NODE_ENV !== 'production') {
|
||||||
|
console.error('[Vant] TabbarItem must be a child component of Tabbar.');
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const active = computed(() => {
|
const active = computed(() => {
|
||||||
const { route, modelValue } = parent.props;
|
const { route, modelValue } = parent.props;
|
||||||
@ -39,7 +46,7 @@ export default createComponent({
|
|||||||
const { to } = props;
|
const { to } = props;
|
||||||
const config = isObject(to) ? to : { path: to };
|
const config = isObject(to) ? to : { path: to };
|
||||||
const pathMatched = config.path === $route.path;
|
const pathMatched = config.path === $route.path;
|
||||||
const nameMatched = isDef(config.name) && config.name === $route.name;
|
const nameMatched = 'name' in config && config.name === $route.name;
|
||||||
|
|
||||||
return pathMatched || nameMatched;
|
return pathMatched || nameMatched;
|
||||||
}
|
}
|
||||||
@ -47,8 +54,8 @@ export default createComponent({
|
|||||||
return (props.name || index.value) === modelValue;
|
return (props.name || index.value) === modelValue;
|
||||||
});
|
});
|
||||||
|
|
||||||
const onClick = (event) => {
|
const onClick = (event: MouseEvent) => {
|
||||||
parent.setActive(props.name || index.value);
|
parent.setActive(props.name ?? index.value);
|
||||||
emit('click', event);
|
emit('click', event);
|
||||||
route();
|
route();
|
||||||
};
|
};
|
@ -1,9 +1,9 @@
|
|||||||
import { ref } from 'vue';
|
import { ref, PropType } from 'vue';
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
import { createNamespace, isDef } from '../utils';
|
import { createNamespace } from '../utils';
|
||||||
import { BORDER_TOP_BOTTOM } from '../utils/constant';
|
import { BORDER_TOP_BOTTOM } from '../utils/constant';
|
||||||
import { callInterceptor } from '../utils/interceptor';
|
import { callInterceptor, Interceptor } from '../utils/interceptor';
|
||||||
|
|
||||||
// Composition
|
// Composition
|
||||||
import { useChildren } from '@vant/use';
|
import { useChildren } from '@vant/use';
|
||||||
@ -13,13 +13,23 @@ const [createComponent, bem] = createNamespace('tabbar');
|
|||||||
|
|
||||||
export const TABBAR_KEY = 'vanTabbar';
|
export const TABBAR_KEY = 'vanTabbar';
|
||||||
|
|
||||||
|
export type TabbarProvide = {
|
||||||
|
props: {
|
||||||
|
route?: boolean;
|
||||||
|
modelValue: number | string;
|
||||||
|
activeColor?: string;
|
||||||
|
inactiveColor?: string;
|
||||||
|
};
|
||||||
|
setActive: (active: number | string) => void;
|
||||||
|
};
|
||||||
|
|
||||||
export default createComponent({
|
export default createComponent({
|
||||||
props: {
|
props: {
|
||||||
route: Boolean,
|
route: Boolean,
|
||||||
zIndex: [Number, String],
|
zIndex: [Number, String],
|
||||||
placeholder: Boolean,
|
placeholder: Boolean,
|
||||||
activeColor: String,
|
activeColor: String,
|
||||||
beforeChange: Function,
|
beforeChange: Function as PropType<Interceptor>,
|
||||||
inactiveColor: String,
|
inactiveColor: String,
|
||||||
modelValue: {
|
modelValue: {
|
||||||
type: [Number, String],
|
type: [Number, String],
|
||||||
@ -34,7 +44,7 @@ export default createComponent({
|
|||||||
default: true,
|
default: true,
|
||||||
},
|
},
|
||||||
safeAreaInsetBottom: {
|
safeAreaInsetBottom: {
|
||||||
type: Boolean,
|
type: Boolean as PropType<boolean | null>,
|
||||||
default: null,
|
default: null,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -42,12 +52,12 @@ export default createComponent({
|
|||||||
emits: ['change', 'update:modelValue'],
|
emits: ['change', 'update:modelValue'],
|
||||||
|
|
||||||
setup(props, { emit, slots }) {
|
setup(props, { emit, slots }) {
|
||||||
const root = ref();
|
const root = ref<HTMLElement>();
|
||||||
const { linkChildren } = useChildren(TABBAR_KEY);
|
const { linkChildren } = useChildren(TABBAR_KEY);
|
||||||
const renderPlaceholder = usePlaceholder(root, bem);
|
const renderPlaceholder = usePlaceholder(root, bem);
|
||||||
|
|
||||||
const isUnfit = () => {
|
const isUnfit = () => {
|
||||||
if (isDef(props.safeAreaInsetBottom)) {
|
if (props.safeAreaInsetBottom !== null) {
|
||||||
return !props.safeAreaInsetBottom;
|
return !props.safeAreaInsetBottom;
|
||||||
}
|
}
|
||||||
// enable safe-area-inset-bottom by default when fixed
|
// enable safe-area-inset-bottom by default when fixed
|
||||||
@ -56,19 +66,21 @@ export default createComponent({
|
|||||||
|
|
||||||
const renderTabbar = () => {
|
const renderTabbar = () => {
|
||||||
const { fixed, zIndex, border } = props;
|
const { fixed, zIndex, border } = props;
|
||||||
const unfit = isUnfit();
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
ref={root}
|
ref={root}
|
||||||
style={{ zIndex }}
|
style={{ zIndex: zIndex !== undefined ? +zIndex : undefined }}
|
||||||
class={[bem({ unfit, fixed }), { [BORDER_TOP_BOTTOM]: border }]}
|
class={[
|
||||||
|
bem({ unfit: isUnfit(), fixed }),
|
||||||
|
{ [BORDER_TOP_BOTTOM]: border },
|
||||||
|
]}
|
||||||
>
|
>
|
||||||
{slots.default?.()}
|
{slots.default?.()}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const setActive = (active) => {
|
const setActive = (active: number | string) => {
|
||||||
if (active !== props.modelValue) {
|
if (active !== props.modelValue) {
|
||||||
callInterceptor({
|
callInterceptor({
|
||||||
interceptor: props.beforeChange,
|
interceptor: props.beforeChange,
|
Loading…
x
Reference in New Issue
Block a user