[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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 47 deletions

View File

@ -62,7 +62,6 @@ export default {
| area-list | 地区列表 | `Object` | - | - | | area-list | 地区列表 | `Object` | - | - |
| address-info | 收货人信息 | `Object` | `{}` | - | | address-info | 收货人信息 | `Object` | `{}` | - |
| search-result | 详细地址搜索结果 | `Array` | `[]` | - | | search-result | 详细地址搜索结果 | `Array` | `[]` | - |
| address-text | "地址"文案前缀 | `String` | `收货` | - |
| show-postal | 是否显示邮政编码 | `Boolean` | `false` | - | | show-postal | 是否显示邮政编码 | `Boolean` | `false` | - |
| show-delete | 是否显示删除按钮 | `Boolean` | `false` | - | | show-delete | 是否显示删除按钮 | `Boolean` | `false` | - |
| show-set-default | 是否显示默认地址栏 | `Boolean` | `false` | - | | show-set-default | 是否显示默认地址栏 | `Boolean` | `false` | - |

View File

@ -5,6 +5,7 @@ import routes from './router';
import Vant, { Lazyload } from 'packages'; import Vant, { Lazyload } from 'packages';
import VantDoc from 'vant-doc'; import VantDoc from 'vant-doc';
import 'packages/vant-css/src/index.css'; import 'packages/vant-css/src/index.css';
import 'packages/vant-css/src/icon-local.css';
import 'vant-doc/src/helper/touch-simulator'; import 'vant-doc/src/helper/touch-simulator';
import './components/nprogress.css'; import './components/nprogress.css';

View File

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

View File

@ -4,10 +4,10 @@
name="success" name="success"
class="van-checkbox__icon" class="van-checkbox__icon"
:class="[ :class="[
`van-checkbox--${shape}`, `van-checkbox--${shape}`, {
{ 'van-checkbox--disabled': isDisabled }, 'van-checkbox--disabled': isDisabled,
{ 'van-checkbox--checked': isChecked } 'van-checkbox--checked': isChecked
]" }]"
@click="onClick" @click="onClick"
/> />
<span class="van-checkbox__label" @click="onClick('label')"> <span class="van-checkbox__label" @click="onClick('label')">

View File

@ -60,11 +60,11 @@ export default {
addressOverlimit: '详细地址不能超过200个字符', addressOverlimit: '详细地址不能超过200个字符',
addressEmpty: '请填写详细地址', addressEmpty: '请填写详细地址',
postalEmpty: '邮政编码格式不正确', postalEmpty: '邮政编码格式不正确',
defaultAddress: text => `设为默认${text}地址`, defaultAddress: '设为默认收货地址',
deleteAddress: text => `删除${text}地址`, deleteAddress: '删除收货地址',
confirmDelete: text => `确定要删除这个${text}地址么`, confirmDelete: '确定要删除这个收货地址么',
label: { label: {
name: text => `${text}`, name: '收货人',
postal: '邮政编码' postal: '邮政编码'
}, },
placeholder: { placeholder: {