Skip to content
Open
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
8 changes: 5 additions & 3 deletions src/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,26 @@ interface BlockProps {
dragStartAnimationStyle: StyleProp<any>
onPress?: () => void
onLongPress: () => void
onLongPressOut: () => void
panHandlers: GestureResponderHandlers
delayLongPress:number
children?:React.ReactNode
delayLongPress: number
children?: React.ReactNode
}

export const Block: FunctionComponent<BlockProps> = ({
style,
dragStartAnimationStyle,
onPress,
onLongPress,
onLongPressOut,
children,
panHandlers,
delayLongPress
}) => {
return (
<Animated.View style={[styles.blockContainer, style, dragStartAnimationStyle]} {...panHandlers}>
<Animated.View>
<TouchableWithoutFeedback delayLongPress={delayLongPress} onPress={onPress} onLongPress={onLongPress}>
<TouchableWithoutFeedback delayLongPress={delayLongPress} delayPressOut={100} onPress={onPress} onLongPress={onLongPress} onPressOut={onLongPressOut}>
{children}
</TouchableWithoutFeedback>
</Animated.View>
Expand Down
19 changes: 16 additions & 3 deletions src/draggable-grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface IDraggableGridProps<DataType extends IBaseItemType> {
onDragging?: (gestureState: PanResponderGestureState) => void
onDragRelease?: (newSortedData: DataType[]) => void
onResetSort?: (newSortedData: DataType[]) => void
delayLongPress?:number
delayLongPress?: number
}
interface IMap<T> {
[key: string]: T
Expand All @@ -53,7 +53,7 @@ interface IItem<DataType> {
}
let activeBlockOffset = { x: 0, y: 0 }

export const DraggableGrid = function<DataType extends IBaseItemType>(
export const DraggableGrid = function <DataType extends IBaseItemType>(
props: IDraggableGridProps<DataType>,
) {
const [blockPositions] = useState<IPositionOffset[]>([])
Expand All @@ -72,6 +72,7 @@ export const DraggableGrid = function<DataType extends IBaseItemType>(
height: 0,
})
const [activeItemIndex, setActiveItemIndex] = useState<undefined | number>()
const [isDragging, setIsDragging] = useState(false)

const assessGridSize = (event: IOnLayoutEvent) => {
if (!hadInitBlockSize) {
Expand Down Expand Up @@ -146,6 +147,7 @@ export const DraggableGrid = function<DataType extends IBaseItemType>(
const activeItem = getActiveItem()
if (!activeItem) return false
const { moveX, moveY } = gestureState
setIsDragging(true)
props.onDragging && props.onDragging(gestureState)

const xChokeAmount = Math.max(0, activeBlockOffset.x + moveX - (gridLayout.width - blockWidth))
Expand Down Expand Up @@ -188,6 +190,7 @@ export const DraggableGrid = function<DataType extends IBaseItemType>(
function onHandRelease() {
const activeItem = getActiveItem()
if (!activeItem) return false
setIsDragging(false)
props.onDragRelease && props.onDragRelease(getSortData())
setPanResponderCapture(false)
activeItem.currentPosition.flattenOffset()
Expand Down Expand Up @@ -253,6 +256,13 @@ export const DraggableGrid = function<DataType extends IBaseItemType>(
setPanResponderCapture(true)
setActiveItemIndex(itemIndex)
}
function endLongPressAnimation() {
if (!props.dragStartAnimation) {
if (!isDragging) {
setActiveItemIndex(undefined)
}
}
}
function startDragStartAnimation() {
if (!props.dragStartAnimation) {
dragStartAnimatedValue.setValue(1)
Expand Down Expand Up @@ -350,7 +360,9 @@ export const DraggableGrid = function<DataType extends IBaseItemType>(
})
}
useEffect(() => {
startDragStartAnimation()
if (activeItemIndex !== undefined) {
startDragStartAnimation()
}
}, [activeItemIndex])
useEffect(() => {
if (hadInitBlockSize) {
Expand All @@ -368,6 +380,7 @@ export const DraggableGrid = function<DataType extends IBaseItemType>(
<Block
onPress={onBlockPress.bind(null, itemIndex)}
onLongPress={setActiveBlock.bind(null, itemIndex, item.itemData)}
onLongPressOut={endLongPressAnimation}
panHandlers={panResponder.panHandlers}
style={getBlockStyle(itemIndex)}
dragStartAnimationStyle={getDragStartAnimation(itemIndex)}
Expand Down