-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathwheel.tsx
321 lines (292 loc) · 8.88 KB
/
wheel.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import React, { memo, useRef } from 'react'
import type { ReactNode } from 'react'
import { useSpring, animated } from '@react-spring/web'
import {
EventTypes,
FullGestureState,
useDrag,
useWheel,
} from '@use-gesture/react'
import { rubberbandIfOutOfBounds } from '../../utils/rubberband'
import { bound } from '../../utils/bound'
import { PickerColumnItem, PickerValue } from './index'
import isEqual from 'react-fast-compare'
import { useIsomorphicLayoutEffect } from 'ahooks'
import { measureCSSLength } from '../../utils/measure-css-length'
import { supportsPassive } from '../../utils/supports-passive'
import classNames from 'classnames'
const classPrefix = `adm-picker-view`
type Props = {
index: number
column: PickerColumnItem[]
value: PickerValue
onSelect: (value: PickerValue, index: number) => void
renderLabel: (item: PickerColumnItem) => ReactNode
mouseWheel: boolean
wheelResistance: boolean
}
export const Wheel = memo<Props>(
props => {
const { value, column, renderLabel } = props
function onSelect(val: PickerValue) {
props.onSelect(val, props.index)
}
const [{ y }, api] = useSpring(() => ({
from: { y: 0 },
config: {
tension: 400,
mass: 0.8,
},
}))
const draggingRef = useRef(false)
const rootRef = useRef<HTMLDivElement>(null)
const itemHeightMeasureRef = useRef<HTMLDivElement>(null)
const itemHeight = useRef<number>(34)
useIsomorphicLayoutEffect(() => {
const itemHeightMeasure = itemHeightMeasureRef.current
if (!itemHeightMeasure) return
itemHeight.current = measureCSSLength(
window.getComputedStyle(itemHeightMeasure).getPropertyValue('height')
)
})
useIsomorphicLayoutEffect(() => {
if (draggingRef.current) return
if (value === null) return
const targetIndex = column.findIndex(item => item.value === value)
if (targetIndex < 0) return
const finalPosition = targetIndex * -itemHeight.current
api.start({ y: finalPosition, immediate: y.goal !== finalPosition })
}, [value, column])
useIsomorphicLayoutEffect(() => {
if (column.length === 0) {
if (value !== null) {
onSelect(null)
}
} else {
if (!column.some(item => item.value === value)) {
const firstItem = column[0]
onSelect(firstItem.value)
}
}
}, [column, value])
function scrollSelect(index: number) {
const finalPosition = index * -itemHeight.current
api.start({ y: finalPosition })
const item = column[index]
if (!item) return
onSelect(item.value)
}
const handleGestureState = (
state:
| (Omit<FullGestureState<'wheel'>, 'event'> & {
event: EventTypes['wheel']
})
| (Omit<FullGestureState<'drag'>, 'event'> & {
event: EventTypes['drag']
})
) => {
const {
direction: [, direction],
distance: [, distance],
velocity: [, velocity],
offset: [, offset],
last,
} = state
return {
direction,
distance,
velocity,
offset,
last,
}
}
const handleDrag = (
state: Omit<FullGestureState<'drag'>, 'event'> & {
event: EventTypes['drag']
}
) => {
draggingRef.current = true
const min = -((column.length - 1) * itemHeight.current)
const max = 0
const { direction, last, velocity, offset } = handleGestureState(state)
if (last) {
draggingRef.current = false
const position = offset + velocity * direction * 50
const boundNum = bound(position, min, max)
const targetIndex = -Math.round(boundNum / itemHeight.current)
scrollSelect(targetIndex)
} else {
const position = offset
api.start({
y: rubberbandIfOutOfBounds(
position,
min,
max,
itemHeight.current * 50,
0.2
),
})
}
}
const handleWheel = (
state: Omit<FullGestureState<'wheel'>, 'event'> & {
event: EventTypes['wheel']
}
) => {
draggingRef.current = true
const min = -((column.length - 1) * itemHeight.current)
const max = 0
const { direction, last, velocity, distance } = handleGestureState(state)
const whellDir = -direction // 取反
const scrollY = y.get()
if (last) {
draggingRef.current = false
const speed = velocity * whellDir * 50
const position =
scrollY +
(distance / (props.wheelResistance ? 4 : 1)) * whellDir +
speed
const boundNum = bound(position, min, max)
const targetIndex = -Math.round(boundNum / itemHeight.current)
scrollSelect(targetIndex)
} else {
const position =
scrollY + (distance / (props.wheelResistance ? 4 : 1)) * whellDir
api.start({
y: rubberbandIfOutOfBounds(
position,
min,
max,
itemHeight.current * 50,
0.2
),
})
}
}
useDrag(
state => {
state.event.stopPropagation()
handleDrag(state)
},
{
axis: 'y',
from: () => [0, y.get()],
filterTaps: true,
pointer: { touch: true },
target: rootRef,
}
)
useWheel(
state => {
state.event.stopPropagation()
handleWheel(state)
},
{
target: props.mouseWheel ? rootRef : undefined,
axis: 'y',
from: () => [0, y.get()],
preventDefault: true,
eventOptions: supportsPassive ? { passive: false } : undefined,
}
)
let selectedIndex: number | null = null
function renderAccessible() {
if (selectedIndex === null) {
return null
}
const current = column[selectedIndex]
const previousIndex = selectedIndex - 1
const nextIndex = selectedIndex + 1
const previous = column[previousIndex]
const next = column[nextIndex]
return (
<div className={`${classPrefix}-column-accessible`}>
<div
className={`${classPrefix}-column-accessible-current`}
role='button'
aria-label={
current ? `当前选择的是:${current.label}` : '当前未选择'
}
>
-
</div>
<div
className={`${classPrefix}-column-accessible-button`}
onClick={() => {
if (!previous) return
scrollSelect(previousIndex)
}}
role={previous ? 'button' : 'text'}
aria-label={
!previous ? '没有上一项' : `选择上一项:${previous.label}`
}
>
-
</div>
<div
className={`${classPrefix}-column-accessible-button`}
onClick={() => {
if (!next) return
scrollSelect(nextIndex)
}}
role={next ? 'button' : 'text'}
aria-label={!next ? '没有下一项' : `选择下一项:${next.label}`}
>
-
</div>
</div>
)
}
return (
<div className={`${classPrefix}-column`}>
<div
className={`${classPrefix}-item-height-measure`}
ref={itemHeightMeasureRef}
/>
<animated.div
ref={rootRef}
style={{ translateY: y }}
className={`${classPrefix}-column-wheel`}
aria-hidden
>
{column.map((item, index) => {
const selected = props.value === item.value
if (selected) selectedIndex = index
function handleClick() {
draggingRef.current = false
scrollSelect(index)
}
return (
<div
key={item.key ?? item.value}
data-selected={selected}
className={classNames(`${classPrefix}-column-item`, {
[`${classPrefix}-column-item-active`]: selected,
})}
onClick={handleClick}
aria-hidden={!selected}
aria-label={selected ? 'active' : ''}
>
<div className={`${classPrefix}-column-item-label`}>
{renderLabel(item)}
</div>
</div>
)
})}
</animated.div>
{renderAccessible()}
</div>
)
},
(prev, next) => {
if (prev.index !== next.index) return false
if (prev.value !== next.value) return false
if (prev.onSelect !== next.onSelect) return false
if (prev.renderLabel !== next.renderLabel) return false
if (prev.mouseWheel !== next.mouseWheel) return false
if (prev.wheelResistance !== next.wheelResistance) return false
if (!isEqual(prev.column, next.column)) return false
return true
}
)
Wheel.displayName = 'Wheel'