vant/src/composition/use-parent.ts
2020-09-22 20:29:14 +08:00

31 lines
571 B
TypeScript

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 {};
}