Skip to content

Commit bd1778b

Browse files
authored
chore: Clean up order updaters and sort strategies management (#415)
## Description Just some cleanup and code improvements
1 parent 3617f86 commit bd1778b

11 files changed

Lines changed: 87 additions & 124 deletions

File tree

example/app/src/examples/SortableGrid/PlaygroundExample.tsx

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,13 @@ import Sortable from 'react-native-sortables';
66
import { ScrollScreen } from '@/components';
77
import { colors, radius, sizes, spacing, text } from '@/theme';
88

9-
const DATA = [
10-
{ height: 50, id: 1 },
11-
{ height: 50, id: 2 },
12-
{ height: 50, id: 3 },
13-
{ height: 300, id: 4 },
14-
{ height: 50, id: 5 }
15-
];
9+
const DATA = Array.from({ length: 12 }, (_, index) => `Item ${index + 1}`);
1610

1711
export default function PlaygroundExample() {
18-
const renderItem = useCallback<SortableGridRenderItem<(typeof DATA)[number]>>(
12+
const renderItem = useCallback<SortableGridRenderItem<string>>(
1913
({ item }) => (
20-
<View style={[styles.card, { height: item.height }]}>
21-
<Text style={styles.text}>{item.id}</Text>
14+
<View style={styles.card}>
15+
<Text style={styles.text}>{item}</Text>
2216
</View>
2317
),
2418
[]
@@ -32,8 +26,6 @@ export default function PlaygroundExample() {
3226
data={DATA}
3327
renderItem={renderItem}
3428
rowGap={10}
35-
strategy='swap'
36-
debug
3729
/>
3830
</ScrollScreen>
3931
);

packages/react-native-sortables/src/components/SortableFlex.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
OrderUpdaterComponent,
1515
SharedProvider,
1616
useCommonValuesContext,
17-
useFlexLayoutContext,
1817
useStrategyKey
1918
} from '../providers';
2019
import type { DropIndicatorSettings, SortableFlexProps } from '../types';
@@ -79,7 +78,6 @@ function SortableFlex(props: SortableFlexProps) {
7978
predefinedStrategies={FLEX_STRATEGIES}
8079
strategy={strategy}
8180
triggerOrigin={reorderTriggerOrigin}
82-
useAdditionalValues={useFlexLayoutContext}
8381
/>
8482
<SortableFlexInner
8583
childrenArray={childrenArray}

packages/react-native-sortables/src/components/SortableGrid.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ function SortableGrid<I>(props: SortableGridProps<I>) {
104104
predefinedStrategies={GRID_STRATEGIES}
105105
strategy={strategy}
106106
triggerOrigin={reorderTriggerOrigin}
107-
useAdditionalValues={useGridLayoutContext}
108107
/>
109108
<SortableGridInner
110109
columnGap={columnGapValue}

packages/react-native-sortables/src/index.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,23 @@ import {
66
SortableLayer,
77
SortableTouchable
88
} from './components';
9-
export { useItemContext } from './providers';
9+
import { MultiZoneProvider, PortalProvider } from './providers';
1010

11-
import { MultiZoneProvider, PortalProvider, useZoneContext } from './providers';
1211
export type { CustomHandleProps, SortableLayerProps } from './components';
1312
export * from './constants/layoutAnimations';
13+
export { useItemContext } from './providers';
14+
export {
15+
useAutoScrollContext,
16+
useCommonValuesContext,
17+
useCustomHandleContext,
18+
useDragContext,
19+
useFlexLayoutContext,
20+
useGridLayoutContext,
21+
useIsInPortalOutlet,
22+
useMeasurementsContext,
23+
usePortalContext,
24+
useZoneContext
25+
} from './providers';
1426
export type {
1527
ActiveItemDroppedCallback,
1628
ActiveItemDroppedParams,
@@ -28,15 +40,14 @@ export type {
2840
SortableFlexDragEndParams,
2941
SortableFlexProps,
3042
SortableFlexStrategy,
31-
SortableFlexStrategyFactory,
3243
SortableFlexStyle,
3344
SortableGridDragEndCallback,
3445
SortableGridDragEndParams,
3546
SortableGridProps,
3647
SortableGridRenderItem,
3748
SortableGridRenderItemInfo,
3849
SortableGridStrategy,
39-
SortableGridStrategyFactory
50+
SortStrategyFactory
4051
} from './types';
4152
export { DragActivationState } from './types';
4253

@@ -232,6 +243,4 @@ const Sortable = {
232243
Touchable: SortableTouchable
233244
};
234245

235-
export { useZoneContext };
236-
237246
export default Sortable;

packages/react-native-sortables/src/providers/layout/flex/updates/insert/index.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
Coordinate,
77
Dimension,
88
FlexLayout,
9-
SortableFlexStrategyFactory
9+
SortStrategyFactory
1010
} from '../../../../../types';
1111
import {
1212
error,
@@ -16,29 +16,32 @@ import {
1616
} from '../../../../../utils';
1717
import {
1818
getAdditionalSwapOffset,
19+
useCommonValuesContext,
20+
useCustomHandleContext,
1921
useDebugBoundingBox
2022
} from '../../../../shared';
23+
import { useFlexLayoutContext } from '../../FlexLayoutProvider';
2124
import type { ItemGroupSwapProps, ItemGroupSwapResult } from './utils';
2225
import {
2326
getSwappedToGroupAfterIndices,
2427
getSwappedToGroupBeforeIndices,
2528
getTotalGroupSize
2629
} from './utils';
2730

28-
const useInsertStrategy: SortableFlexStrategyFactory = ({
29-
activeItemKey,
30-
appliedLayout,
31-
calculateFlexLayout,
32-
columnGap,
33-
fixedItemKeys,
34-
flexDirection,
35-
indexToKey,
36-
itemDimensions,
37-
keyToGroup,
38-
keyToIndex,
39-
rowGap,
40-
useFlexLayoutReaction
41-
}) => {
31+
const useInsertStrategy: SortStrategyFactory = () => {
32+
const { activeItemKey, indexToKey, itemDimensions, keyToIndex } =
33+
useCommonValuesContext();
34+
const {
35+
appliedLayout,
36+
calculateFlexLayout,
37+
columnGap,
38+
flexDirection,
39+
keyToGroup,
40+
rowGap,
41+
useFlexLayoutReaction
42+
} = useFlexLayoutContext();
43+
const { fixedItemKeys } = useCustomHandleContext() ?? {};
44+
4245
useAnimatedReaction(
4346
() => fixedItemKeys?.value,
4447
fixedKeys => {

packages/react-native-sortables/src/providers/layout/grid/updates/common.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,35 @@ import type {
44
Coordinate,
55
Dimension,
66
ReorderFunction,
7-
SortableGridStrategyFactory
7+
SortStrategyFactory
88
} from '../../../../types';
9-
import { getAdditionalSwapOffset, useDebugBoundingBox } from '../../../shared';
9+
import {
10+
getAdditionalSwapOffset,
11+
useCommonValuesContext,
12+
useCustomHandleContext,
13+
useDebugBoundingBox
14+
} from '../../../shared';
15+
import { useGridLayoutContext } from '../GridLayoutProvider';
1016
import { getCrossIndex, getMainIndex } from '../utils';
1117

1218
export const createGridStrategy =
1319
(
1420
useInactiveIndexToKey: () => SharedValue<Array<string>>,
1521
reorder: ReorderFunction
16-
): SortableGridStrategyFactory =>
17-
({
18-
containerHeight,
19-
containerWidth,
20-
crossGap,
21-
fixedItemKeys,
22-
indexToKey,
23-
isVertical,
24-
mainGap,
25-
mainGroupSize,
26-
numGroups,
27-
useGridLayout
28-
}) => {
22+
): SortStrategyFactory =>
23+
() => {
24+
const { containerHeight, containerWidth, indexToKey } =
25+
useCommonValuesContext();
26+
const {
27+
crossGap,
28+
isVertical,
29+
mainGap,
30+
mainGroupSize,
31+
numGroups,
32+
useGridLayout
33+
} = useGridLayoutContext();
34+
const { fixedItemKeys } = useCustomHandleContext() ?? {};
35+
2936
const othersIndexToKey = useInactiveIndexToKey();
3037
const othersLayout = useGridLayout(othersIndexToKey);
3138
const debugBox = useDebugBoundingBox();

packages/react-native-sortables/src/providers/shared/components/OrderUpdaterComponent.tsx

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import { useMemo, useRef } from 'react';
22

3-
import { useDebugContext } from '../../../debug';
43
import type {
5-
AnyStrategyFactory,
64
OrderUpdaterProps,
7-
PredefinedStrategies
5+
PredefinedStrategies,
6+
SortStrategyFactory
87
} from '../../../types';
98
import { error, typedMemo } from '../../../utils';
10-
import { useCommonValuesContext } from '../CommonValuesProvider';
11-
import { useCustomHandleContext } from '../CustomHandleProvider';
129
import { useOrderUpdater } from '../hooks';
1310

14-
export function useStrategyKey(strategy: AnyStrategyFactory | string) {
11+
export function useStrategyKey(strategy: SortStrategyFactory | string) {
1512
const counterRef = useRef(0);
1613

1714
return useMemo(
@@ -24,23 +21,16 @@ export function useStrategyKey(strategy: AnyStrategyFactory | string) {
2421
function OrderUpdaterComponent<P extends PredefinedStrategies>({
2522
predefinedStrategies,
2623
strategy,
27-
triggerOrigin,
28-
useAdditionalValues
24+
triggerOrigin
2925
}: OrderUpdaterProps<P>) {
30-
const factory =
26+
const useStrategy =
3127
typeof strategy === 'string' ? predefinedStrategies[strategy] : strategy;
3228

33-
if (!factory && typeof strategy === 'string') {
34-
throw error(`'${strategy}' is not a valid ordering strategy`);
29+
if (!useStrategy || typeof useStrategy !== 'function') {
30+
throw error(`'${String(useStrategy)}' is not a valid ordering strategy`);
3531
}
3632

37-
const updater = (factory as AnyStrategyFactory)({
38-
debugContext: useDebugContext(),
39-
...useCommonValuesContext(),
40-
...useCustomHandleContext(),
41-
...useAdditionalValues()
42-
});
43-
33+
const updater = useStrategy();
4434
useOrderUpdater(updater, triggerOrigin);
4535

4636
return null;

packages/react-native-sortables/src/providers/shared/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { AutoScrollProvider } from './AutoScrollProvider';
1+
export { AutoScrollProvider, useAutoScrollContext } from './AutoScrollProvider';
22
export {
33
CommonValuesContext,
44
CommonValuesProvider,

packages/react-native-sortables/src/types/props/flex.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@ import type {
88
FlexWrap,
99
JustifyContent
1010
} from '../layout/flex';
11-
import type {
12-
CommonValuesContextType,
13-
CustomHandleContextType,
14-
DebugContextType,
15-
FlexLayoutContextType,
16-
OrderUpdater
17-
} from '../providers';
11+
import type { SortStrategyFactory } from '../providers';
1812
import type { DragEndParams, SharedProps } from './shared';
1913

2014
/** Parameters passed to the onDragEnd callback of a sortable flex container */
@@ -37,24 +31,12 @@ export type SortableFlexDragEndCallback = (
3731
params: SortableFlexDragEndParams
3832
) => void;
3933

40-
/** Factory function for creating custom reordering strategies
41-
* @param props Context values and layout information
42-
* @returns Function to update item order
43-
*/
44-
export type SortableFlexStrategyFactory = (
45-
props: Simplify<
46-
CommonValuesContextType &
47-
FlexLayoutContextType &
48-
Partial<CustomHandleContextType> & { debugContext?: DebugContextType }
49-
>
50-
) => OrderUpdater;
51-
5234
/** Strategy to use for reordering items:
5335
* - 'insert': Shifts all items between the dragged item and the target
5436
* position to make space for the dragged item
5537
* - Or a custom strategy factory function
5638
*/
57-
export type SortableFlexStrategy = 'insert' | SortableFlexStrategyFactory;
39+
export type SortableFlexStrategy = 'insert' | SortStrategyFactory;
5840

5941
/** Style properties for the flex container */
6042
export type SortableFlexStyle = {

packages/react-native-sortables/src/types/props/grid.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@ import type { ReactNode } from 'react';
22

33
import type { Simplify } from '../../helperTypes';
44
import type { AnimatableProps } from '../../integrations/reanimated';
5-
import type {
6-
CommonValuesContextType,
7-
CustomHandleContextType,
8-
DebugContextType,
9-
GridLayoutContextType,
10-
OrderUpdater
11-
} from '../providers';
5+
import type { SortStrategyFactory } from '../providers';
126
import type { DragEndParams, SharedProps } from './shared';
137

148
/** Parameters passed to the onDragEnd callback of a sortable grid */
@@ -61,28 +55,13 @@ export type SortableGridRenderItem<I> = (
6155
info: SortableGridRenderItemInfo<I>
6256
) => ReactNode;
6357

64-
/** Factory function for creating custom reordering strategies
65-
* @param props Context values and layout information
66-
* @returns Function to update item order
67-
*/
68-
export type SortableGridStrategyFactory = (
69-
props: Simplify<
70-
CommonValuesContextType &
71-
GridLayoutContextType &
72-
Partial<CustomHandleContextType> & { debugContext?: DebugContextType }
73-
>
74-
) => OrderUpdater;
75-
7658
/** Strategy to use for reordering items:
7759
* - 'insert': Shifts all items between the dragged item and the target
7860
* position to make space for the dragged item
7961
* - 'swap': Swaps the dragged item with the item at the target position
8062
* - Or a custom strategy factory function
8163
*/
82-
export type SortableGridStrategy =
83-
| 'insert'
84-
| 'swap'
85-
| SortableGridStrategyFactory;
64+
export type SortableGridStrategy = 'insert' | 'swap' | SortStrategyFactory;
8665

8766
/** Props for the SortableGrid component */
8867
export type SortableGridProps<I> = Simplify<

0 commit comments

Comments
 (0)