Skip to content

SortableGridList - single source of truth for itemWidth and itemSpacing #3275

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 1 commit into from
Oct 15, 2024
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
10 changes: 9 additions & 1 deletion src/components/gridList/useGridLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ const useGridLayout = (props: GridListBaseProps) => {
return [{columnGap: itemSpacing}, columnWrapperStyle];
}, [itemSpacing, columnWrapperStyle]);

return {itemContainerStyle, numberOfColumns, listStyle, listContentStyle, listColumnWrapperStyle};
return {
itemContainerStyle,
numberOfColumns,
itemWidth,
itemSpacing,
listStyle,
listContentStyle,
listColumnWrapperStyle
};
};

export default useGridLayout;
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {Constants} from '../../../commons/new';

describe('SortableGridList presenter tests', () => {
const makeSUT = ({numOfColumns = DEFAULT_NUM_COLUMNS, itemSpacing = DEFAULT_ITEM_SPACINGS}) => {
return renderHook(() => usePresenter(numOfColumns, itemSpacing));
const itemSize = Constants.screenWidth / numOfColumns;
return renderHook(() => usePresenter(numOfColumns, itemSize, itemSpacing));
};

describe('ltr', () => {
Expand Down
17 changes: 12 additions & 5 deletions src/components/sortableGridList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SortableItem from './SortableItem';
import usePresenter from './usePresenter';
import {ItemsOrder, SortableGridListProps, ItemProps} from './types';

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

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

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

// TODO: Remove once flexMigration migration is completed
Expand All @@ -36,7 +43,7 @@ function SortableGridList<T = any>(props: SortableGridListProps<T>) {
itemsOrder.value = generateItemsOrder(data);
}, [data]);

const presenter = usePresenter(numberOfColumns, itemSpacing);
const presenter = usePresenter(numberOfColumns, itemWidth, itemSpacing);

const onChange = useCallback(() => {
const newData: ItemProps<T>[] = [];
Expand Down
4 changes: 1 addition & 3 deletions src/components/sortableGridList/usePresenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ import {ItemLayout, ItemsOrder} from './types';

export const WINDOW_WIDTH = Constants.windowWidth;
export const DEFAULT_NO_OF_COLUMNS = 3;
export const getItemSize = (numOfColumns: number, viewWidth: number) => viewWidth / numOfColumns;

export const animationConfig = {
easing: Easing.inOut(Easing.ease),
duration: 350
};

const usePresenter = (numOfColumns: number, itemSpacing: number) => {
const usePresenter = (numOfColumns: number, itemSize: number, itemSpacing: number) => {
const itemLayout = useSharedValue<ItemLayout>(undefined);
const itemSize = getItemSize(numOfColumns, Constants.screenWidth);

return {
updateItemLayout: (layout: {width: number; height: number}) => {
Expand Down