[Improvement] Picker: optimize render performance (#1391)

This commit is contained in:
neverland
2018-07-03 23:36:36 +08:00
committed by GitHub
parent 6adf501441
commit 7f4ad6a28d
5 changed files with 50 additions and 64 deletions

View File

@@ -25,6 +25,7 @@
<script>
import create from '../utils/create';
import deepClone from '../utils/deep-clone';
import { isObj } from '../utils';
const DEFAULT_DURATION = 200;
@@ -38,7 +39,7 @@ export default create({
className: String,
itemHeight: Number,
visibleItemCount: Number,
options: {
initialOptions: {
type: Array,
default: () => []
},
@@ -54,15 +55,13 @@ export default create({
offset: 0,
duration: 0,
startOffset: 0,
options: deepClone(this.initialOptions),
currentIndex: this.defaultIndex
};
},
created() {
this.$parent.children && this.$parent.children.push(this);
},
mounted() {
this.setIndex(this.currentIndex);
},
@@ -73,12 +72,6 @@ export default create({
watch: {
defaultIndex() {
this.setIndex(this.defaultIndex);
},
options(next, prev) {
if (JSON.stringify(next) !== JSON.stringify(prev)) {
this.setIndex(0);
}
}
},
@@ -103,10 +96,6 @@ export default create({
transform: `translate3d(0, ${this.offset + this.baseOffset}px, 0)`,
lineHeight: this.itemHeight + 'px'
};
},
currentValue() {
return this.options[this.currentIndex];
}
},
@@ -168,10 +157,13 @@ export default create({
const { options } = this;
for (let i = 0; i < options.length; i++) {
if (this.getOptionText(options[i]) === value) {
this.setIndex(i);
return;
return this.setIndex(i);
}
}
},
getValue() {
return this.options[this.currentIndex];
}
}
});