Skip to content

Commit f890f88

Browse files
authored
Add flexMigration to SortableList and SortableGridList components (#3067)
1 parent b624614 commit f890f88

File tree

8 files changed

+55
-7
lines changed

8 files changed

+55
-7
lines changed

demo/src/screens/componentScreens/SortableGridListScreen.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class SortableGridListScreen extends Component {
100100
</View>
101101
<View flex>
102102
<SortableGridList
103+
flexMigration
103104
data={items}
104105
renderItem={this.renderItem}
105106
// numColumns={2}

demo/src/screens/componentScreens/SortableListScreen.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ const SortableListScreen = () => {
112112
</View>
113113
<View flex useSafeArea>
114114
<SortableList
115+
flexMigration
115116
data={items}
116117
renderItem={renderItem}
117118
keyExtractor={keyExtractor}

src/components/sortableGridList/index.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import React, {useCallback} from 'react';
1+
import React, {useCallback, useEffect} from 'react';
22
import {StyleSheet, ScrollView, ListRenderItemInfo} from 'react-native';
33
import {GestureHandlerRootView} from 'react-native-gesture-handler';
44
import {useSharedValue} from 'react-native-reanimated';
55
import _ from 'lodash';
66
import {useDidUpdate} from 'hooks';
7+
import {LogService} from 'services';
78

89
import SortableItem from './SortableItem';
910
import usePresenter from './usePresenter';
@@ -16,12 +17,20 @@ function generateItemsOrder(data: SortableGridListProps['data']) {
1617
}
1718

1819
function SortableGridList<T = any>(props: SortableGridListProps<T>) {
19-
const {renderItem, onOrderChange, ...others} = props;
20+
const {renderItem, onOrderChange, flexMigration, ...others} = props;
2021

2122
const {itemContainerStyle, numberOfColumns, listContentStyle} = useGridLayout(props);
2223
const {itemSpacing = DEFAULT_ITEM_SPACINGS, data} = others;
2324
const itemsOrder = useSharedValue<ItemsOrder>(generateItemsOrder(data));
2425

26+
// TODO: Remove once flexMigration migration is completed
27+
useEffect(() => {
28+
if (flexMigration === undefined) {
29+
LogService.error(`SortableGridList "flexMigration" prop is a temporary migration flag to transition to a flex behavior for SortableList.
30+
Please make sure to pass it and check your UI before it becomes true by default`);
31+
}
32+
}, []);
33+
2534
useDidUpdate(() => {
2635
itemsOrder.value = generateItemsOrder(data);
2736
}, [data]);
@@ -61,7 +70,7 @@ function SortableGridList<T = any>(props: SortableGridListProps<T>) {
6170
[data]);
6271

6372
return (
64-
<GestureHandlerRootView>
73+
<GestureHandlerRootView style={flexMigration ? styles.container : undefined}>
6574
<ScrollView contentContainerStyle={[styles.listContent, listContentStyle]}>
6675
{_.map(data, (item, index) => _renderItem({item, index} as ListRenderItemInfo<ItemProps<T>>))}
6776
</ScrollView>
@@ -73,6 +82,9 @@ export {SortableGridListProps};
7382
export default SortableGridList;
7483

7584
const styles = StyleSheet.create({
85+
container: {
86+
flex: 1
87+
},
7688
listContent: {
7789
flexWrap: 'wrap',
7890
flexDirection: 'row'

src/components/sortableGridList/sortableGridList.api.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
"name": "extraData",
2323
"type": "any",
2424
"description": "Pass any extra data that should trigger a re-render"
25+
},
26+
{
27+
"name": "flexMigration",
28+
"type": "boolean",
29+
"description": "A temporary migration flag for enabling flex on the list's container (like it should be by default)"
2530
}
2631
],
2732
"snippet": [

src/components/sortableGridList/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export interface SortableGridListProps<T = any> extends GridListBaseProps, Scrol
1212
renderItem: FlatListProps<ItemProps<T>>['renderItem'];
1313
onOrderChange?: (newData: ItemProps<T>[], newOrder: ItemsOrder) => void;
1414
extraData?: any;
15+
/**
16+
* Temporary migration flag for enabling flex on the container of the list (like it should be by default)
17+
*/
18+
flexMigration?: boolean;
1519
}
1620

1721
export interface SortableItemProps {

src/components/sortableList/SortableList.api.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
"name": "itemProps",
3636
"type": "{margins?: {marginTop?: number; marginBottom?: number; marginLeft?: number; marginRight?: number}}",
3737
"description": "Extra props for the item."
38+
},
39+
{
40+
"name": "flexMigration",
41+
"type": "boolean",
42+
"description": "A temporary migration flag for enabling flex on the list's container (like it should be by default)"
3843
}
3944
],
4045
"snippet": [

src/components/sortableList/index.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/* eslint-disable react-hooks/exhaustive-deps */
22
import {map, mapKeys, filter, reduce} from 'lodash';
3-
import React, {useMemo, useCallback} from 'react';
4-
import {FlatList, LayoutChangeEvent} from 'react-native';
3+
import React, {useMemo, useCallback, useEffect} from 'react';
4+
import {StyleSheet, FlatList, LayoutChangeEvent} from 'react-native';
55
import {useSharedValue} from 'react-native-reanimated';
66
import {GestureHandlerRootView} from 'react-native-gesture-handler';
7+
import {LogService} from 'services';
78
import SortableListContext from './SortableListContext';
89
import SortableListItem, {DEFAULT_LIST_ITEM_SIZE} from './SortableListItem';
910
import {useDidUpdate, useThemeProps} from 'hooks';
@@ -26,12 +27,21 @@ function generateLockedIds<ItemT extends SortableListItemProps>(data: SortableLi
2627

2728
const SortableList = <ItemT extends SortableListItemProps>(props: SortableListProps<ItemT>) => {
2829
const themeProps = useThemeProps(props, 'SortableList');
29-
const {data, onOrderChange, enableHaptic, scale, itemProps, horizontal, listRef, ...others} = themeProps;
30+
const {data, onOrderChange, enableHaptic, scale, itemProps, horizontal, listRef, flexMigration, ...others} =
31+
themeProps;
3032

3133
const itemsOrder = useSharedValue<string[]>(generateItemsOrder(data));
3234
const lockedIds = useSharedValue<Dictionary<boolean>>(generateLockedIds(data));
3335
const itemSize = useSharedValue<number>(DEFAULT_LIST_ITEM_SIZE);
3436

37+
// TODO: Remove once flexMigration migration is completed
38+
useEffect(() => {
39+
if (flexMigration === undefined) {
40+
LogService.error(`SortableList "flexMigration" prop is a temporary migration flag to transition to a flex behavior for SortableList.
41+
Please make sure to pass it and check your UI before it becomes true by default`);
42+
}
43+
}, []);
44+
3545
useDidUpdate(() => {
3646
itemsOrder.value = generateItemsOrder(data);
3747
}, [data]);
@@ -76,7 +86,7 @@ const SortableList = <ItemT extends SortableListItemProps>(props: SortableListPr
7686
};
7787
}, [data]);
7888
return (
79-
<GestureHandlerRootView>
89+
<GestureHandlerRootView style={flexMigration ? styles.container : undefined}>
8090
<SortableListContext.Provider value={context}>
8191
<FlatList
8292
{...others}
@@ -91,4 +101,10 @@ const SortableList = <ItemT extends SortableListItemProps>(props: SortableListPr
91101
);
92102
};
93103

104+
const styles = StyleSheet.create({
105+
container: {
106+
flex: 1
107+
}
108+
});
109+
94110
export default SortableList;

src/components/sortableList/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,8 @@ export interface SortableListProps<ItemT extends SortableListItemProps>
3535
* List forwarded ref.
3636
*/
3737
listRef?: ForwardedRef<FlatList<ItemT>>
38+
/**
39+
* Temporary migration flag for enabling flex on the container of the list (like it should be by default)
40+
*/
41+
flexMigration?: boolean;
3842
}

0 commit comments

Comments
 (0)