Skip to content

Commit dd3143e

Browse files
authored
fix: Column and row count issues (#354)
## Description This PR fixes 2 issues with column/row count in the sortable grid: - Don't allow non-positive column/row count - Fix invalid layout animation on android when column/row count changes It also improves the column count change in the drag handle example - adds layout animation to grid card dimensions. ## Example recordings Example recording for column change on Android | Before | After | |-|-| | <video src="https://github.com/user-attachments/assets/a4a2e8fb-a46e-4726-817d-e9e636407348" /> | <video src="https://github.com/user-attachments/assets/5ef3c8d3-8eaa-4477-b0c4-2af63c2faca4" /> |
1 parent f794bbe commit dd3143e

2 files changed

Lines changed: 17 additions & 12 deletions

File tree

example/app/src/examples/SortableGrid/features/DragHandleExample.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { faGripVertical } from '@fortawesome/free-solid-svg-icons';
22
import { FontAwesomeIcon } from '@fortawesome/react-native-fontawesome';
33
import { useCallback, useState } from 'react';
44
import { StyleSheet, Text, View } from 'react-native';
5-
import Animated, { useAnimatedRef } from 'react-native-reanimated';
5+
import Animated, {
6+
LinearTransition,
7+
useAnimatedRef
8+
} from 'react-native-reanimated';
69
import type { OverDrag, SortableGridRenderItem } from 'react-native-sortables';
710
import Sortable from 'react-native-sortables';
811

@@ -25,12 +28,14 @@ export default function DragHandleExample() {
2528

2629
const renderItem = useCallback<SortableGridRenderItem<string>>(
2730
({ item }) => (
28-
<View style={styles.card}>
31+
<Animated.View layout={LinearTransition} style={styles.card}>
2932
<Text style={styles.text}>{item}</Text>
30-
<Sortable.Handle>
31-
<FontAwesomeIcon color={colors.white} icon={faGripVertical} />
32-
</Sortable.Handle>
33-
</View>
33+
<Animated.View layout={LinearTransition}>
34+
<Sortable.Handle>
35+
<FontAwesomeIcon color={colors.white} icon={faGripVertical} />
36+
</Sortable.Handle>
37+
</Animated.View>
38+
</Animated.View>
3439
),
3540
[]
3641
);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,12 @@ function SortableGrid<I>(props: SortableGridProps<I>) {
6464
if (!isVertical && !rowHeight) {
6565
throw error('rowHeight is required for horizontal Sortable.Grid');
6666
}
67-
68-
// this allows changing the number of columns/rows while developing
69-
// code with no necessity to reload the app
70-
// (state of the grid must be reset when this param changes)
71-
const key = (groups << 1) | (isVertical ? 1 : 0);
67+
if (columns !== undefined && columns < 1) {
68+
throw error('columns must be greater than 0');
69+
}
70+
if (rows !== undefined && rows < 1) {
71+
throw error('rows must be greater than 0');
72+
}
7273

7374
const columnGapValue = useAnimatableValue(columnGap);
7475
const rowGapValue = useAnimatableValue(rowGap);
@@ -88,7 +89,6 @@ function SortableGrid<I>(props: SortableGridProps<I>) {
8889
{...sharedProps}
8990
controlledContainerDimensions={controlledContainerDimensions}
9091
itemKeys={itemKeys}
91-
key={key}
9292
initialItemsStyleOverride={
9393
isVertical ? undefined : styles.horizontalStyleOverride
9494
}

0 commit comments

Comments
 (0)