refactor(Layout): use relation

This commit is contained in:
chenjiahan 2020-08-24 19:00:40 +08:00
parent 704f4734df
commit b463fa5f69
2 changed files with 34 additions and 25 deletions

View File

@ -1,11 +1,11 @@
import { computed } from 'vue';
import { createNamespace } from '../utils'; import { createNamespace } from '../utils';
import { ChildrenMixin } from '../mixins/relation'; import { useParent } from '../api/use-relation';
import { ROW_KEY } from '../row';
const [createComponent, bem] = createNamespace('col'); const [createComponent, bem] = createNamespace('col');
export default createComponent({ export default createComponent({
mixins: [ChildrenMixin('vanRow')],
props: { props: {
span: [Number, String], span: [Number, String],
offset: [Number, String], offset: [Number, String],
@ -16,25 +16,29 @@ export default createComponent({
}, },
setup(props, { slots }) { setup(props, { slots }) {
const getStyle = (vm) => { const { parent, index } = useParent(
const { index } = vm; ROW_KEY,
const { spaces } = vm.parent || {}; computed(() => props.span)
);
if (spaces && spaces[index]) { const style = computed(() => {
const { left, right } = spaces[index]; const { spaces } = parent || {};
if (spaces && spaces.value && spaces.value[index.value]) {
const { left, right } = spaces.value[index.value];
return { return {
paddingLeft: left ? `${left}px` : null, paddingLeft: left ? `${left}px` : null,
paddingRight: right ? `${right}px` : null, paddingRight: right ? `${right}px` : null,
}; };
} }
}; });
return (vm) => { return () => {
const { tag, span, offset } = props; const { tag, span, offset } = props;
return ( return (
<tag <tag
style={getStyle(vm)} style={style.value}
class={bem({ [span]: span, [`offset-${offset}`]: offset })} class={bem({ [span]: span, [`offset-${offset}`]: offset })}
> >
{slots.default?.()} {slots.default?.()}

View File

@ -1,11 +1,11 @@
import { ref, computed, provide } from 'vue';
import { createNamespace } from '../utils'; import { createNamespace } from '../utils';
import { ParentMixin } from '../mixins/relation';
const [createComponent, bem] = createNamespace('row'); const [createComponent, bem] = createNamespace('row');
export default createComponent({ export const ROW_KEY = 'van-row';
mixins: [ParentMixin('vanRow')],
export default createComponent({
props: { props: {
type: String, type: String,
align: String, align: String,
@ -20,13 +20,15 @@ export default createComponent({
}, },
}, },
computed: { setup(props, { slots }) {
groups() { const children = ref([]);
const groups = computed(() => {
const groups = [[]]; const groups = [[]];
let totalSpan = 0; let totalSpan = 0;
this.children.forEach((item, index) => { children.value.forEach((item, index) => {
totalSpan += Number(item.span); totalSpan += Number(item.value);
if (totalSpan > 24) { if (totalSpan > 24) {
groups.push([index]); groups.push([index]);
@ -37,10 +39,10 @@ export default createComponent({
}); });
return groups; return groups;
}, });
spaces() { const spaces = computed(() => {
const gutter = Number(this.gutter); const gutter = Number(props.gutter);
if (!gutter) { if (!gutter) {
return; return;
@ -48,7 +50,7 @@ export default createComponent({
const spaces = []; const spaces = [];
this.groups.forEach((group) => { groups.value.forEach((group) => {
const averagePadding = (gutter * (group.length - 1)) / group.length; const averagePadding = (gutter * (group.length - 1)) / group.length;
group.forEach((item, index) => { group.forEach((item, index) => {
@ -63,10 +65,13 @@ export default createComponent({
}); });
return spaces; return spaces;
}, });
},
provide(ROW_KEY, {
spaces,
children,
});
setup(props, { slots }) {
return () => { return () => {
const { tag, type, align, justify } = props; const { tag, type, align, justify } = props;
const flex = type === 'flex'; const flex = type === 'flex';