From 6ee013136c57eea0573f1f2a2602c9bbf31804e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=81opaci=C5=84ski?= Date: Wed, 25 Nov 2026 20:43:42 +0100 Subject: [PATCH 1/7] Apply the flex wrap tolerance to the sort strategy fit checks --- .../updates/insert/index.test.tsx | 78 +++++++++++++++++++ .../flex/FlexLayoutProvider/utils/layout.ts | 5 +- 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx new file mode 100644 index 00000000..2b24e89b --- /dev/null +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx @@ -0,0 +1,78 @@ +import { act, render } from '@testing-library/react-native'; +import { GestureHandlerRootView } from 'react-native-gesture-handler'; + +import Sortable, { useCommonValuesContext } from '../../../../../index'; +import type { OrderUpdater, Vector } from '../../../../../types'; +import useInsertStrategy from '.'; + +const ITEM_HEIGHT = 50; + +type Options = { + fixedIndex?: number; + rowGap?: number; +}; + +// Renders a wrapping row of items with the given widths and returns their keys +// with a function that drags one of them. +const setup = ( + widths: Array, + containerWidth: number, + { fixedIndex, rowGap }: Options = {} +) => { + let context!: ReturnType; + let updater!: OrderUpdater; + const useStrategy = () => { + context = useCommonValuesContext(); + return (updater = useInsertStrategy()); + }; + + render( + + + {widths.map((_, index) => ( + + ))} + + + ); + + const keys = context.indexToKey.value; + act(() => { + context.containerWidth.value = containerWidth; + context.itemHeights.value = ITEM_HEIGHT; + context.itemWidths.value = Object.fromEntries( + keys.map((key, index) => [key, widths[index]!]) + ); + jest.runAllTimers(); + }); + + // Returns the order after dragging the item to the given position + const drag = (activeIndex: number, position: Vector) => { + const activeKey = keys[activeIndex]!; + act(() => { + context.activeItemKey.value = activeKey; + jest.runAllTimers(); + }); + return ( + updater({ + activeIndex, + activeKey, + dimensions: { height: ITEM_HEIGHT, width: widths[activeIndex]! }, + position + }) ?? keys + ); + }; + + return { context, drag, keys }; +}; + +it('moves an item to the next line when rounded sizes overflow the line', () => { + // a b c | d e f -> b c d | a e f + const { drag, keys } = setup(Array(6).fill(125.00001), 375); + const [a, b, c, d, e, f] = keys; + + expect(drag(0, { x: 60, y: 75 })).toEqual([b, c, d, a, e, f]); +}); diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/utils/layout.ts b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/utils/layout.ts index 34ffa864..85e99e56 100644 --- a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/utils/layout.ts +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/utils/layout.ts @@ -42,7 +42,7 @@ const createGroups = ( const currentTotalSize = totalGroupItemsMainSize + currentGroup.length * gap + mainItemDimension; - if (currentTotalSize > groupMainSizeLimit + 0.1) { + if (currentTotalSize > groupMainSizeLimit) { groups.push(currentGroup); crossAxisGroupSizes.push(groupCrossSize); currentGroup = []; @@ -368,6 +368,9 @@ export const calculateLayout = ({ if (groupSizeLimit <= 0) { return null; } + // Tolerance for rounding errors, kept in the limit so that the sort + // strategy wraps items exactly as the layout does + groupSizeLimit += 0.1; const groupingResult = createGroups( indexToKey, From a5c0eb47bc467f75e947fa721b76a960ac91037b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=81opaci=C5=84ski?= Date: Wed, 25 Nov 2026 20:44:20 +0100 Subject: [PATCH 2/7] Fix the flex reorder index of an item moved to a following line --- .../updates/insert/index.test.tsx | 18 +++++++++ .../updates/insert/index.ts | 11 ++++-- .../updates/insert/utils.test.ts | 37 +++++++++++++++++++ .../updates/insert/utils.ts | 8 ++-- 4 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.test.ts diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx index 2b24e89b..bba63d44 100644 --- a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx @@ -76,3 +76,21 @@ it('moves an item to the next line when rounded sizes overflow the line', () => expect(drag(0, { x: 60, y: 75 })).toEqual([b, c, d, a, e, f]); }); + +it('moves an item next to a fixed last item without replacing it', () => { + // a b | c D -> b | c a D + const { drag, keys } = setup([50, 200, 100, 50], 250, { fixedIndex: 3 }); + const [a, b, c, d] = keys; + + expect(drag(0, { x: 130, y: 75 })).toEqual([b, c, a, d]); + // The fixed item cannot be passed + expect(drag(0, { x: 240, y: 75 })).toEqual(keys); +}); + +it('keeps a fixed last item when the target line does not exist', () => { + // a | b c | d | E: with b taken out, c joins a, so the layout has one line + // less than the strategy expects + const { drag, keys } = setup([220, 210, 70, 240, 90], 300, { fixedIndex: 4 }); + + expect(drag(1, { x: 150, y: 125 })).toEqual(keys); +}); diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.ts b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.ts index e97664a4..87574ee4 100644 --- a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.ts +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.ts @@ -266,7 +266,10 @@ const useInsertStrategy: SortStrategyFactory = () => { const currentGroup = currentLayout.itemGroups[groupIndex]; if (!currentGroup) { const lastItemIndex = indexToKey.value.length - 1; - if (activeIndex === lastItemIndex) { + if ( + activeIndex === lastItemIndex || + fixedItemKeys?.value?.[indexToKey.value[lastItemIndex]!] + ) { return null; } return reorderInsert( @@ -468,8 +471,10 @@ const useInsertStrategy: SortStrategyFactory = () => { } } - const newIndex = - firstAvailableInGroupIndex + (itemIndexInGroup - initialItemIndexInGroup); + const newIndex = Math.min( + firstAvailableInGroupIndex + (itemIndexInGroup - initialItemIndexInGroup), + indexToKey.value.length - 1 + ); if ( newIndex === activeIndex || diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.test.ts b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.test.ts new file mode 100644 index 00000000..23be6bdc --- /dev/null +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.test.ts @@ -0,0 +1,37 @@ +import { getKeyToIndex } from '../../../../../utils'; +import { getSwappedToGroupAfterIndices } from './utils'; + +it.each([ + // a b | c d -> b | c a | d + [undefined, ['b', 'c', 'a', 'd']], + // a B | c d -> c | B a | d (the fixed B keeps its index) + [{ b: true }, ['c', 'b', 'a', 'd']] +])( + 'returns the in-group index of an item moved to the next group (fixed keys: %j)', + (fixedKeys, expectedOrder) => { + const indexToKey = ['a', 'b', 'c', 'd']; + + expect( + getSwappedToGroupAfterIndices({ + activeItemIndex: 0, + activeItemKey: 'a', + currentGroupIndex: 0, + fixedKeys, + groupSizeLimit: 250, + indexToKey, + itemGroups: [ + ['a', 'b'], + ['c', 'd'] + ], + keyToIndex: getKeyToIndex(indexToKey), + mainGap: 0, + mainItemSizes: { a: 50, b: 200, c: 180, d: 60 } + }) + ).toMatchObject({ + groupIndex: 1, + indexToKey: expectedOrder, + itemIndex: 2, + itemIndexInGroup: 1 + }); + } +); diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts index cc5efd1a..53790f16 100644 --- a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts @@ -185,12 +185,12 @@ const getIndexesWhenSwappedToGroupAfter = ({ const doesNotFitInGroup = (key: string) => totalGroupSize + getItemMainSize(key) > groupSizeLimit; - const includeItem = (key: string) => { + const includeItem = (key: string, newIndex: number) => { const itemMainSize = getItemMainSize(key); if (doesNotFitInGroup(key)) { groupIndex++; totalGroupSize = 0; - firstInGroupIndex = keyToIndex[key]!; + firstInGroupIndex = newIndex; } totalGroupSize += itemMainSize + mainGap; }; @@ -204,9 +204,9 @@ const getIndexesWhenSwappedToGroupAfter = ({ // Fill the empty slot with the current item and add all fixed // position items that were before this item - includeItem(key); + includeItem(key, emptyIndex); for (let j = emptyIndex + 1; j < i; j++) { - includeItem(indexToKey[j]!); + includeItem(indexToKey[j]!, j); } emptyIndex = i; From 043dbeecc967886a979e943bceae5889e8477a87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=81opaci=C5=84ski?= Date: Wed, 25 Nov 2026 20:44:30 +0100 Subject: [PATCH 3/7] Let a wide flex item move toward the end of its line --- .../flex/FlexLayoutProvider/updates/insert/index.test.tsx | 7 +++++++ .../flex/FlexLayoutProvider/updates/insert/index.ts | 6 ------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx index bba63d44..0d9a8920 100644 --- a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx @@ -94,3 +94,10 @@ it('keeps a fixed last item when the target line does not exist', () => { expect(drag(1, { x: 150, y: 125 })).toEqual(keys); }); + +it('moves an item wider than half of the line to the end of the line', () => { + const { drag, keys } = setup([200, 50, 50], 300); + const [a, b, c] = keys; + + expect(drag(0, { x: 290, y: 25 })).toEqual([b, c, a]); +}); diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.ts b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.ts index 87574ee4..615b20ca 100644 --- a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.ts +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.ts @@ -296,7 +296,6 @@ const useInsertStrategy: SortStrategyFactory = () => { // Item after let swapItemAfterOffset = -Infinity; let swapItemAfterBound = -Infinity; - let totalGroupSize = 0; do { if (swapItemAfterBound !== -Infinity) { @@ -340,11 +339,6 @@ const useInsertStrategy: SortStrategyFactory = () => { const additionalSwapOffset = getAdditionalSwapOffset(sizeToAdd); swapItemAfterBound = averageOffset + (isCurrentBeforeNext ? 1 : -1) * additionalSwapOffset; - - totalGroupSize += itemMainSize + mainGap; - if (totalGroupSize + activeItemMainSize > currentLayout.groupSizeLimit) { - break; - } } while ( itemIndexInGroup < currentGroup.length - 1 && gt(mainAxisPosition, swapItemAfterBound) From df9009290122a87e8496e4ee9d21d448a7a607df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=81opaci=C5=84ski?= Date: Wed, 25 Nov 2026 20:45:01 +0100 Subject: [PATCH 4/7] Keep a flex item wider than the container in the line it starts --- .../updates/insert/index.test.tsx | 6 ++++++ .../updates/insert/utils.test.ts | 20 +++++++++++++++++++ .../updates/insert/utils.ts | 3 ++- .../flex/FlexLayoutProvider/utils/layout.ts | 3 ++- 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx index 0d9a8920..da32e588 100644 --- a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx @@ -101,3 +101,9 @@ it('moves an item wider than half of the line to the end of the line', () => { expect(drag(0, { x: 290, y: 25 })).toEqual([b, c, a]); }); + +it('does not leave an empty line before a first item wider than the container', () => { + const { context, keys } = setup([300, 80], 250, { rowGap: 12 }); + + expect(context.itemLayoutPositions.value[keys[0]!]).toEqual({ x: 0, y: 0 }); +}); diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.test.ts b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.test.ts index 23be6bdc..4145676e 100644 --- a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.test.ts +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.test.ts @@ -35,3 +35,23 @@ it.each([ }); } ); + +it('keeps an item wider than the limit in the line it starts', () => { + // a | B | c -> B | a c (B is wider than the limit and never wraps alone) + const indexToKey = ['a', 'b', 'c']; + + expect( + getSwappedToGroupAfterIndices({ + activeItemIndex: 0, + activeItemKey: 'a', + currentGroupIndex: 0, + fixedKeys: undefined, + groupSizeLimit: 100, + indexToKey, + itemGroups: [['a'], ['b'], ['c']], + keyToIndex: getKeyToIndex(indexToKey), + mainGap: 0, + mainItemSizes: { a: 40, b: 120, c: 40 } + }) + ).toMatchObject({ groupIndex: 1, itemIndex: 1 }); +}); diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts index 53790f16..7bf38d1b 100644 --- a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts @@ -187,7 +187,8 @@ const getIndexesWhenSwappedToGroupAfter = ({ const includeItem = (key: string, newIndex: number) => { const itemMainSize = getItemMainSize(key); - if (doesNotFitInGroup(key)) { + // An empty line never wraps (same rule as in the layout) + if (totalGroupSize > 0 && doesNotFitInGroup(key)) { groupIndex++; totalGroupSize = 0; firstInGroupIndex = newIndex; diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/utils/layout.ts b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/utils/layout.ts index 85e99e56..a4c860c7 100644 --- a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/utils/layout.ts +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/utils/layout.ts @@ -42,7 +42,8 @@ const createGroups = ( const currentTotalSize = totalGroupItemsMainSize + currentGroup.length * gap + mainItemDimension; - if (currentTotalSize > groupMainSizeLimit) { + // An item wider than the limit doesn't wrap if it starts the line + if (currentGroup.length && currentTotalSize > groupMainSizeLimit) { groups.push(currentGroup); crossAxisGroupSizes.push(groupCrossSize); currentGroup = []; From be5f4f2d5ccdb1688d506882cb885a3e414e7a37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=81opaci=C5=84ski?= Date: Wed, 25 Nov 2026 21:28:18 +0100 Subject: [PATCH 5/7] Treat only the first item of a line as non-wrapping in the flex sort strategy --- .../updates/insert/utils.test.ts | 20 +++++++++++++++++++ .../updates/insert/utils.ts | 4 ++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.test.ts b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.test.ts index 4145676e..f4831378 100644 --- a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.test.ts +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.test.ts @@ -55,3 +55,23 @@ it('keeps an item wider than the limit in the line it starts', () => { }) ).toMatchObject({ groupIndex: 1, itemIndex: 1 }); }); + +it('wraps an item wider than the limit after an item without a size', () => { + // a z | B -> z | B | a (z has no size, but B still cannot join its line) + const indexToKey = ['a', 'z', 'b']; + + expect( + getSwappedToGroupAfterIndices({ + activeItemIndex: 0, + activeItemKey: 'a', + currentGroupIndex: 0, + fixedKeys: undefined, + groupSizeLimit: 100, + indexToKey, + itemGroups: [['a', 'z'], ['b']], + keyToIndex: getKeyToIndex(indexToKey), + mainGap: 0, + mainItemSizes: { a: 40, b: 120, z: 0 } + }) + ).toMatchObject({ groupIndex: 2, itemIndex: 2 }); +}); diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts index 7bf38d1b..fde32012 100644 --- a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts @@ -187,8 +187,8 @@ const getIndexesWhenSwappedToGroupAfter = ({ const includeItem = (key: string, newIndex: number) => { const itemMainSize = getItemMainSize(key); - // An empty line never wraps (same rule as in the layout) - if (totalGroupSize > 0 && doesNotFitInGroup(key)) { + // The first item of a line never wraps (same rule as in the layout) + if (newIndex > firstInGroupIndex && doesNotFitInGroup(key)) { groupIndex++; totalGroupSize = 0; firstInGroupIndex = newIndex; From 7809b688c986c2726e8a524778afcd349ad4a4e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=81opaci=C5=84ski?= Date: Thu, 26 Nov 2026 14:47:43 +0100 Subject: [PATCH 6/7] Keep a flex item movable next to fixed items and across changed lines - an item whose walk along a line ended on a fixed item was not moved at all, it now takes the closest free slot - an item could not move to the last line when only fixed items followed its slot there - the place of the active item in its line is read from the layout, which can have one line less than expected once the item is taken out - the check for the line above includes the gap and lets an item wider than the container start a line, as the layout does --- .../updates/insert/index.test.tsx | 72 ++++++++++++++++--- .../updates/insert/index.ts | 32 ++++++--- .../updates/insert/utils.ts | 20 ++++-- 3 files changed, 101 insertions(+), 23 deletions(-) diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx index da32e588..26feecf6 100644 --- a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx @@ -8,6 +8,7 @@ import useInsertStrategy from '.'; const ITEM_HEIGHT = 50; type Options = { + columnGap?: number; fixedIndex?: number; rowGap?: number; }; @@ -17,7 +18,7 @@ type Options = { const setup = ( widths: Array, containerWidth: number, - { fixedIndex, rowGap }: Options = {} + { columnGap, fixedIndex, rowGap }: Options = {} ) => { let context!: ReturnType; let updater!: OrderUpdater; @@ -28,7 +29,11 @@ const setup = ( render( - + {widths.map((_, index) => ( { const [a, b, c, d] = keys; expect(drag(0, { x: 130, y: 75 })).toEqual([b, c, a, d]); - // The fixed item cannot be passed - expect(drag(0, { x: 240, y: 75 })).toEqual(keys); + // The fixed item cannot be passed, so the slot before it is taken + expect(drag(0, { x: 240, y: 75 })).toEqual([b, c, a, d]); }); -it('keeps a fixed last item when the target line does not exist', () => { - // a | b c | d | E: with b taken out, c joins a, so the layout has one line - // less than the strategy expects - const { drag, keys } = setup([220, 210, 70, 240, 90], 300, { fixedIndex: 4 }); +it('moves an item to the next line when the pointer is over its fixed last item', () => { + // a b | c D -> a | c b D + const { drag, keys } = setup([54, 298, 270, 48], 352, { fixedIndex: 3 }); + const [a, b, c, d] = keys; + + expect(drag(1, { x: 203, y: 75 })).toEqual([a, c, b, d]); +}); + +it('moves an item to the last line when only a fixed item can follow it', () => { + // a b | c d | e F -> a b | c e | d F + const { drag, keys } = setup([233, 129, 160, 154, 146, 58], 380, { + fixedIndex: 5 + }); + const [a, b, c, d, e, f] = keys; + + expect(drag(3, { x: 263, y: 125 })).toEqual([a, b, c, e, d, f]); +}); + +describe('when taking the item out lets the next item join the previous line', () => { + it('moves the first item of a line to the next line', () => { + // a | b c d | e -> a c | d e | b + const { drag, keys } = setup([220, 100, 60, 110, 180], 300); + const [a, b, c, d, e] = keys; + + expect(drag(1, { x: 50, y: 125 })).toEqual([a, c, d, e, b]); + }); + + it('moves an item before a fixed last item', () => { + // a | b c | d | E -> a c | d | b E + const { drag, keys } = setup([220, 210, 70, 240, 90], 300, { + fixedIndex: 4 + }); + const [a, b, c, d, e] = keys; + + expect(drag(1, { x: 150, y: 125 })).toEqual([a, c, d, b, e]); + }); +}); + +it('moves an item one line up when the line above has no room for it and the gap', () => { + // a | b | c -> a | c | b + const { drag, keys } = setup([200, 250, 95], 300, { + columnGap: 10, + rowGap: 10 + }); + const [a, b, c] = keys; + + expect(drag(2, { x: 47, y: 105 })).toEqual([a, c, b]); +}); + +it('moves an item wider than the container to the previous line', () => { + // a | b | c d -> b | a c d + const { drag, keys } = setup([57, 368, 162, 35], 362); + const [a, b, c, d] = keys; - expect(drag(1, { x: 150, y: 125 })).toEqual(keys); + expect(drag(1, { x: 184, y: 40 })).toEqual([b, a, c, d]); }); it('moves an item wider than half of the line to the end of the line', () => { diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.ts b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.ts index 615b20ca..8fa0bb8f 100644 --- a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.ts +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.ts @@ -263,7 +263,15 @@ const useInsertStrategy: SortStrategyFactory = () => { // MAIN AXIS BOUNDS // currentGroup is the updated group after new layout calculation // that contains the active item - const currentGroup = currentLayout.itemGroups[groupIndex]; + let currentGroup = currentLayout.itemGroups[groupIndex]; + // Taking the active item out of its line can let the next item join the + // previous line, so the active item is one line before the expected one + if ( + !currentGroup?.includes(activeKey) && + currentLayout.itemGroups[groupIndex - 1]?.includes(activeKey) + ) { + currentGroup = currentLayout.itemGroups[--groupIndex]; + } if (!currentGroup) { const lastItemIndex = indexToKey.value.length - 1; if ( @@ -281,13 +289,10 @@ const useInsertStrategy: SortStrategyFactory = () => { } const mainAxisPosition = position[mainCoordinate]; - // Find the itemIndexInGroup of the active item if it is in the same group - if (groupIndex === activeGroupIndex.value) { - const firstItemKey = currentGroup[0]; - if (firstItemKey === undefined) return; - const firstItemIndex = keyToIndex.value[firstItemKey]; - if (firstItemIndex === undefined) return; - itemIndexInGroup = activeIndex - firstItemIndex; + // The layout knows where in its line the active item really is + const activeIndexInGroup = currentGroup.indexOf(activeKey); + if (activeIndexInGroup !== -1) { + itemIndexInGroup = activeIndexInGroup; } const initialItemIndexInGroup = itemIndexInGroup; @@ -465,10 +470,19 @@ const useInsertStrategy: SortStrategyFactory = () => { } } - const newIndex = Math.min( + let newIndex = Math.min( firstAvailableInGroupIndex + (itemIndexInGroup - initialItemIndexInGroup), indexToKey.value.length - 1 ); + // A fixed item cannot be replaced, so take the closest free slot on the + // way to it + const step = Math.sign(firstAvailableInGroupIndex - newIndex); + while ( + newIndex !== firstAvailableInGroupIndex && + fixedItemKeys?.value?.[indexToKey.value[newIndex]!] + ) { + newIndex += step; + } if ( newIndex === activeIndex || diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts index fde32012..aabcc8b8 100644 --- a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts @@ -104,7 +104,7 @@ const getIndexesWhenSwappedToGroupBefore = ({ mainGap ); const canBeFirstInGroupBefore = - totalGroupBeforeBeforeSize + activeMainSize > groupSizeLimit; + totalGroupBeforeBeforeSize + mainGap + activeMainSize > groupSizeLimit; if (!canBeFirstInGroupBefore) { const firstItemKey = groupBefore[0]; if (firstItemKey === undefined) { @@ -121,7 +121,11 @@ const getIndexesWhenSwappedToGroupBefore = ({ if (itemKey === undefined) { return null; } - if (totalGroupSize + activeMainSize > groupSizeLimit) { + // The first item of a line never wraps (same rule as in the layout) + if ( + itemIndex > firstInGroupBeforeIndex && + totalGroupSize + activeMainSize > groupSizeLimit + ) { break; } @@ -196,7 +200,14 @@ const getIndexesWhenSwappedToGroupAfter = ({ totalGroupSize += itemMainSize + mainGap; }; - for (let i = activeItemIndex + 1; i < indexToKey.length; i++) { + // Fixed items at the end cannot be passed, so the slot before them is the + // last one the active item can take + let lastFreeIndex = indexToKey.length - 1; + while (fixedKeys?.[indexToKey[lastFreeIndex]!]) { + lastFreeIndex--; + } + + for (let i = activeItemIndex + 1; i <= lastFreeIndex; i++) { const key = indexToKey[i]!; if (fixedKeys?.[key]) { @@ -212,8 +223,7 @@ const getIndexesWhenSwappedToGroupAfter = ({ emptyIndex = i; if (doesNotFitInGroup(activeItemKey)) { - const nextItemKey = indexToKey[i + 1]; - if (nextItemKey === undefined || doesNotFitInGroup(nextItemKey)) { + if (i === lastFreeIndex || doesNotFitInGroup(indexToKey[i + 1]!)) { return { groupIndex: groupIndex + 1, itemIndex: i, From 3f50bd39758e6d2cabee76bf8b9621a5ec6eb18c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=81opaci=C5=84ski?= Date: Thu, 26 Nov 2026 17:14:02 +0100 Subject: [PATCH 7/7] Revert "Keep a flex item movable next to fixed items and across changed lines" With fixed items the order could flip back and forth with the pointer at rest, and with gaps an item could end two lines away from the pointer. This reverts commit 7809b688. --- .../updates/insert/index.test.tsx | 72 +++---------------- .../updates/insert/index.ts | 32 +++------ .../updates/insert/utils.ts | 20 ++---- 3 files changed, 23 insertions(+), 101 deletions(-) diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx index 26feecf6..da32e588 100644 --- a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.test.tsx @@ -8,7 +8,6 @@ import useInsertStrategy from '.'; const ITEM_HEIGHT = 50; type Options = { - columnGap?: number; fixedIndex?: number; rowGap?: number; }; @@ -18,7 +17,7 @@ type Options = { const setup = ( widths: Array, containerWidth: number, - { columnGap, fixedIndex, rowGap }: Options = {} + { fixedIndex, rowGap }: Options = {} ) => { let context!: ReturnType; let updater!: OrderUpdater; @@ -29,11 +28,7 @@ const setup = ( render( - + {widths.map((_, index) => ( { const [a, b, c, d] = keys; expect(drag(0, { x: 130, y: 75 })).toEqual([b, c, a, d]); - // The fixed item cannot be passed, so the slot before it is taken - expect(drag(0, { x: 240, y: 75 })).toEqual([b, c, a, d]); + // The fixed item cannot be passed + expect(drag(0, { x: 240, y: 75 })).toEqual(keys); }); -it('moves an item to the next line when the pointer is over its fixed last item', () => { - // a b | c D -> a | c b D - const { drag, keys } = setup([54, 298, 270, 48], 352, { fixedIndex: 3 }); - const [a, b, c, d] = keys; - - expect(drag(1, { x: 203, y: 75 })).toEqual([a, c, b, d]); -}); - -it('moves an item to the last line when only a fixed item can follow it', () => { - // a b | c d | e F -> a b | c e | d F - const { drag, keys } = setup([233, 129, 160, 154, 146, 58], 380, { - fixedIndex: 5 - }); - const [a, b, c, d, e, f] = keys; - - expect(drag(3, { x: 263, y: 125 })).toEqual([a, b, c, e, d, f]); -}); - -describe('when taking the item out lets the next item join the previous line', () => { - it('moves the first item of a line to the next line', () => { - // a | b c d | e -> a c | d e | b - const { drag, keys } = setup([220, 100, 60, 110, 180], 300); - const [a, b, c, d, e] = keys; - - expect(drag(1, { x: 50, y: 125 })).toEqual([a, c, d, e, b]); - }); - - it('moves an item before a fixed last item', () => { - // a | b c | d | E -> a c | d | b E - const { drag, keys } = setup([220, 210, 70, 240, 90], 300, { - fixedIndex: 4 - }); - const [a, b, c, d, e] = keys; - - expect(drag(1, { x: 150, y: 125 })).toEqual([a, c, d, b, e]); - }); -}); - -it('moves an item one line up when the line above has no room for it and the gap', () => { - // a | b | c -> a | c | b - const { drag, keys } = setup([200, 250, 95], 300, { - columnGap: 10, - rowGap: 10 - }); - const [a, b, c] = keys; - - expect(drag(2, { x: 47, y: 105 })).toEqual([a, c, b]); -}); - -it('moves an item wider than the container to the previous line', () => { - // a | b | c d -> b | a c d - const { drag, keys } = setup([57, 368, 162, 35], 362); - const [a, b, c, d] = keys; +it('keeps a fixed last item when the target line does not exist', () => { + // a | b c | d | E: with b taken out, c joins a, so the layout has one line + // less than the strategy expects + const { drag, keys } = setup([220, 210, 70, 240, 90], 300, { fixedIndex: 4 }); - expect(drag(1, { x: 184, y: 40 })).toEqual([b, a, c, d]); + expect(drag(1, { x: 150, y: 125 })).toEqual(keys); }); it('moves an item wider than half of the line to the end of the line', () => { diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.ts b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.ts index 8fa0bb8f..615b20ca 100644 --- a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.ts +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/index.ts @@ -263,15 +263,7 @@ const useInsertStrategy: SortStrategyFactory = () => { // MAIN AXIS BOUNDS // currentGroup is the updated group after new layout calculation // that contains the active item - let currentGroup = currentLayout.itemGroups[groupIndex]; - // Taking the active item out of its line can let the next item join the - // previous line, so the active item is one line before the expected one - if ( - !currentGroup?.includes(activeKey) && - currentLayout.itemGroups[groupIndex - 1]?.includes(activeKey) - ) { - currentGroup = currentLayout.itemGroups[--groupIndex]; - } + const currentGroup = currentLayout.itemGroups[groupIndex]; if (!currentGroup) { const lastItemIndex = indexToKey.value.length - 1; if ( @@ -289,10 +281,13 @@ const useInsertStrategy: SortStrategyFactory = () => { } const mainAxisPosition = position[mainCoordinate]; - // The layout knows where in its line the active item really is - const activeIndexInGroup = currentGroup.indexOf(activeKey); - if (activeIndexInGroup !== -1) { - itemIndexInGroup = activeIndexInGroup; + // Find the itemIndexInGroup of the active item if it is in the same group + if (groupIndex === activeGroupIndex.value) { + const firstItemKey = currentGroup[0]; + if (firstItemKey === undefined) return; + const firstItemIndex = keyToIndex.value[firstItemKey]; + if (firstItemIndex === undefined) return; + itemIndexInGroup = activeIndex - firstItemIndex; } const initialItemIndexInGroup = itemIndexInGroup; @@ -470,19 +465,10 @@ const useInsertStrategy: SortStrategyFactory = () => { } } - let newIndex = Math.min( + const newIndex = Math.min( firstAvailableInGroupIndex + (itemIndexInGroup - initialItemIndexInGroup), indexToKey.value.length - 1 ); - // A fixed item cannot be replaced, so take the closest free slot on the - // way to it - const step = Math.sign(firstAvailableInGroupIndex - newIndex); - while ( - newIndex !== firstAvailableInGroupIndex && - fixedItemKeys?.value?.[indexToKey.value[newIndex]!] - ) { - newIndex += step; - } if ( newIndex === activeIndex || diff --git a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts index aabcc8b8..fde32012 100644 --- a/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts +++ b/packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts @@ -104,7 +104,7 @@ const getIndexesWhenSwappedToGroupBefore = ({ mainGap ); const canBeFirstInGroupBefore = - totalGroupBeforeBeforeSize + mainGap + activeMainSize > groupSizeLimit; + totalGroupBeforeBeforeSize + activeMainSize > groupSizeLimit; if (!canBeFirstInGroupBefore) { const firstItemKey = groupBefore[0]; if (firstItemKey === undefined) { @@ -121,11 +121,7 @@ const getIndexesWhenSwappedToGroupBefore = ({ if (itemKey === undefined) { return null; } - // The first item of a line never wraps (same rule as in the layout) - if ( - itemIndex > firstInGroupBeforeIndex && - totalGroupSize + activeMainSize > groupSizeLimit - ) { + if (totalGroupSize + activeMainSize > groupSizeLimit) { break; } @@ -200,14 +196,7 @@ const getIndexesWhenSwappedToGroupAfter = ({ totalGroupSize += itemMainSize + mainGap; }; - // Fixed items at the end cannot be passed, so the slot before them is the - // last one the active item can take - let lastFreeIndex = indexToKey.length - 1; - while (fixedKeys?.[indexToKey[lastFreeIndex]!]) { - lastFreeIndex--; - } - - for (let i = activeItemIndex + 1; i <= lastFreeIndex; i++) { + for (let i = activeItemIndex + 1; i < indexToKey.length; i++) { const key = indexToKey[i]!; if (fixedKeys?.[key]) { @@ -223,7 +212,8 @@ const getIndexesWhenSwappedToGroupAfter = ({ emptyIndex = i; if (doesNotFitInGroup(activeItemKey)) { - if (i === lastFreeIndex || doesNotFitInGroup(indexToKey[i + 1]!)) { + const nextItemKey = indexToKey[i + 1]; + if (nextItemKey === undefined || doesNotFitInGroup(nextItemKey)) { return { groupIndex: groupIndex + 1, itemIndex: i,