feat(Area): add columns placeholder

This commit is contained in:
Jake 2019-10-14 19:14:41 +08:00 committed by rex
parent be1b6c69e9
commit 31feeac3bd
3 changed files with 83 additions and 4 deletions

View File

@ -30,5 +30,17 @@
/> />
</demo-block> </demo-block>
<demo-block title="配置列占位提示文字">
<van-area
title="标题"
columns-num="{{ 2 }}"
loading="{{ loading }}"
area-list="{{ areaList }}"
columns-placeholder="{{ ['请选择', '请选择', '请选择'] }}"
bind:change="onChange"
bind:confirm="onConfirm"
/>
</demo-block>
<van-toast id="van-toast" /> <van-toast id="van-toast" />

View File

@ -41,6 +41,18 @@
<van-area area-list="{{ areaList }}" columns-num="{{ 2 }}" title="标题" /> <van-area area-list="{{ areaList }}" columns-num="{{ 2 }}" title="标题" />
``` ```
### 配置列占位提示文字
可以通过`columns-placeholder`属性配置每一列的占位提示文字
```html
<van-area
:area-list="{{ areaList }}"
:columns-placeholder="{{ ['请选择', '请选择', '请选择'] }}"
title="标题"
/>
```
## API ## API
### Props ### Props
@ -51,6 +63,7 @@
| title | 顶部栏标题 | *string* | - | - | | title | 顶部栏标题 | *string* | - | - |
| area-list | 省市区数据,格式见下方 | *object* | - | - | | area-list | 省市区数据,格式见下方 | *object* | - | - |
| columns-num | 省市区显示列数3-省市区2-省市1-省 | *string \| number* | `3` | - | | columns-num | 省市区显示列数3-省市区2-省市1-省 | *string \| number* | `3` | - |
| columns-placeholder | 列占位提示文字 | *string[]* | `[]` | - |
| loading | 是否显示加载状态 | *boolean* | `false` | - | | loading | 是否显示加载状态 | *boolean* | `false` | - |
| item-height | 选项高度 | *number* | `44` | - | | item-height | 选项高度 | *number* | `44` | - |
| visible-item-count | 可见的选项个数 | *number* | `5` | - | | visible-item-count | 可见的选项个数 | *number* | `5` | - |

View File

@ -7,6 +7,8 @@ type AreaItem = {
code: string; code: string;
}; };
const COLUMNSPLACEHOLDERCODE = '000000';
VantComponent({ VantComponent({
classes: ['active-class', 'toolbar-class', 'column-class'], classes: ['active-class', 'toolbar-class', 'column-class'],
@ -20,12 +22,25 @@ VantComponent({
columnsNum: { columnsNum: {
type: null, type: null,
value: 3 value: 3
},
columnsPlaceholder: {
type: Array,
observer(val) {
this.setData({
typeToColumnsPlaceholder: {
province: val[0] || '',
city: val[1] || '',
county: val[2] || '',
}
});
}
} }
}, },
data: { data: {
columns: [{ values: [] }, { values: [] }, { values: [] }], columns: [{ values: [] }, { values: [] }, { values: [] }],
displayColumns: [{ values: [] }, { values: [] }, { values: [] }] displayColumns: [{ values: [] }, { values: [] }, { values: [] }],
typeToColumnsPlaceholder: {}
}, },
watch: { watch: {
@ -62,7 +77,10 @@ VantComponent({
}, },
onConfirm(event: Weapp.Event) { onConfirm(event: Weapp.Event) {
this.emit('confirm', event.detail); const { index } = event.detail;
let { value } = event.detail;
value = this.parseOutputValues(value);
this.emit('confirm', { value, index });
}, },
emit(type: string, detail) { emit(type: string, detail) {
@ -71,13 +89,28 @@ VantComponent({
this.$emit(type, detail); this.$emit(type, detail);
}, },
// parse output columns data
parseOutputValues(values) {
const { columnsPlaceholder } = this.data;
return values.map((value = {}, index) => {
value = JSON.parse(JSON.stringify(value));
if (!value.code || value.name === columnsPlaceholder[index]) {
value.code = '';
value.name = '';
}
return value;
});
},
onChange(event: Weapp.Event) { onChange(event: Weapp.Event) {
const { index, picker, value } = event.detail; const { index, picker, value } = event.detail;
this.code = value[index].code; this.code = value[index].code;
let getValues = picker.getValues();
getValues = this.parseOutputValues(getValues);
this.setValues().then(() => { this.setValues().then(() => {
this.$emit('change', { this.$emit('change', {
picker, picker,
values: picker.getValues(), values: getValues,
index index
}); });
}); });
@ -89,6 +122,7 @@ VantComponent({
}, },
getList(type: string, code?: string): AreaItem[] { getList(type: string, code?: string): AreaItem[] {
const { typeToColumnsPlaceholder } = this.data;
let result = []; let result = [];
if (type !== 'province' && !code) { if (type !== 'province' && !code) {
return result; return result;
@ -109,6 +143,15 @@ VantComponent({
result = result.filter(item => item.code.indexOf(code) === 0); result = result.filter(item => item.code.indexOf(code) === 0);
} }
if (typeToColumnsPlaceholder[type] && result.length) {
// set columns placeholder
const codeFill = type === 'province' ? '' : type === 'city' ? COLUMNSPLACEHOLDERCODE.slice(2, 4) : COLUMNSPLACEHOLDERCODE.slice(4, 6);
result.unshift({
code: `${code}${codeFill}`,
name: typeToColumnsPlaceholder[type]
});
}
return result; return result;
}, },
@ -133,7 +176,18 @@ VantComponent({
setValues() { setValues() {
const county = this.getConfig('county'); const county = this.getConfig('county');
let code = this.code || Object.keys(county)[0] || ''; 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 province = this.getList('province');
const city = this.getList('city', code.slice(0, 2)); const city = this.getList('city', code.slice(0, 2));