diff --git a/package.json b/package.json index 8c85811..6ad9b83 100644 --- a/package.json +++ b/package.json @@ -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": "*", diff --git a/src/block.tsx b/src/block.tsx index 6b2927f..c727dcf 100644 --- a/src/block.tsx +++ b/src/block.tsx @@ -13,11 +13,11 @@ import { FunctionComponent } from 'react' interface BlockProps { style?: StyleProp dragStartAnimationStyle: StyleProp - onPress?: () => void + onPress: () => void onLongPress: () => void panHandlers: GestureResponderHandlers - delayLongPress:number - children?:React.ReactNode + delayLongPress: number + children?: React.ReactNode } export const Block: FunctionComponent = ({ @@ -27,12 +27,15 @@ export const Block: FunctionComponent = ({ onLongPress, children, panHandlers, - delayLongPress + delayLongPress, }) => { return ( - + {children} diff --git a/src/draggable-grid.tsx b/src/draggable-grid.tsx index 213e515..18c6ee2 100644 --- a/src/draggable-grid.tsx +++ b/src/draggable-grid.tsx @@ -1,3 +1,5 @@ +/** @format */ + import * as React from 'react' import { useState, useEffect } from 'react' import { @@ -9,7 +11,7 @@ import { PanResponderGestureState, ViewStyle, Platform, - I18nManager + I18nManager, } from 'react-native' import { Block } from './block' import { findKey, findIndex, differenceBy } from './utils' @@ -122,14 +124,14 @@ export const DraggableGrid = function( 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({ @@ -147,8 +149,8 @@ export const DraggableGrid = function( } 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) @@ -159,7 +161,7 @@ export const DraggableGrid = function( 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) @@ -171,7 +173,7 @@ export const DraggableGrid = function( if (index != activeItemIndex) { const dragPositionToItemPositionDistance = getDistance( dragPosition, - blockPositions[orderMap[item.key].order], + blockPositions[orderMap[item.key]!.order]!, ) if ( dragPositionToItemPositionDistance < closetDistance && @@ -183,15 +185,15 @@ export const DraggableGrid = function( } }) 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() @@ -207,7 +209,7 @@ export const DraggableGrid = function( if (item && item.disabledReSorted) { disabledReSortedItemCount++ } else { - orderMap[key].order += disabledReSortedItemCount + 1 + orderMap[key]!.order += disabledReSortedItemCount + 1 disabledReSortedItemCount = 0 moveBlockToBlockOrderPosition(key) } @@ -219,7 +221,7 @@ export const DraggableGrid = function( if (item && item.disabledReSorted) { disabledReSortedItemCount++ } else { - orderMap[key].order -= disabledReSortedItemCount + 1 + orderMap[key]!.order -= disabledReSortedItemCount + 1 disabledReSortedItemCount = 0 moveBlockToBlockOrderPosition(key) } @@ -228,9 +230,9 @@ export const DraggableGrid = function( } 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() @@ -242,7 +244,7 @@ export const DraggableGrid = function( function getSortData() { const sortData: DataType[] = [] items.forEach(item => { - sortData[orderMap[item.key].order] = item.itemData + sortData[orderMap[item.key]!.order] = item.itemData }) return sortData } @@ -279,10 +281,16 @@ export const DraggableGrid = function( 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) { @@ -338,8 +346,8 @@ export const DraggableGrid = function( 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) @@ -380,7 +388,7 @@ export const DraggableGrid = function( 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)} ) }) diff --git a/src/utils.ts b/src/utils.ts index c865163..c3a3457 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,15 +1,18 @@ -function findKey(map: { [key: string]: T }, fn: (item: T) => boolean) { +/** @format */ + +function findKey(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(arr: T[], fn: (item: T) => boolean) { for (let i = 0; i < arr.length; i++) { - if (fn(arr[i])) { + if (fn(arr[i]!)) { return i } } diff --git a/tsconfig.json b/tsconfig.json index 9bcd0c5..0dd991a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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, diff --git a/yarn.lock b/yarn.lock index f87d7b7..13a2a00 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"