Skip to content

SortableList - onDragStart and onDragEnd #3780

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

Open
wants to merge 1 commit into
base: master
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
6 changes: 5 additions & 1 deletion src/components/sortableList/SortableListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ const SortableListItem = (props: Props) => {
lockedIds,
onChange,
enableHaptic,
scale: propsScale = 1
scale: propsScale = 1,
onDragStart,
onDragEnd
} = useContext(SortableListContext);
const {getTranslationByIndexChange, getItemIndexById, getIndexByPosition, getIdByItemIndex} = usePresenter();
const id: string = data[index].id;
Expand Down Expand Up @@ -106,6 +108,7 @@ const SortableListItem = (props: Props) => {
lastSwap.value = {...lastSwap.value, from: currIndex.value};
tempTranslation.value = translation.value;
tempItemsOrder.value = itemsOrder.value;
runOnJS(onDragStart)();
})
.onTouchesMove(() => {
if (enableHaptic && !isDragging.value) {
Expand Down Expand Up @@ -156,6 +159,7 @@ const SortableListItem = (props: Props) => {
runOnJS(onChange)({...lastSwap.value});
}
});
runOnJS(onDragEnd)();
})
.onFinalize(() => {
if (isDragging.value) {
Expand Down
6 changes: 4 additions & 2 deletions src/components/sortableList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function generateLockedIds<ItemT extends SortableListItemProps>(data: SortableLi

const SortableList = <ItemT extends SortableListItemProps>(props: SortableListProps<ItemT>) => {
const themeProps = useThemeProps(props, 'SortableList');
const {data, onOrderChange, enableHaptic, scale, itemProps, horizontal, listRef, flexMigration, ...others} =
const {data, onOrderChange, enableHaptic, scale, itemProps, horizontal, listRef, flexMigration, onDragStart, onDragEnd, ...others} =
themeProps;

const itemsOrder = useSharedValue<string[]>(generateItemsOrder(data));
Expand Down Expand Up @@ -82,7 +82,9 @@ const SortableList = <ItemT extends SortableListItemProps>(props: SortableListPr
itemProps,
onItemLayout,
enableHaptic,
scale
scale,
onDragStart,
onDragEnd
};
}, [data, onChange]);

Expand Down
8 changes: 8 additions & 0 deletions src/components/sortableList/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,12 @@ export interface SortableListProps<ItemT extends SortableListItemProps>
* Temporary migration flag for enabling flex on the container of the list (like it should be by default)
*/
flexMigration?: boolean;
/**
* A callback triggered when the user starts dragging an item.
*/
onDragStart?: () => void;
/**
* A callback triggered when the user finishes dragging an item.
*/
onDragEnd?: () => void;
}