-
Notifications
You must be signed in to change notification settings - Fork 734
Feat/sortable grid list component #1918
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
Changes from 8 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
c76c2e5
Initial implementation of GridList component based on old GridView
ethanshar 21fe9a3
Support orientation change and keepItemSize in GridList
ethanshar f113f6d
Initial implementation of SortableGridList component
ethanshar 8255109
Adjust SortableGridList to work with ScrollView
ethanshar 5d2c319
Update src/components/gridList/gridList.api.json
ethanshar 417ecee
Move grid list logic to useGridLayout hook and fix code review
ethanshar 4f51e2d
Merge GridList changes
ethanshar 7a15a86
Minor fixes
ethanshar 9371830
Merge branch 'master' into feat/GridListComponent
ethanshar 08e997c
Merge branch 'feat/GridListComponent' into feat/SortableGridListCompo…
ethanshar fbd2df3
Update src/components/gridList/gridList.api.json
ethanshar e6b0d17
Merge branch 'master' into feat/GridListComponent
ethanshar 1d2accd
Code review fixes
ethanshar 432b9ce
Merge branch 'feat/GridListComponent' into feat/SortableGridListCompo…
ethanshar a9a3088
Merge branch 'master' into feat/SortableGridListComponent
ethanshar b0b68c3
Fix issue with canceling drag before it starts
ethanshar 5b1ee63
Code review fixes
ethanshar 4fce6f1
Add api json file
ethanshar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import React, {Component} from 'react'; | ||
import {StyleSheet} from 'react-native'; | ||
import {View, Text, Constants, GridList, Card, Spacings, BorderRadiuses, GridListProps} from 'react-native-ui-lib'; | ||
import products from '../../data/products'; | ||
|
||
class GridListScreen extends Component { | ||
state = { | ||
orientation: Constants.orientation | ||
}; | ||
|
||
renderItem: GridListProps<typeof products[0]>['renderItem'] = ({item}) => { | ||
return ( | ||
<Card flex> | ||
<Card.Section imageSource={{uri: item.mediaUrl}} imageStyle={styles.itemImage}/> | ||
<View padding-s2> | ||
<Text>{item.name}</Text> | ||
<Text>{item.formattedPrice}</Text> | ||
{item.inventory.status === 'Out of Stock' && ( | ||
<Text text90M $textDangerLight> | ||
{item.inventory.status} | ||
</Text> | ||
)} | ||
</View> | ||
</Card> | ||
); | ||
}; | ||
|
||
render() { | ||
return ( | ||
<GridList<typeof products[0]> | ||
ListHeaderComponent={ | ||
<Text h1 marginB-s5> | ||
GridList | ||
</Text> | ||
} | ||
data={products} | ||
renderItem={this.renderItem} | ||
// numColumns={2} | ||
maxItemWidth={140} | ||
itemSpacing={Spacings.s3} | ||
listPadding={Spacings.s5} | ||
// keepItemSize | ||
contentContainerStyle={styles.list} | ||
/> | ||
); | ||
} | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
list: { | ||
paddingTop: Spacings.s5 | ||
}, | ||
itemImage: { | ||
width: '100%', | ||
height: 85, | ||
borderRadius: BorderRadiuses.br10 | ||
} | ||
}); | ||
|
||
export default GridListScreen; |
69 changes: 69 additions & 0 deletions
69
demo/src/screens/componentScreens/SortableGridListScreen.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React, {Component} from 'react'; | ||
import {StyleSheet} from 'react-native'; | ||
import { | ||
View, | ||
Text, | ||
Constants, | ||
SortableGridList, | ||
Card, | ||
Spacings, | ||
BorderRadiuses, | ||
GridListProps, | ||
SortableGridListProps | ||
} from 'react-native-ui-lib'; | ||
|
||
import products from '../../data/products'; | ||
|
||
class GridListScreen extends Component { | ||
state = { | ||
orientation: Constants.orientation | ||
}; | ||
|
||
onOrderChange: SortableGridListProps['onOrderChange'] = (_newOrderedData, newOrder) => { | ||
console.log('newOrder:', newOrder); | ||
}; | ||
|
||
renderItem: GridListProps<typeof products[0]>['renderItem'] = ({item}) => { | ||
return ( | ||
<Card flex onPress={() => console.log('item press')}> | ||
<Card.Section imageSource={{uri: item.mediaUrl}} imageStyle={styles.itemImage}/> | ||
</Card> | ||
); | ||
}; | ||
|
||
render() { | ||
return ( | ||
<View flex> | ||
<Text h1 margin-s5> | ||
SortableGridList | ||
</Text> | ||
<SortableGridList | ||
data={products} | ||
renderItem={this.renderItem} | ||
// numColumns={2} | ||
maxItemWidth={140} | ||
itemSpacing={Spacings.s3} | ||
// itemSpacing={0} | ||
listPadding={Spacings.s5} | ||
// keepItemSize | ||
contentContainerStyle={styles.list} | ||
onOrderChange={this.onOrderChange} | ||
/> | ||
</View> | ||
); | ||
} | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
list: { | ||
padding: Spacings.s5 | ||
}, | ||
itemImage: { | ||
width: '100%', | ||
// height: 85, | ||
height: 108.7, | ||
borderRadius: BorderRadiuses.br10 | ||
} | ||
}); | ||
|
||
export default GridListScreen; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/// <reference types="react" /> | ||
import { GridListProps, GridListBaseProps } from './types'; | ||
declare function GridList<T = any>(props: GridListProps<T>): JSX.Element; | ||
export { GridListBaseProps, GridListProps }; | ||
export default GridList; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { FlatListProps } from 'react-native'; | ||
export interface GridListBaseProps { | ||
/** | ||
* 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; | ||
} | ||
export declare type GridListProps<T = any> = GridListBaseProps & FlatListProps<T>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { GridListBaseProps } from './types'; | ||
export declare const DEFAULT_NUM_COLUMNS = 3; | ||
export declare const DEFAULT_ITEM_SPACINGS: number; | ||
declare const useGridLayout: (props: GridListBaseProps) => { | ||
itemContainerStyle: { | ||
width: number; | ||
marginRight: number; | ||
marginBottom: number; | ||
}; | ||
numberOfColumns: number; | ||
itemSize: number; | ||
}; | ||
export default useGridLayout; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
generatedTypes/src/components/sortableGridList/SortableItem.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { PropsWithChildren } from 'react'; | ||
import { StyleProp, ViewStyle } from 'react-native'; | ||
import Animated from 'react-native-reanimated'; | ||
import usePresenter, { ItemsOrder } from './usePresenter'; | ||
interface SortableItemProps extends ReturnType<typeof usePresenter> { | ||
index: number; | ||
itemsOrder: Animated.SharedValue<ItemsOrder>; | ||
onChange: () => void; | ||
style: StyleProp<ViewStyle>; | ||
} | ||
declare function SortableItem(props: PropsWithChildren<SortableItemProps>): JSX.Element; | ||
export default SortableItem; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/// <reference types="react" /> | ||
import { FlatListProps, ScrollViewProps } from 'react-native'; | ||
import { GridListBaseProps } from '../gridList'; | ||
import { ItemsOrder } from './usePresenter'; | ||
export interface SortableGridListProps<T = any> extends GridListBaseProps, ScrollViewProps { | ||
data: FlatListProps<T>['data']; | ||
renderItem: FlatListProps<T>['renderItem']; | ||
onOrderChange?: (newData: T[], newOrder: ItemsOrder) => void; | ||
} | ||
declare function SortableGridList<T = any>(props: SortableGridListProps<T>): JSX.Element; | ||
export default SortableGridList; |
26 changes: 26 additions & 0 deletions
26
generatedTypes/src/components/sortableGridList/usePresenter.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export declare const WINDOW_WIDTH: number; | ||
export declare const DEFAULT_NO_OF_COLUMNS = 3; | ||
export declare const getItemSize: (numOfColumns: number, viewWidth: number) => number; | ||
export declare const animationConfig: { | ||
easing: (value: number) => number; | ||
duration: number; | ||
}; | ||
export declare type ItemsLayouts = ({ | ||
width: number; | ||
height: number; | ||
} | undefined)[]; | ||
export declare type ItemsOrder = number[]; | ||
declare const usePresenter: (itemsSize: number, numOfColumns: number, itemSpacing: number) => { | ||
updateItemLayout: (index: number, layout: { | ||
width: number; | ||
height: number; | ||
}) => void; | ||
getTranslationByOrderChange: (newOrder: number, oldOrder: number) => { | ||
x: number; | ||
y: number; | ||
}; | ||
getOrderByPosition: (positionX: number, positionY: number) => number; | ||
getItemOrderById: (itemsOrder: ItemsOrder, itemId: number) => number; | ||
getIdByItemOrder: (itemsOrder: ItemsOrder, orderIndex: number) => number; | ||
}; | ||
export default usePresenter; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"name": "GridList", | ||
"category": "layoutsAndTemplates", | ||
"description": "An auto-generated grid list that calculate item size according to given props", | ||
"extends": ["FlatList"], | ||
"example": "https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/GridListScreen.tsx", | ||
"props": [ | ||
{"name": "numColumns", "type": "number", "description": Number of items to show in a row (ignored when passing maxItemWidth)"}, | ||
{"name": "itemSpacing", "type": "number", "description": "Spacing between each item"}, | ||
{ | ||
"name": "maxItemWidth", | ||
"type": "number", | ||
"description": "Allow a responsive item width to the maximum item width" | ||
}, | ||
{ | ||
"name": "listPadding", | ||
"type": "number", | ||
"description": "List padding (used for item size calculation)" | ||
}, | ||
{ | ||
"name": "containerWidth", | ||
"type": "number", | ||
"description": "Pass when you want to use a custom container width for calculation" | ||
}, | ||
{ | ||
"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." | ||
} | ||
], | ||
"snippet": [ | ||
"<GridList>", | ||
" data={items$1}", | ||
" maxItemWidth={140$2}", | ||
" numColumns={2$3}", | ||
" itemSpacing={Spacings.s3$4}", | ||
" containerWidth={Constants.screenWidth - (Spacings.s5 * 2)}", | ||
" contentContainerStyle={{padding: Spacings.s5}}", | ||
"/>" | ||
] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.