refactor: refactor relation (#2760)

This commit is contained in:
Waiter 2020-02-14 16:56:37 +08:00 committed by GitHub
parent 6ea006006f
commit 9d6ef0e955
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 89 additions and 121 deletions

View File

@ -8,16 +8,10 @@ VantComponent({
relation: { relation: {
name: 'checkbox', name: 'checkbox',
type: 'descendant', type: 'descendant',
current: 'checkbox-group',
linked(target) { linked(target) {
this.children = this.children || [];
this.children.push(target);
this.updateChild(target); this.updateChild(target);
}, },
unlinked(target) {
this.children = this.children.filter(
(child: TrivialInstance) => child !== target
);
}
}, },
props: { props: {

View File

@ -11,12 +11,7 @@ VantComponent({
relation: { relation: {
name: 'checkbox-group', name: 'checkbox-group',
type: 'ancestor', type: 'ancestor',
linked(target) { current: 'checkbox',
this.parent = target;
},
unlinked() {
this.parent = null;
}
}, },
classes: ['icon-class', 'label-class'], classes: ['icon-class', 'label-class'],

View File

@ -3,7 +3,8 @@ import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
relation: { relation: {
name: 'row', name: 'row',
type: 'ancestor' type: 'ancestor',
current: 'col',
}, },
props: { props: {

View File

@ -8,9 +8,7 @@ VantComponent({
relation: { relation: {
name: 'collapse', name: 'collapse',
type: 'ancestor', type: 'ancestor',
linked(parent) { current: 'collapse-item',
this.parent = parent;
}
}, },
props: { props: {

View File

@ -6,14 +6,7 @@ VantComponent({
relation: { relation: {
name: 'collapse-item', name: 'collapse-item',
type: 'descendant', type: 'descendant',
linked(child) { current: 'collapse',
this.children.push(child);
},
unlinked(child) {
this.children = this.children.filter(
(item: TrivialInstance) => item !== child
);
}
}, },
props: { props: {
@ -31,10 +24,6 @@ VantComponent({
} }
}, },
beforeCreate() {
this.children = [];
},
methods: { methods: {
updateExpanded() { updateExpanded() {
this.children.forEach((child: TrivialInstance) => { this.children.forEach((child: TrivialInstance) => {

View File

@ -1,6 +1,26 @@
import { basic } from '../mixins/basic'; import { basic } from '../mixins/basic';
import { VantComponentOptions, CombinedComponentInstance } from 'definitions/index'; import { VantComponentOptions, CombinedComponentInstance } from 'definitions/index';
const relationFunctions = {
ancestor: {
linked(parent) {
this.parent = parent;
},
unlinked() {
this.parent = null;
},
},
descendant: {
linked(child) {
this.children = this.children || [];
this.children.push(child);
},
unlinked(child) {
this.children = (this.children || []).filter(it => it !== child);
},
},
};
function mapKeys(source: object, target: object, map: object) { function mapKeys(source: object, target: object, map: object) {
Object.keys(map).forEach(key => { Object.keys(map).forEach(key => {
if (source[key]) { if (source[key]) {
@ -9,6 +29,37 @@ function mapKeys(source: object, target: object, map: object) {
}); });
} }
function makeRelation(options, vantOptions, relation) {
const { type, name, linked, unlinked, linkChanged } = relation;
const { beforeCreate, destroyed } = vantOptions;
if (type === 'descendant') {
options.created = function () {
beforeCreate && beforeCreate.bind(this)();
this.children = this.children || [];
};
options.detached = function () {
this.children = [];
destroyed && destroyed.bind(this)();
};
}
options.relations = Object.assign(options.relations || {}, {
[`../${name}/index`]: {
type,
linked(node) {
relationFunctions[type].linked.bind(this)(node);
linked && linked.bind(this)(node);
},
linkChanged(node) {
linkChanged && linkChanged.bind(this)(node);
},
unlinked(node) {
relationFunctions[type].unlinked.bind(this)(node);
unlinked && unlinked.bind(this)(node);
},
}
});
}
function VantComponent<Data, Props, Methods>( function VantComponent<Data, Props, Methods>(
vantOptions: VantComponentOptions< vantOptions: VantComponentOptions<
Data, Data,
@ -34,9 +85,7 @@ function VantComponent<Data, Props, Methods>(
const { relation } = vantOptions; const { relation } = vantOptions;
if (relation) { if (relation) {
options.relations = Object.assign(options.relations || {}, { makeRelation(options, vantOptions, relation);
[`../${relation.name}/index`]: relation
});
} }
// add default externalClasses // add default externalClasses

View File

@ -19,7 +19,11 @@ export interface VantComponentOptions<Data, Props, Methods, Instance> {
classes?: string[]; classes?: string[];
mixins?: string[]; mixins?: string[];
props?: Props & Weapp.PropertyOption; props?: Props & Weapp.PropertyOption;
relation?: Weapp.RelationOption<Instance> & { name: string }; relation?: Weapp.RelationOption<Instance> & {
type: 'ancestor' | 'descendant';
name: string;
current: string;
};
relations?: { relations?: {
[componentName: string]: Weapp.RelationOption<Instance>; [componentName: string]: Weapp.RelationOption<Instance>;
}; };

View File

@ -7,13 +7,10 @@ VantComponent({
relation: { relation: {
name: 'dropdown-menu', name: 'dropdown-menu',
type: 'ancestor', type: 'ancestor',
linked(target) { current: 'dropdown-item',
this.parent = target; linked() {
this.updateDataFromParent(); this.updateDataFromParent();
}, },
unlinked() {
this.parent = null;
}
}, },
props: { props: {

View File

@ -11,14 +11,11 @@ VantComponent({
relation: { relation: {
name: 'dropdown-item', name: 'dropdown-item',
type: 'descendant', type: 'descendant',
linked(target) { current: 'dropdown-menu',
this.children.push(target); linked() {
this.updateItemListData(); this.updateItemListData();
}, },
unlinked(target) { unlinked() {
this.children = this.children.filter(
(child: TrivialInstance) => child !== target
);
this.updateItemListData(); this.updateItemListData();
} }
}, },
@ -65,7 +62,6 @@ VantComponent({
beforeCreate() { beforeCreate() {
const { windowHeight } = wx.getSystemInfoSync(); const { windowHeight } = wx.getSystemInfoSync();
this.windowHeight = windowHeight; this.windowHeight = windowHeight;
this.children = [];
ARRAY.push(this); ARRAY.push(this);
}, },

View File

@ -9,9 +9,7 @@ VantComponent({
relation: { relation: {
type: 'ancestor', type: 'ancestor',
name: 'goods-action', name: 'goods-action',
linked(parent) { current: 'goods-action-button',
this.parent = parent;
}
}, },
props: { props: {
text: String, text: String,

View File

@ -4,15 +4,7 @@ VantComponent({
relation: { relation: {
type: 'descendant', type: 'descendant',
name: 'goods-action-button', name: 'goods-action-button',
linked(child) { current: 'goods-action',
this.children.push(child);
},
unlinked(child) {
this.children = this.children.filter((item) => item !== child);
}
},
beforeCreate() {
this.children = [];
}, },
props: { props: {
safeAreaInsetBottom: { safeAreaInsetBottom: {

View File

@ -6,9 +6,7 @@ VantComponent({
relation: { relation: {
name: 'grid', name: 'grid',
type: 'ancestor', type: 'ancestor',
linked(parent) { current: 'grid-item',
this.parent = parent;
}
}, },
mixins: [link], mixins: [link],

View File

@ -5,14 +5,7 @@ VantComponent({
relation: { relation: {
name: 'grid-item', name: 'grid-item',
type: 'descendant', type: 'descendant',
linked(child) { current: 'grid',
this.children.push(child);
},
unlinked(child) {
this.children = this.children.filter(
(item: WechatMiniprogram.Component.TrivialInstance) => item !== child
);
}
}, },
props: { props: {

View File

@ -4,12 +4,7 @@ VantComponent({
relation: { relation: {
name: 'index-bar', name: 'index-bar',
type: 'ancestor', type: 'ancestor',
linked(target) { current: 'index-anchor',
this.parent = target;
},
unlinked() {
this.parent = null;
}
}, },
props: { props: {

View File

@ -16,6 +16,7 @@ VantComponent({
relation: { relation: {
name: 'index-anchor', name: 'index-anchor',
type: 'descendant', type: 'descendant',
current: 'index-bar',
linked() { linked() {
this.updateData(); this.updateData();
}, },

View File

@ -6,16 +6,10 @@ VantComponent({
relation: { relation: {
name: 'radio', name: 'radio',
type: 'descendant', type: 'descendant',
current: 'radio-group',
linked(target) { linked(target) {
this.children = this.children || [];
this.children.push(target);
this.updateChild(target); this.updateChild(target);
}, },
unlinked(target) {
this.children = this.children.filter(
(child: WechatMiniprogram.Component.TrivialInstance) => child !== target
);
}
}, },
props: { props: {

View File

@ -6,12 +6,7 @@ VantComponent({
relation: { relation: {
name: 'radio-group', name: 'radio-group',
type: 'ancestor', type: 'ancestor',
linked(target) { current: 'radio',
this.parent = target;
},
unlinked() {
this.parent = null;
}
}, },
classes: ['icon-class', 'label-class'], classes: ['icon-class', 'label-class'],

View File

@ -4,6 +4,7 @@ VantComponent({
relation: { relation: {
name: 'col', name: 'col',
type: 'descendant', type: 'descendant',
current: 'row',
linked(target) { linked(target) {
if (this.data.gutter) { if (this.data.gutter) {
target.setGutter(this.data.gutter); target.setGutter(this.data.gutter);

View File

@ -9,9 +9,7 @@ VantComponent({
relation: { relation: {
type: 'ancestor', type: 'ancestor',
name: 'sidebar', name: 'sidebar',
linked(target) { current: 'sidebar-item',
this.parent = target;
}
}, },
props: { props: {

View File

@ -4,14 +4,11 @@ VantComponent({
relation: { relation: {
name: 'sidebar-item', name: 'sidebar-item',
type: 'descendant', type: 'descendant',
linked(target) { current: 'sidebar',
this.children.push(target); linked() {
this.setActive(this.data.activeKey); this.setActive(this.data.activeKey);
}, },
unlinked(target) { unlinked() {
this.children = this.children.filter(
(item: WechatMiniprogram.Component.TrivialInstance) => item !== target
);
this.setActive(this.data.activeKey); this.setActive(this.data.activeKey);
} }
}, },
@ -25,7 +22,6 @@ VantComponent({
}, },
beforeCreate() { beforeCreate() {
this.children = [];
this.currentActive = -1; this.currentActive = -1;
}, },

View File

@ -4,12 +4,7 @@ VantComponent({
relation: { relation: {
name: 'tabs', name: 'tabs',
type: 'ancestor', type: 'ancestor',
linked(target) { current: 'tab',
this.parent = target;
},
unlinked() {
this.parent = null;
}
}, },
props: { props: {

View File

@ -10,7 +10,8 @@ VantComponent({
relation: { relation: {
name: 'tabbar', name: 'tabbar',
type: 'ancestor' type: 'ancestor',
current: 'tabbar-item',
}, },
data: { data: {

View File

@ -6,15 +6,12 @@ VantComponent({
relation: { relation: {
name: 'tabbar-item', name: 'tabbar-item',
type: 'descendant', type: 'descendant',
current: 'tabbar',
linked(target) { linked(target) {
this.children.push(target);
target.parent = this; target.parent = this;
target.updateFromParent(); target.updateFromParent();
}, },
unlinked(target) { unlinked() {
this.children = this.children.filter(
(item: TrivialInstance) => item !== target
);
this.updateChildren(); this.updateChildren();
} }
}, },
@ -50,10 +47,6 @@ VantComponent({
} }
}, },
beforeCreate() {
this.children = [];
},
methods: { methods: {
updateChildren() { updateChildren() {
const { children } = this; const { children } = this;

View File

@ -13,14 +13,13 @@ VantComponent({
relation: { relation: {
name: 'tab', name: 'tab',
type: 'descendant', type: 'descendant',
current: 'tabs',
linked(target) { linked(target) {
target.index = this.children.length; target.index = this.children.length - 1;
this.children.push(target);
this.updateTabs(); this.updateTabs();
}, },
unlinked(target) { unlinked() {
this.children = this.children this.children = this.children
.filter((child: TrivialInstance) => child !== target)
.map((child: TrivialInstance, index: number) => { .map((child: TrivialInstance, index: number) => {
child.index = index; child.index = index;
return child; return child;
@ -114,10 +113,6 @@ VantComponent({
container: null container: null
}, },
beforeCreate() {
this.children = [];
},
mounted() { mounted() {
this.setData({ this.setData({
container: () => this.createSelectorQuery().select('.van-tabs') container: () => this.createSelectorQuery().select('.van-tabs')