mirror of
https://gitee.com/vant-contrib/vant-weapp.git
synced 2025-04-06 03:58:05 +08:00
refactor: refactor relation (#2760)
This commit is contained in:
parent
6ea006006f
commit
9d6ef0e955
@ -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: {
|
||||||
|
@ -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'],
|
||||||
|
@ -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: {
|
||||||
|
@ -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: {
|
||||||
|
@ -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) => {
|
||||||
|
@ -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
|
||||||
|
@ -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>;
|
||||||
};
|
};
|
||||||
|
@ -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: {
|
||||||
|
@ -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);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -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,
|
||||||
|
@ -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: {
|
||||||
|
@ -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],
|
||||||
|
@ -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: {
|
||||||
|
@ -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: {
|
||||||
|
@ -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();
|
||||||
},
|
},
|
||||||
|
@ -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: {
|
||||||
|
@ -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'],
|
||||||
|
@ -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);
|
||||||
|
@ -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: {
|
||||||
|
@ -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;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -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: {
|
||||||
|
@ -10,7 +10,8 @@ VantComponent({
|
|||||||
|
|
||||||
relation: {
|
relation: {
|
||||||
name: 'tabbar',
|
name: 'tabbar',
|
||||||
type: 'ancestor'
|
type: 'ancestor',
|
||||||
|
current: 'tabbar-item',
|
||||||
},
|
},
|
||||||
|
|
||||||
data: {
|
data: {
|
||||||
|
@ -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;
|
||||||
|
@ -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')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user