diff --git a/packages/webpack-plugin/lib/runtime/components/react/mpx-picker-view-column/index.tsx b/packages/webpack-plugin/lib/runtime/components/react/mpx-picker-view-column/index.tsx index 60bd010771..9fe4c6b245 100644 --- a/packages/webpack-plugin/lib/runtime/components/react/mpx-picker-view-column/index.tsx +++ b/packages/webpack-plugin/lib/runtime/components/react/mpx-picker-view-column/index.tsx @@ -66,14 +66,14 @@ const _PickerViewColumn = forwardRef, }) const { height: pickerH, itemHeight } = wrapperStyle - const [itemRawH, setItemRawH] = useState(itemHeight) + const [itemRawH, setItemRawH] = useState(Math.round(itemHeight)) const maxIndex = useMemo(() => columnData.length - 1, [columnData]) const prevScrollingInfo = useRef({ index: initialIndex, y: 0 }) const dragging = useRef(false) const scrolling = useRef(false) - const timerResetPosition = useRef(null) - const timerScrollTo = useRef(null) - const timerClickOnce = useRef(null) + const timerResetPosition = useRef | null>(null) + const timerScrollTo = useRef | null>(null) + const timerClickOnce = useRef | null>(null) const activeIndex = useRef(initialIndex) const prevIndex = usePrevious(initialIndex) const prevMaxIndex = usePrevious(maxIndex) @@ -89,8 +89,8 @@ const _PickerViewColumn = forwardRef, }) const paddingHeight = useMemo( - () => Math.round((pickerH - itemHeight) / 2), - [pickerH, itemHeight] + () => (pickerH - itemRawH) / 2, + [pickerH, itemRawH] ) const snapToOffsets = useMemo( @@ -164,13 +164,20 @@ const _PickerViewColumn = forwardRef, }, isIOS ? 0 : 200) }, [itemRawH, maxIndex, initialIndex]) + useEffect(() => { + const roundedH = Math.round(itemHeight) + if (roundedH) { + setItemRawH(roundedH) + } + }, [itemHeight]) + const onItemLayout = useCallback((e: LayoutChangeEvent) => { const { height: rawH } = e.nativeEvent.layout const roundedH = Math.round(rawH) - if (roundedH && roundedH !== itemRawH) { + if (roundedH) { setItemRawH(roundedH) } - }, [itemRawH]) + }, []) const resetScrollPosition = useCallback((y: number) => { if (dragging.current || scrolling.current) { @@ -293,7 +300,7 @@ const _PickerViewColumn = forwardRef, key={index} item={item} index={index} - itemHeight={itemHeight} + itemHeight={itemRawH} visibleCount={visibleCount} onItemLayout={onItemLayout} />) @@ -301,7 +308,7 @@ const _PickerViewColumn = forwardRef, key={index} item={item} index={index} - itemHeight={itemHeight} + itemHeight={itemRawH} onItemLayout={onItemLayout} />) }) @@ -346,14 +353,14 @@ const _PickerViewColumn = forwardRef, const renderIndicator = () => ( ) const renderMask = () => ( )