UNPKG

lulouis-vant

Version:

Lightweight Mobile UI Components built on Vue

162 lines (150 loc) 3.53 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import { use } from '../utils'; import Button from '../button'; import Field from '../field'; import Toast from '../toast'; import Dialog from '../dialog'; import validateMobile from '../utils/validate/mobile'; var _use = use('contact-edit'), sfc = _use[0], bem = _use[1], t = _use[2]; var defaultContact = { tel: '', name: '' }; export default sfc({ 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({}, defaultContact, this.contactInfo), errorInfo: { name: false, tel: false } }; }, watch: { contactInfo: function contactInfo(val) { this.data = _extends({}, defaultContact, 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 ? '' : t('nameEmpty'); case 'tel': return this.telValidator(value) ? '' : 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: t('confirmDelete') }).then(function () { _this2.$emit('delete', _this2.data); }); } }, render: function render(h) { var _this3 = this; var data = this.data, errorInfo = this.errorInfo; var onFocus = function onFocus(name) { return function () { return _this3.onFocus(name); }; }; return h("div", { "class": bem() }, [h(Field, { "attrs": { "clearable": true, "maxlength": "30", "label": t('name'), "placeholder": t('nameEmpty'), "error": errorInfo.name }, "on": { "focus": onFocus('name') }, "model": { value: data.name, callback: function callback($$v) { data.name = $$v; } } }), h(Field, { "attrs": { "clearable": true, "type": "tel", "label": t('tel'), "placeholder": t('telEmpty'), "error": errorInfo.tel }, "on": { "focus": onFocus('tel') }, "model": { value: data.tel, callback: function callback($$v) { data.tel = $$v; } } }), h("div", { "class": bem('buttons') }, [h(Button, { "attrs": { "block": true, "type": "danger", "text": t('save'), "loading": this.isSaving }, "on": { "click": this.onSave } }), this.isEdit && h(Button, { "attrs": { "block": true, "text": t('delete'), "loading": this.isDeleting }, "on": { "click": this.onDelete } })])]); } });