UNPKG

lulouis-vant

Version:

Lightweight Mobile UI Components built on Vue

213 lines (187 loc) 5.18 kB
"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 _picker = _interopRequireDefault(require("../picker")); var _picker2 = _interopRequireDefault(require("../mixins/picker")); var _use = (0, _utils.use)('area'), sfc = _use[0], bem = _use[1]; var _default2 = sfc({ mixins: [_picker2.default], props: { value: String, areaList: { type: Object, default: function _default() { return {}; } }, columnsNum: { type: [String, Number], default: 3 } }, data: function data() { return { code: this.value, columns: [{ values: [] }, { values: [] }, { values: [] }] }; }, computed: { province: function province() { return this.areaList.province_list || {}; }, city: function city() { return this.areaList.city_list || {}; }, county: function county() { return this.areaList.county_list || {}; }, displayColumns: function displayColumns() { return this.columns.slice(0, +this.columnsNum); } }, watch: { value: function value() { this.code = this.value; this.setValues(); }, areaList: { deep: true, handler: function handler() { this.setValues(); } } }, mounted: function mounted() { this.setValues(); }, methods: { // get list by code getList: function getList(type, code) { var result = []; if (type !== 'province' && !code) { return result; } var list = this[type]; result = Object.keys(list).map(function (listCode) { return { code: listCode, name: list[listCode] }; }); if (code) { // oversea code if (code[0] === '9' && type === 'city') { code = '9'; } result = result.filter(function (item) { return item.code.indexOf(code) === 0; }); } return result; }, // get index by code getIndex: function getIndex(type, code) { var compareNum = type === 'province' ? 2 : type === 'city' ? 4 : 6; var list = this.getList(type, code.slice(0, compareNum - 2)); // oversea code if (code[0] === '9' && type === 'province') { compareNum = 1; } code = code.slice(0, compareNum); for (var i = 0; i < list.length; i++) { if (list[i].code.slice(0, compareNum) === code) { return i; } } return 0; }, onChange: function onChange(picker, values, index) { this.code = values[index].code; this.setValues(); this.$emit('change', picker, values, index); }, setValues: function setValues() { var code = this.code || Object.keys(this.county)[0] || ''; var picker = this.$refs.picker; var province = this.getList('province'); var city = this.getList('city', code.slice(0, 2)); if (!picker) { return; } picker.setColumnValues(0, province); picker.setColumnValues(1, city); if (city.length && code.slice(2, 4) === '00') { code = city[0].code; } picker.setColumnValues(2, this.getList('county', code.slice(0, 4))); picker.setIndexes([this.getIndex('province', code), this.getIndex('city', code), this.getIndex('county', code)]); }, getValues: function getValues() { return this.$refs.picker ? this.$refs.picker.getValues().filter(function (value) { return !!value; }) : []; }, getArea: function getArea() { var values = this.getValues(); var area = { code: '', country: '', province: '', city: '', county: '' }; if (!values.length) { return area; } var names = values.map(function (item) { return item.name; }); area.code = values[values.length - 1].code; if (area.code[0] === '9') { area.country = names[1] || ''; area.province = names[2] || ''; } else { area.province = names[0] || ''; area.city = names[1] || ''; area.county = names[2] || ''; } return area; }, reset: function reset() { this.code = ''; this.setValues(); } }, render: function render(h) { var on = (0, _extends2.default)({}, this.$listeners, { change: this.onChange }); return h(_picker.default, { "ref": "picker", "class": bem(), "attrs": { "showToolbar": true, "valueKey": "name", "title": this.title, "loading": this.loading, "columns": this.displayColumns, "itemHeight": this.itemHeight, "visibleItemCount": this.visibleItemCount, "cancelButtonText": this.cancelButtonText, "confirmButtonText": this.confirmButtonText }, "on": (0, _extends2.default)({}, on) }); } }); exports.default = _default2;