This commit is contained in:
陈嘉涵 2018-09-27 14:27:00 +08:00
parent b57b524787
commit 1bd233534c
41 changed files with 1262 additions and 1459 deletions

20
dist/area/index.js vendored
View File

@ -2,14 +2,8 @@ import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
props: { props: {
title: String, title: String,
value: String,
loading: Boolean, loading: Boolean,
value: {
type: String,
observer(value) {
this.code = value;
this.setValues();
}
},
itemHeight: { itemHeight: {
type: Number, type: Number,
value: 44 value: 44
@ -24,10 +18,7 @@ VantComponent({
}, },
areaList: { areaList: {
type: Object, type: Object,
value: {}, value: {}
observer() {
this.setValues();
}
} }
}, },
data: { data: {
@ -40,6 +31,13 @@ VantComponent({
return columns.slice(0, +columnsNum); return columns.slice(0, +columnsNum);
} }
}, },
watch: {
value(value) {
this.code = value;
this.setValues();
},
areaList: 'setValues'
},
methods: { methods: {
onCancel() { onCancel() {
this.$emit('cancel', { this.$emit('cancel', {

View File

@ -1,7 +1,7 @@
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);
@ -11,18 +11,19 @@ VantComponent({
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: {
active: { active: {
type: Number, type: Number,
value: 0, value: 0
observer: 'setActive'
} }
}, },
data: { data: {
badges: [] badges: []
}, },
watch: {
active: 'setActive'
},
beforeCreate() { beforeCreate() {
this.currentActive = -1; this.currentActive = -1;
}, },

7
dist/badge/index.js vendored
View File

@ -1,9 +1,8 @@
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: {
info: Number, info: Number,

View File

@ -1,7 +1,8 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
import { button } from '../mixins/button'; import { button } from '../mixins/button';
import { openType } from '../mixins/open-type';
VantComponent({ VantComponent({
mixins: [button], mixins: [button, openType],
props: { props: {
plain: Boolean, plain: Boolean,
block: Boolean, block: Boolean,

View File

@ -1,5 +1,6 @@
<button <button
class="custom-class van-button {{ classes }}" class="custom-class van-button {{ classes }}"
id="{{ id }}"
disabled="{{ disabled }}" disabled="{{ disabled }}"
hover-class="button-hover" hover-class="button-hover"
open-type="{{ openType }}" open-type="{{ openType }}"
@ -14,11 +15,11 @@
send-message-img="{{ sendMessageImg }}" send-message-img="{{ sendMessageImg }}"
show-message-card="{{ showMessageCard }}" show-message-card="{{ showMessageCard }}"
bind:tap="onClick" bind:tap="onClick"
bindcontact="bindcontact" bindcontact="bindContact"
bindgetuserinfo="bindgetuserinfo" bindgetuserinfo="bindGetUserInfo"
bindgetphonenumber="bindgetphonenumber" bindgetphonenumber="bindGetPhoneNumber"
binderror="binderror" binderror="bindError"
bindopensetting="bindopensetting" bindopensetting="bindOpenSetting"
> >
<van-loading <van-loading
wx:if="{{ loading }}" wx:if="{{ loading }}"

5
dist/col/index.js vendored
View File

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

View File

@ -2,12 +2,14 @@ import { basic } from '../mixins/basic';
import { observe } from '../mixins/observer/index'; import { observe } from '../mixins/observer/index';
function mapKeys(source, target, map) { function mapKeys(source, target, map) {
Object.keys(map).forEach(key => { Object.keys(map).forEach(key => {
if (source[key]) {
target[map[key]] = source[key]; target[map[key]] = source[key];
}
}); });
} }
function VantComponent(sfc) { function VantComponent(vantOptions) {
const options = {}; const options = {};
mapKeys(sfc, options, { mapKeys(vantOptions, options, {
data: 'data', data: 'data',
props: 'properties', props: 'properties',
mixins: 'behaviors', mixins: 'behaviors',
@ -15,10 +17,16 @@ function VantComponent(sfc) {
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');
@ -26,7 +34,7 @@ function VantComponent(sfc) {
options.behaviors = options.behaviors || []; options.behaviors = options.behaviors || [];
options.behaviors.push(basic); options.behaviors.push(basic);
// map field to form-field behavior // map field to form-field behavior
if (sfc.field) { if (vantOptions.field) {
options.behaviors.push('wx://form-field'); options.behaviors.push('wx://form-field');
} }
// add default options // add default options
@ -34,7 +42,7 @@ function VantComponent(sfc) {
multipleSlots: true, multipleSlots: true,
addGlobalClass: true addGlobalClass: true
}; };
observe(sfc, options); observe(vantOptions, options);
Component(options); Component(options);
} }
export { VantComponent }; export { VantComponent };

32
dist/dialog/index.js vendored
View File

@ -1,25 +1,15 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
import { openType } from '../mixins/open-type';
VantComponent({ VantComponent({
mixins: [openType],
props: { props: {
show: Boolean,
title: String, title: String,
message: String, message: String,
useSlot: Boolean, useSlot: Boolean,
asyncClose: Boolean, asyncClose: Boolean,
showCancelButton: Boolean, showCancelButton: Boolean,
confirmButtonOpenType: String, confirmButtonOpenType: String,
show: {
type: Boolean,
observer(show) {
if (!show) {
this.setData({
loading: {
confirm: false,
cancel: false
}
});
}
}
},
zIndex: { zIndex: {
type: Number, type: Number,
value: 100 value: 100
@ -51,6 +41,18 @@ VantComponent({
cancel: false cancel: false
} }
}, },
watch: {
show(show) {
if (!show) {
this.setData({
loading: {
confirm: false,
cancel: false
}
});
}
}
},
methods: { methods: {
onConfirm() { onConfirm() {
this.handleAction('confirm'); this.handleAction('confirm');
@ -70,7 +72,9 @@ VantComponent({
this.onClose(action); this.onClose(action);
}, },
close() { close() {
this.setData({ show: false }); this.setData({
show: false
});
}, },
onClose(action) { onClose(action) {
if (!this.data.asyncClose) { if (!this.data.asyncClose) {

View File

@ -37,6 +37,11 @@
custom-class="van-dialog__confirm" custom-class="van-dialog__confirm"
open-type="{{ confirmButtonOpenType }}" open-type="{{ confirmButtonOpenType }}"
bind:click="onConfirm" bind:click="onConfirm"
bindcontact="bindContact"
bindgetuserinfo="bindGetUserInfo"
bindgetphonenumber="bindGetPhoneNumber"
binderror="bindError"
bindopensetting="bindOpenSetting"
> >
{{ confirmButtonText }} {{ confirmButtonText }}
</van-button> </van-button>

25
dist/mixins/button.js vendored
View File

@ -1,7 +1,6 @@
export const button = Behavior({ export const button = Behavior({
properties: { properties: {
loading: Boolean, id: String,
openType: String,
appParameter: String, appParameter: String,
sendMessageTitle: String, sendMessageTitle: String,
sendMessagePath: String, sendMessagePath: String,
@ -24,27 +23,5 @@ export const button = Behavior({
type: String, type: String,
value: '' value: ''
} }
},
methods: {
bindgetuserinfo(event = {}) {
this.$emit('getuserinfo', event.detail);
},
bindcontact(event = {}) {
this.$emit('contact', event.detail);
},
bindgetphonenumber(event = {}) {
this.$emit('getphonenumber', event.detail);
},
bindopensetting(event = {}) {
this.$emit('opensetting', event.detail);
},
binderror(event = {}) {
this.$emit('error', event.detail);
}
} }
}); });

View File

@ -1,10 +1,25 @@
import { behavior } from './behavior'; import { behavior } from './behavior';
import { observeProps } from './props'; import { observeProps } from './props';
export function observe(sfc, options) { export function observe(vantOptions, options) {
if (sfc.computed) { const { watch, computed } = vantOptions;
if (watch) {
const props = options.properties || {};
Object.keys(watch).forEach(key => {
if (key in props) {
let prop = props[key];
if (prop === null || !prop.type) {
prop = { type: prop };
}
prop.observer = watch[key];
props[key] = prop;
}
});
options.properties = props;
}
if (computed) {
options.behaviors.push(behavior); options.behaviors.push(behavior);
options.methods = options.methods || {}; options.methods = options.methods || {};
options.methods.$options = () => sfc; options.methods.$options = () => vantOptions;
if (options.properties) { if (options.properties) {
observeProps(options.properties); observeProps(options.properties);
} }

22
dist/mixins/open-type.js vendored Normal file
View File

@ -0,0 +1,22 @@
export const openType = Behavior({
properties: {
openType: String
},
methods: {
bindGetUserInfo(event) {
this.$emit('getuserinfo', event.detail);
},
bindContact(event) {
this.$emit('contact', event.detail);
},
bindGetPhoneNumber(event) {
this.$emit('getphonenumber', event.detail);
},
bindOpenSetting(event) {
this.$emit('opensetting', event.detail);
},
bindError(event) {
this.$emit('error', event.detail);
}
}
});

View File

@ -9,7 +9,6 @@ export const touch = Behavior({
this.startX = event.touches[0].clientX; this.startX = event.touches[0].clientX;
this.startY = event.touches[0].clientY; this.startY = event.touches[0].clientY;
}, },
touchMove(event) { touchMove(event) {
const touch = event.touches[0]; const touch = event.touches[0];
this.deltaX = touch.clientX - this.startX; this.deltaX = touch.clientX - this.startX;

View File

@ -3,37 +3,36 @@ export const transition = function(showDefaultValue) {
properties: { properties: {
customStyle: String, customStyle: String,
show: { show: {
value: showDefaultValue,
type: Boolean, type: Boolean,
observer(value) { value: showDefaultValue,
if (value) { observer: 'observeShow'
this.show();
} else {
this.setData({
type: 'leave'
});
}
}
}, },
duration: { duration: {
type: Number, type: Number,
value: 300 value: 300
} }
}, },
data: { data: {
type: '', type: '',
inited: false, inited: false,
display: false display: false
}, },
attached() { attached() {
if (this.data.show) { if (this.data.show) {
this.show(); this.show();
} }
}, },
methods: { methods: {
observeShow(value) {
if (value) {
this.show();
}
else {
this.setData({
type: 'leave'
});
}
},
show() { show() {
this.setData({ this.setData({
inited: true, inited: true,
@ -41,7 +40,6 @@ export const transition = function(showDefaultValue) {
type: 'enter' type: 'enter'
}); });
}, },
onAnimationEnd() { onAnimationEnd() {
if (!this.data.show) { if (!this.data.show) {
this.setData({ this.setData({

View File

@ -1,8 +1,6 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
classes: ['title-class'], classes: ['title-class'],
props: { props: {
title: String, title: String,
leftText: String, leftText: String,
@ -14,12 +12,10 @@ VantComponent({
value: 1 value: 1
} }
}, },
methods: { methods: {
onClickLeft() { onClickLeft() {
this.$emit('click-left'); this.$emit('click-left');
}, },
onClickRight() { onClickRight() {
this.$emit('click-right'); this.$emit('click-right');
} }

View File

@ -1,16 +1,11 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
const FONT_COLOR = '#f60'; const FONT_COLOR = '#f60';
const BG_COLOR = '#fff7cc'; const BG_COLOR = '#fff7cc';
VantComponent({ VantComponent({
props: { props: {
text: { text: {
type: String, type: String,
value: '', value: ''
observer() {
this.setData({}, this.init);
}
}, },
mode: { mode: {
type: String, type: String,
@ -49,7 +44,6 @@ VantComponent({
value: BG_COLOR value: BG_COLOR
} }
}, },
data: { data: {
show: true, show: true,
hasRightIcon: false, hasRightIcon: false,
@ -60,7 +54,11 @@ VantComponent({
resetAnimation: null, resetAnimation: null,
timer: null timer: null
}, },
watch: {
text() {
this.setData({}, this.init);
}
},
created() { created() {
if (this.data.mode) { if (this.data.mode) {
this.setData({ this.setData({
@ -68,12 +66,10 @@ VantComponent({
}); });
} }
}, },
destroyed() { destroyed() {
const { timer } = this.data; const { timer } = this.data;
timer && clearTimeout(timer); timer && clearTimeout(timer);
}, },
methods: { methods: {
init() { init() {
this.getRect('.van-notice-bar__content').then(rect => { this.getRect('.van-notice-bar__content').then(rect => {
@ -83,17 +79,12 @@ VantComponent({
this.setData({ this.setData({
width: rect.width width: rect.width
}); });
this.getRect('.van-notice-bar__content-wrap').then(rect => { this.getRect('.van-notice-bar__content-wrap').then(rect => {
if (!rect || !rect.width) { if (!rect || !rect.width) {
return; return;
} }
const wrapWidth = rect.width; const wrapWidth = rect.width;
const { const { width, speed, scrollable, delay } = this.data;
width, speed, scrollable, delay
} = this.data;
if (scrollable && wrapWidth < width) { if (scrollable && wrapWidth < width) {
const elapse = width / speed * 1000; const elapse = width / speed * 1000;
const animation = wx.createAnimation({ const animation = wx.createAnimation({
@ -105,7 +96,6 @@ VantComponent({
duration: 0, duration: 0,
timeingFunction: 'linear' timeingFunction: 'linear'
}); });
this.setData({ this.setData({
elapse, elapse,
wrapWidth, wrapWidth,
@ -118,11 +108,8 @@ VantComponent({
}); });
}); });
}, },
scroll() { scroll() {
const { const { animation, resetAnimation, wrapWidth, elapse, speed } = this.data;
animation, resetAnimation, wrapWidth, elapse, speed
} = this.data;
resetAnimation.translateX(wrapWidth).step(); resetAnimation.translateX(wrapWidth).step();
const animationData = animation.translateX(-(elapse * speed) / 1000).step(); const animationData = animation.translateX(-(elapse * speed) / 1000).step();
this.setData({ this.setData({
@ -133,16 +120,13 @@ VantComponent({
animationData: animationData.export() animationData: animationData.export()
}); });
}, 100); }, 100);
const timer = setTimeout(() => { const timer = setTimeout(() => {
this.scroll(); this.scroll();
}, elapse); }, elapse);
this.setData({ this.setData({
timer timer
}); });
}, },
onClickIcon() { onClickIcon() {
const { timer } = this.data; const { timer } = this.data;
timer && clearTimeout(timer); timer && clearTimeout(timer);
@ -151,7 +135,6 @@ VantComponent({
timer: null timer: null
}); });
}, },
onClick(event) { onClick(event) {
this.$emit('click', event); this.$emit('click', event);
} }

View File

@ -1,5 +1,4 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
props: { props: {
text: String, text: String,
@ -16,23 +15,19 @@ VantComponent({
value: 3000 value: 3000
} }
}, },
methods: { methods: {
show() { show() {
const { duration } = this.data; const { duration } = this.data;
clearTimeout(this.timer); clearTimeout(this.timer);
this.setData({ this.setData({
show: true show: true
}); });
if (duration > 0 && duration !== Infinity) { if (duration > 0 && duration !== Infinity) {
this.timer = setTimeout(() => { this.timer = setTimeout(() => {
this.hide(); this.hide();
}, duration); }, duration);
} }
}, },
hide() { hide() {
clearTimeout(this.timer); clearTimeout(this.timer);
this.setData({ this.setData({

View File

@ -1,23 +1,17 @@
import { isObj } from '../common/utils'; import { isObj } from '../common/utils';
const defaultOptions = { const defaultOptions = {
selector: '#van-notify', selector: '#van-notify',
duration: 3000 duration: 3000
}; };
function parseOptions(text) { function parseOptions(text) {
return isObj(text) ? text : { text }; return isObj(text) ? text : { text };
} }
export default function Notify(options = {}) { export default function Notify(options = {}) {
const pages = getCurrentPages(); const pages = getCurrentPages();
const ctx = pages[pages.length - 1]; const ctx = pages[pages.length - 1];
options = Object.assign({}, defaultOptions, parseOptions(options)); options = Object.assign({}, defaultOptions, parseOptions(options));
const el = ctx.selectComponent(options.selector); const el = ctx.selectComponent(options.selector);
delete options.selector; delete options.selector;
if (el) { if (el) {
el.setData(options); el.setData(options);
el.show(); el.show();

View File

@ -1,5 +1,4 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
props: { props: {
show: Boolean, show: Boolean,
@ -10,12 +9,10 @@ VantComponent({
value: 1 value: 1
} }
}, },
methods: { methods: {
onClick() { onClick() {
this.$emit('click'); this.$emit('click');
}, },
// for prevent touchmove // for prevent touchmove
noop() { } noop() { }
} }

2
dist/panel/index.js vendored
View File

@ -1,8 +1,6 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
classes: ['footer-class'], classes: ['footer-class'],
props: { props: {
desc: String, desc: String,
title: String, title: String,

4
dist/popup/index.js vendored
View File

@ -1,9 +1,7 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
import { transition } from '../mixins/transition'; import { transition } from '../mixins/transition';
VantComponent({ VantComponent({
mixins: [transition(false)], mixins: [transition(false)],
props: { props: {
transition: String, transition: String,
customStyle: String, customStyle: String,
@ -25,11 +23,9 @@ VantComponent({
value: 'center' value: 'center'
} }
}, },
methods: { methods: {
onClickOverlay() { onClickOverlay() {
this.$emit('click-overlay'); this.$emit('click-overlay');
if (this.data.closeOnClickOverlay) { if (this.data.closeOnClickOverlay) {
this.$emit('close'); this.$emit('close');
} }

View File

@ -1,103 +1,63 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
props: { props: {
inactive: { inactive: Boolean,
type: Boolean, percentage: Number,
observer() { pivotText: String,
this.setPivotStyle(); pivotColor: String,
this.setPortionStyle();
}
},
pivotColor: {
type: String,
observer: 'setPivotStyle'
},
percentage: {
type: Number,
observer() {
this.setText();
this.setPortionStyle();
}
},
showPivot: { showPivot: {
type: Boolean, type: Boolean,
value: true, value: true
observer: 'getWidth'
},
pivotText: {
type: String,
observer() {
this.setText();
this.getWidth();
}
}, },
color: { color: {
type: String, type: String,
value: '#38f', value: '#38f'
observer() {
this.setPivotStyle();
this.setPortionStyle();
}
}, },
textColor: { textColor: {
type: String, type: String,
value: '#fff', value: '#fff'
observer: 'setPivotStyle'
} }
}, },
data: { data: {
pivotWidth: 0, pivotWidth: 0,
progressWidth: 0 progressWidth: 0
}, },
watch: {
pivotText: 'getWidth',
showPivot: 'getWidth'
},
computed: {
portionStyle() {
const width = (this.data.progressWidth - this.data.pivotWidth) * this.data.percentage / 100 + 'px';
const background = this.getCurrentColor();
return `width: ${width}; background: ${background}; `;
},
pivotStyle() {
const color = this.data.textColor;
const background = this.data.pivotColor || this.getCurrentColor();
return `color: ${color}; background: ${background}`;
},
text() {
return this.data.pivotText || this.data.percentage + '%';
}
},
mounted() { mounted() {
this.setText();
this.setPivotStyle();
this.getWidth(); this.getWidth();
}, },
methods: { methods: {
getCurrentColor() { getCurrentColor() {
return this.data.inactive ? '#cacaca' : this.data.color; return this.data.inactive ? '#cacaca' : this.data.color;
}, },
setText() {
this.setData({
text: this.data.pivotText || this.data.percentage + '%'
});
},
setPortionStyle() {
const width = (this.data.progressWidth - this.data.pivotWidth) * this.data.percentage / 100 + 'px';
const background = this.getCurrentColor();
this.setData({
portionStyle: `width: ${width}; background: ${background}; `
});
},
setPivotStyle() {
const color = this.data.textColor;
const background = this.data.pivotColor || this.getCurrentColor();
this.setData({
pivotStyle: `color: ${color}; background: ${background}`
});
},
getWidth() { getWidth() {
this.getRect('.van-progress').then(rect => { this.getRect('.van-progress').then(rect => {
this.setData({ this.setData({
progressWidth: rect.width progressWidth: rect.width
}); });
this.setPortionStyle();
}); });
this.getRect('.van-progress__pivot').then(rect => { this.getRect('.van-progress__pivot').then(rect => {
this.setData({ this.setData({
pivotWidth: rect.width || 0 pivotWidth: rect.width || 0
}); });
this.setPortionStyle();
}); });
} }
} }

View File

@ -1,8 +1,7 @@
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,27 +10,23 @@ VantComponent({
disabled: disabled || target.data.disabled disabled: disabled || target.data.disabled
}); });
} }
}
}, },
props: { props: {
value: { value: null,
type: null, disabled: Boolean
observer(value) { },
watch: {
value(value) {
const children = this.getRelationNodes('../radio/index'); const children = this.getRelationNodes('../radio/index');
children.forEach(child => { children.forEach(child => {
child.setData({ value }); child.setData({ value });
}); });
}
}, },
disabled: { disabled(disabled) {
type: Boolean,
observer(disabled) {
const children = this.getRelationNodes('../radio/index'); const children = this.getRelationNodes('../radio/index');
children.forEach(child => { children.forEach(child => {
child.setData({ disabled: disabled || children.data.disabled }); child.setData({ disabled: disabled || child.data.disabled });
}); });
} }
} }
}
}); });

21
dist/radio/index.js vendored
View File

@ -1,14 +1,10 @@
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'],
props: { props: {
name: null, name: null,
value: null, value: null,
@ -16,7 +12,6 @@ VantComponent({
labelDisabled: Boolean, labelDisabled: Boolean,
labelPosition: String labelPosition: String
}, },
computed: { computed: {
iconClass() { iconClass() {
const { disabled, name, value } = this.data; const { disabled, name, value } = this.data;
@ -27,19 +22,15 @@ VantComponent({
}); });
} }
}, },
methods: { methods: {
emitChange(value) { emitChange(value) {
const parent = this.getRelationNodes('../radio-group/index')[0]; const instance = this.getRelationNodes('../radio-group/index')[0] || this;
(parent || this).$emit('input', value); instance.$emit('input', value);
(parent || this).$emit('change', value); instance.$emit('change', value);
}, },
onChange(event) { onChange(event) {
const { value } = event.detail; this.emitChange(event.detail.value);
this.emitChange(value);
}, },
onClickLabel() { onClickLabel() {
if (!this.data.disabled && !this.data.labelDisabled) { if (!this.data.disabled && !this.data.labelDisabled) {
this.emitChange(this.data.name); this.emitChange(this.data.name);

25
dist/row/index.js vendored
View File

@ -1,39 +1,34 @@
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: {
gutter: { gutter: Number
type: Number, },
observer: 'setGutter' watch: {
} gutter: 'setGutter'
}, },
mounted() { mounted() {
if (this.data.gutter) { if (this.data.gutter) {
this.setGutter(); this.setGutter();
} }
}, },
methods: { methods: {
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);
}); });
} }

11
dist/search/index.js vendored
View File

@ -1,10 +1,7 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
field: true, field: true,
classes: ['cancel-class'], classes: ['cancel-class'],
props: { props: {
focus: Boolean, focus: Boolean,
disabled: Boolean, disabled: Boolean,
@ -21,27 +18,25 @@ VantComponent({
value: -1 value: -1
} }
}, },
data: {
value: ''
},
methods: { methods: {
onChange(event) { onChange(event) {
this.setData({ value: event.detail }); this.setData({ value: event.detail });
this.$emit('change', event.detail); this.$emit('change', event.detail);
}, },
onCancel() { onCancel() {
this.setData({ value: '' }); this.setData({ value: '' });
this.$emit('cancel'); this.$emit('cancel');
this.$emit('change', ''); this.$emit('change', '');
}, },
onSearch() { onSearch() {
this.$emit('search', this.data.value); this.$emit('search', this.data.value);
}, },
onFocus() { onFocus() {
this.$emit('focus'); this.$emit('focus');
}, },
onBlur() { onBlur() {
this.$emit('blur'); this.$emit('blur');
} }

26
dist/slider/index.js vendored
View File

@ -1,9 +1,7 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
import { touch } from '../mixins/touch'; import { touch } from '../mixins/touch';
VantComponent({ VantComponent({
mixins: [touch], mixins: [touch],
props: { props: {
disabled: Boolean, disabled: Boolean,
max: { max: {
@ -27,56 +25,48 @@ VantComponent({
value: '2px' value: '2px'
} }
}, },
created() { created() {
this.updateValue(this.data.value); this.updateValue(this.data.value);
}, },
methods: { methods: {
onTouchStart(event) { onTouchStart(event) {
if (this.data.disabled) return; if (this.data.disabled)
return;
this.touchStart(event); this.touchStart(event);
this.startValue = this.format(this.data.value); this.startValue = this.format(this.data.value);
}, },
onTouchMove(event) { onTouchMove(event) {
if (this.data.disabled) return; if (this.data.disabled)
return;
this.touchMove(event); this.touchMove(event);
this.getRect('.van-slider').then(rect => { this.getRect('.van-slider').then(rect => {
const diff = this.deltaX / rect.width * 100; const diff = this.deltaX / rect.width * 100;
this.updateValue(this.startValue + diff); this.updateValue(this.startValue + diff);
}); });
}, },
onTouchEnd() { onTouchEnd() {
if (this.data.disabled) return; if (this.data.disabled)
return;
this.updateValue(this.data.value, true); this.updateValue(this.data.value, true);
}, },
onClick(event) { onClick(event) {
if (this.data.disabled) return; if (this.data.disabled)
return;
this.getRect(rect => { this.getRect(rect => {
const value = (event.detail.x - rect.left) / rect.width * 100; const value = (event.detail.x - rect.left) / rect.width * 100;
this.updateValue(value, true); this.updateValue(value, true);
}); });
}, },
updateValue(value, end) { updateValue(value, end) {
value = this.format(value); value = this.format(value);
this.setData({ this.setData({
value, value,
barStyle: `width: ${value}%; height: ${this.data.barHeight};` barStyle: `width: ${value}%; height: ${this.data.barHeight};`
}); });
if (end) { if (end) {
this.$emit('change', value); this.$emit('change', value);
} }
}, },
format(value) { format(value) {
const { max, min, step } = this.data; const { max, min, step } = this.data;
return Math.round(Math.max(min, Math.min(value, max)) / step) * step; return Math.round(Math.max(min, Math.min(value, max)) / step) * step;

16
dist/stepper/index.js vendored
View File

@ -1,18 +1,14 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
// Note that the bitwise operators and shift operators operate on 32-bit ints // Note that the bitwise operators and shift operators operate on 32-bit ints
// so in that case, the max safe integer is 2^31-1, or 2147483647 // so in that case, the max safe integer is 2^31-1, or 2147483647
const MAX = 2147483647; const MAX = 2147483647;
VantComponent({ VantComponent({
field: true, field: true,
classes: [ classes: [
'input-class', 'input-class',
'plus-class', 'plus-class',
'minus-class' 'minus-class'
], ],
props: { props: {
integer: Boolean, integer: Boolean,
disabled: Boolean, disabled: Boolean,
@ -30,50 +26,44 @@ VantComponent({
value: 1 value: 1
} }
}, },
data: {
value: 0
},
created() { created() {
this.setData({ this.setData({
value: this.range(this.data.value) value: this.range(this.data.value)
}); });
}, },
methods: { methods: {
// limit value range // limit value range
range(value) { range(value) {
return Math.max(Math.min(this.data.max, value), this.data.min); return Math.max(Math.min(this.data.max, value), this.data.min);
}, },
onInput(event) { onInput(event) {
const { value = '' } = event.detail || {}; const { value = '' } = event.detail || {};
this.triggerInput(value); this.triggerInput(value);
}, },
onChange(type) { onChange(type) {
if (this[`${type}Disabled`]) { if (this[`${type}Disabled`]) {
this.$emit('overlimit', type); this.$emit('overlimit', type);
return; return;
} }
const diff = type === 'minus' ? -this.data.step : +this.data.step; const diff = type === 'minus' ? -this.data.step : +this.data.step;
const value = Math.round((this.data.value + diff) * 100) / 100; const value = Math.round((this.data.value + diff) * 100) / 100;
this.triggerInput(this.range(value)); this.triggerInput(this.range(value));
this.$emit(type); this.$emit(type);
}, },
onBlur(event) { onBlur(event) {
const value = this.range(this.data.value); const value = this.range(this.data.value);
this.triggerInput(value); this.triggerInput(value);
this.$emit('blur', event); this.$emit('blur', event);
}, },
onMinus() { onMinus() {
this.onChange('minus'); this.onChange('minus');
}, },
onPlus() { onPlus() {
this.onChange('plus'); this.onChange('plus');
}, },
triggerInput(value) { triggerInput(value) {
this.setData({ value }); this.setData({ value });
this.$emit('change', value); this.$emit('change', value);

23
dist/steps/index.js vendored
View File

@ -1,16 +1,9 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
props: { props: {
icon: String, icon: String,
steps: { steps: Array,
type: Array, active: Number,
observer: 'formatSteps'
},
active: {
type: Number,
observer: 'formatSteps'
},
direction: { direction: {
type: String, type: String,
value: 'horizontal' value: 'horizontal'
@ -20,11 +13,13 @@ VantComponent({
value: '#06bf04' value: '#06bf04'
} }
}, },
watch: {
steps: 'formatSteps',
active: 'formatSteps'
},
created() { created() {
this.formatSteps(); this.formatSteps();
}, },
methods: { methods: {
formatSteps() { formatSteps() {
const { steps } = this.data; const { steps } = this.data;
@ -33,16 +28,14 @@ VantComponent({
}); });
this.setData({ steps }); this.setData({ steps });
}, },
getStatus(index) { getStatus(index) {
const { active } = this.data; const { active } = this.data;
if (index < active) { if (index < active) {
return 'finish'; return 'finish';
} else if (index === active) { }
else if (index === active) {
return 'process'; return 'process';
} }
return ''; return '';
} }
} }

View File

@ -1,12 +1,10 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
classes: [ classes: [
'bar-class', 'bar-class',
'price-class', 'price-class',
'button-class' 'button-class'
], ],
props: { props: {
tip: [String, Boolean], tip: [String, Boolean],
type: Number, type: Number,
@ -24,22 +22,18 @@ VantComponent({
value: 'danger' value: 'danger'
} }
}, },
computed: { computed: {
hasPrice() { hasPrice() {
return typeof this.data.price === 'number'; return typeof this.data.price === 'number';
}, },
priceStr() { priceStr() {
return (this.data.price / 100).toFixed(2); return (this.data.price / 100).toFixed(2);
}, },
tipStr() { tipStr() {
const { tip } = this.data; const { tip } = this.data;
return typeof tip === 'string' ? tip : ''; return typeof tip === 'string' ? tip : '';
} }
}, },
methods: { methods: {
onSubmit(event) { onSubmit(event) {
this.$emit('submit', event.detail); this.$emit('submit', event.detail);

View File

@ -1,29 +1,25 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
field: true, field: true,
props: { props: {
title: String, title: String,
border: Boolean, border: Boolean,
checked: Boolean,
loading: Boolean, loading: Boolean,
disabled: Boolean, disabled: Boolean,
checked: {
type: Boolean,
observer(value) {
this.setData({ value });
}
},
size: { size: {
type: String, type: String,
value: '26px' value: '26px'
} }
}, },
watch: {
checked(value) {
this.setData({ value });
}
},
created() { created() {
this.setData({ value: this.data.checked }); this.setData({ value: this.data.checked });
}, },
methods: { methods: {
onChange(event) { onChange(event) {
this.$emit('change', event.detail); this.$emit('change', event.detail);

17
dist/switch/index.js vendored
View File

@ -1,29 +1,24 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
field: true, field: true,
classes: ['node-class'], classes: ['node-class'],
props: { props: {
checked: Boolean,
loading: Boolean, loading: Boolean,
disabled: Boolean, disabled: Boolean,
checked: {
type: Boolean,
observer(value) {
this.setData({ value });
}
},
size: { size: {
type: String, type: String,
value: '30px' value: '30px'
} }
}, },
watch: {
checked(value) {
this.setData({ value });
}
},
created() { created() {
this.setData({ value: this.data.checked }); this.setData({ value: this.data.checked });
}, },
methods: { methods: {
onClick() { onClick() {
if (!this.data.disabled && !this.data.loading) { if (!this.data.disabled && !this.data.loading) {

34
dist/tab/index.js vendored
View File

@ -1,19 +1,25 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
relation: {
name: 'tabs',
type: 'ancestor'
},
props: { props: {
disabled: { title: String,
type: Boolean, disabled: Boolean
observer() { },
data: {
inited: false,
active: false
},
watch: {
disabled() {
const parent = this.getRelationNodes('../tabs/index')[0]; const parent = this.getRelationNodes('../tabs/index')[0];
if (parent) { if (parent) {
parent.updateTabs(); parent.updateTabs();
} }
}
}, },
title: { title() {
type: String,
observer() {
const parent = this.getRelationNodes('../tabs/index')[0]; const parent = this.getRelationNodes('../tabs/index')[0];
if (parent) { if (parent) {
parent.setLine(); parent.setLine();
@ -21,16 +27,4 @@ VantComponent({
} }
} }
} }
},
relations: {
'../tabs/index': {
type: 'ancestor'
}
},
data: {
inited: false,
active: false
}
}); });

View File

@ -1,23 +1,18 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
props: { props: {
info: null, info: null,
icon: String, icon: String,
dot: Boolean dot: Boolean
}, },
relation: {
relations: { name: 'tabbar',
'../tabbar/index': {
type: 'ancestor' type: 'ancestor'
}
}, },
data: { data: {
active: false, active: false,
count: 0 count: 0
}, },
methods: { methods: {
onClick() { onClick() {
const parent = this.getRelationNodes('../tabbar/index')[0]; const parent = this.getRelationNodes('../tabbar/index')[0];
@ -26,7 +21,6 @@ VantComponent({
} }
this.$emit('click'); this.$emit('click');
}, },
setActive(data) { setActive(data) {
const { active, count } = this.data; const { active, count } = this.data;
if (active !== data.active || count !== data.count) { if (active !== data.active || count !== data.count) {

51
dist/tabbar/index.js vendored
View File

@ -1,14 +1,23 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
props: { relation: {
active: { name: 'tabbar-item',
type: Number, type: 'descendant',
observer(active) { linked(target) {
this.setData({ currentActive: active }); this.data.items.push(target);
setTimeout(() => {
this.setActiveItem(); this.setActiveItem();
});
},
unlinked(target) {
this.data.items = this.data.items.filter(item => item !== target);
setTimeout(() => {
this.setActiveItem();
});
} }
}, },
props: {
active: Number,
fixed: { fixed: {
type: Boolean, type: Boolean,
value: true value: true
@ -18,36 +27,19 @@ VantComponent({
value: 1 value: 1
} }
}, },
data: { data: {
items: [], items: [],
currentActive: -1 currentActive: -1
}, },
watch: {
active(active) {
this.setData({ currentActive: active });
this.setActiveItem();
}
},
created() { created() {
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) => {
@ -57,7 +49,6 @@ VantComponent({
}); });
}); });
}, },
onChange(child) { onChange(child) {
const active = this.data.items.indexOf(child); const active = this.data.items.indexOf(child);
if (active !== this.data.currentActive && active !== -1) { if (active !== this.data.currentActive && active !== -1) {

61
dist/tabs/index.js vendored
View File

@ -1,10 +1,8 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
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,
@ -12,7 +10,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({
@ -21,22 +18,13 @@ VantComponent({
}); });
this.setActiveTab(); this.setActiveTab();
} }
}
}, },
props: { props: {
color: { color: String,
type: String, lineWidth: Number,
observer: 'setLine'
},
lineWidth: {
type: Number,
observer: 'setLine'
},
active: { active: {
type: null, type: null,
value: 0, value: 0
observer: 'setActiveTab'
}, },
type: { type: {
type: String, type: String,
@ -52,26 +40,29 @@ VantComponent({
}, },
swipeThreshold: { swipeThreshold: {
type: Number, type: Number,
value: 4, value: 4
observer() {
this.setData({
scrollable: this.data.tabs.length > this.data.swipeThreshold
});
}
} }
}, },
data: { data: {
tabs: [], tabs: [],
lineStyle: '', lineStyle: '',
scrollLeft: 0 scrollLeft: 0,
scrollable: false
},
watch: {
swipeThreshold() {
this.setData({
scrollable: this.data.tabs.length > this.data.swipeThreshold
});
},
color: 'setLine',
lineWidth: 'setLine',
active: 'setActiveTab'
}, },
mounted() { mounted() {
this.setLine(); this.setLine();
this.scrollIntoView(); this.scrollIntoView();
}, },
methods: { methods: {
updateTabs() { updateTabs() {
const { tabs } = this.data; const { tabs } = this.data;
@ -81,24 +72,22 @@ VantComponent({
}); });
this.setActiveTab(); this.setActiveTab();
}, },
trigger(eventName, index) { trigger(eventName, index) {
this.$emit(eventName, { this.$emit(eventName, {
index, index,
title: this.data.tabs[index].data.title title: this.data.tabs[index].data.title
}); });
}, },
onTap(event) { onTap(event) {
const { index } = event.currentTarget.dataset; const { index } = event.currentTarget.dataset;
if (this.data.tabs[index].data.disabled) { if (this.data.tabs[index].data.disabled) {
this.trigger('disabled', index); this.trigger('disabled', index);
} else { }
else {
this.trigger('click', index); this.trigger('click', index);
this.setActive(index); this.setActive(index);
} }
}, },
setActive(active) { setActive(active) {
if (active !== this.data.active) { if (active !== this.data.active) {
this.trigger('change', active); this.trigger('change', active);
@ -106,12 +95,10 @@ VantComponent({
this.setActiveTab(); this.setActiveTab();
} }
}, },
setLine() { setLine() {
if (this.data.type !== 'line') { if (this.data.type !== 'line') {
return; return;
} }
this.getRect('.van-tab', true).then(rects => { this.getRect('.van-tab', true).then(rects => {
const rect = rects[this.data.active]; const rect = rects[this.data.active];
const width = this.data.lineWidth || rect.width; const width = this.data.lineWidth || rect.width;
@ -119,7 +106,6 @@ VantComponent({
.slice(0, this.data.active) .slice(0, this.data.active)
.reduce((prev, curr) => prev + curr.width, 0); .reduce((prev, curr) => prev + curr.width, 0);
left += (rect.width - width) / 2; left += (rect.width - width) / 2;
this.setData({ this.setData({
lineStyle: ` lineStyle: `
width: ${width}px; width: ${width}px;
@ -130,39 +116,32 @@ VantComponent({
}); });
}); });
}, },
setActiveTab() { setActiveTab() {
this.data.tabs.forEach((item, index) => { this.data.tabs.forEach((item, index) => {
const data = { const data = {
active: index === this.data.active active: index === this.data.active
}; };
if (data.active) { if (data.active) {
data.inited = true; data.inited = true;
} }
if (data.active !== item.instance.data.active) { if (data.active !== item.instance.data.active) {
item.instance.setData(data); item.instance.setData(data);
} }
}); });
this.setLine(); this.setLine();
this.scrollIntoView(); this.scrollIntoView();
}, },
// scroll active tab into view // scroll active tab into view
scrollIntoView(immediate) { scrollIntoView(immediate) {
if (!this.data.scrollable) { if (!this.data.scrollable) {
return; return;
} }
this.getRect('.van-tab', true).then(tabRects => { this.getRect('.van-tab', true).then(tabRects => {
const tabRect = tabRects[this.data.active]; const tabRect = tabRects[this.data.active];
const offsetLeft = tabRects const offsetLeft = tabRects
.slice(0, this.data.active) .slice(0, this.data.active)
.reduce((prev, curr) => prev + curr.width, 0); .reduce((prev, curr) => prev + curr.width, 0);
const tabWidth = tabRect.width; const tabWidth = tabRect.width;
this.getRect('.van-tabs__nav').then(navRect => { this.getRect('.van-tabs__nav').then(navRect => {
const navWidth = navRect.width; const navWidth = navRect.width;
this.setData({ this.setData({

1
dist/tag/index.js vendored
View File

@ -1,5 +1,4 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
props: { props: {
type: String, type: String,

3
dist/toast/index.js vendored
View File

@ -1,5 +1,4 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
props: { props: {
show: Boolean, show: Boolean,
@ -23,14 +22,12 @@ VantComponent({
value: 'middle' value: 'middle'
} }
}, },
methods: { methods: {
clear() { clear() {
this.setData({ this.setData({
show: false show: false
}); });
}, },
// for prevent touchmove // for prevent touchmove
noop() { } noop() { }
} }

32
dist/toast/toast.js vendored
View File

@ -1,5 +1,4 @@
import { isObj } from '../common/utils'; import { isObj } from '../common/utils';
const defaultOptions = { const defaultOptions = {
type: 'text', type: 'text',
mask: false, mask: false,
@ -12,61 +11,42 @@ const defaultOptions = {
loadingType: 'circular', loadingType: 'circular',
selector: '#van-toast' selector: '#van-toast'
}; };
let queue = []; let queue = [];
let currentOptions = { ...defaultOptions }; let currentOptions = Object.assign({}, defaultOptions);
function parseOptions(message) { function parseOptions(message) {
return isObj(message) ? message : { message }; return isObj(message) ? message : { message };
} }
const Toast = (options = {}) => {
function Toast(options = {}) { options = Object.assign({}, currentOptions, parseOptions(options));
options = {
...currentOptions,
...parseOptions(options)
};
const pages = getCurrentPages(); const pages = getCurrentPages();
const ctx = pages[pages.length - 1]; const ctx = pages[pages.length - 1];
const toast = ctx.selectComponent(options.selector); const toast = ctx.selectComponent(options.selector);
delete options.selector; delete options.selector;
queue.push(toast); queue.push(toast);
toast.setData(options); toast.setData(options);
clearTimeout(toast.timer); clearTimeout(toast.timer);
if (options.duration > 0) { if (options.duration > 0) {
toast.timer = setTimeout(() => { toast.timer = setTimeout(() => {
toast.clear(); toast.clear();
queue = queue.filter(item => item !== toast); queue = queue.filter(item => item !== toast);
}, options.duration); }, options.duration);
} }
return toast; return toast;
}; };
const createMethod = type => options => Toast(Object.assign({ type }, parseOptions(options)));
const createMethod = type => options => Toast({
type, ...parseOptions(options)
});
['loading', 'success', 'fail'].forEach(method => { ['loading', 'success', 'fail'].forEach(method => {
Toast[method] = createMethod(method); Toast[method] = createMethod(method);
}); });
Toast.clear = () => {
Toast.clear = all => {
queue.forEach(toast => { queue.forEach(toast => {
toast.clear(); toast.clear();
}); });
queue = []; queue = [];
}; };
Toast.setDefaultOptions = options => { Toast.setDefaultOptions = options => {
Object.assign(currentOptions, options); Object.assign(currentOptions, options);
}; };
Toast.resetDefaultOptions = () => { Toast.resetDefaultOptions = () => {
currentOptions = { ...defaultOptions }; currentOptions = Object.assign({}, defaultOptions);
}; };
export default Toast; export default Toast;

View File

@ -1,9 +1,7 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
import { transition } from '../mixins/transition'; import { transition } from '../mixins/transition';
VantComponent({ VantComponent({
mixins: [transition(true)], mixins: [transition(true)],
props: { props: {
name: { name: {
type: String, type: String,

View File

@ -1,20 +1,11 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
const ITEM_HEIGHT = 44; const ITEM_HEIGHT = 44;
VantComponent({ VantComponent({
props: { props: {
items: { items: Array,
type: Array,
observer() {
this.updateSubItems();
this.updateMainHeight();
}
},
mainActiveIndex: { mainActiveIndex: {
type: Number, type: Number,
value: 0, value: 0
observer: 'updateSubItems'
}, },
activeId: { activeId: {
type: Number, type: Number,
@ -22,51 +13,51 @@ VantComponent({
}, },
maxHeight: { maxHeight: {
type: Number, type: Number,
value: 300, value: 300
observer() {
this.updateItemHeight();
this.updateMainHeight();
}
} }
}, },
data: { data: {
subItems: [], subItems: [],
mainHeight: 0, mainHeight: 0,
itemHeight: 0 itemHeight: 0
}, },
watch: {
items() {
this.updateSubItems();
this.updateMainHeight();
},
maxHeight() {
this.updateItemHeight();
this.updateMainHeight();
},
mainActiveIndex: 'updateSubItems'
},
methods: { methods: {
// 当一个子项被选择时 // 当一个子项被选择时
onSelectItem(event) { onSelectItem(event) {
this.$emit('click-item', event.currentTarget.dataset.item); this.$emit('click-item', event.currentTarget.dataset.item);
}, },
// 当一个导航被点击时 // 当一个导航被点击时
onClickNav(event) { onClickNav(event) {
const { index } = event.currentTarget.dataset; const { index } = event.currentTarget.dataset;
this.$emit('click-nav', { index }); this.$emit('click-nav', { index });
}, },
// 更新子项列表 // 更新子项列表
updateSubItems() { updateSubItems() {
const selectedItem = this.data.items[this.data.mainActiveIndex] || {}; const selectedItem = this.data.items[this.data.mainActiveIndex] || {};
this.setData({ subItems: selectedItem.children || [] }); this.setData({ subItems: selectedItem.children || [] });
this.updateItemHeight(); this.updateItemHeight();
}, },
// 更新组件整体高度,根据最大高度和当前组件需要展示的高度来决定 // 更新组件整体高度,根据最大高度和当前组件需要展示的高度来决定
updateMainHeight() { updateMainHeight() {
const maxHeight = Math.max(this.data.items.length * ITEM_HEIGHT, this.data.subItems.length * ITEM_HEIGHT); const maxHeight = Math.max(this.data.items.length * ITEM_HEIGHT, this.data.subItems.length * ITEM_HEIGHT);
this.setData({ mainHeight: Math.min(maxHeight, this.data.maxHeight) }); this.setData({ mainHeight: Math.min(maxHeight, this.data.maxHeight) });
}, },
// 更新子项列表高度,根据可展示的最大高度和当前子项列表的高度决定 // 更新子项列表高度,根据可展示的最大高度和当前子项列表的高度决定
updateItemHeight() { updateItemHeight() {
this.setData({ itemHeight: Math.min(this.data.subItems.length * ITEM_HEIGHT, this.data.maxHeight) }); this.setData({
itemHeight: Math.min(this.data.subItems.length * ITEM_HEIGHT, this.data.maxHeight)
});
} }
} }
}); });