-
Notifications
You must be signed in to change notification settings - Fork 269
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
feat(drag): add the ability to support onDrag, onDragStart, onDragEnd callbacks #2418
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ import React, { FunctionComponent, useState, useEffect, useRef } from 'react' | |||||||||||||||||||
import { getSystemInfoSync, createSelectorQuery } from '@tarojs/taro' | ||||||||||||||||||||
import { BasicComponent, ComponentDefaults } from '@/utils/typings' | ||||||||||||||||||||
import { getRectByTaro } from '@/utils/get-rect-by-taro' | ||||||||||||||||||||
import { DragState } from './drag' | ||||||||||||||||||||
|
||||||||||||||||||||
export interface DragProps extends BasicComponent { | ||||||||||||||||||||
attract: boolean | ||||||||||||||||||||
|
@@ -12,6 +13,9 @@ export interface DragProps extends BasicComponent { | |||||||||||||||||||
right: number | ||||||||||||||||||||
bottom: number | ||||||||||||||||||||
} | ||||||||||||||||||||
onDragStart: () => void | ||||||||||||||||||||
onDragEnd: (state: DragState) => void | ||||||||||||||||||||
onDrag: (state: DragState) => void | ||||||||||||||||||||
} | ||||||||||||||||||||
const defaultProps = { | ||||||||||||||||||||
...ComponentDefaults, | ||||||||||||||||||||
|
@@ -28,11 +32,21 @@ const defaultProps = { | |||||||||||||||||||
export const Drag: FunctionComponent< | ||||||||||||||||||||
Partial<DragProps> & React.HTMLAttributes<HTMLDivElement> | ||||||||||||||||||||
> = (props) => { | ||||||||||||||||||||
const { attract, direction, boundary, children, className, style, ...reset } = | ||||||||||||||||||||
{ | ||||||||||||||||||||
...defaultProps, | ||||||||||||||||||||
...props, | ||||||||||||||||||||
} | ||||||||||||||||||||
const { | ||||||||||||||||||||
attract, | ||||||||||||||||||||
direction, | ||||||||||||||||||||
boundary, | ||||||||||||||||||||
onDrag, | ||||||||||||||||||||
onDragStart, | ||||||||||||||||||||
onDragEnd, | ||||||||||||||||||||
children, | ||||||||||||||||||||
className, | ||||||||||||||||||||
style, | ||||||||||||||||||||
...reset | ||||||||||||||||||||
} = { | ||||||||||||||||||||
...defaultProps, | ||||||||||||||||||||
...props, | ||||||||||||||||||||
} | ||||||||||||||||||||
const classPrefix = 'nut-drag' | ||||||||||||||||||||
const [boundaryState, setBoundaryState] = useState(boundary) | ||||||||||||||||||||
const myDrag = useRef<HTMLDivElement>(null) | ||||||||||||||||||||
|
@@ -72,7 +86,7 @@ export const Drag: FunctionComponent< | |||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
const touchStart = (e: React.TouchEvent<HTMLDivElement>) => { | ||||||||||||||||||||
const target = e.currentTarget as HTMLElement | ||||||||||||||||||||
onDragStart && onDragStart() | ||||||||||||||||||||
const touches = e.touches[0] | ||||||||||||||||||||
axisCache.current = { x: touches.clientX, y: touches.clientY } | ||||||||||||||||||||
transformCache.current = { x: translateX.current, y: translateY.current } | ||||||||||||||||||||
|
@@ -85,7 +99,10 @@ export const Drag: FunctionComponent< | |||||||||||||||||||
const y = touch.clientY - axisCache.current.y | ||||||||||||||||||||
translateX.current = x + transformCache.current.x | ||||||||||||||||||||
translateY.current = y + transformCache.current.y | ||||||||||||||||||||
|
||||||||||||||||||||
onDrag && | ||||||||||||||||||||
onDrag({ | ||||||||||||||||||||
offset: [translateX.current, translateY.current], | ||||||||||||||||||||
}) | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 优化可选链使用 在调用 - onDrag &&
- onDrag({
- offset: [translateX.current, translateY.current],
- })
+ onDrag?.({
+ offset: [translateX.current, translateY.current],
+ }) Committable suggestion
Suggested change
ToolsBiome
|
||||||||||||||||||||
// 边界判断 | ||||||||||||||||||||
if (translateX.current < boundaryState.left) { | ||||||||||||||||||||
translateX.current = boundaryState.left | ||||||||||||||||||||
|
@@ -107,6 +124,10 @@ export const Drag: FunctionComponent< | |||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
const touchEnd = (e: React.TouchEvent) => { | ||||||||||||||||||||
onDragEnd && | ||||||||||||||||||||
onDragEnd({ | ||||||||||||||||||||
offset: [translateX.current, translateY.current], | ||||||||||||||||||||
}) | ||||||||||||||||||||
if (direction !== 'y' && attract && dragRef.current) { | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 优化可选链使用 在调用 - onDragEnd &&
- onDragEnd({
- offset: [translateX.current, translateY.current],
- })
+ onDragEnd?.({
+ offset: [translateX.current, translateY.current],
+ }) Committable suggestion
Suggested change
ToolsBiome
|
||||||||||||||||||||
if (translateX.current < middleLine.current) { | ||||||||||||||||||||
translateX.current = boundaryState.left | ||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
优化可选链使用
在调用
onDragStart
,onDrag
, 和onDragEnd
时,建议使用可选链操作符以确保安全调用。Committable suggestion
Tools
Biome