Skip to content

Commit

Permalink
fix: Picker Wheel not scrolling in Android ScrollView
Browse files Browse the repository at this point in the history
  • Loading branch information
1uokun committed Mar 15, 2024
1 parent 5049e3a commit 2a63ff4
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 4 deletions.
1 change: 1 addition & 0 deletions components/date-picker-view/PropsType.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface DatePickerViewPropsType
| 'numberOfLines'
| 'renderMaskTop'
| 'renderMaskBottom'
| 'withWrapper'
> {
value?: PickerDate
defaultValue?: PickerDate
Expand Down
7 changes: 7 additions & 0 deletions components/date-picker-view/demo/basic.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState } from 'react'
import { ScrollView, Text } from 'react-native'
import { createNativeWrapper } from 'react-native-gesture-handler'
import { DatePickerView } from '../../'
import { DatePickerFilter } from '../../date-picker/date-picker-utils'

Expand Down Expand Up @@ -39,6 +40,12 @@ export default () => {
precision="hour"
renderLabel={labelRenderer}
filter={dateFilter}
withWrapper={(Component) =>
createNativeWrapper(Component, {
disallowInterruption: true,
shouldCancelWhenOutside: false,
})
}
/>
</ScrollView>
)
Expand Down
16 changes: 15 additions & 1 deletion components/picker-view/PropsType.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ReactNode } from 'react'
import { ComponentType } from 'react'
import { StyleProp, TextStyle, ViewStyle } from 'react-native'

export type PickerValue = string | number
Expand All @@ -16,7 +17,6 @@ export type PickerColumnItem = {
}

export type PickerColumn = PickerColumnItem[]
// type PickerColumn = (string | PickerColumnItem)[] // TODO: support string type

export interface PickerViewPropsType {
value?: PickerValue[]
Expand All @@ -35,4 +35,18 @@ export interface PickerViewPropsType {
renderLabel?: (item: PickerColumnItem, index: number) => ReactNode
renderMaskTop?: () => ReactNode
renderMaskBottom?: () => ReactNode
/**
* @description HOC for Wheel
* @example ```tsx
* import { createNativeWrapper } from 'react-native-gesture-handler'
*
* withWrapper={(Component) =>
* createNativeWrapper(Component, {
* disallowInterruption: true,
* shouldCancelWhenOutside: false,
* })
* }
* ```
*/
withWrapper?: (Component: ComponentType<any>) => ComponentType<any>
}
1 change: 0 additions & 1 deletion components/picker-view/Wheel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// 需要重构成translate3d
import type { ReactNode } from 'react'
import React from 'react'
import {
Expand Down
8 changes: 8 additions & 0 deletions components/picker-view/demo/basic.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import { ScrollView, Text } from 'react-native'
import { createNativeWrapper } from 'react-native-gesture-handler'
import { PickerView } from '../../'

const basicColumns = [
Expand Down Expand Up @@ -40,6 +41,13 @@ export default class PickerViewExample extends React.Component {
itemStyle={{
padding: 0,
}}
// To fix: Wheel not scrolling in Android ScrollView
withWrapper={(Component) =>
createNativeWrapper(Component, {
disallowInterruption: true,
shouldCancelWhenOutside: false,
})
}
/>

<Text style={{ margin: 16 }}>受控模式</Text>
Expand Down
6 changes: 4 additions & 2 deletions components/picker-view/picker-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default class RMCPickerView extends React.Component<
renderMaskBottom: () => (
<View style={{ flex: 1, backgroundColor: 'rgba(255,255,255,0.8)' }} />
),
withWrapper: (Component: any) => Component,
}

constructor(props: RMCPickerViewProps) {
Expand Down Expand Up @@ -110,8 +111,10 @@ export default class RMCPickerView extends React.Component<
numberOfLines,
handleSelect,
loadingContent,
withWrapper,
} = this.props
const itemHeight = this.props.itemHeight || this.state.itemHeight
const WheelHOC = withWrapper?.(Wheel) || Wheel
return (
<WithTheme themeStyles={pickerViewStyles} styles={styles}>
{(s) =>
Expand All @@ -133,7 +136,6 @@ export default class RMCPickerView extends React.Component<
<View style={[s.wheelWrapper, { height: wheelHeight }]}>
{(loading || columns?.length === 0) && loading !== false
? loadingContent || (
// TODO-luokun: loading样式优化
<View style={{ flex: 1, alignSelf: 'center' }}>
<ActivityIndicator
animating
Expand All @@ -144,7 +146,7 @@ export default class RMCPickerView extends React.Component<
: itemHeight > 0 &&
wheelHeight > 0 &&
columns.map((column, index) => (
<Wheel
<WheelHOC
key={index}
index={index}
column={column}
Expand Down

0 comments on commit 2a63ff4

Please sign in to comment.