Skip to content

Commit d7ef84d

Browse files
JakeLaoyuchenjiahan
authored andcommitted
feat(Area): columns placeholder (youzan#4623)
1 parent 3e855f6 commit d7ef84d

File tree

5 files changed

+34
-26
lines changed

5 files changed

+34
-26
lines changed

src/area/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Set `columns-num` with 2, you'll have a 2 level picker.
4747
```html
4848
<van-area
4949
:area-list="areaList"
50-
:columns-placeholder="['Choose']"
50+
:columns-placeholder="['Choose', 'Choose', 'Choose']"
5151
title="Title"
5252
/>
5353
```

src/area/README.zh-CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Vue.use(Area);
4646
```html
4747
<van-area
4848
:area-list="areaList"
49-
:columns-placeholder="['请选择']"
49+
:columns-placeholder="['请选择', '请选择', '请选择']"
5050
title="标题"
5151
/>
5252
```

src/area/demo/index.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ export default {
3939
title2: '选中省市区',
4040
title3: '配置显示列',
4141
title4: '配置列占位提示文字',
42-
columnsPlaceholder: ['请选择'],
42+
columnsPlaceholder: ['请选择', '请选择', '请选择'],
4343
areaList: AreaList,
4444
},
4545
'en-US': {
4646
title2: 'Initial Value',
4747
title3: 'Columns Number',
4848
title4: 'Columns Placeholder',
49-
columnsPlaceholder: ['Choose'],
49+
columnsPlaceholder: ['Choose', 'Choose', 'Choose'],
5050
areaList: AreaListEn,
5151
}
5252
},

src/area/index.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { pickerProps } from '../picker/shared';
44

55
const [createComponent, bem] = createNamespace('area');
66

7+
const COLUMNSPLACEHOLDERCODE = '000000';
8+
79
function isOverseaCode(code) {
810
return code[0] === '9';
911
}
@@ -99,14 +101,6 @@ export default createComponent({
99101
name: list[listCode]
100102
}));
101103

102-
if (this.columnsPlaceholder.length) {
103-
// set columns placeholder
104-
result.unshift({
105-
code: '000000',
106-
name: this.typeToColumnsPlaceholder[type]
107-
});
108-
}
109-
110104
if (code) {
111105
// oversea code
112106
if (this.isOverseaCode(code) && type === 'city') {
@@ -116,6 +110,15 @@ export default createComponent({
116110
result = result.filter(item => item.code.indexOf(code) === 0);
117111
}
118112

113+
if (this.typeToColumnsPlaceholder[type] && result.length) {
114+
// set columns placeholder
115+
const codeFill = type === 'province' ? '' : type === 'city' ? COLUMNSPLACEHOLDERCODE.slice(2, 4) : COLUMNSPLACEHOLDERCODE.slice(4, 6);
116+
result.unshift({
117+
code: `${code}${codeFill}`,
118+
name: this.typeToColumnsPlaceholder[type]
119+
});
120+
}
121+
119122
return result;
120123
},
121124

@@ -140,19 +143,28 @@ export default createComponent({
140143
return 0;
141144
},
142145

146+
// parse output columns data
147+
parseOutputValues(values) {
148+
return values.map((value = {}, index) => {
149+
value = JSON.parse(JSON.stringify(value));
150+
if (!value.code || value.name === this.columnsPlaceholder[index]) {
151+
value.code = '';
152+
value.name = '';
153+
}
154+
return value;
155+
});
156+
},
157+
143158
onChange(picker, values, index) {
144159
this.code = values[index].code;
145160
this.setValues();
146-
this.$emit('change', picker, picker.getValues(), index);
161+
let getValues = picker.getValues();
162+
getValues = this.parseOutputValues(getValues);
163+
this.$emit('change', picker, getValues, index);
147164
},
148165

149166
onConfirm(values, index) {
150-
values.forEach(value => {
151-
if (value.code === '000000') {
152-
value.code = '';
153-
value.name = '';
154-
}
155-
});
167+
values = this.parseOutputValues(values);
156168
this.setValues();
157169
this.$emit('confirm', values, index);
158170
},
@@ -162,7 +174,7 @@ export default createComponent({
162174

163175
if (!code) {
164176
if (this.columnsPlaceholder.length) {
165-
code = '000000';
177+
code = COLUMNSPLACEHOLDERCODE;
166178
} else if (Object.keys(this.county)[0]) {
167179
code = Object.keys(this.county)[0];
168180
} else {

src/area/test/__snapshots__/demo.spec.js.snap

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,10 @@ exports[`renders demo correctly 1`] = `
262262
</ul>
263263
</div>
264264
<div class="van-picker-column">
265-
<ul class="van-picker-column__wrapper" style="transform: translate3d(0, 88px, 0); transition-duration: 0ms; transition-property: none; line-height: 44px;">
266-
<li role="button" tabindex="0" class="van-ellipsis van-picker-column__item van-picker-column__item--selected" style="height: 44px;"></li>
267-
</ul>
265+
<ul class="van-picker-column__wrapper" style="transform: translate3d(0, 88px, 0); transition-duration: 0ms; transition-property: none; line-height: 44px;"></ul>
268266
</div>
269267
<div class="van-picker-column">
270-
<ul class="van-picker-column__wrapper" style="transform: translate3d(0, 88px, 0); transition-duration: 0ms; transition-property: none; line-height: 44px;">
271-
<li role="button" tabindex="0" class="van-ellipsis van-picker-column__item van-picker-column__item--selected" style="height: 44px;"></li>
272-
</ul>
268+
<ul class="van-picker-column__wrapper" style="transform: translate3d(0, 88px, 0); transition-duration: 0ms; transition-property: none; line-height: 44px;"></ul>
273269
</div>
274270
<div class="van-picker__mask" style="background-size: 100% 88px;"></div>
275271
<div class="van-hairline-unset--top-bottom van-picker__frame" style="height: 44px;"></div>

0 commit comments

Comments
 (0)