vant-fork
Version:
Lightweight Mobile UI Components built on Vue
159 lines (149 loc) • 3.65 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import Field from '../field';
import VanButton from '../button';
import Dialog from '../dialog';
import Toast from '../toast';
import validateMobile from '../utils/validate/mobile';
import create from '../utils/create';
var defaultContact = {
id: '',
tel: '',
name: ''
};
export default create({
render: function render() {
var _vm = this;
var _h = _vm.$createElement;
var _c = _vm._self._c || _h;
return _c('div', {
class: _vm.b()
}, [_c('cell-group', [_c('field', {
attrs: {
"maxlength": "30",
"label": _vm.$t('name'),
"placeholder": _vm.$t('nameEmpty'),
"error": _vm.errorInfo.name
},
on: {
"focus": function focus($event) {
_vm.onFocus('name');
}
},
model: {
value: _vm.data.name,
callback: function callback($$v) {
_vm.$set(_vm.data, "name", $$v);
},
expression: "data.name"
}
}), _c('field', {
attrs: {
"type": "tel",
"label": _vm.$t('tel'),
"placeholder": _vm.$t('telEmpty'),
"error": _vm.errorInfo.tel
},
on: {
"focus": function focus($event) {
_vm.onFocus('tel');
}
},
model: {
value: _vm.data.tel,
callback: function callback($$v) {
_vm.$set(_vm.data, "tel", $$v);
},
expression: "data.tel"
}
})], 1), _c('div', {
class: _vm.b('buttons')
}, [_c('van-button', {
attrs: {
"block": "",
"loading": _vm.isSaving,
"type": "danger"
},
on: {
"click": _vm.onSave
}
}, [_vm._v(_vm._s(_vm.$t('save')))]), _vm.isEdit ? _c('van-button', {
attrs: {
"block": "",
"loading": _vm.isDeleting
},
on: {
"click": _vm.onDelete
}
}, [_vm._v(_vm._s(_vm.$t('delete')))]) : _vm._e()], 1)], 1);
},
name: 'contact-edit',
components: {
Field: Field,
VanButton: VanButton
},
props: {
isEdit: Boolean,
isSaving: Boolean,
isDeleting: Boolean,
contactInfo: {
type: Object,
default: function _default() {
return _extends({}, defaultContact);
}
},
telValidator: {
type: Function,
default: validateMobile
}
},
data: function data() {
return {
data: _extends({}, this.defaultContact, this.contactInfo),
errorInfo: {
name: false,
tel: false
}
};
},
watch: {
contactInfo: function contactInfo(val) {
this.data = val;
}
},
methods: {
onFocus: function onFocus(key) {
this.errorInfo[key] = false;
},
getErrorMessageByKey: function getErrorMessageByKey(key) {
var value = this.data[key].trim();
switch (key) {
case 'name':
return value ? '' : this.$t('nameEmpty');
case 'tel':
return this.telValidator(value) ? '' : this.$t('telInvalid');
}
},
onSave: function onSave() {
var _this = this;
var isValid = ['name', 'tel'].every(function (item) {
var msg = _this.getErrorMessageByKey(item);
if (msg) {
_this.errorInfo[item] = true;
Toast(msg);
}
return !msg;
});
if (isValid && !this.isSaving) {
this.$emit('save', this.data);
}
},
onDelete: function onDelete() {
var _this2 = this;
Dialog.confirm({
message: this.$t('confirmDelete')
}).then(function () {
_this2.$emit('delete', _this2.data);
});
}
}
});