Skip to content

GridView and SortableGridView - support tablet and other fixes #2611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/commons/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
AccessibilityInfo,
AccessibilityChangeEvent
} from 'react-native';
import {LogService} from '../services';

export enum orientations {
PORTRAIT = 'portrait',
Expand Down Expand Up @@ -138,7 +137,6 @@ const constants = {
},
getPageMargins(): number {
if (!breakpoints) {
LogService.warn('UILib breakpoints must be set via setBreakpoints before using getPageMargins');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll now use this internally (GridView and SortableGridView) without setting the breakpoints, so the user will get a warning if they'll use these components

return 0;
}

Expand Down
7 changes: 1 addition & 6 deletions src/commons/__tests__/constants.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@ describe('Constants', () => {
});

describe('Breakpoints and Page Margins', () => {
it('getPageMargins without init should return 0 and trigger a warn', () => {
const warn = console.warn;
console.warn = jest.fn();
it('getPageMargins without init should return 0', () => {
expect(Constants.getPageMargins()).toBe(0);
expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith('UILib breakpoints must be set via setBreakpoints before using getPageMargins');
console.warn = warn;
});

it('getPageMargins with one breakpoint', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/gridList/useGridLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const useGridLayout = (props: GridListBaseProps) => {
const {orientation} = useOrientation();

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

Expand Down
2 changes: 1 addition & 1 deletion src/components/gridView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class GridView extends UIComponent<GridViewProps, GridViewState> {
};

getDefaultViewWidth() {
return Constants.screenWidth - Spacings.s5 * 2;
return Constants.screenWidth - (Constants.getPageMargins() || Spacings.s5) * 2;
}

getGridContainerWidth() {
Expand Down
7 changes: 3 additions & 4 deletions src/components/sortableGridList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import SortableItem from './SortableItem';
import usePresenter from './usePresenter';
import {ItemsOrder, SortableGridListProps, ItemProps} from './types';

import useGridLayout, {DEFAULT_ITEM_SPACINGS, DEFAULT_NUM_COLUMNS} from '../gridList/useGridLayout';
import useGridLayout, {DEFAULT_ITEM_SPACINGS} from '../gridList/useGridLayout';

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

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

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

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

const onChange = useCallback(() => {
const newData: ItemProps<T>[] = [];
Expand Down
6 changes: 4 additions & 2 deletions src/components/sortableGridList/usePresenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ const usePresenter = (numOfColumns: number, itemSpacing: number) => {
},
getOrderByPosition: (positionX: number, positionY: number) => {
'worklet';
const col = (Constants.isRTL ? -1 : 1) * Math.round(positionX / itemSize);
const row = Math.round(positionY / itemSize);
const width = itemLayout.value?.width || itemSize;
const height = itemLayout.value?.height || itemSize;
const col = (Constants.isRTL ? -1 : 1) * Math.round(positionX / width);
const row = Math.round(positionY / height);
return row * numOfColumns + col;
},

Expand Down