feat(Picker): remove computed (#2001)

This commit is contained in:
neverland 2019-09-09 20:07:21 +08:00 committed by GitHub
parent 78681032b8
commit f178552d75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 25 deletions

View File

@ -42,26 +42,6 @@ VantComponent({
}); });
}, },
computed: {
count() {
return this.data.options.length;
},
baseOffset() {
const { data } = this;
return (data.itemHeight * (data.visibleItemCount - 1)) / 2;
},
wrapperStyle() {
const { data } = this;
return [
`transition: transform ${data.duration}ms`,
`transform: translate3d(0, ${data.offset + data.baseOffset}px, 0)`,
`line-height: ${data.itemHeight}px`
].join('; ');
}
},
watch: { watch: {
defaultIndex(value: number) { defaultIndex(value: number) {
this.setIndex(value); this.setIndex(value);
@ -69,6 +49,10 @@ VantComponent({
}, },
methods: { methods: {
getCount() {
return this.data.options.length;
},
onTouchStart(event: Weapp.TouchEvent) { onTouchStart(event: Weapp.TouchEvent) {
this.set({ this.set({
startY: event.touches[0].clientY, startY: event.touches[0].clientY,
@ -83,7 +67,7 @@ VantComponent({
this.set({ this.set({
offset: range( offset: range(
data.startOffset + deltaY, data.startOffset + deltaY,
-(data.count * data.itemHeight), -(this.getCount() * data.itemHeight),
data.itemHeight data.itemHeight
) )
}); });
@ -98,7 +82,7 @@ VantComponent({
const index = range( const index = range(
Math.round(-data.offset / data.itemHeight), Math.round(-data.offset / data.itemHeight),
0, 0,
data.count - 1 this.getCount() - 1
); );
this.setIndex(index, true); this.setIndex(index, true);
} }
@ -111,8 +95,10 @@ VantComponent({
adjustIndex(index: number) { adjustIndex(index: number) {
const { data } = this; const { data } = this;
index = range(index, 0, data.count); const count = this.getCount();
for (let i = index; i < data.count; i++) {
index = range(index, 0, count);
for (let i = index; i < count; i++) {
if (!this.isDisabled(data.options[i])) return i; if (!this.isDisabled(data.options[i])) return i;
} }
for (let i = index - 1; i >= 0; i--) { for (let i = index - 1; i >= 0; i--) {

View File

@ -8,7 +8,7 @@
bind:touchend="onTouchEnd" bind:touchend="onTouchEnd"
bind:touchcancel="onTouchEnd" bind:touchcancel="onTouchEnd"
> >
<view style="{{ wrapperStyle }}"> <view style="transition: transform {{ duration }}ms; line-height: {{ itemHeight }}px; transform: translate3d(0, {{ offset + (itemHeight * (visibleItemCount - 1)) / 2 }}px, 0)">
<view <view
wx:for="{{ options }}" wx:for="{{ options }}"
wx:for-item="option" wx:for-item="option"