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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"react-native": "*",
"ts-jest": "^24.0.2",
"prettier": "^1.18.2",
"typescript": "^3.8.0"
"typescript": "^5.9.3"
},
"peerDependencies": {
"react": "*",
Expand Down
13 changes: 8 additions & 5 deletions src/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import { FunctionComponent } from 'react'
interface BlockProps {
style?: StyleProp<any>
dragStartAnimationStyle: StyleProp<any>
onPress?: () => void
onPress: () => void
onLongPress: () => void
panHandlers: GestureResponderHandlers
delayLongPress:number
children?:React.ReactNode
delayLongPress: number
children?: React.ReactNode
}

export const Block: FunctionComponent<BlockProps> = ({
Expand All @@ -27,12 +27,15 @@ export const Block: FunctionComponent<BlockProps> = ({
onLongPress,
children,
panHandlers,
delayLongPress
delayLongPress,
}) => {
return (
<Animated.View style={[styles.blockContainer, style, dragStartAnimationStyle]} {...panHandlers}>
<Animated.View>
<TouchableWithoutFeedback delayLongPress={delayLongPress} onPress={onPress} onLongPress={onLongPress}>
<TouchableWithoutFeedback
delayLongPress={delayLongPress}
onPress={onPress}
onLongPress={onLongPress}>
{children}
</TouchableWithoutFeedback>
</Animated.View>
Expand Down
58 changes: 33 additions & 25 deletions src/draggable-grid.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/** @format */

import * as React from 'react'
import { useState, useEffect } from 'react'
import {
Expand All @@ -9,7 +11,7 @@ import {
PanResponderGestureState,
ViewStyle,
Platform,
I18nManager
I18nManager,
} from 'react-native'
import { Block } from './block'
import { findKey, findIndex, differenceBy } from './utils'
Expand Down Expand Up @@ -122,14 +124,14 @@ export const DraggableGrid = function<DataType extends IBaseItemType>(
gridHeight.setValue(rowCount * blockHeight)
}
function onBlockPress(itemIndex: number) {
props.onItemPress && props.onItemPress(items[itemIndex].itemData)
props.onItemPress && props.onItemPress(items[itemIndex]!.itemData)
}
function onStartDrag(_: GestureResponderEvent, gestureState: PanResponderGestureState) {
const activeItem = getActiveItem()
if (!activeItem) return false
if (!activeItem) return
props.onDragStart && props.onDragStart(activeItem.itemData)
const { x0, y0, moveX, moveY } = gestureState
const activeOrigin = blockPositions[orderMap[activeItem.key].order]
const activeOrigin = blockPositions[orderMap[activeItem.key]!.order]!
const x = activeOrigin.x + (I18nManager.isRTL ? x0 : -x0)
const y = activeOrigin.y - y0
activeItem.currentPosition.setOffset({
Expand All @@ -147,8 +149,8 @@ export const DraggableGrid = function<DataType extends IBaseItemType>(
}
function onHandMove(_: GestureResponderEvent, gestureState: PanResponderGestureState) {
const activeItem = getActiveItem()
if (!activeItem) return false
const { moveX:moveXOriginal, moveY } = gestureState
if (!activeItem) return
const { moveX: moveXOriginal, moveY } = gestureState
const moveX = I18nManager.isRTL ? -moveXOriginal : moveXOriginal
props.onDragging && props.onDragging(gestureState)

Expand All @@ -159,7 +161,7 @@ export const DraggableGrid = function<DataType extends IBaseItemType>(
x: moveX - xChokeAmount - xMinChokeAmount,
y: moveY,
}
const originPosition = blockPositions[orderMap[activeItem.key].order]
const originPosition = blockPositions[orderMap[activeItem.key]!.order]!
const dragPositionToActivePositionDistance = getDistance(dragPosition, originPosition)
activeItem.currentPosition.setValue(dragPosition)

Expand All @@ -171,7 +173,7 @@ export const DraggableGrid = function<DataType extends IBaseItemType>(
if (index != activeItemIndex) {
const dragPositionToItemPositionDistance = getDistance(
dragPosition,
blockPositions[orderMap[item.key].order],
blockPositions[orderMap[item.key]!.order]!,
)
if (
dragPositionToItemPositionDistance < closetDistance &&
Expand All @@ -183,15 +185,15 @@ export const DraggableGrid = function<DataType extends IBaseItemType>(
}
})
if (activeItemIndex != closetItemIndex) {
const closetOrder = orderMap[items[closetItemIndex].key].order
resetBlockPositionByOrder(orderMap[activeItem.key].order, closetOrder)
orderMap[activeItem.key].order = closetOrder
const closetOrder = orderMap[items[closetItemIndex]!.key]!.order
resetBlockPositionByOrder(orderMap[activeItem.key]!.order, closetOrder)
orderMap[activeItem.key]!.order = closetOrder
props.onResetSort && props.onResetSort(getSortData())
}
}
function onHandRelease() {
const activeItem = getActiveItem()
if (!activeItem) return false
if (!activeItem) return
props.onDragRelease && props.onDragRelease(getSortData())
setPanResponderCapture(false)
activeItem.currentPosition.flattenOffset()
Expand All @@ -207,7 +209,7 @@ export const DraggableGrid = function<DataType extends IBaseItemType>(
if (item && item.disabledReSorted) {
disabledReSortedItemCount++
} else {
orderMap[key].order += disabledReSortedItemCount + 1
orderMap[key]!.order += disabledReSortedItemCount + 1
disabledReSortedItemCount = 0
moveBlockToBlockOrderPosition(key)
}
Expand All @@ -219,7 +221,7 @@ export const DraggableGrid = function<DataType extends IBaseItemType>(
if (item && item.disabledReSorted) {
disabledReSortedItemCount++
} else {
orderMap[key].order -= disabledReSortedItemCount + 1
orderMap[key]!.order -= disabledReSortedItemCount + 1
disabledReSortedItemCount = 0
moveBlockToBlockOrderPosition(key)
}
Expand All @@ -228,9 +230,9 @@ export const DraggableGrid = function<DataType extends IBaseItemType>(
}
function moveBlockToBlockOrderPosition(itemKey: string | number) {
const itemIndex = findIndex(items, item => `${item.key}` === `${itemKey}`)
items[itemIndex].currentPosition.flattenOffset()
Animated.timing(items[itemIndex].currentPosition, {
toValue: blockPositions[orderMap[itemKey].order],
items[itemIndex]!.currentPosition.flattenOffset()
Animated.timing(items[itemIndex]!.currentPosition, {
toValue: blockPositions[orderMap[itemKey]!.order]!,
duration: 200,
useNativeDriver: false,
}).start()
Expand All @@ -242,7 +244,7 @@ export const DraggableGrid = function<DataType extends IBaseItemType>(
function getSortData() {
const sortData: DataType[] = []
items.forEach(item => {
sortData[orderMap[item.key].order] = item.itemData
sortData[orderMap[item.key]!.order] = item.itemData
})
return sortData
}
Expand Down Expand Up @@ -279,10 +281,16 @@ export const DraggableGrid = function<DataType extends IBaseItemType>(
width: blockWidth,
height: blockHeight,
position: 'absolute',
top: items[itemIndex].currentPosition.getLayout().top,
left: I18nManager.isRTL && Platform.OS === 'web' ? undefined: items[itemIndex].currentPosition.getLayout().left,
right: I18nManager.isRTL && Platform.OS === 'web' ? items[itemIndex].currentPosition.getLayout().left : undefined,
},
top: items[itemIndex]!.currentPosition.getLayout()['top'],
left:
I18nManager.isRTL && Platform.OS === 'web'
? undefined
: items[itemIndex]!.currentPosition.getLayout()['left'],
right:
I18nManager.isRTL && Platform.OS === 'web'
? items[itemIndex]!.currentPosition.getLayout()['left']
: undefined,
},
]
}
function getDragStartAnimation(itemIndex: number) {
Expand Down Expand Up @@ -338,8 +346,8 @@ export const DraggableGrid = function<DataType extends IBaseItemType>(
function diffData() {
props.data.forEach((item, index) => {
if (orderMap[item.key]) {
if (orderMap[item.key].order != index) {
orderMap[item.key].order = index
if (orderMap[item.key]!.order != index) {
orderMap[item.key]!.order = index
moveBlockToBlockOrderPosition(item.key)
}
const currentItem = items.find(i => i.key === item.key)
Expand Down Expand Up @@ -380,7 +388,7 @@ export const DraggableGrid = function<DataType extends IBaseItemType>(
dragStartAnimationStyle={getDragStartAnimation(itemIndex)}
delayLongPress={props.delayLongPress || 300}
key={item.key}>
{props.renderItem(item.itemData, orderMap[item.key].order)}
{props.renderItem(item.itemData, orderMap[item.key]!.order)}
</Block>
)
})
Expand Down
9 changes: 6 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
function findKey<T>(map: { [key: string]: T }, fn: (item: T) => boolean) {
/** @format */

function findKey<T>(map: { [key: string]: T }, fn: (item: T) => boolean): string | undefined {
const keys = Object.keys(map)
for (let i = 0; i < keys.length; i++) {
if (fn(map[keys[i]])) {
if (fn(map[keys[i]!]!)) {
return keys[i]
}
}
return undefined
}

function findIndex<T>(arr: T[], fn: (item: T) => boolean) {
for (let i = 0; i < arr.length; i++) {
if (fn(arr[i])) {
if (fn(arr[i]!)) {
return i
}
}
Expand Down
9 changes: 9 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
{
"compilerOptions": {
"module": "commonjs",
"noUncheckedSideEffectImports": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,
"noImplicitReturns": true,
"noImplicitOverride": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"noPropertyAccessFromIndexSignature": true,
"strict": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5815,10 +5815,10 @@ typedarray@^0.0.6:
version "0.0.6"
resolved "http://registry.npm.taobao.org/typedarray/download/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"

typescript@^3.8.0:
version "3.9.10"
resolved "https://npm.corp.kuaishou.com/typescript/-/typescript-3.9.10.tgz#70f3910ac7a51ed6bef79da7800690b19bf778b8"
integrity sha1-cPORCselHta+952ngAaQsZv3eLg=
typescript@^5.9.3:
version "5.9.3"
resolved "http://registry.npm.taobao.org/typescript/-/typescript-5.9.3.tgz#5b4f59e15310ab17a216f5d6cf53ee476ede670f"
integrity sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==

ua-parser-js@^0.7.18:
version "0.7.18"
Expand Down