mirror of
https://gitee.com/vant-contrib/vant-weapp.git
synced 2025-04-06 03:58:05 +08:00
fix(Area): fix initial option error @rex-zsd (#1196)
This commit is contained in:
parent
94fc0d2892
commit
df1a90c86b
example/pages/area
packages
@ -1,5 +1,6 @@
|
|||||||
<demo-block title="基础用法">
|
<demo-block title="基础用法">
|
||||||
<van-area
|
<van-area
|
||||||
|
value="{{ value }}"
|
||||||
loading="{{ loading }}"
|
loading="{{ loading }}"
|
||||||
area-list="{{ areaList }}"
|
area-list="{{ areaList }}"
|
||||||
bind:change="onChange"
|
bind:change="onChange"
|
||||||
|
@ -36,14 +36,14 @@ VantComponent({
|
|||||||
},
|
},
|
||||||
|
|
||||||
watch: {
|
watch: {
|
||||||
value(value) {
|
value(value: string) {
|
||||||
this.code = value;
|
this.code = value;
|
||||||
this.setValues();
|
this.setValues();
|
||||||
},
|
},
|
||||||
|
|
||||||
areaList: 'setValues',
|
areaList: 'setValues',
|
||||||
|
|
||||||
columnsNum(value) {
|
columnsNum(value: number) {
|
||||||
this.set({
|
this.set({
|
||||||
displayColumns: this.data.columns.slice(0, +value)
|
displayColumns: this.data.columns.slice(0, +value)
|
||||||
});
|
});
|
||||||
@ -58,15 +58,15 @@ VantComponent({
|
|||||||
return this.picker;
|
return this.picker;
|
||||||
},
|
},
|
||||||
|
|
||||||
onCancel(event) {
|
onCancel(event: Weapp.Event) {
|
||||||
this.emit('cancel', event.detail);
|
this.emit('cancel', event.detail);
|
||||||
},
|
},
|
||||||
|
|
||||||
onConfirm(event) {
|
onConfirm(event: Weapp.Event) {
|
||||||
this.emit('confirm', event.detail);
|
this.emit('confirm', event.detail);
|
||||||
},
|
},
|
||||||
|
|
||||||
emit(type, detail) {
|
emit(type: string, detail) {
|
||||||
detail.values = detail.value;
|
detail.values = detail.value;
|
||||||
delete detail.value;
|
delete detail.value;
|
||||||
this.$emit(type, detail);
|
this.$emit(type, detail);
|
||||||
@ -143,19 +143,28 @@ VantComponent({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
picker.setColumnValues(0, province);
|
const stack = [];
|
||||||
picker.setColumnValues(1, city);
|
|
||||||
|
stack.push(picker.setColumnValues(0, province));
|
||||||
|
stack.push(picker.setColumnValues(1, city));
|
||||||
|
|
||||||
if (city.length && code.slice(2, 4) === '00') {
|
if (city.length && code.slice(2, 4) === '00') {
|
||||||
;[{ code }] = city;
|
;[{ code }] = city;
|
||||||
}
|
}
|
||||||
|
|
||||||
picker.setColumnValues(2, this.getList('county', code.slice(0, 4)));
|
stack.push(
|
||||||
picker.setIndexes([
|
picker.setColumnValues(2, this.getList('county', code.slice(0, 4)))
|
||||||
this.getIndex('province', code),
|
);
|
||||||
this.getIndex('city', code),
|
|
||||||
this.getIndex('county', code)
|
return Promise.all(stack)
|
||||||
]);
|
.then(() =>
|
||||||
|
picker.setIndexes([
|
||||||
|
this.getIndex('province', code),
|
||||||
|
this.getIndex('city', code),
|
||||||
|
this.getIndex('county', code)
|
||||||
|
])
|
||||||
|
)
|
||||||
|
.catch(() => {});
|
||||||
},
|
},
|
||||||
|
|
||||||
getValues() {
|
getValues() {
|
||||||
@ -177,7 +186,7 @@ VantComponent({
|
|||||||
return area;
|
return area;
|
||||||
}
|
}
|
||||||
|
|
||||||
const names = values.map(item => item.name);
|
const names = values.map((item: AreaItem) => item.name);
|
||||||
area.code = values[values.length - 1].code;
|
area.code = values[values.length - 1].code;
|
||||||
if (area.code[0] === '9') {
|
if (area.code[0] === '9') {
|
||||||
area.country = names[1] || '';
|
area.country = names[1] || '';
|
||||||
@ -193,7 +202,7 @@ VantComponent({
|
|||||||
|
|
||||||
reset() {
|
reset() {
|
||||||
this.code = '';
|
this.code = '';
|
||||||
this.setValues();
|
return this.setValues();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -33,7 +33,7 @@ function getTrueValue(formattedValue: string): number {
|
|||||||
return parseInt(formattedValue, 10);
|
return parseInt(formattedValue, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getMonthEndDay(year, month): number {
|
function getMonthEndDay(year: number, month: number): number {
|
||||||
return 32 - new Date(year, month - 1, 32).getDate();
|
return 32 - new Date(year, month - 1, 32).getDate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,19 +116,11 @@ VantComponent({
|
|||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
asyncSet(data) {
|
|
||||||
return new Promise(resolve => {
|
|
||||||
this.set(data, resolve);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
getPicker() {
|
getPicker() {
|
||||||
if (this.picker == null) {
|
if (this.picker == null) {
|
||||||
const picker = (this.picker = this.selectComponent(
|
const picker = this.picker = this.selectComponent('.van-datetime-picker');
|
||||||
'.van-datetime-picker'
|
|
||||||
));
|
|
||||||
const { setColumnValues } = picker;
|
const { setColumnValues } = picker;
|
||||||
picker.setColumnValues = (...args) =>
|
picker.setColumnValues = (...args: any) =>
|
||||||
setColumnValues.apply(picker, [...args, false]);
|
setColumnValues.apply(picker, [...args, false]);
|
||||||
}
|
}
|
||||||
return this.picker;
|
return this.picker;
|
||||||
@ -145,7 +137,7 @@ VantComponent({
|
|||||||
return { values };
|
return { values };
|
||||||
});
|
});
|
||||||
|
|
||||||
return this.asyncSet({ columns: results });
|
return this.set({ columns: results });
|
||||||
},
|
},
|
||||||
|
|
||||||
getRanges(): object[] {
|
getRanges(): object[] {
|
||||||
@ -337,11 +329,9 @@ VantComponent({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.asyncSet({ innerValue: value })
|
return this.set({ innerValue: value })
|
||||||
.then(() => this.updateColumns())
|
.then(() => this.updateColumns())
|
||||||
.then(() => {
|
.then(() => picker.setValues(values));
|
||||||
picker.setValues(values);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -1,3 +1,9 @@
|
|||||||
|
function setAsync(context: Weapp.Component, data: object) {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
context.setData(data, resolve);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
export const behavior = Behavior({
|
export const behavior = Behavior({
|
||||||
created() {
|
created() {
|
||||||
if (!this.$options) {
|
if (!this.$options) {
|
||||||
@ -28,14 +34,23 @@ export const behavior = Behavior({
|
|||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
// set data and set computed data
|
// set data and set computed data
|
||||||
set(data, callback) {
|
set(data: object, callback: Function) {
|
||||||
|
const stack = [];
|
||||||
|
|
||||||
if (data) {
|
if (data) {
|
||||||
this.setData(data, callback);
|
stack.push(setAsync(this, data));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.calcComputed) {
|
if (this.calcComputed) {
|
||||||
this.setData(this.calcComputed());
|
stack.push(setAsync(this, this.calcComputed()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Promise.all(stack).then(res => {
|
||||||
|
if (callback && typeof callback === 'function') {
|
||||||
|
callback.call(this);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -32,12 +32,15 @@ VantComponent({
|
|||||||
|
|
||||||
created() {
|
created() {
|
||||||
const { defaultIndex, initialOptions } = this.data;
|
const { defaultIndex, initialOptions } = this.data;
|
||||||
this.set({
|
this.set(
|
||||||
currentIndex: defaultIndex,
|
{
|
||||||
options: initialOptions
|
currentIndex: defaultIndex,
|
||||||
}, () => {
|
options: initialOptions
|
||||||
this.setIndex(defaultIndex);
|
},
|
||||||
});
|
() => {
|
||||||
|
this.setIndex(defaultIndex);
|
||||||
|
}
|
||||||
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
computed: {
|
computed: {
|
||||||
@ -132,16 +135,14 @@ VantComponent({
|
|||||||
setIndex(index: number, userAction: boolean) {
|
setIndex(index: number, userAction: boolean) {
|
||||||
const { data } = this;
|
const { data } = this;
|
||||||
index = this.adjustIndex(index) || 0;
|
index = this.adjustIndex(index) || 0;
|
||||||
|
const offset = -index * data.itemHeight;
|
||||||
this.set({
|
|
||||||
offset: -index * data.itemHeight
|
|
||||||
});
|
|
||||||
|
|
||||||
if (index !== data.currentIndex) {
|
if (index !== data.currentIndex) {
|
||||||
this.set({
|
return this.set({ offset, currentIndex: index }).then(() => {
|
||||||
currentIndex: index
|
userAction && this.$emit('change', index);
|
||||||
});
|
});
|
||||||
userAction && this.$emit('change', index);
|
} else {
|
||||||
|
return this.set({ offset });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -152,6 +153,7 @@ VantComponent({
|
|||||||
return this.setIndex(i);
|
return this.setIndex(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return Promise.resolve();
|
||||||
},
|
},
|
||||||
|
|
||||||
getValue() {
|
getValue() {
|
||||||
|
@ -30,10 +30,10 @@ VantComponent({
|
|||||||
value: [],
|
value: [],
|
||||||
observer(columns = []) {
|
observer(columns = []) {
|
||||||
this.simple = isSimple(columns);
|
this.simple = isSimple(columns);
|
||||||
const children = this.children = this.selectAllComponents('.van-picker__column');
|
this.children = this.selectAllComponents('.van-picker__column');
|
||||||
|
|
||||||
if (Array.isArray(children) && children.length) {
|
if (Array.isArray(this.children) && this.children.length) {
|
||||||
this.setColumns();
|
this.setColumns().catch(() => {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -49,9 +49,10 @@ VantComponent({
|
|||||||
setColumns() {
|
setColumns() {
|
||||||
const { data } = this;
|
const { data } = this;
|
||||||
const columns = this.simple ? [{ values: data.columns }] : data.columns;
|
const columns = this.simple ? [{ values: data.columns }] : data.columns;
|
||||||
columns.forEach((columns, index: number) => {
|
const stack = columns.map((column, index: number) =>
|
||||||
this.setColumnValues(index, columns.values);
|
this.setColumnValues(index, column.values)
|
||||||
});
|
);
|
||||||
|
return Promise.all(stack);
|
||||||
},
|
},
|
||||||
|
|
||||||
emit(event: Weapp.Event) {
|
emit(event: Weapp.Event) {
|
||||||
@ -99,7 +100,10 @@ VantComponent({
|
|||||||
// set column value by index
|
// set column value by index
|
||||||
setColumnValue(index: number, value: any) {
|
setColumnValue(index: number, value: any) {
|
||||||
const column = this.getColumn(index);
|
const column = this.getColumn(index);
|
||||||
column && column.setValue(value);
|
if (column) {
|
||||||
|
return column.setValue(value);
|
||||||
|
}
|
||||||
|
return Promise.reject('setColumnValue: 对应列不存在');
|
||||||
},
|
},
|
||||||
|
|
||||||
// get column option index by column index
|
// get column option index by column index
|
||||||
@ -110,7 +114,10 @@ VantComponent({
|
|||||||
// set column option index by column index
|
// set column option index by column index
|
||||||
setColumnIndex(columnIndex: number, optionIndex: number) {
|
setColumnIndex(columnIndex: number, optionIndex: number) {
|
||||||
const column = this.getColumn(columnIndex);
|
const column = this.getColumn(columnIndex);
|
||||||
column && column.setIndex(optionIndex);
|
if (column) {
|
||||||
|
return column.setIndex(optionIndex);
|
||||||
|
}
|
||||||
|
return Promise.reject('setColumnIndex: 对应列不存在');
|
||||||
},
|
},
|
||||||
|
|
||||||
// get options of column by index
|
// get options of column by index
|
||||||
@ -126,12 +133,14 @@ VantComponent({
|
|||||||
column &&
|
column &&
|
||||||
JSON.stringify(column.data.options) !== JSON.stringify(options)
|
JSON.stringify(column.data.options) !== JSON.stringify(options)
|
||||||
) {
|
) {
|
||||||
column.set({ options }, () => {
|
return column.set({ options }).then(() => {
|
||||||
if (needReset) {
|
if (needReset) {
|
||||||
column.setIndex(0);
|
column.setIndex(0);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Promise.reject('setColumnValues: 对应列不存在');
|
||||||
},
|
},
|
||||||
|
|
||||||
// get values of all columns
|
// get values of all columns
|
||||||
@ -141,21 +150,25 @@ VantComponent({
|
|||||||
|
|
||||||
// set values of all columns
|
// set values of all columns
|
||||||
setValues(values: []) {
|
setValues(values: []) {
|
||||||
values.forEach((value, index) => {
|
const stack = values.map((value, index) =>
|
||||||
this.setColumnValue(index, value);
|
this.setColumnValue(index, value)
|
||||||
});
|
);
|
||||||
|
return Promise.all(stack);
|
||||||
},
|
},
|
||||||
|
|
||||||
// get indexes of all columns
|
// get indexes of all columns
|
||||||
getIndexes() {
|
getIndexes() {
|
||||||
return this.children.map((child: Weapp.Component) => child.data.currentIndex);
|
return this.children.map(
|
||||||
|
(child: Weapp.Component) => child.data.currentIndex
|
||||||
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
// set indexes of all columns
|
// set indexes of all columns
|
||||||
setIndexes(indexes: number[]) {
|
setIndexes(indexes: number[]) {
|
||||||
indexes.forEach((optionIndex, columnIndex) => {
|
const stack = indexes.map((optionIndex, columnIndex) =>
|
||||||
this.setColumnIndex(columnIndex, optionIndex);
|
this.setColumnIndex(columnIndex, optionIndex)
|
||||||
});
|
);
|
||||||
|
return Promise.all(stack);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user