chore(Layout): use relation

This commit is contained in:
chenjiahan 2020-09-25 14:00:59 +08:00
parent 36239090b9
commit a1bca37a45
3 changed files with 33 additions and 24 deletions

View File

@ -1,6 +1,6 @@
import { computed, PropType } from 'vue'; import { computed, PropType } from 'vue';
import { createNamespace } from '../utils'; import { createNamespace } from '../utils';
import { useParent } from '../composition/use-parent'; import { useParent } from '../composition/use-relation';
import { ROW_KEY, RowProvide } from '../row'; import { ROW_KEY, RowProvide } from '../row';
const [createComponent, bem] = createNamespace('col'); const [createComponent, bem] = createNamespace('col');
@ -19,7 +19,7 @@ export default createComponent({
}, },
setup(props, { slots }) { setup(props, { slots }) {
const { parent, index } = useParent<RowProvide>(ROW_KEY, () => +props.span); const { parent, index } = useParent<RowProvide>(ROW_KEY);
const style = computed(() => { const style = computed(() => {
if (!parent) { if (!parent) {

View File

@ -94,24 +94,36 @@ export function useChildren(key: string) {
}; };
} }
export function useParent(key: string) { type ParentProvide<T> = T & {
const parent = inject<any | null>(key, null); link(child: InternalInstance): void;
unlink(child: InternalInstance): void;
children: PublicInstance[];
internalChildren: InternalInstance[];
};
export function useParent<T>(key: string) {
const parent = inject<ParentProvide<T> | null>(key, null);
if (parent) { if (parent) {
const instance = getCurrentInstance(); const instance = getCurrentInstance();
const index = computed(() => parent.internalChildren.indexOf(instance));
parent.link(instance); if (instance) {
const { link, unlink, internalChildren, ...rest } = parent;
link(instance);
onUnmounted(() => { onUnmounted(() => {
parent.unlink(instance); unlink(instance);
}); });
const index = computed(() => internalChildren.indexOf(instance));
return { return {
parent, parent: rest,
index, index,
}; };
} }
}
return {}; return {};
} }

View File

@ -1,5 +1,6 @@
import { provide, computed, reactive, PropType, ComputedRef } from 'vue'; import { computed, PropType, ComputedRef } from 'vue';
import { createNamespace } from '../utils'; import { createNamespace } from '../utils';
import { useChildren } from '../composition/use-relation';
const [createComponent, bem] = createNamespace('row'); const [createComponent, bem] = createNamespace('row');
@ -7,11 +8,8 @@ export const ROW_KEY = 'vanRow';
export type RowSpaces = { left?: number; right: number }[]; export type RowSpaces = { left?: number; right: number }[];
export type RowChild = () => number;
export type RowProvide = { export type RowProvide = {
spaces: ComputedRef<RowSpaces>; spaces: ComputedRef<RowSpaces>;
children: RowChild[];
}; };
export type RowAlign = 'top' | 'center' | 'bottom'; export type RowAlign = 'top' | 'center' | 'bottom';
@ -39,14 +37,16 @@ export default createComponent({
}, },
setup(props, { slots }) { setup(props, { slots }) {
const children = reactive<RowChild[]>([]); const { children, linkChildren } = useChildren(ROW_KEY);
const groups = computed(() => { const groups = computed(() => {
const groups: number[][] = [[]]; const groups: number[][] = [[]];
let totalSpan = 0; let totalSpan = 0;
children.forEach((getSpan, index) => { children.forEach((child, index) => {
totalSpan += getSpan(); // TODO
// @ts-ignore
totalSpan += Number(child.span);
if (totalSpan > 24) { if (totalSpan > 24) {
groups.push([index]); groups.push([index]);
@ -84,10 +84,7 @@ export default createComponent({
return spaces; return spaces;
}); });
provide(ROW_KEY, { linkChildren({ spaces });
spaces,
children,
});
return () => { return () => {
const { tag, type, align, justify } = props; const { tag, type, align, justify } = props;