lulouis-vant
Version:
Lightweight Mobile UI Components built on Vue
423 lines (382 loc) • 10.4 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _utils = require("../utils");
var _area = _interopRequireDefault(require("../area"));
var _field = _interopRequireDefault(require("../field"));
var _popup = _interopRequireDefault(require("../popup"));
var _toast = _interopRequireDefault(require("../toast"));
var _button = _interopRequireDefault(require("../button"));
var _dialog = _interopRequireDefault(require("../dialog"));
var _Detail = _interopRequireDefault(require("./Detail"));
var _switchCell = _interopRequireDefault(require("../switch-cell"));
var _mobile = _interopRequireDefault(require("../utils/validate/mobile"));
var _use = (0, _utils.use)('address-edit'),
sfc = _use[0],
bem = _use[1],
t = _use[2];
var defaultData = {
name: '',
tel: '',
country: '',
province: '',
city: '',
county: '',
areaCode: '',
postalCode: '',
addressDetail: '',
isDefault: false
};
var _default2 = sfc({
props: {
areaList: Object,
isSaving: Boolean,
isDeleting: Boolean,
validator: Function,
showDelete: Boolean,
showPostal: Boolean,
searchResult: Array,
showSetDefault: Boolean,
showSearchResult: Boolean,
saveButtonText: String,
deleteButtonText: String,
showArea: {
type: Boolean,
default: true
},
showDetail: {
type: Boolean,
default: true
},
detailRows: {
type: Number,
default: 1
},
addressInfo: {
type: Object,
default: function _default() {
return (0, _extends2.default)({}, defaultData);
}
},
telValidator: {
type: Function,
default: _mobile.default
}
},
data: function data() {
return {
data: {},
showAreaPopup: false,
detailFocused: false,
errorInfo: {
tel: false,
name: false,
postalCode: false,
addressDetail: false
}
};
},
computed: {
areaListLoaded: function areaListLoaded() {
return (0, _utils.isObj)(this.areaList) && Object.keys(this.areaList).length;
},
areaText: function areaText() {
var _this$data = this.data,
country = _this$data.country,
province = _this$data.province,
city = _this$data.city,
county = _this$data.county,
areaCode = _this$data.areaCode;
if (areaCode) {
var arr = [country, province, city, county];
if (province && province === city) {
arr.splice(1, 1);
}
return arr.filter(function (text) {
return text;
}).join('/');
}
return '';
}
},
watch: {
addressInfo: {
handler: function handler(val) {
this.data = (0, _extends2.default)({}, defaultData, val);
this.setAreaCode(val.areaCode);
},
deep: true,
immediate: true
},
areaList: function areaList() {
this.setAreaCode(this.data.areaCode);
}
},
methods: {
onFocus: function onFocus(key) {
this.errorInfo[key] = false;
this.detailFocused = key === 'addressDetail';
this.$emit('focus', key);
},
onChangeDetail: function onChangeDetail(val) {
this.data.addressDetail = val;
this.$emit('change-detail', val);
},
onAreaConfirm: function onAreaConfirm(values) {
this.showAreaPopup = false;
this.assignAreaValues();
this.$emit('change-area', values);
},
assignAreaValues: function assignAreaValues() {
var area = this.$refs.area;
if (area) {
var detail = area.getArea();
detail.areaCode = detail.code;
delete detail.code;
(0, _extends2.default)(this.data, detail);
}
},
onSave: function onSave() {
var _this = this;
var items = ['name', 'tel', 'areaCode', 'addressDetail'];
if (this.showPostal) {
items.push('postalCode');
}
var isValid = items.every(function (item) {
var msg = _this.getErrorMessage(item);
if (msg) {
_this.errorInfo[item] = true;
(0, _toast.default)(msg);
}
return !msg;
});
if (isValid && !this.isSaving) {
this.$emit('save', this.data);
}
},
getErrorMessage: function getErrorMessage(key) {
var value = String(this.data[key] || '').trim();
if (this.validator) {
var message = this.validator(key, value);
if (message) {
return message;
}
}
switch (key) {
case 'name':
return value ? '' : t('nameEmpty');
case 'tel':
return this.telValidator(value) ? '' : t('telInvalid');
case 'areaCode':
return value ? '' : t('areaEmpty');
case 'addressDetail':
return value ? '' : t('addressEmpty');
case 'postalCode':
return value && !/^\d{6}$/.test(value) ? t('postalEmpty') : '';
}
},
onDelete: function onDelete() {
var _this2 = this;
_dialog.default.confirm({
title: t('confirmDelete')
}).then(function () {
_this2.$emit('delete', _this2.data);
}).catch(function () {
_this2.$emit('cancel-delete', _this2.data);
});
},
// get values of area component
getArea: function getArea() {
return this.$refs.area ? this.$refs.area.getValues() : [];
},
// set area code to area component
setAreaCode: function setAreaCode(code) {
this.data.areaCode = code || '';
if (code) {
this.$nextTick(this.assignAreaValues);
}
},
setAddressDetail: function setAddressDetail(value) {
this.data.addressDetail = value;
}
},
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);
};
}; // hide bottom field when use search && detail get focused
var hideBottomFields = this.searchResult.length && this.detailFocused;
return h("div", {
"class": bem()
}, [h(_field.default, {
"attrs": {
"clearable": true,
"label": t('name'),
"placeholder": t('namePlaceholder'),
"error": errorInfo.name
},
"on": {
"focus": onFocus('name')
},
"model": {
value: data.name,
callback: function callback($$v) {
data.name = $$v;
}
}
}), h(_field.default, {
"attrs": {
"clearable": true,
"type": "tel",
"label": t('tel'),
"placeholder": t('telPlaceholder'),
"error": errorInfo.tel
},
"on": {
"focus": onFocus('tel')
},
"model": {
value: data.tel,
callback: function callback($$v) {
data.tel = $$v;
}
}
}), h(_field.default, {
"directives": [{
name: "show",
value: this.showArea
}],
"attrs": {
"readonly": true,
"label": t('area'),
"placeholder": t('areaPlaceholder'),
"value": this.areaText
},
"on": {
"click": function click() {
_this3.showAreaPopup = true;
}
}
}), h(_Detail.default, {
"directives": [{
name: "show",
value: this.showDetail
}],
"attrs": {
"focused": this.detailFocused,
"value": data.addressDetail,
"error": errorInfo.addressDetail,
"detailRows": this.detailRows,
"searchResult": this.searchResult,
"showSearchResult": this.showSearchResult
},
"on": {
"focus": onFocus('addressDetail'),
"blur": function blur() {
_this3.detailFocused = false;
},
"input": this.onChangeDetail,
"select-search": function selectSearch(event) {
_this3.$emit('select-search', event);
}
}
}), this.showPostal && h(_field.default, {
"directives": [{
name: "show",
value: !hideBottomFields
}],
"attrs": {
"type": "tel",
"maxlength": "6",
"label": t('postal'),
"placeholder": t('postal'),
"error": errorInfo.postalCode
},
"on": {
"focus": onFocus('postalCode')
},
"model": {
value: data.postalCode,
callback: function callback($$v) {
data.postalCode = $$v;
}
}
}), this.$slots.default, this.showSetDefault && h(_switchCell.default, {
"directives": [{
name: "show",
value: !hideBottomFields
}],
"attrs": {
"title": t('defaultAddress')
},
"on": {
"change": function change(event) {
_this3.$emit('change-default', event);
}
},
"model": {
value: data.isDefault,
callback: function callback($$v) {
data.isDefault = $$v;
}
}
}), h("div", {
"directives": [{
name: "show",
value: !hideBottomFields
}],
"class": bem('buttons')
}, [h(_button.default, {
"attrs": {
"block": true,
"loading": this.isSaving,
"type": "danger",
"text": this.saveButtonText || t('save')
},
"on": {
"click": this.onSave
}
}), this.showDelete && h(_button.default, {
"attrs": {
"block": true,
"loading": this.isDeleting,
"text": this.deleteButtonText || t('delete')
},
"on": {
"click": this.onDelete
}
})]), h(_popup.default, {
"attrs": {
"position": "bottom",
"lazyRender": false,
"getContainer": "body"
},
"model": {
value: _this3.showAreaPopup,
callback: function callback($$v) {
_this3.showAreaPopup = $$v;
}
}
}, [h(_area.default, {
"ref": "area",
"attrs": {
"loading": !this.areaListLoaded,
"value": data.areaCode,
"areaList": this.areaList
},
"on": {
"confirm": this.onAreaConfirm,
"cancel": function cancel() {
_this3.showAreaPopup = false;
}
}
})])]);
}
});
exports.default = _default2;