[Improvement] optimize area performance (#457)

This commit is contained in:
neverland 2017-12-20 17:39:51 +08:00 committed by GitHub
parent df67b005ce
commit 021d30efb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 98 additions and 122 deletions

View File

@ -2,7 +2,7 @@
### 使用指南 ### 使用指南
``` javascript ```javascript
import { Area } from 'vant'; import { Area } from 'vant';
Vue.use(Area); Vue.use(Area);
@ -12,25 +12,23 @@ Vue.use(Area);
#### 基础用法 #### 基础用法
要初始化一个`Area`组件,你需要传入一个`areaList`属性,`areaList`数据格式具体可看下面数据格式章节 要初始化一个`Area`组件,你需要传入一个`areaList`属性,`areaList`数据格式具体可看下面数据格式章节
```html ```html
<van-area :areaList="areaList" /> <van-area :areaList="areaList" />
``` ```
#### 选中省市县 #### 选中省市县
如果想选中某个省市县,需要传入一个`value`属性,绑定对应的省市县`code` 如果想选中某个省市县,需要传入一个`value`属性,绑定对应的省市县`code`
```html ```html
<van-area :areaList="areaList" value="110101" /> <van-area :areaList="areaList" value="110101" />
``` ```
#### 配置显示列 #### 配置显示列
可以通过`columnsNum`属性配置省市县显示的列数,默认情况下会显示省市县,当你设置为`2`,则只会显示省市选择 可以通过`columnsNum`属性配置省市县显示的列数,默认情况下会显示省市县,当你设置为`2`,则只会显示省市选择
```html ```html
<van-area :areaList="areaList" :columnsNum="2" title="标题" /> <van-area :areaList="areaList" :columnsNum="2" title="标题" />
@ -38,29 +36,30 @@ Vue.use(Area);
### API ### API
| 参数 | 说明 | 类型 | 默认值 | 可选值 | | 参数 | 说明 | 类型 | 默认值 | 可选值 |
|-----------|-----------|-----------|-------------|-------------| | ---------- | ------------------------------------------------------------------- | ----------------- | ------ | ------ |
| value | 当前选中的省市区`code` | `String` | - | - | | value | 当前选中的省市区`code` | `String` | - | - |
| title | 顶部栏标题 | `String` | `''` | - | | title | 顶部栏标题 | `String` | `''` | - |
| areaList | 省市县数据,必须与`province_list``city_list``county_list`为key | `Object` | - | - | | areaList | 省市县数据,必须与`province_list``city_list``county_list` key | `Object` | - | - |
| columnsNum | 省市县显示列数3-省市县2-省市1-省 | `String`,`Number` | 3 | - | | columnsNum | 省市县显示列数3-省市县2-省市1-省 | `String`,`Number` | 3 | - |
### Event ### Event
| 事件名称 | 说明 | 回调参数 | | 事件名称 | 说明 | 回调参数 |
|-----------|-----------|-----------| | -------- | ------------------ | ---------------------------------------- |
| confirm | 点击右上方完成按钮 | 一个数组参数,具体格式看下方数据格式章节 | | confirm | 点击右上方完成按钮 | 一个数组参数,具体格式看下方数据格式章节 |
| cancel | 点击取消按钮时 | - | | cancel | 点击取消按钮时 | - |
### 数据格式 ### 数据格式
#### 省市县列表数据格式 #### 省市县列表数据格式
整体是一个Object包含 `province_list`, `city_list`, `county_list` 三个key。 整体是一个 Object包含 `province_list`, `city_list`, `county_list` 三个 key。
每项以省市区编码作为key省市区名字作为value。编码为6位数字前两位代表省份中间两位代表城市后两位代表区县以0补足6位。如北京编码为 `11`以零补足6位`110000` 每项以省市区编码作为 key省市区名字作为 value。编码为 6 位数字,前两位代表省份,中间两位代表城市,后两位代表区县,以 0 补足 6 位。如北京编码为 `11`,以零补足 6 位,为 `110000`
`AreaList`具体格式如下: `AreaList`具体格式如下:
```javascript ```javascript
{ {
province_list: { province_list: {
@ -91,18 +90,24 @@ Vue.use(Area);
完整数据见 [Area.json](https://github.com/youzan/vant/blob/dev/docs/demos/mock/area.json) 完整数据见 [Area.json](https://github.com/youzan/vant/blob/dev/docs/demos/mock/area.json)
#### 点击完成时返回的数据格式 #### 点击完成时返回的数据格式
返回的数据整体为一个数组,数组内包含 `columnsNum` 个数据, 每个数据对应一列选项中被选中的数据。 返回的数据整体为一个数组,数组内包含 `columnsNum` 个数据, 每个数据对应一列选项中被选中的数据。
`code` 代表被选中的地区编码, `name` 代表被选中的地区名称 `code` 代表被选中的地区编码, `name` 代表被选中的地区名称
```javascript ```javascript
[{ [
code: '110000', {
name: '北京市' code: '110000',
}, { name: '北京市'
code: '110100', },
name: '北京市' {
},{ code: '110100',
code: '110101', name: '北京市'
name: '东城区' },
}] {
code: '110101',
name: '东城区'
}
];
``` ```

View File

@ -1,26 +1,21 @@
<template> <template>
<div class="van-area"> <picker
<picker class="van-area"
ref="picker" ref="picker"
showToolbar showToolbar
:title="title" valueKey="name"
valueKey="name" :title="title"
:columns="areaColumns" :columns="columns"
@change="onChange" @change="onChange"
@confirm="$emit('confirm', $event)" @confirm="$emit('confirm', $event)"
@cancel="$emit('cancel', $event)" @cancel="$emit('cancel', $event)"
/> />
</div>
</template> </template>
<script> <script>
import { create } from '../utils'; import { create } from '../utils';
import Picker from '../picker'; import Picker from '../picker';
const PROVINCE_TYPE = 'provice';
const CITY_TYPE = 'city';
const COUNTY_TYPE = 'county';
export default create({ export default create({
name: 'van-area', name: 'van-area',
@ -40,52 +35,33 @@ export default create({
}, },
computed: { computed: {
DEFAULT_PROVINCE() { columns() {
return {
code: '-1',
name: this.$t('province')
};
},
DEFAULT_CITY() {
return {
code: '-1',
name: this.$t('city')
};
},
DEFAULT_COUNTY() {
return {
code: '-1',
name: this.$t('district')
};
},
areaColumns() {
const areaList = this.areaList;
if (!areaList || (areaList && typeof areaList.province_list !== 'object')) return [];
const columns = []; const columns = [];
const curValue = this.value || ''; const { areaList } = this;
const { columnsNum } = this;
if (!areaList || typeof areaList.province_list !== 'object') {
return columns;
}
const code = this.value || '';
const columnsNum = +this.columnsNum;
columns.push({ columns.push({
values: [this.DEFAULT_PROVINCE].concat(this.computedAreaList(PROVINCE_TYPE)), values: this.getList('province'),
className: 'van-area__province', defaultIndex: this.getIndex('province', code)
defaultIndex: this.getAreaIndex(PROVINCE_TYPE, curValue)
}); });
if (+columnsNum > 1) { if (columnsNum > 1) {
columns.push({ columns.push({
values: [this.DEFAULT_CITY].concat(this.computedAreaList(CITY_TYPE, curValue.slice(0, 2))), values: this.getList('city', code.slice(0, 2)),
className: 'van-area__city', defaultIndex: this.getIndex('city', code)
defaultIndex: this.getAreaIndex(CITY_TYPE, curValue)
}); });
} }
if (+columnsNum > 2) { if (columnsNum > 2) {
columns.push({ columns.push({
values: [this.DEFAULT_COUNTY].concat(this.computedAreaList(COUNTY_TYPE, curValue.slice(0, 4))), values: this.getList('county', code.slice(0, 4)),
className: 'van-area__county', defaultIndex: this.getIndex('county', code)
defaultIndex: this.getAreaIndex(COUNTY_TYPE, curValue)
}); });
} }
@ -95,37 +71,40 @@ export default create({
methods: { methods: {
// `code` // `code`
computedAreaList(type, code) { getList(type, code) {
const result = []; const { areaList } = this;
const curAreaList = this.areaList; const list =
const areaList = type === PROVINCE_TYPE type === 'province'
? curAreaList.province_list ? areaList.province_list
: (type === CITY_TYPE ? curAreaList.city_list : curAreaList.county_list); : type === 'city' ? areaList.city_list : areaList.county_list;
for (const i in areaList) { let result = Object.keys(list).map(code => ({
// code,
// name: list[code]
if (type === PROVINCE_TYPE || (code && i.slice(0, code.length) === code)) { }));
result.push({
code: i, if (type !== 'province' && code) {
name: areaList[i] result = result.filter(item => item.code.indexOf(code) === 0);
});
}
} }
result.unshift({
code: '-1',
name: this.$t(type)
});
return result; return result;
}, },
// //
getAreaIndex(type, code) { getIndex(type, code) {
const compareNum = type === PROVINCE_TYPE const compareNum = type === 'province' ? 2 : type === 'city' ? 4 : 6;
? 2 const areaList = this.getList(type, code.slice(0, compareNum - 2));
: (type === CITY_TYPE ? 4 : 6);
const areaList = this.computedAreaList(type, code.slice(0, compareNum - 2));
for (let i = 0; i < areaList.length; i++) { for (let i = 0; i < areaList.length; i++) {
if (+areaList[i].code.slice(0, compareNum) === +code.slice(0, compareNum)) { if (
return i + 1; +areaList[i].code.slice(0, compareNum) === +code.slice(0, compareNum)
) {
return i;
} }
} }
@ -136,25 +115,15 @@ export default create({
const code = values[index].code; const code = values[index].code;
// //
if (index === 0) { if (index === 0) {
picker.setColumnValues( picker.setColumnValues(1, this.getList('city', code.slice(0, 2)));
1, picker.setColumnValues(2, this.getList('county', code.slice(0, 4)));
[this.DEFAULT_CITY].concat(this.computedAreaList(CITY_TYPE, code.slice(0, 2)))
);
picker.setColumnValues(
2,
[this.DEFAULT_COUNTY].concat(this.computedAreaList(COUNTY_TYPE, code.slice(0, 4)))
);
} else if (index === 1) { } else if (index === 1) {
picker.setColumnValues( picker.setColumnValues(2, this.getList('county', code.slice(0, 4)));
2,
[this.DEFAULT_COUNTY].concat(this.computedAreaList(COUNTY_TYPE, code.slice(0, 4)))
);
} }
}, },
getValues() { getValues() {
const { picker } = this.$refs; return this.$refs.picker ? this.$refs.picker.getValues() : [];
return picker ? picker.getValues() : [];
} }
} }
}); });

View File

@ -58,7 +58,7 @@ export default {
vanArea: { vanArea: {
province: 'Province', province: 'Province',
city: 'City', city: 'City',
district: 'District' county: 'District'
}, },
vanAddressEdit: { vanAddressEdit: {
areaTitle: 'Area', areaTitle: 'Area',

View File

@ -61,7 +61,7 @@ export default {
vanArea: { vanArea: {
province: '选择省份', province: '选择省份',
city: '选择城市', city: '选择城市',
district: '选择地区' county: '选择地区'
}, },
vanAddressEdit: { vanAddressEdit: {
areaTitle: '收件地区', areaTitle: '收件地区',

View File

@ -13,7 +13,9 @@ inquirer.prompt([{
name: 'select', name: 'select',
message: '请选择要运行的测试文件:', message: '请选择要运行的测试文件:',
choices: files choices: files
}], (result) => { }]).then(result => {
const file = result.select.replace('.spec.js', ''); const file = result.select.replace('.spec.js', '');
shell.exec('karma start test/unit/karma.conf.js --color alway --file ' + file); shell.exec('karma start test/unit/karma.conf.js --color alway --file ' + file);
}).catch(error => {
console.log(error);
}); });

View File

@ -64,7 +64,7 @@ describe('Area', () => {
} }
}); });
expect(wrapper.vm.areaColumns.length).to.equal(0); expect(wrapper.vm.columns.length).to.equal(0);
}); });
it('create an area with columnsNum equal 2', () => { it('create an area with columnsNum equal 2', () => {
@ -75,7 +75,7 @@ describe('Area', () => {
} }
}); });
expect(wrapper.vm.areaColumns.length).to.equal(2); expect(wrapper.vm.columns.length).to.equal(2);
}); });
it('create an area with columnsNum equal 1', () => { it('create an area with columnsNum equal 1', () => {
@ -86,7 +86,7 @@ describe('Area', () => {
} }
}); });
expect(wrapper.vm.areaColumns.length).to.equal(1); expect(wrapper.vm.columns.length).to.equal(1);
}); });
it('create an area and click cancel', done => { it('create an area and click cancel', done => {