提交 149d2164 编写于 作者: D DCloud_LXH

fix(app-nvue): picker-column set values error

上级 c42f0dca
...@@ -8,6 +8,8 @@ import { ...@@ -8,6 +8,8 @@ import {
computed, computed,
getCurrentInstance, getCurrentInstance,
onMounted, onMounted,
ExtractPropTypes,
WritableComputedRef,
} from 'vue' } from 'vue'
import { extend, isString } from '@vue/shared' import { extend, isString } from '@vue/shared'
import { Props, GetPickerViewColumn } from '../picker-view' import { Props, GetPickerViewColumn } from '../picker-view'
...@@ -20,6 +22,7 @@ type ScrollOptions = { ...@@ -20,6 +22,7 @@ type ScrollOptions = {
scrollY: boolean scrollY: boolean
scrollTop?: number scrollTop?: number
} }
type PickerColumnProps = ExtractPropTypes<typeof props>
const dom = weex.requireModule('dom') const dom = weex.requireModule('dom')
const isAndroid = weex.config.env.platform.toLowerCase() === 'android' const isAndroid = weex.config.env.platform.toLowerCase() === 'android'
...@@ -27,14 +30,16 @@ function getStyle(val: string) { ...@@ -27,14 +30,16 @@ function getStyle(val: string) {
return extend({}, isString(val) ? parseStyleText(val) : val) return extend({}, isString(val) ? parseStyleText(val) : val)
} }
const props = {
length: {
type: [Number, String],
default: 0,
},
}
export default defineComponent({ export default defineComponent({
name: 'PickerViewColumn', name: 'PickerViewColumn',
props: { props,
length: {
type: [Number, String],
default: 0,
},
},
data: () => ({ data: () => ({
_isMounted: false, _isMounted: false,
}), }),
...@@ -61,45 +66,13 @@ export default defineComponent({ ...@@ -61,45 +66,13 @@ export default defineComponent({
let pickerViewHeight = ref(0) let pickerViewHeight = ref(0)
pickerViewHeight.value = parseFloat(pickerViewProps.height as string) pickerViewHeight.value = parseFloat(pickerViewProps.height as string)
watch( const { setCurrent, onScrollend } = usePickerColumnScroll(
() => props.length, props,
() => { current,
setTimeout(() => { contentRef,
setCurrent(current.value, true, true) indicatorHeight
}, 150)
}
) )
let scrollToElementTime: number
const setCurrent = (_current: number, animated = true, force: Boolean) => {
if (current.value === _current && !force) {
return
}
dom.scrollToElement(contentRef.value, {
offset: _current * indicatorHeight.value,
animated,
})
current.value = _current
if (animated) {
scrollToElementTime = Date.now()
}
}
const onScrollend = (event: {
detail: {
contentOffset: { x: number; y: number }
}
}) => {
if (Date.now() - scrollToElementTime < 340) {
return
}
const y = event.detail.contentOffset.y
const _current = Math.round(y / indicatorHeight.value)
if (y % indicatorHeight.value) {
setCurrent(_current, true, true)
} else {
current.value = _current
}
}
const checkMounted = () => { const checkMounted = () => {
let height_: number let height_: number
let indicatorHeight_: number let indicatorHeight_: number
...@@ -273,3 +246,63 @@ function getHeight(style: Record<string, any>) { ...@@ -273,3 +246,63 @@ function getHeight(style: Record<string, any>) {
} }
return value return value
} }
function usePickerColumnScroll(
props: PickerColumnProps,
current: WritableComputedRef<number>,
contentRef: Ref<HTMLElement | null>,
indicatorHeight: Ref<number>
) {
let scrollToElementTime: number
watch(
() => props.length,
() => {
setTimeout(() => {
setCurrent(current.value, true, true)
}, 150)
}
)
watch(
() => current.value,
(_current) => {
dom.scrollToElement(contentRef.value, {
offset: _current * indicatorHeight.value,
animated: true,
})
scrollToElementTime = Date.now()
}
)
const setCurrent = (_current: number, animated = true, force: Boolean) => {
if (current.value === _current && !force) {
return
}
dom.scrollToElement(contentRef.value, {
offset: _current * indicatorHeight.value,
animated,
})
current.value = _current
if (animated) {
scrollToElementTime = Date.now()
}
}
const onScrollend = (event: {
detail: {
contentOffset: { x: number; y: number }
}
}) => {
if (Date.now() - scrollToElementTime < 340) {
return
}
const y = event.detail.contentOffset.y
const _current = Math.round(y / indicatorHeight.value)
if (y % indicatorHeight.value) {
setCurrent(_current, true, true)
} else {
current.value = _current
}
}
return { setCurrent, onScrollend }
}
...@@ -8,6 +8,7 @@ import { ...@@ -8,6 +8,7 @@ import {
VNode, VNode,
provide, provide,
ExtractPropTypes, ExtractPropTypes,
Comment,
} from 'vue' } from 'vue'
import { extend } from '@vue/shared' import { extend } from '@vue/shared'
import { import {
...@@ -35,8 +36,12 @@ export default defineComponent({ ...@@ -35,8 +36,12 @@ export default defineComponent({
const trigger = useCustomEvent<EmitEvent<typeof emit>>(rootRef, emit) const trigger = useCustomEvent<EmitEvent<typeof emit>>(rootRef, emit)
let columnVNodes: VNode[] = [] let columnVNodes: VNode[] = []
const getItemIndex = (vnode: VNode) => const getItemIndex = (vnode: VNode) => {
Array.prototype.indexOf.call(columnVNodes, vnode) return Array.prototype.indexOf.call(
columnVNodes.filter((vnode) => vnode.type !== Comment),
vnode
)
}
const getPickerViewColumn: GetPickerViewColumn = (columnInstance) => { const getPickerViewColumn: GetPickerViewColumn = (columnInstance) => {
return computed({ return computed({
get() { get() {
...@@ -81,7 +86,7 @@ export default defineComponent({ ...@@ -81,7 +86,7 @@ export default defineComponent({
preventGesture: true, preventGesture: true,
}} }}
> >
<view class="uni-picker-view-wrapper">{defaultSlots}</view> <view class="uni-picker-view-wrapper">{columnVNodes}</view>
</view> </view>
) )
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册