Skip to content

Commit

Permalink
fix: imports
Browse files Browse the repository at this point in the history
  • Loading branch information
kosmydel committed Aug 24, 2023
1 parent 0244f7a commit 0f0d874
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 113 deletions.
16 changes: 16 additions & 0 deletions src/DraggableList/index.native.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import DraggableFlatList from 'react-native-draggable-flatlist';
import type { DefaultItemProps, DraggableListProps } from './types';

export const DraggableList = <T extends DefaultItemProps>({
items,
...props
}: DraggableListProps<T>) => {
return (
<DraggableFlatList
data={items}
keyExtractor={(item) => item.id}
{...props}
/>
);
};
103 changes: 94 additions & 9 deletions src/DraggableList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,101 @@
import React from 'react';
import DraggableFlatList from 'react-native-draggable-flatlist';
import type { DefaultItemProps, DraggableListProps } from './types';
import React, { useCallback } from 'react';
import {
DragDropContext,
Droppable,
Draggable,
type OnDragEndResponder,
type OnDragUpdateResponder,
} from 'react-beautiful-dnd';
import { View } from 'react-native';
import type { DraggableListProps, DefaultItemProps } from './types';

type ReoderParams<T> = {
list: T[];
startIndex: number;
endIndex: number;
};

// Function to help us with reordering the result
const reorder = <T,>({ list, startIndex, endIndex }: ReoderParams<T>): T[] => {
const result = Array.from(list);
const [removed] = result.splice(startIndex, 1);

if (removed !== undefined) {
result.splice(endIndex, 0, removed);
}

return result;
};

export const DraggableList = <T extends DefaultItemProps>({
items,
...props
renderItem,
onDragEnd: onDragEndCallback,
onDragBegin: onDragBegin,
onPlaceholderIndexChange,
}: DraggableListProps<T>) => {
const onDragEnd: OnDragEndResponder = useCallback(
(result) => {
if (!result.destination) {
return;
}

const reorderedItems = reorder({
list: items,
startIndex: result.source.index,
endIndex: result.destination.index,
});

onDragEndCallback && onDragEndCallback({ data: reorderedItems });
},
[items, onDragEndCallback]
);

const onDragUpdate: OnDragUpdateResponder = useCallback(
(result) => {
if (!result.destination) {
return;
}
onPlaceholderIndexChange &&
onPlaceholderIndexChange(result.destination.index);
},
[onPlaceholderIndexChange]
);

return (
<DraggableFlatList
data={items}
keyExtractor={(item) => item.id}
{...props}
/>
<DragDropContext
onDragEnd={onDragEnd}
onDragStart={onDragBegin}
onDragUpdate={onDragUpdate}
>
<Droppable droppableId="droppable">
{(droppableProvided) => (
<View
{...droppableProvided.droppableProps}
ref={droppableProvided.innerRef}
>
{items.map((item, index) => (
<Draggable key={item.id} draggableId={item.id} index={index}>
{(draggableProvided, snapshot) => (
<div
ref={draggableProvided.innerRef}
{...draggableProvided.draggableProps}
{...draggableProvided.dragHandleProps}
>
{renderItem({
item,
getIndex: () => 1,
isActive: snapshot.isDragging,
drag: () => {},
})}
</div>
)}
</Draggable>
))}
{droppableProvided.placeholder}
</View>
)}
</Droppable>
</DragDropContext>
);
};
101 changes: 0 additions & 101 deletions src/DraggableList/index.web.tsx

This file was deleted.

6 changes: 3 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { DraggableList } from './DraggableList';
import 'react-native-gesture-handler';

export { OpacityDecorator } from './decorators/OpacityDecorator/index.native';
export { ScaleDecorator } from './decorators/ScaleDecorator/index.native';
export { ShadowDecorator } from './decorators/ShadowDecorator/index.native';
export { OpacityDecorator } from './decorators/OpacityDecorator';
export { ScaleDecorator } from './decorators/ScaleDecorator';
export { ShadowDecorator } from './decorators/ShadowDecorator';

export default DraggableList;

0 comments on commit 0f0d874

Please sign in to comment.