feat(Area): columns placeholder (#4623)

This commit is contained in:
Jake 2019-09-30 09:38:51 +08:00 committed by neverland
parent 3e855f6c21
commit d7ef84d8cf
5 changed files with 36 additions and 28 deletions

View File

@ -47,7 +47,7 @@ Set `columns-num` with 2, you'll have a 2 level picker.
```html ```html
<van-area <van-area
:area-list="areaList" :area-list="areaList"
:columns-placeholder="['Choose']" :columns-placeholder="['Choose', 'Choose', 'Choose']"
title="Title" title="Title"
/> />
``` ```

View File

@ -46,7 +46,7 @@ Vue.use(Area);
```html ```html
<van-area <van-area
:area-list="areaList" :area-list="areaList"
:columns-placeholder="['请选择']" :columns-placeholder="['请选择', '请选择', '请选择']"
title="标题" title="标题"
/> />
``` ```

View File

@ -39,14 +39,14 @@ export default {
title2: '选中省市区', title2: '选中省市区',
title3: '配置显示列', title3: '配置显示列',
title4: '配置列占位提示文字', title4: '配置列占位提示文字',
columnsPlaceholder: ['请选择'], columnsPlaceholder: ['请选择', '请选择', '请选择'],
areaList: AreaList, areaList: AreaList,
}, },
'en-US': { 'en-US': {
title2: 'Initial Value', title2: 'Initial Value',
title3: 'Columns Number', title3: 'Columns Number',
title4: 'Columns Placeholder', title4: 'Columns Placeholder',
columnsPlaceholder: ['Choose'], columnsPlaceholder: ['Choose', 'Choose', 'Choose'],
areaList: AreaListEn, areaList: AreaListEn,
} }
}, },

View File

@ -4,6 +4,8 @@ import { pickerProps } from '../picker/shared';
const [createComponent, bem] = createNamespace('area'); const [createComponent, bem] = createNamespace('area');
const COLUMNSPLACEHOLDERCODE = '000000';
function isOverseaCode(code) { function isOverseaCode(code) {
return code[0] === '9'; return code[0] === '9';
} }
@ -99,14 +101,6 @@ export default createComponent({
name: list[listCode] name: list[listCode]
})); }));
if (this.columnsPlaceholder.length) {
// set columns placeholder
result.unshift({
code: '000000',
name: this.typeToColumnsPlaceholder[type]
});
}
if (code) { if (code) {
// oversea code // oversea code
if (this.isOverseaCode(code) && type === 'city') { if (this.isOverseaCode(code) && type === 'city') {
@ -116,6 +110,15 @@ export default createComponent({
result = result.filter(item => item.code.indexOf(code) === 0); result = result.filter(item => item.code.indexOf(code) === 0);
} }
if (this.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: this.typeToColumnsPlaceholder[type]
});
}
return result; return result;
}, },
@ -140,19 +143,28 @@ export default createComponent({
return 0; return 0;
}, },
onChange(picker, values, index) { // parse output columns data
this.code = values[index].code; parseOutputValues(values) {
this.setValues(); return values.map((value = {}, index) => {
this.$emit('change', picker, picker.getValues(), index); value = JSON.parse(JSON.stringify(value));
}, if (!value.code || value.name === this.columnsPlaceholder[index]) {
onConfirm(values, index) {
values.forEach(value => {
if (value.code === '000000') {
value.code = ''; value.code = '';
value.name = ''; value.name = '';
} }
return value;
}); });
},
onChange(picker, values, index) {
this.code = values[index].code;
this.setValues();
let getValues = picker.getValues();
getValues = this.parseOutputValues(getValues);
this.$emit('change', picker, getValues, index);
},
onConfirm(values, index) {
values = this.parseOutputValues(values);
this.setValues(); this.setValues();
this.$emit('confirm', values, index); this.$emit('confirm', values, index);
}, },
@ -162,7 +174,7 @@ export default createComponent({
if (!code) { if (!code) {
if (this.columnsPlaceholder.length) { if (this.columnsPlaceholder.length) {
code = '000000'; code = COLUMNSPLACEHOLDERCODE;
} else if (Object.keys(this.county)[0]) { } else if (Object.keys(this.county)[0]) {
code = Object.keys(this.county)[0]; code = Object.keys(this.county)[0];
} else { } else {

View File

@ -262,14 +262,10 @@ exports[`renders demo correctly 1`] = `
</ul> </ul>
</div> </div>
<div class="van-picker-column"> <div class="van-picker-column">
<ul class="van-picker-column__wrapper" style="transform: translate3d(0, 88px, 0); transition-duration: 0ms; transition-property: none; line-height: 44px;"> <ul class="van-picker-column__wrapper" style="transform: translate3d(0, 88px, 0); transition-duration: 0ms; transition-property: none; line-height: 44px;"></ul>
<li role="button" tabindex="0" class="van-ellipsis van-picker-column__item van-picker-column__item--selected" style="height: 44px;"></li>
</ul>
</div> </div>
<div class="van-picker-column"> <div class="van-picker-column">
<ul class="van-picker-column__wrapper" style="transform: translate3d(0, 88px, 0); transition-duration: 0ms; transition-property: none; line-height: 44px;"> <ul class="van-picker-column__wrapper" style="transform: translate3d(0, 88px, 0); transition-duration: 0ms; transition-property: none; line-height: 44px;"></ul>
<li role="button" tabindex="0" class="van-ellipsis van-picker-column__item van-picker-column__item--selected" style="height: 44px;"></li>
</ul>
</div> </div>
<div class="van-picker__mask" style="background-size: 100% 88px;"></div> <div class="van-picker__mask" style="background-size: 100% 88px;"></div>
<div class="van-hairline-unset--top-bottom van-picker__frame" style="height: 44px;"></div> <div class="van-hairline-unset--top-bottom van-picker__frame" style="height: 44px;"></div>