[type] getCurrentPages (#626)

This commit is contained in:
neverland 2018-09-21 20:17:48 +08:00 committed by GitHub
parent 265f609bf3
commit f35edf2662
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 222 additions and 247 deletions

25
dist/field/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: ['input-class'], classes: ['input-class'],
props: { props: {
icon: String, icon: String,
label: String, label: String,
@ -53,12 +50,10 @@ VantComponent({
value: '90px' value: '90px'
} }
}, },
data: { data: {
focused: false, focused: false,
showClear: false showClear: false
}, },
computed: { computed: {
inputClass() { inputClass() {
const { data } = this; const { data } = this;
@ -70,7 +65,6 @@ VantComponent({
}); });
} }
}, },
methods: { methods: {
onInput(event) { onInput(event) {
const { value = '' } = event.detail || {}; const { value = '' } = event.detail || {};
@ -81,36 +75,28 @@ VantComponent({
showClear: this.getShowClear({ value }) showClear: this.getShowClear({ value })
}); });
}, },
onFocus() {
onFocus(event) { this.$emit('focus');
this.$emit('focus', event);
this.setData({ this.setData({
focused: true, focused: true,
showClear: this.getShowClear({ focused: true }) showClear: this.getShowClear({ focused: true })
}); });
}, },
onBlur() {
onBlur(event) {
this.focused = false; this.focused = false;
this.$emit('blur', event); this.$emit('blur');
this.setData({ this.setData({
focused: false, focused: false,
showClear: this.getShowClear({ focused: false }) showClear: this.getShowClear({ focused: false })
}); });
}, },
onClickIcon() { onClickIcon() {
this.$emit('click-icon'); this.$emit('click-icon');
}, },
getShowClear(options) { getShowClear(options) {
const { focused = this.data.focused, value = this.data.value } = options; const { focused = this.data.focused, value = this.data.value } = options;
return (this.data.clearable && focused && value !== '' && !this.data.readonly);
return (
this.data.clearable && focused && value !== '' && !this.data.readonly
);
}, },
onClear() { onClear() {
this.setData({ this.setData({
value: '', value: '',
@ -119,7 +105,6 @@ VantComponent({
this.$emit('input', ''); this.$emit('input', '');
this.$emit('change', ''); this.$emit('change', '');
}, },
onConfirm() { onConfirm() {
this.$emit('confirm', this.data.value); this.$emit('confirm', this.data.value);
} }

2
dist/icon/index.js vendored
View File

@ -1,5 +1,4 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
VantComponent({ VantComponent({
props: { props: {
info: null, info: null,
@ -11,7 +10,6 @@ VantComponent({
value: 'van-icon' value: 'van-icon'
} }
}, },
methods: { methods: {
onClick() { onClick() {
this.$emit('click'); this.$emit('click');

View File

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

View File

@ -1,22 +1,18 @@
import { classNames } from '../common/class-names'; import { classNames } from '../common/class-names';
export const basic = Behavior({ export const basic = Behavior({
methods: { methods: {
classNames, classNames,
$emit() { $emit() {
this.triggerEvent.apply(this, arguments); this.triggerEvent.apply(this, arguments);
}, },
getRect(selector, all) { getRect(selector, all) {
return new Promise((resolve, reject) => { return new Promise(resolve => {
wx.createSelectorQuery() wx.createSelectorQuery()
.in(this)[all ? 'selectAll' : 'select'](selector) .in(this)[all ? 'selectAll' : 'select'](selector)
.boundingClientRect(rect => { .boundingClientRect(rect => {
if (all && Array.isArray(rect) && rect.length) { if (all && Array.isArray(rect) && rect.length) {
resolve(rect); resolve(rect);
} }
if (!all && rect) { if (!all && rect) {
resolve(rect); resolve(rect);
} }

View File

@ -3,33 +3,26 @@ export const behavior = Behavior({
if (!this.$options) { if (!this.$options) {
return; return;
} }
const cache = {}; const cache = {};
const { setData } = this; const { setData } = this;
const { computed } = this.$options(); const { computed } = this.$options();
const keys = Object.keys(computed); const keys = Object.keys(computed);
const calcComputed = () => { const calcComputed = () => {
const needUpdate = {}; const needUpdate = {};
keys.forEach(key => { keys.forEach(key => {
const value = computed[key].call(this); const value = computed[key].call(this);
if (cache[key] !== value) { if (cache[key] !== value) {
cache[key] = needUpdate[key] = value; cache[key] = needUpdate[key] = value;
} }
}); });
return needUpdate; return needUpdate;
}; };
Object.defineProperty(this, 'setData', { writable: true }); Object.defineProperty(this, 'setData', { writable: true });
this.setData = (data, callback) => { this.setData = (data, callback) => {
data && setData.call(this, data, callback); data && setData.call(this, data, callback);
setData.call(this, calcComputed()); setData.call(this, calcComputed());
}; };
}, },
attached() { attached() {
this.setData(); this.setData();
} }

View File

@ -1,12 +1,10 @@
import { behavior } from './behavior'; import { behavior } from './behavior';
import { observeProps } from './props'; import { observeProps } from './props';
export function observe(sfc, options) { export function observe(sfc, options) {
if (sfc.computed) { if (sfc.computed) {
options.behaviors.push(behavior); options.behaviors.push(behavior);
options.methods = options.methods || {}; options.methods = options.methods || {};
options.methods.$options = () => sfc; options.methods.$options = () => sfc;
if (options.properties) { if (options.properties) {
observeProps(options.properties); observeProps(options.properties);
} }

View File

@ -2,15 +2,13 @@ export function observeProps(props) {
if (!props) { if (!props) {
return; return;
} }
Object.keys(props).forEach(key => { Object.keys(props).forEach(key => {
let prop = props[key]; let prop = props[key];
if (prop === null || !prop.type) { if (prop === null || !prop.type) {
prop = { type: prop }; prop = { type: prop };
} }
let { observer } = prop; let { observer } = prop;
prop.observer = function() { prop.observer = function () {
if (observer) { if (observer) {
if (typeof observer === 'string') { if (typeof observer === 'string') {
observer = this[observer]; observer = this[observer];
@ -19,7 +17,6 @@ export function observeProps(props) {
} }
this.setData(); this.setData();
}; };
props[key] = prop; props[key] = prop;
}); });
} }

View File

@ -60,7 +60,7 @@ VantComponent({
}, },
computed: { computed: {
inputClass() { inputClass(): string {
const { data } = this; const { data } = this;
return this.classNames('input-class', 'van-field__input', { return this.classNames('input-class', 'van-field__input', {
'van-field--error': data.error, 'van-field--error': data.error,
@ -72,7 +72,7 @@ VantComponent({
}, },
methods: { methods: {
onInput(event) { onInput(event: Weapp.Event) {
const { value = '' } = event.detail || {}; const { value = '' } = event.detail || {};
this.$emit('input', value); this.$emit('input', value);
this.$emit('change', value); this.$emit('change', value);
@ -82,17 +82,17 @@ VantComponent({
}); });
}, },
onFocus(event) { onFocus() {
this.$emit('focus', event); this.$emit('focus');
this.setData({ this.setData({
focused: true, focused: true,
showClear: this.getShowClear({ focused: true }) showClear: this.getShowClear({ focused: true })
}); });
}, },
onBlur(event) { onBlur() {
this.focused = false; this.focused = false;
this.$emit('blur', event); this.$emit('blur');
this.setData({ this.setData({
focused: false, focused: false,
showClear: this.getShowClear({ focused: false }) showClear: this.getShowClear({ focused: false })
@ -103,7 +103,7 @@ VantComponent({
this.$emit('click-icon'); this.$emit('click-icon');
}, },
getShowClear(options) { getShowClear(options): boolean {
const { focused = this.data.focused, value = this.data.value } = options; const { focused = this.data.focused, value = this.data.value } = options;
return ( return (

View File

@ -9,7 +9,7 @@ export const basic = Behavior({
}, },
getRect(selector, all) { getRect(selector, all) {
return new Promise((resolve, reject) => { return new Promise(resolve => {
wx.createSelectorQuery() wx.createSelectorQuery()
.in(this)[all ? 'selectAll' : 'select'](selector) .in(this)[all ? 'selectAll' : 'select'](selector)
.boundingClientRect(rect => { .boundingClientRect(rect => {

15
types/weapp.d.ts vendored
View File

@ -1,10 +1,15 @@
declare function Component(options: any): void; type BehaviorOptions = {
[key: string]: any & ThisType<any>
};
interface wx { type WX = {
[key: string]: any [key: string]: any
} }
declare const wx: wx; declare const wx: WX;
declare function Behavior(options: BehaviorOptions): void;
declare function Component(options: any): void;
declare function getCurrentPages(): Weapp.Page[];
declare namespace Weapp { declare namespace Weapp {
interface Component { interface Component {
@ -38,4 +43,8 @@ declare namespace Weapp {
touches: Array<Touch>; touches: Array<Touch>;
changedTouches: Array<Touch>; changedTouches: Array<Touch>;
} }
interface Page {
selectComponent(selector: string): Component
}
} }