Skip to content

fix/grid list list padding #2117

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 7 commits into from
Jul 17, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class SortableGridListScreen extends Component {

const styles = StyleSheet.create({
list: {
padding: Spacings.s5
paddingTop: Spacings.s5
},
itemImage: {
width: '100%',
Expand Down
5 changes: 5 additions & 0 deletions src/components/gridList/gridList.api.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
"name": "keepItemSize",
"type": "boolean",
"description": "whether to keep the items initial size when orientation changes, in which case the apt number of columns will be calculated automatically."
},
{
"name": "contentContainerStyle",
"type": "ScrollView[contentContainerStyle]",
"description": "Custom content container style"
}
],
"snippet": [
Expand Down
11 changes: 4 additions & 7 deletions src/components/gridList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useCallback, useMemo} from 'react';
import React, {useCallback} from 'react';
import {FlatList} from 'react-native';
import useGridLayout from './useGridLayout';
import View from '../view';
Expand All @@ -17,19 +17,16 @@ function GridList<T = any>(props: GridListProps<T>) {
...others
} = props;

const {itemContainerStyle, numberOfColumns} = useGridLayout({
const {itemContainerStyle, numberOfColumns, listContentStyle} = useGridLayout({
numColumns,
itemSpacing,
maxItemWidth,
listPadding,
keepItemSize,
containerWidth
containerWidth,
contentContainerStyle
});

const listContentStyle = useMemo(() => {
return [{paddingHorizontal: listPadding}, contentContainerStyle];
}, [listPadding, contentContainerStyle]);

const _renderItem = useCallback((...args: any[]) => {
// @ts-expect-error
return <View style={itemContainerStyle}>{renderItem?.(...args)}</View>;
Expand Down
51 changes: 25 additions & 26 deletions src/components/gridList/types.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
import {FlatListProps} from 'react-native';
import {FlatListProps, ScrollViewProps} from 'react-native';

export interface GridListBaseProps {
export interface GridListBaseProps extends Pick<ScrollViewProps, 'contentContainerStyle'> {
/**
* Allow a responsive item width to the maximum item width
*/
maxItemWidth?: number;
/**
* Number of items to show in a row (ignored when passing maxItemWidth)
*/
numColumns?: number;
/**
* Spacing between each item
*/
itemSpacing?: number;
/**
* List padding (used for item size calculation)
*/
listPadding?: number;
/**
* whether to keep the items initial size when orientation changes,
* in which case the apt number of columns will be calculated automatically.
*/
keepItemSize?: boolean;
/**
* Pass when you want to use a custom container width for calculation
*/
containerWidth?: number;

maxItemWidth?: number;
/**
* Number of items to show in a row (ignored when passing maxItemWidth)
*/
numColumns?: number;
/**
* Spacing between each item
*/
itemSpacing?: number;
/**
* List padding (used for item size calculation)
*/
listPadding?: number;
/**
* whether to keep the items initial size when orientation changes,
* in which case the apt number of columns will be calculated automatically.
*/
keepItemSize?: boolean;
/**
* Pass when you want to use a custom container width for calculation
*/
containerWidth?: number;
}

export type GridListProps<T = any> = GridListBaseProps & FlatListProps<T>
export type GridListProps<T = any> = GridListBaseProps & FlatListProps<T>;
9 changes: 7 additions & 2 deletions src/components/gridList/useGridLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const useGridLayout = (props: GridListBaseProps) => {
maxItemWidth,
listPadding = 0,
keepItemSize,
containerWidth
containerWidth,
contentContainerStyle
} = props;

const {orientation} = useOrientation();
Expand Down Expand Up @@ -43,7 +44,11 @@ const useGridLayout = (props: GridListBaseProps) => {
return {width: itemWidth /* + itemSpacing */, marginRight: itemSpacing, marginBottom: itemSpacing};
}, [itemWidth, itemSpacing]);

return {itemContainerStyle, numberOfColumns};
const listContentStyle = useMemo(() => {
return [{paddingHorizontal: listPadding}, contentContainerStyle];
}, [listPadding, contentContainerStyle]);

return {itemContainerStyle, numberOfColumns, listContentStyle};
};

export default useGridLayout;
6 changes: 3 additions & 3 deletions src/components/sortableGridList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ function generateItemsOrder(data: SortableGridListProps['data']) {
}

function SortableGridList<T = any>(props: SortableGridListProps<T>) {
const {renderItem, onOrderChange, contentContainerStyle, ...others} = props;
const {renderItem, onOrderChange, ...others} = props;

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

Expand Down Expand Up @@ -63,7 +63,7 @@ function SortableGridList<T = any>(props: SortableGridListProps<T>) {

return (
<GestureHandlerRootView>
<ScrollView contentContainerStyle={[styles.listContent, contentContainerStyle]}>
<ScrollView contentContainerStyle={[styles.listContent, listContentStyle]}>
{data?.map((item, index) => _renderItem({item, index} as ListRenderItemInfo<ItemProps<T>>))}
</ScrollView>
</GestureHandlerRootView>
Expand Down