Skip to content
Draft
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
20 changes: 18 additions & 2 deletions example/app/src/examples/SortableGrid/PlaygroundExample.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback } from 'react';
import { useCallback, useState } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import type { SortableGridRenderItem } from 'react-native-sortables';
import Sortable from 'react-native-sortables';
Expand All @@ -9,6 +9,8 @@
const DATA = Array.from({ length: 12 }, (_, index) => `Item ${index + 1}`);

export default function PlaygroundExample() {
const [data, setData] = useState(DATA);

const renderItem = useCallback<SortableGridRenderItem<string>>(
({ item }) => (
<View style={styles.card}>
Expand All @@ -23,9 +25,23 @@
<Sortable.Grid
columnGap={10}
columns={3}
data={DATA}
data={data}
renderItem={renderItem}
reorderOnDrag={false}
rowGap={10}
strategy='swap'
onDragEnd={params => {
console.log(

Check warning on line 34 in example/app/src/examples/SortableGrid/PlaygroundExample.tsx

View workflow job for this annotation

GitHub Actions / 🧹 ESLint

Unexpected console statement. Only these console methods are allowed: warn, error
'item',
params.key,
'moved from',
params.fromIndex,
'to',
params.toIndex,
`(${params.data[params.toIndex]})`
);
setData(params.data.filter(item => item !== params.key));
}}
/>
</ScrollScreen>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default function DropIndicatorExample() {
columns={COLUMNS}
data={DATA}
renderItem={renderItem}
reorderOnDrag={false}
rowGap={spacing.xs}
showDropIndicator
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ const SortableFlexInner = memo(function SortableFlexInner({
paddingRight,
paddingTop,
paddingVertical,
reorderOnDrag,
rowGap,
showDropIndicator,
strategy,
Expand Down Expand Up @@ -159,6 +160,7 @@ const SortableFlexInner = memo(function SortableFlexInner({
itemEntering={itemEntering}
itemExiting={itemExiting}
overflow={overflow}
reorderOnDrag={reorderOnDrag}
showDropIndicator={showDropIndicator}
strategy={strategy}
styleProps={styleProps}
Expand All @@ -176,13 +178,15 @@ type SortableFlexComponentProps = Pick<
| 'itemEntering'
| 'itemExiting'
| 'overflow'
| 'reorderOnDrag'
| 'showDropIndicator'
| 'strategy'
> & {
styleProps: FlexStyleProps;
};

function SortableFlexComponent({
reorderOnDrag,
strategy,
styleProps,
...rest
Expand All @@ -200,7 +204,7 @@ function SortableFlexComponent({
width
} = styleProps;

useOrderUpdater(strategy, FLEX_STRATEGIES);
useOrderUpdater(strategy, FLEX_STRATEGIES, reorderOnDrag);

const baseContainerStyle: ViewStyle = {
...styleProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ const SortableGridInner = typedMemo(function SortableGridInner<I>({
itemEntering,
itemExiting,
overflow,
reorderOnDrag,
rowGap: _rowGap,
rowHeight,
showDropIndicator,
Expand Down Expand Up @@ -156,6 +157,7 @@ const SortableGridInner = typedMemo(function SortableGridInner<I>({
itemEntering={itemEntering}
itemExiting={itemExiting}
overflow={overflow}
reorderOnDrag={reorderOnDrag}
rowGap={rowGap}
rowHeight={rowHeight}
showDropIndicator={showDropIndicator}
Expand All @@ -176,6 +178,7 @@ type SortableGridComponentProps<I> = Pick<
| 'itemEntering'
| 'itemExiting'
| 'overflow'
| 'reorderOnDrag'
| 'rowHeight'
| 'showDropIndicator'
| 'strategy'
Expand All @@ -188,6 +191,7 @@ function SortableGridComponent<I>({
columnGap,
groups,
isVertical,
reorderOnDrag,
rowGap,
rowHeight,
strategy,
Expand All @@ -199,7 +203,7 @@ function SortableGridComponent<I>({

const isFirstRenderRef = useRef(true);

useOrderUpdater(strategy, GRID_STRATEGIES);
useOrderUpdater(strategy, GRID_STRATEGIES, reorderOnDrag);

useLayoutEffect(() => {
if (isFirstRenderRef.current) {
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-sortables/src/constants/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const DEFAULT_SHARED_PROPS = {
onOrderChange: undefined,
overDrag: 'both',
overflow: 'visible',
reorderOnDrag: true,
reorderTriggerOrigin: 'center',
scrollableRef: undefined,
showDropIndicator: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ function useInactiveIndexToKey() {
}),
({ excludedKey, fixedKeys, idxToKey }) => {
if (excludedKey === null) {
result.value = EMPTY_ARRAY;
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ function useInactiveIndexToKey() {
({ excludedKey, idxToKey, keyToIdx }) => {
const excludedIndex = excludedKey ? keyToIdx[excludedKey] : undefined;
if (excludedIndex === undefined) {
result.value = EMPTY_ARRAY;
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import { useCallback } from 'react';
import { useAnimatedReaction } from 'react-native-reanimated';

import { useDebugContext } from '../../../debug';
import type { PredefinedStrategies, SortStrategyFactory } from '../../../types';
import type {
OrderUpdaterCallbackProps,
PredefinedStrategies,
SortStrategyFactory
} from '../../../types';
import { error } from '../../../utils';
import { useCommonValuesContext } from '../CommonValuesProvider';
import { useDragContext } from '../DragProvider';

export default function useOrderUpdater<
P extends PredefinedStrategies = PredefinedStrategies
>(strategy: keyof P | SortStrategyFactory, predefinedStrategies: P) {
>(
strategy: keyof P | SortStrategyFactory,
predefinedStrategies: P,
reorderOnDrag: boolean
) {
const useStrategy =
typeof strategy === 'string' ? predefinedStrategies[strategy] : strategy;

Expand All @@ -25,27 +34,18 @@ export default function useOrderUpdater<

const updater = useStrategy();

useAnimatedReaction(
() => ({
activeKey: activeItemKey.value,
dimensions: activeItemDimensions.value,
position: triggerOriginPosition.value
}),
({ activeKey, dimensions, position }) => {
if (!activeKey || !dimensions || !position) {
if (debugCross) debugCross.set({ position: null });
return;
}

const handleOrderUpdate = useCallback(
({
activeKey,
dimensions,
position
}: Omit<OrderUpdaterCallbackProps, 'activeIndex'>) => {
'worklet';
const activeIndex = keyToIndex.value[activeKey];
if (activeIndex === undefined) {
return;
}

if (debugCross) {
debugCross.set({ color: '#00007e', position });
}

const newOrder = updater({
activeIndex,
activeKey,
Expand All @@ -61,6 +61,38 @@ export default function useOrderUpdater<
newOrder
);
}
},
[handleOrderChange, keyToIndex, updater]
);

useAnimatedReaction(
() => ({
activeKey: activeItemKey.value,
dimensions: activeItemDimensions.value,
position: triggerOriginPosition.value
}),
({ activeKey, dimensions, position }, prevProps) => {
debugCross?.set({ color: '#00007e', position });

if (reorderOnDrag) {
if (activeKey !== null && dimensions && position) {
handleOrderUpdate({ activeKey, dimensions, position });
} else {
debugCross?.set({ position: null });
}
} else if (
activeKey === null &&
prevProps?.dimensions &&
prevProps?.position &&
prevProps?.activeKey !== null
) {
debugCross?.set({ position: null });
handleOrderUpdate({
activeKey: prevProps.activeKey,
dimensions: prevProps.dimensions,
position: prevProps.position
});
}
}
);
}
4 changes: 4 additions & 0 deletions packages/react-native-sortables/src/types/props/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ export type SharedProps = Simplify<
* @default 'visible'
*/
overflow: Overflow;
/** Whether to reorder items during drag gesture or after the active item is dropped
* @default true
*/
reorderOnDrag: boolean;
/** Enables debug mode to show additional visual helpers and console logs.
* @note This only works in development builds and has no effect in production.
* @default false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export type DebugContextType = {

// ORDER UPDATER

type OrderUpdaterCallbackProps = {
export type OrderUpdaterCallbackProps = {
activeKey: string;
activeIndex: number;
dimensions: Dimensions;
Expand Down
Loading