diff --git a/example/pages/area/index.js b/example/pages/area/index.js
index ef1f5201..d9945fcc 100644
--- a/example/pages/area/index.js
+++ b/example/pages/area/index.js
@@ -1,4 +1,5 @@
import Page from '../../common/page';
+import Toast from '../../dist/toast/toast';
Page({
data: {
@@ -20,6 +21,16 @@ Page({
},
onChange(event) {
+ const { values } = event.detail;
+
+ Toast(values.map(item => item.name).join('-'));
+ },
+
+ onConfirm(event) {
+ console.log(event);
+ },
+
+ onCancel(event) {
console.log(event);
}
});
diff --git a/example/pages/area/index.wxml b/example/pages/area/index.wxml
index d05a9d9e..236a9b25 100644
--- a/example/pages/area/index.wxml
+++ b/example/pages/area/index.wxml
@@ -2,6 +2,9 @@
@@ -10,6 +13,8 @@
value="{{ value }}"
loading="{{ loading }}"
area-list="{{ areaList }}"
+ bind:change="onChange"
+ bind:confirm="onConfirm"
/>
@@ -20,6 +25,9 @@
loading="{{ loading }}"
area-list="{{ areaList }}"
bind:change="onChange"
- bind:confirm="onChange"
+ bind:confirm="onConfirm"
/>
+
+
+
diff --git a/packages/area/index.json b/packages/area/index.json
index 01077f5d..a778e91c 100644
--- a/packages/area/index.json
+++ b/packages/area/index.json
@@ -1,6 +1,6 @@
{
"component": true,
"usingComponents": {
- "van-loading": "../loading/index"
+ "van-picker": "../picker/index"
}
}
diff --git a/packages/area/index.less b/packages/area/index.less
index b82bf76d..e69de29b 100644
--- a/packages/area/index.less
+++ b/packages/area/index.less
@@ -1,71 +0,0 @@
-@import '../common/style/var.less';
-
-.van-picker {
- -webkit-text-size-adjust: 100%; /* avoid iOS text size adjust */
- position: relative;
- overflow: hidden;
- background-color: @white;
- user-select: none;
-
- &__toolbar {
- display: flex;
- justify-content: space-between;
- height: 44px;
- line-height: 44px;
- }
-
- &__cancel,
- &__confirm {
- color: @blue;
- padding: 0 15px;
- font-size: 14px;
-
- &:active {
- background-color: @active-color;
- }
- }
-
- &__title {
- max-width: 50%;
- font-size: 16px;
- font-weight: 500;
- text-align: center;
- }
-
- &__columns {
- position: relative;
- }
-
- &__loading {
- display: flex;
- z-index: 4;
- position: absolute;
- top: 0;
- right: 0;
- bottom: 0;
- left: 0;
- align-items: center;
- justify-content: center;
- background-color: rgba(255, 255, 255, .9);
- }
-
- &-column {
- flex: 1;
- overflow: hidden;
- font-size: 16px;
- text-align: center;
-
- &__item {
- padding: 0 5px;
- color: @text-color;
-
- &--selected {
- font-weight: 500;
- }
-
- &--disabled {
- opacity: .3;
- }
- }
- }
-}
diff --git a/packages/area/index.ts b/packages/area/index.ts
index c4f28f73..52c9532e 100644
--- a/packages/area/index.ts
+++ b/packages/area/index.ts
@@ -1,11 +1,13 @@
import { VantComponent } from '../common/component';
type AreaItem = {
- name: string;
- code: string;
+ name: string
+ code: string
};
VantComponent({
+ classes: ['active-class', 'toolbar-class', 'column-class'],
+
props: {
title: String,
value: String,
@@ -29,8 +31,8 @@ VantComponent({
},
data: {
- pickerValue: [0, 0, 0],
- columns: []
+ columns: [{ values: [] }, { values: [] }, { values: [] }],
+ displayColumns: [{ values: [] }, { values: [] }, { values: [] }]
},
watch: {
@@ -39,55 +41,60 @@ VantComponent({
this.setValues();
},
- areaList: 'setValues'
+ areaList: 'setValues',
+
+ columnsNum(value) {
+ this.set({
+ displayColumns: this.data.columns.slice(0, +value)
+ });
+ }
},
methods: {
- onCancel() {
- this.$emit('cancel', {
- values: this.getValues(),
- indexs: this.getIndexs(),
- detail: this.getDetail()
- });
+ getPicker() {
+ if (this.picker == null) {
+ this.picker = this.selectComponent('.van-area__picker');
+ }
+ return this.picker;
},
- onConfirm() {
- this.$emit('confirm', {
- values: this.getValues(),
- indexs: this.getIndexs(),
- detail: this.getDetail()
- });
+ onCancel(event) {
+ this.emit('cancel', event.detail);
+ },
+
+ onConfirm(event) {
+ this.emit('confirm', event.detail);
+ },
+
+ emit(type, detail) {
+ detail.values = detail.value;
+ delete detail.value;
+ this.$emit(type, detail);
},
onChange(event: Weapp.Event) {
- const { value } = event.detail;
- const { pickerValue } = this.data;
- const displayColumns = this.getDisplayColumns();
- const index = pickerValue.findIndex(
- (item, index) => item !== value[index]
- );
- const values = displayColumns[index];
-
- if (index < 0 || value[index] < 0 || !values[value[index]]) {
- return;
- }
-
- this.code = values[value[index]].code;
+ const { index, picker, value } = event.detail;
+ this.code = value[index].code;
this.setValues();
this.$emit('change', {
- picker: this,
- values: this.getValues(),
+ picker,
+ values: picker.getValues(),
index
});
},
+ getConfig(type: string) {
+ const { areaList } = this.data;
+ return (areaList && areaList[`${type}_list`]) || {};
+ },
+
getList(type: string, code?: string): AreaItem[] {
let result = [];
if (type !== 'province' && !code) {
return result;
}
- const list = this.data.areaList && this.data.areaList[`${type}_list`] || {};
+ const list = this.getConfig(type);
result = Object.keys(list).map(code => ({
code,
name: list[code]
@@ -125,44 +132,35 @@ VantComponent({
},
setValues() {
- let code =
- this.code ||
- (this.data.areaList &&
- Object.keys(this.data.areaList.county_list || {})[0]) ||
- '';
+ const county = this.getConfig('county');
+ let code = this.code || Object.keys(county)[0] || '';
const province = this.getList('province');
const city = this.getList('city', code.slice(0, 2));
- this.set({
- 'columns[0]': province,
- 'columns[1]': city
- });
+ const picker = this.getPicker();
- if (city.length && code.slice(2, 4) === '00') {
- code = city[0].code;
+ if (!picker) {
+ return;
}
- this.set({
- 'columns[2]': this.getList('county', code.slice(0, 4)),
- pickerValue: [
- this.getIndex('province', code),
- this.getIndex('city', code),
- this.getIndex('county', code)
- ]
- });
+ picker.setColumnValues(0, province);
+ picker.setColumnValues(1, city);
+
+ if (city.length && code.slice(2, 4) === '00') {
+ ;[{ code }] = city;
+ }
+
+ picker.setColumnValues(2, this.getList('county', code.slice(0, 4)));
+ picker.setIndexes([
+ this.getIndex('province', code),
+ this.getIndex('city', code),
+ this.getIndex('county', code)
+ ]);
},
getValues() {
- const { pickerValue = [] } = this.data;
- const displayColumns = this.getDisplayColumns();
- return displayColumns
- .map((option, index) => option[pickerValue[index]])
- .filter(value => !!value);
- },
-
- getIndexs() {
- const { pickerValue, columnsNum } = this.data;
- return pickerValue.slice(0, columnsNum);
+ const picker = this.getPicker();
+ return picker ? picker.getValues().filter(value => !!value) : [];
},
getDetail() {
@@ -196,11 +194,6 @@ VantComponent({
reset() {
this.code = '';
this.setValues();
- },
-
- getDisplayColumns() {
- const { columns = [], columnsNum } = this.data;
- return columns.slice(0, +columnsNum);
}
}
});
diff --git a/packages/area/index.wxml b/packages/area/index.wxml
index bec4249b..636070b1 100644
--- a/packages/area/index.wxml
+++ b/packages/area/index.wxml
@@ -1,35 +1,16 @@
-
-
- 取消
- {{ title }}
- 确定
-
-
-
-
-
-
-
-
- {{ item.name }}
-
-
-
+
diff --git a/packages/picker/index.less b/packages/picker/index.less
index f134472b..dbf53f98 100644
--- a/packages/picker/index.less
+++ b/packages/picker/index.less
@@ -38,7 +38,8 @@
}
&__column {
- flex: 1;
+ flex: 1 1;
+ width: 0;
}
&__loading {
diff --git a/packages/picker/index.ts b/packages/picker/index.ts
index 115fe3f9..896f43a7 100644
--- a/packages/picker/index.ts
+++ b/packages/picker/index.ts
@@ -38,6 +38,10 @@ VantComponent({
}
},
+ beforeCreate() {
+ this.children = [];
+ },
+
methods: {
noop() {},