[bugfix] AddressEdit: should auto select area when created (#748)

This commit is contained in:
neverland
2018-03-22 11:38:58 +08:00
committed by GitHub
parent d3114d34b6
commit 14d7f9dfb0
5 changed files with 44 additions and 47 deletions

View File

@@ -4,7 +4,7 @@
<field
maxlength="15"
:placeholder="$t('name')"
:label="$t('label.name', computedAddressText)"
:label="$t('label.name')"
v-model="data.name"
:error="errorInfo.name"
@focus="onFocus('name')"
@@ -54,7 +54,7 @@
v-if="showSetDefault"
v-show="!hideBottomFields"
v-model="data.is_default"
:title="$t('defaultAddress', computedAddressText)"
:title="$t('defaultAddress')"
/>
</cell-group>
<div v-show="!hideBottomFields" class="van-address-edit__buttons">
@@ -62,7 +62,7 @@
{{ $t('save') }}
</van-button>
<van-button block :loading="isDeleting" @click="onDelete" v-if="isEdit">
{{ $t('deleteAddress', computedAddressText) }}
{{ $t('deleteAddress') }}
</van-button>
</div>
<popup v-model="showArea" position="bottom">
@@ -124,7 +124,6 @@ export default create({
showPostal: Boolean,
showSetDefault: Boolean,
showSearchResult: Boolean,
addressText: String,
addressInfo: {
type: Object,
default: () => ({ ...defaultAddress })
@@ -156,38 +155,12 @@ export default create({
};
},
watch: {
addressInfo: {
handler(val) {
this.data = {
...defaultAddress,
...val
};
if (val.area_code) {
this.setAreaCode(val.area_code);
}
},
deep: true
},
areaList() {
if (this.data.area_code) {
this.setAreaCode(this.data.area_code);
}
}
},
computed: {
// hide bottom field when use search && detail get focused
hideBottomFields() {
return this.searchResult.length && this.detailFocused;
},
computedAddressText() {
return this.addressText || this.$t('addressText');
},
areaListLoaded() {
return isObj(this.areaList) && Object.keys(this.areaList).length;
},
@@ -197,6 +170,28 @@ export default create({
}
},
watch: {
addressInfo: {
handler(val) {
this.data = {
...defaultAddress,
...val
};
this.setAreaCode(val.area_code);
},
deep: true
},
areaList() {
this.setAreaCode(this.data.area_code);
}
},
created() {
this.setAreaCode(this.data.area_code);
},
methods: {
onFocus(key) {
this.errorInfo[key] = false;
@@ -213,18 +208,20 @@ export default create({
if (values.length !== 3 || values.some(value => +value.code === -1)) {
return Toast(this.$t('areaEmpty'));
}
this.data.area_code = values[2].code;
this.assignAreaValues(values);
this.showArea = false;
this.$emit('change-area', values);
},
assignAreaValues(values) {
Object.assign(this.data, {
province: values[0].name,
city: values[1].name,
county: values[2].name,
area_code: values[2].code
});
if (values.length >= 3) {
Object.assign(this.data, {
province: values[0].name,
city: values[1].name,
county: values[2].name
});
}
},
onSave() {
@@ -277,7 +274,7 @@ export default create({
}
Dialog.confirm({
message: this.$t('confirmDelete', this.computedAddressText)
message: this.$t('confirmDelete')
}).then(() => {
this.$emit('delete', this.data);
});
@@ -291,7 +288,7 @@ export default create({
// set area code to area component
setAreaCode(code) {
this.data.area_code = code;
this.data.area_code = code || '';
this.$nextTick(() => {
this.$nextTick(() => {
const { area } = this.$refs;