fix(area): support areaList without county_list (#3824)

fix #3814
This commit is contained in:
rex 2020-12-08 19:02:46 +08:00 committed by GitHub
parent f8c09af273
commit bbbbbe3961
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 47 deletions

View File

@ -7,7 +7,7 @@ type AreaItem = {
code: string; code: string;
}; };
const COLUMNSPLACEHOLDERCODE = '000000'; const EMPTY_CODE = '000000';
VantComponent({ VantComponent({
classes: ['active-class', 'toolbar-class', 'column-class'], classes: ['active-class', 'toolbar-class', 'column-class'],
@ -29,11 +29,6 @@ VantComponent({
columnsNum: { columnsNum: {
type: null, type: null,
value: 3, value: 3,
observer(value: number) {
this.setData({
displayColumns: this.data.columns.slice(0, +value),
});
},
}, },
columnsPlaceholder: { columnsPlaceholder: {
type: Array, type: Array,
@ -51,7 +46,6 @@ VantComponent({
data: { data: {
columns: [{ values: [] }, { values: [] }, { values: [] }], columns: [{ values: [] }, { values: [] }, { values: [] }],
displayColumns: [{ values: [] }, { values: [] }, { values: [] }],
typeToColumnsPlaceholder: {}, typeToColumnsPlaceholder: {},
}, },
@ -76,7 +70,7 @@ VantComponent({
onConfirm(event: WechatMiniprogram.CustomEvent) { onConfirm(event: WechatMiniprogram.CustomEvent) {
const { index } = event.detail; const { index } = event.detail;
let { value } = event.detail; let { value } = event.detail;
value = this.parseOutputValues(value); value = this.parseValues(value);
this.emit('confirm', { value, index }); this.emit('confirm', { value, index });
}, },
@ -86,18 +80,21 @@ VantComponent({
this.$emit(type, detail); this.$emit(type, detail);
}, },
// parse output columns data parseValues(values: AreaItem[]) {
parseOutputValues(values) {
const { columnsPlaceholder } = this.data; const { columnsPlaceholder } = this.data;
return values.map((value, index) => {
// save undefined value
if (!value) return value;
value = JSON.parse(JSON.stringify(value)); return values.map((value, index) => {
if (!value.code || value.name === columnsPlaceholder[index]) { if (
value.code = ''; value &&
value.name = ''; (!value.code || value.name === columnsPlaceholder[index])
) {
return {
...value,
code: '',
name: '',
};
} }
return value; return value;
}); });
}, },
@ -108,7 +105,7 @@ VantComponent({
this.setValues().then(() => { this.setValues().then(() => {
this.$emit('change', { this.$emit('change', {
picker, picker,
values: this.parseOutputValues(picker.getValues()), values: this.parseValues(picker.getValues()),
index, index,
}); });
}); });
@ -120,14 +117,13 @@ VantComponent({
}, },
getList(type: string, code?: string): AreaItem[] { getList(type: string, code?: string): AreaItem[] {
const { typeToColumnsPlaceholder } = this.data;
let result: { code: string; name: string }[] = [];
if (type !== 'province' && !code) { if (type !== 'province' && !code) {
return result; return [];
} }
const list = this.getConfig(type); const { typeToColumnsPlaceholder } = this.data;
result = Object.keys(list).map((code) => ({ const list: Record<string, string> = this.getConfig(type);
let result = Object.keys(list).map((code) => ({
code, code,
name: list[code], name: list[code],
})); }));
@ -149,8 +145,9 @@ VantComponent({
type === 'province' type === 'province'
? '' ? ''
: type === 'city' : type === 'city'
? COLUMNSPLACEHOLDERCODE.slice(2, 4) ? EMPTY_CODE.slice(2, 4)
: COLUMNSPLACEHOLDERCODE.slice(4, 6); : EMPTY_CODE.slice(4, 6);
result.unshift({ result.unshift({
code: `${code}${codeFill}`, code: `${code}${codeFill}`,
name: typeToColumnsPlaceholder[type], name: typeToColumnsPlaceholder[type],
@ -180,42 +177,30 @@ VantComponent({
}, },
setValues() { setValues() {
const county = this.getConfig('county');
let { code } = this;
if (!code) {
if (this.data.columnsPlaceholder.length) {
code = COLUMNSPLACEHOLDERCODE;
} else if (Object.keys(county)[0]) {
code = Object.keys(county)[0];
} else {
code = '';
}
}
const province = this.getList('province');
const city = this.getList('city', code.slice(0, 2));
const picker = this.getPicker(); const picker = this.getPicker();
if (!picker) { if (!picker) {
return; return;
} }
let code = this.code || this.getDefaultCode();
const provinceList = this.getList('province');
const cityList = this.getList('city', code.slice(0, 2));
const stack: Promise<void>[] = []; const stack: Promise<void>[] = [];
const indexes: number[] = []; const indexes: number[] = [];
const { columnsNum } = this.data; const { columnsNum } = this.data;
if (columnsNum >= 1) { if (columnsNum >= 1) {
stack.push(picker.setColumnValues(0, province, false)); stack.push(picker.setColumnValues(0, provinceList, false));
indexes.push(this.getIndex('province', code)); indexes.push(this.getIndex('province', code));
} }
if (columnsNum >= 2) { if (columnsNum >= 2) {
stack.push(picker.setColumnValues(1, city, false)); stack.push(picker.setColumnValues(1, cityList, false));
indexes.push(this.getIndex('city', code)); indexes.push(this.getIndex('city', code));
if (city.length && code.slice(2, 4) === '00') { if (cityList.length && code.slice(2, 4) === '00') {
[{ code }] = city; [{ code }] = cityList;
} }
} }
@ -236,9 +221,34 @@ VantComponent({
.catch(() => {}); .catch(() => {});
}, },
getDefaultCode() {
const { columnsPlaceholder } = this.data;
if (columnsPlaceholder.length) {
return EMPTY_CODE;
}
const countyCodes = Object.keys(this.getConfig('county'));
if (countyCodes[0]) {
return countyCodes[0];
}
const cityCodes = Object.keys(this.getConfig('city'));
if (cityCodes[0]) {
return cityCodes[0];
}
return '';
},
getValues() { getValues() {
const picker = this.getPicker(); const picker = this.getPicker();
return picker ? picker.getValues().filter((value) => !!value) : [];
if (!picker) {
return [];
}
return this.parseValues(picker.getValues().filter((value) => !!value));
}, },
getDetail() { getDetail() {

View File

@ -1,3 +1,5 @@
<wxs src="./index.wxs" module="computed" />
<van-picker <van-picker
class="van-area__picker" class="van-area__picker"
active-class="active-class" active-class="active-class"
@ -7,7 +9,7 @@
value-key="name" value-key="name"
title="{{ title }}" title="{{ title }}"
loading="{{ loading }}" loading="{{ loading }}"
columns="{{ displayColumns }}" columns="{{ computed.displayColumns(columns, columnsNum) }}"
item-height="{{ itemHeight }}" item-height="{{ itemHeight }}"
visible-item-count="{{ visibleItemCount }}" visible-item-count="{{ visibleItemCount }}"
cancel-button-text="{{ cancelButtonText }}" cancel-button-text="{{ cancelButtonText }}"

8
packages/area/index.wxs Normal file
View File

@ -0,0 +1,8 @@
/* eslint-disable */
function displayColumns(columns, columnsNum) {
return columns.slice(0, +columnsNum);
}
module.exports = {
displayColumns: displayColumns,
};