chore(Tabbar): use relation

This commit is contained in:
chenjiahan 2020-09-26 11:13:49 +08:00
parent 11890ef3c1
commit 367a273bd5
3 changed files with 9 additions and 38 deletions

View File

@ -1,30 +0,0 @@
import { inject, computed, onUnmounted } from 'vue';
type Parent = { children: unknown[] };
type Child<T> = T extends { children: (infer U)[] } ? U : never;
export function useParent<P extends Parent>(
key: string,
child = {} as Child<P>
) {
const parent = inject<P | null>(key, null);
if (parent) {
const { children } = parent;
const index = computed(() => children.indexOf(child));
children.push(child);
onUnmounted(() => {
children.splice(index.value, 1);
});
return {
index,
parent,
};
}
return {};
}

View File

@ -5,7 +5,7 @@ import { TABBAR_KEY } from '../tabbar';
import { createNamespace, isObject, isDef } from '../utils'; import { createNamespace, isObject, isDef } from '../utils';
// Composition // Composition
import { useParent } from '../composition/use-parent'; import { useParent } from '../composition/use-relation';
import { routeProps, useRoute } from '../composition/use-route'; import { routeProps, useRoute } from '../composition/use-route';
// Components // Components

View File

@ -1,7 +1,12 @@
import { ref, reactive, provide } from 'vue'; import { ref } from 'vue';
// Utils
import { createNamespace, isDef } from '../utils'; import { createNamespace, isDef } from '../utils';
import { BORDER_TOP_BOTTOM } from '../utils/constant'; import { BORDER_TOP_BOTTOM } from '../utils/constant';
import { callInterceptor } from '../utils/interceptor'; import { callInterceptor } from '../utils/interceptor';
// Composition
import { useChildren } from '../composition/use-relation';
import { usePlaceholder } from '../composition/use-placeholder'; import { usePlaceholder } from '../composition/use-placeholder';
const [createComponent, bem] = createNamespace('tabbar'); const [createComponent, bem] = createNamespace('tabbar');
@ -38,7 +43,7 @@ export default createComponent({
setup(props, { emit, slots }) { setup(props, { emit, slots }) {
const root = ref(); const root = ref();
const children = reactive([]); const { linkChildren } = useChildren(TABBAR_KEY);
const renderPlaceholder = usePlaceholder(root, bem); const renderPlaceholder = usePlaceholder(root, bem);
const isUnfit = () => { const isUnfit = () => {
@ -76,11 +81,7 @@ export default createComponent({
} }
}; };
provide(TABBAR_KEY, { linkChildren({ props, setActive });
props,
children,
setActive,
});
return () => { return () => {
if (props.fixed && props.placeholder) { if (props.fixed && props.placeholder) {