Skip to content
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

Add onCellMoved callback #517

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
4 changes: 3 additions & 1 deletion src/components/CellRendererComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ type Props<T> = {
children: React.ReactNode;
onLayout?: (e: LayoutChangeEvent) => void;
style?: StyleProp<ViewStyle>;
onCellMoved?: () => void;
};

function CellRendererComponent<T>(props: Props<T>) {
const { item, index, onLayout, children, ...rest } = props;
const { item, index, onLayout, children, onCellMoved, ...rest } = props;

const viewRef = useRef<Animated.View>(null);
const { cellDataRef, propsRef, containerRef } = useRefs<T>();
Expand All @@ -52,6 +53,7 @@ function CellRendererComponent<T>(props: Props<T>) {
cellOffset: offset,
cellSize: size,
cellIndex: index,
onCellMoved: onCellMoved,
});

const isActive = activeKey === key;
Expand Down
9 changes: 8 additions & 1 deletion src/components/DraggableFlatList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,13 @@ function DraggableFlatListInner<T>(props: DraggableFlatListProps<T>) {
props.onViewableItemsChanged?.(info);
});

const memoCellRendererComponent = useCallback(
(cellProps) => (
<CellRendererComponent {...cellProps} onCellMoved={props.onCellMoved} />
),
[]
);

return (
<DraggableFlatListProvider
activeKey={activeKey}
Expand All @@ -377,7 +384,7 @@ function DraggableFlatListInner<T>(props: DraggableFlatListProps<T>) {
{...props}
data={props.data}
onViewableItemsChanged={onViewableItemsChanged}
CellRendererComponent={CellRendererComponent}
CellRendererComponent={memoCellRendererComponent}
ref={flatlistRef}
onContentSizeChange={onListContentSizeChange}
scrollEnabled={!activeKey && scrollEnabled}
Expand Down
17 changes: 15 additions & 2 deletions src/hooks/useCellTranslate.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import Animated, { useDerivedValue, withSpring } from "react-native-reanimated";
import Animated, {
runOnJS,
useDerivedValue,
withSpring,
} from "react-native-reanimated";
import { useAnimatedValues } from "../context/animatedValueContext";
import { useDraggableFlatListContext } from "../context/draggableFlatListContext";
import { useRefs } from "../context/refContext";
Expand All @@ -7,9 +11,15 @@ type Params = {
cellIndex: number;
cellSize: Animated.SharedValue<number>;
cellOffset: Animated.SharedValue<number>;
onCellMoved?: () => void;
};

export function useCellTranslate({ cellIndex, cellSize, cellOffset }: Params) {
export function useCellTranslate({
cellIndex,
cellSize,
cellOffset,
onCellMoved,
}: Params) {
const {
activeIndexAnim,
activeCellSize,
Expand Down Expand Up @@ -76,6 +86,9 @@ export function useCellTranslate({ cellIndex, cellSize, cellOffset }: Params) {

if (result !== -1 && result !== spacerIndexAnim.value) {
spacerIndexAnim.value = result;
if (onCellMoved) {
runOnJS(onCellMoved)();
}
}

if (spacerIndexAnim.value === cellIndex) {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export type DraggableFlatListProps<T> = Modify<
layout: LayoutChangeEvent["nativeEvent"]["layout"];
containerRef: React.RefObject<Animated.View>;
}) => void;
onCellMoved?: () => void;
} & Partial<DefaultProps>
>;

Expand Down