[improvement] relations (#647)

This commit is contained in:
neverland 2018-09-26 19:47:40 +08:00 committed by GitHub
parent 2168cc06b5
commit a3539e373d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 103 additions and 101 deletions

View File

@ -1,20 +1,17 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
relations: { relation: {
'../badge/index': { name: 'badge',
type: 'descendant', type: 'descendant',
linked(target) { linked(target) {
this.data.badges.push(target); this.data.badges.push(target);
this.setActive(); this.setActive();
}, },
unlinked(target) { unlinked(target) {
this.data.badges = this.data.badges.filter(item => item !== target); this.data.badges = this.data.badges.filter(item => item !== target);
this.setActive(); this.setActive();
} }
}
}, },
props: { props: {

View File

@ -1,10 +1,9 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
relations: { relation: {
'../badge-group/index': { type: 'ancestor',
type: 'ancestor' name: 'badge-group'
}
}, },
props: { props: {

View File

@ -1,10 +1,9 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
relations: { relation: {
'../row/index': { name: 'row',
type: 'ancestor' type: 'ancestor'
}
}, },
props: { props: {

View File

@ -31,11 +31,18 @@ function VantComponent<Data, Props, Watch, Methods, Computed>(
beforeCreate: 'created', beforeCreate: 'created',
created: 'attached', created: 'attached',
mounted: 'ready', mounted: 'ready',
destroyed: 'detached',
relations: 'relations', relations: 'relations',
destroyed: 'detached',
classes: 'externalClasses' classes: 'externalClasses'
}); });
const { relation } = vantOptions;
if (relation) {
options.relations = Object.assign(options.relations || {}, {
[`../${relation.name}/index`]: relation
});
}
// add default externalClasses // add default externalClasses
options.externalClasses = options.externalClasses || []; options.externalClasses = options.externalClasses || [];
options.externalClasses.push('custom-class'); options.externalClasses.push('custom-class');

View File

@ -1,8 +1,8 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
relations: { relation: {
'../radio/index': { name: 'radio',
type: 'descendant', type: 'descendant',
linked(target) { linked(target) {
const { value, disabled } = this.data; const { value, disabled } = this.data;
@ -11,7 +11,6 @@ VantComponent({
disabled: disabled || target.data.disabled disabled: disabled || target.data.disabled
}); });
} }
}
}, },
props: { props: {

View File

@ -1,10 +1,9 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
relations: { relation: {
'../radio-group/index': { name: 'radio-group',
type: 'ancestor' type: 'ancestor'
}
}, },
classes: ['icon-class', 'label-class'], classes: ['icon-class', 'label-class'],

View File

@ -1,16 +1,14 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
relations: { relation: {
'../col/index': { name: 'col',
type: 'descendant', type: 'descendant',
linked(target) { linked(target) {
if (this.data.gutter) { if (this.data.gutter) {
target.setGutter(this.data.gutter); target.setGutter(this.data.gutter);
} }
} }
}
}, },
props: { props: {
@ -31,10 +29,12 @@ VantComponent({
setGutter() { setGutter() {
const { gutter } = this.data; const { gutter } = this.data;
const margin = `-${Number(gutter) / 2}px`; const margin = `-${Number(gutter) / 2}px`;
const style = gutter ? `margin-right: ${margin}; margin-left: ${margin};` : ''; const style = gutter
? `margin-right: ${margin}; margin-left: ${margin};`
: '';
this.setData({ style }); this.setData({ style });
this.getRelationNodes('../col/index').forEach((col) => { this.getRelationNodes('../col/index').forEach(col => {
col.setGutter(this.data.gutter); col.setGutter(this.data.gutter);
}); });
} }

View File

@ -1,10 +1,9 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
relations: { relation: {
'../tabs/index': { name: 'tabs',
type: 'ancestor' type: 'ancestor'
}
}, },
props: { props: {

View File

@ -7,10 +7,9 @@ VantComponent({
dot: Boolean dot: Boolean
}, },
relations: { relation: {
'../tabbar/index': { name: 'tabbar',
type: 'ancestor' type: 'ancestor'
}
}, },
data: { data: {

View File

@ -1,6 +1,23 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
relation: {
name: 'tabbar-item',
type: 'descendant',
linked(target) {
this.data.items.push(target);
setTimeout(() => {
this.setActiveItem();
});
},
unlinked(target) {
this.data.items = this.data.items.filter(item => item !== target);
setTimeout(() => {
this.setActiveItem();
});
}
},
props: { props: {
active: Number, active: Number,
fixed: { fixed: {
@ -29,26 +46,6 @@ VantComponent({
this.setData({ currentActive: this.data.active }); this.setData({ currentActive: this.data.active });
}, },
relations: {
'../tabbar-item/index': {
type: 'descendant',
linked(target) {
this.data.items.push(target);
setTimeout(() => {
this.setActiveItem();
});
},
unlinked(target) {
this.data.items = this.data.items.filter(item => item !== target);
setTimeout(() => {
this.setActiveItem();
});
}
}
},
methods: { methods: {
setActiveItem() { setActiveItem() {
this.data.items.forEach((item, index) => { this.data.items.forEach((item, index) => {

View File

@ -6,10 +6,9 @@ type TabItemData = {
}; };
VantComponent({ VantComponent({
relations: { relation: {
'../tab/index': { name: 'tab',
type: 'descendant', type: 'descendant',
linked(child) { linked(child) {
this.data.tabs.push({ this.data.tabs.push({
instance: child, instance: child,
@ -17,7 +16,6 @@ VantComponent({
}); });
this.updateTabs(); this.updateTabs();
}, },
unlinked(child) { unlinked(child) {
const tabs = this.data.tabs.filter(item => item.instance !== child); const tabs = this.data.tabs.filter(item => item.instance !== child);
this.setData({ this.setData({
@ -26,7 +24,6 @@ VantComponent({
}); });
this.setActiveTab(); this.setActiveTab();
} }
}
}, },
props: { props: {

24
types/index.d.ts vendored
View File

@ -6,17 +6,19 @@ type ExternalClasses = string[];
type LooseObject = { type LooseObject = {
[key: string]: any; [key: string]: any;
}; };
type Relations<Instance> = { type Relation<Instance> = {
[key: string]: { name?: string;
type: string; type: string;
linked?: (this: Instance, target?: any) => void; linked?: (this: Instance, target?: Weapp.Component) => void;
unlinked?: (this: Instance, target?: any) => void; unlinked?: (this: Instance, target?: Weapp.Component) => void;
}; };
type Relations<Instance> = {
[key: string]: Relation<Instance>;
}; };
type RecordToAny<T> = { [K in keyof T]: any }; type RecordToAny<T> = { [K in keyof T]: any };
type RecordToReturn<T> = { type RecordToReturn<T> = {
[P in keyof T]: T[P] extends (...args: any[]) => any ? ReturnType<T[P]> : T[P] [P in keyof T]: T[P] extends (...args: any[]) => any ? ReturnType<T[P]> : T[P]
} };
export type CombinedComponentInstance< export type CombinedComponentInstance<
Data, Data,
@ -31,13 +33,21 @@ export type CombinedComponentInstance<
data: Data & RecordToAny<Props> & RecordToReturn<Computed>; data: Data & RecordToAny<Props> & RecordToReturn<Computed>;
}; };
export type VantComponentOptions<Data, Props, Watch, Methods, Computed, Instance> = { export type VantComponentOptions<
Data,
Props,
Watch,
Methods,
Computed,
Instance
> = {
data?: Data; data?: Data;
field?: boolean; field?: boolean;
mixins?: Mixins; mixins?: Mixins;
props?: Props & ThisType<Instance>; props?: Props & ThisType<Instance>;
watch?: Watch & ThisType<Instance>; watch?: Watch & ThisType<Instance>;
computed?: Computed & ThisType<Instance>; computed?: Computed & ThisType<Instance>;
relation?: Relation<Instance>;
relations?: Relations<Instance>; relations?: Relations<Instance>;
classes?: ExternalClasses; classes?: ExternalClasses;
methods?: Methods & ThisType<Instance>; methods?: Methods & ThisType<Instance>;