70 lines
1.8 KiB
TypeScript
70 lines
1.8 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Manual mock for `react-native-draggable-flatlist`.
|
|
*
|
|
* The real list needs the gesture/reanimated runtime. Tests only need the rows
|
|
* to render (and reorder callbacks to be reachable), so this renders every row
|
|
* with the same props the library supplies and exposes the drag callbacks as
|
|
* spies.
|
|
*/
|
|
import * as React from 'react'
|
|
import { View } from 'react-native'
|
|
|
|
export type RenderItemParams<T> = {
|
|
item: T
|
|
index: number
|
|
drag: () => void
|
|
isActive: boolean
|
|
getIndex: () => number | undefined
|
|
}
|
|
|
|
const DraggableFlatList = ({
|
|
data = [],
|
|
renderItem,
|
|
keyExtractor,
|
|
ListEmptyComponent,
|
|
containerStyle,
|
|
contentContainerStyle,
|
|
...rest
|
|
}: any) => {
|
|
const children: any[] = []
|
|
|
|
if (data.length === 0 && ListEmptyComponent) {
|
|
children.push(
|
|
typeof ListEmptyComponent === 'function'
|
|
? React.createElement(ListEmptyComponent, { key: 'empty' })
|
|
: ListEmptyComponent
|
|
)
|
|
}
|
|
|
|
data.forEach((item: any, index: number) => {
|
|
const key = keyExtractor ? keyExtractor(item, index) : String(index)
|
|
children.push(
|
|
React.createElement(
|
|
React.Fragment,
|
|
{ key },
|
|
renderItem({
|
|
item,
|
|
index,
|
|
drag: jest.fn(),
|
|
isActive: false,
|
|
getIndex: () => index,
|
|
})
|
|
)
|
|
)
|
|
})
|
|
|
|
return React.createElement(
|
|
View,
|
|
{ ...rest, style: containerStyle ?? contentContainerStyle, testID: rest.testID ?? 'draggable-flat-list' },
|
|
children
|
|
)
|
|
}
|
|
|
|
const ScaleDecorator = ({ children }: any) => children
|
|
const ShadowDecorator = ScaleDecorator
|
|
const OpacityDecorator = ScaleDecorator
|
|
const useOnCellActiveAnimation = () => ({ isActive: false })
|
|
|
|
export { ScaleDecorator, ShadowDecorator, OpacityDecorator, useOnCellActiveAnimation }
|
|
export default DraggableFlatList
|