Skip to content

Commit 5862fa8

Browse files
authored
GridView and SortableGridView - support tablet and other fixes (#2611)
* GridView and SortableGridView - support tablet and other fixes * Cannot warn since we're already using this internally without setting
1 parent f157d72 commit 5862fa8

File tree

6 files changed

+10
-16
lines changed

6 files changed

+10
-16
lines changed

src/commons/Constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
AccessibilityInfo,
77
AccessibilityChangeEvent
88
} from 'react-native';
9-
import {LogService} from '../services';
109

1110
export enum orientations {
1211
PORTRAIT = 'portrait',
@@ -138,7 +137,6 @@ const constants = {
138137
},
139138
getPageMargins(): number {
140139
if (!breakpoints) {
141-
LogService.warn('UILib breakpoints must be set via setBreakpoints before using getPageMargins');
142140
return 0;
143141
}
144142

src/commons/__tests__/constants.spec.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,8 @@ describe('Constants', () => {
66
});
77

88
describe('Breakpoints and Page Margins', () => {
9-
it('getPageMargins without init should return 0 and trigger a warn', () => {
10-
const warn = console.warn;
11-
console.warn = jest.fn();
9+
it('getPageMargins without init should return 0', () => {
1210
expect(Constants.getPageMargins()).toBe(0);
13-
expect(console.warn).toHaveBeenCalledTimes(1);
14-
expect(console.warn).toHaveBeenCalledWith('UILib breakpoints must be set via setBreakpoints before using getPageMargins');
15-
console.warn = warn;
1611
});
1712

1813
it('getPageMargins with one breakpoint', () => {

src/components/gridList/useGridLayout.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const useGridLayout = (props: GridListBaseProps) => {
2121
const {orientation} = useOrientation();
2222

2323
const _containerWidth = useMemo(() => {
24-
return (containerWidth ?? Constants.screenWidth) - listPadding * 2;
24+
return (containerWidth ?? Constants.screenWidth - Constants.getPageMargins()) - listPadding * 2;
2525
// eslint-disable-next-line react-hooks/exhaustive-deps
2626
}, [listPadding, orientation, containerWidth]);
2727

src/components/gridView/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class GridView extends UIComponent<GridViewProps, GridViewState> {
119119
};
120120

121121
getDefaultViewWidth() {
122-
return Constants.screenWidth - Spacings.s5 * 2;
122+
return Constants.screenWidth - (Constants.getPageMargins() || Spacings.s5) * 2;
123123
}
124124

125125
getGridContainerWidth() {

src/components/sortableGridList/index.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import SortableItem from './SortableItem';
99
import usePresenter from './usePresenter';
1010
import {ItemsOrder, SortableGridListProps, ItemProps} from './types';
1111

12-
import useGridLayout, {DEFAULT_ITEM_SPACINGS, DEFAULT_NUM_COLUMNS} from '../gridList/useGridLayout';
12+
import useGridLayout, {DEFAULT_ITEM_SPACINGS} from '../gridList/useGridLayout';
1313

1414
function generateItemsOrder(data: SortableGridListProps['data']) {
1515
return _.map(data, item => item.id);
@@ -19,15 +19,14 @@ function SortableGridList<T = any>(props: SortableGridListProps<T>) {
1919
const {renderItem, onOrderChange, ...others} = props;
2020

2121
const {itemContainerStyle, numberOfColumns, listContentStyle} = useGridLayout(props);
22-
const {numColumns = DEFAULT_NUM_COLUMNS, itemSpacing = DEFAULT_ITEM_SPACINGS, data} = others;
22+
const {itemSpacing = DEFAULT_ITEM_SPACINGS, data} = others;
2323
const itemsOrder = useSharedValue<ItemsOrder>(generateItemsOrder(data));
2424

2525
useDidUpdate(() => {
2626
itemsOrder.value = generateItemsOrder(data);
2727
}, [data]);
2828

29-
// TODO: Get the number of columns from GridList calculation somehow
30-
const presenter = usePresenter(numColumns, itemSpacing);
29+
const presenter = usePresenter(numberOfColumns, itemSpacing);
3130

3231
const onChange = useCallback(() => {
3332
const newData: ItemProps<T>[] = [];

src/components/sortableGridList/usePresenter.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ const usePresenter = (numOfColumns: number, itemSpacing: number) => {
3939
},
4040
getOrderByPosition: (positionX: number, positionY: number) => {
4141
'worklet';
42-
const col = (Constants.isRTL ? -1 : 1) * Math.round(positionX / itemSize);
43-
const row = Math.round(positionY / itemSize);
42+
const width = itemLayout.value?.width || itemSize;
43+
const height = itemLayout.value?.height || itemSize;
44+
const col = (Constants.isRTL ? -1 : 1) * Math.round(positionX / width);
45+
const row = Math.round(positionY / height);
4446
return row * numOfColumns + col;
4547
},
4648

0 commit comments

Comments
 (0)