Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(PickView): pickview wheel direction #6216

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/components/picker-view/utils/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// useWheel 的滚动数据特殊处理
export function wheelBound(
position: number,
min: number | undefined,
max: number | undefined

Check warning on line 5 in src/components/picker-view/utils/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/picker-view/utils/index.tsx#L5

Added line #L5 was not covered by tests
) {
let ret = position

Check warning on line 7 in src/components/picker-view/utils/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/picker-view/utils/index.tsx#L7

Added line #L7 was not covered by tests

if (min !== undefined && max !== undefined) {
if (ret > 0) {
ret = Math.max(ret, min)

Check warning on line 11 in src/components/picker-view/utils/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/picker-view/utils/index.tsx#L11

Added line #L11 was not covered by tests

ret = Math.min(ret, max)

Check warning on line 13 in src/components/picker-view/utils/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/picker-view/utils/index.tsx#L13

Added line #L13 was not covered by tests
}
if (ret < 0) {
ret = Math.min(ret, min)
ret = Math.max(ret, min)

Check warning on line 17 in src/components/picker-view/utils/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/picker-view/utils/index.tsx#L16-L17

Added lines #L16 - L17 were not covered by tests
}
}

return ret

Check warning on line 21 in src/components/picker-view/utils/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/picker-view/utils/index.tsx#L21

Added line #L21 was not covered by tests
}
46 changes: 31 additions & 15 deletions src/components/picker-view/wheel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import { useIsomorphicLayoutEffect } from 'ahooks'
import { measureCSSLength } from '../../utils/measure-css-length'
import { supportsPassive } from '../../utils/supports-passive'
import { wheelBound } from './utils'

const classPrefix = `adm-picker-view`

Expand Down Expand Up @@ -86,13 +87,9 @@
}

const handleDrag = (
state:
| (Omit<FullGestureState<'wheel'>, 'event'> & {
event: EventTypes['wheel']
})
| (Omit<FullGestureState<'drag'>, 'event'> & {
event: EventTypes['drag']
})
state: Omit<FullGestureState<'drag'>, 'event'> & {
event: EventTypes['drag']
}
) => {
draggingRef.current = true
const min = -((column.length - 1) * itemHeight.current)
Expand All @@ -101,10 +98,10 @@
draggingRef.current = false
const position =
state.offset[1] + state.velocity[1] * state.direction[1] * 50
const targetIndex =
min < max
? -Math.round(bound(position, min, max) / itemHeight.current)
: 0
const targetIndex = -Math.round(
bound(position, min, max) / itemHeight.current
)

scrollSelect(targetIndex)
} else {
const position = state.offset[1]
Expand All @@ -120,6 +117,26 @@
}
}

const handleWheel = (
state: Omit<FullGestureState<'wheel'>, 'event'> & {
event: EventTypes['wheel']
}
) => {
draggingRef.current = true
const min = -((column.length - 1) * itemHeight.current)
const max = 0

Check warning on line 127 in src/components/picker-view/wheel.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/picker-view/wheel.tsx#L124-L127

Added lines #L124 - L127 were not covered by tests

if (state.last) {
draggingRef.current = false

Check warning on line 130 in src/components/picker-view/wheel.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/picker-view/wheel.tsx#L130

Added line #L130 was not covered by tests
const position =
state.offset[1] + state.velocity[1] * state.direction[1] * 50
const wheelNum = wheelBound(-position, min, max)
const targetIndex = -Math.round(wheelNum / itemHeight.current)

Check warning on line 134 in src/components/picker-view/wheel.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/picker-view/wheel.tsx#L132-L134

Added lines #L132 - L134 were not covered by tests

scrollSelect(targetIndex)

Check warning on line 136 in src/components/picker-view/wheel.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/picker-view/wheel.tsx#L136

Added line #L136 was not covered by tests
}
}

useDrag(
state => {
state.event.stopPropagation()
Expand All @@ -137,7 +154,7 @@
useWheel(
state => {
state.event.stopPropagation()
handleDrag(state)
handleWheel(state)

Check warning on line 157 in src/components/picker-view/wheel.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/picker-view/wheel.tsx#L157

Added line #L157 was not covered by tests
},
{
axis: 'y',
Expand Down Expand Up @@ -245,9 +262,8 @@
if (prev.onSelect !== next.onSelect) return false
if (prev.renderLabel !== next.renderLabel) return false
if (prev.mouseWheel !== next.mouseWheel) return false
if (!isEqual(prev.column, next.column)) {
return false
}
if (!isEqual(prev.column, next.column)) return false

return true
}
)
Expand Down
Loading