-
Notifications
You must be signed in to change notification settings - Fork 734
GridListItem - horizontal alignment support #3001
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 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d733687
horizontal aligment support
adids1221 cea1b7c
passing the alignment style to the custom item and the image wrapper
adids1221 a8af708
added horizontalAlignment to api.json
adids1221 27c3904
fixed overley use case, changed the prop name
adids1221 1732f94
small refactor
adids1221 d15a9ce
removed textStyle, changed the children type
adids1221 66b5067
children can be an array
adids1221 3ab4789
Merge branch 'master' into feat/GridListItem_horizontal_alignment
adids1221 7a6f366
fixed review notes
adids1221 896142e
fixed review notes
adids1221 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,22 @@ | ||
import _ from 'lodash'; | ||
import React, {Component} from 'react'; | ||
import {StyleProp, StyleSheet, ViewStyle} from 'react-native'; | ||
import memoize from 'memoize-one'; | ||
import * as Modifiers from '../../commons/modifiers'; | ||
import {Colors, Spacings, Typography} from 'style'; | ||
import View, {ViewProps} from '../view'; | ||
import TouchableOpacity, {TouchableOpacityProps} from '../touchableOpacity'; | ||
import Text from '../text'; | ||
import Image, {ImageProps} from '../image'; | ||
|
||
export enum ItemHorizontalAlignment { | ||
left = 'left', | ||
center = 'center', | ||
right = 'right' | ||
} | ||
|
||
export type ItemHorizontalAlignmentProp = ItemHorizontalAlignment | `${ItemHorizontalAlignment}`; | ||
|
||
export interface GridListItemProps { | ||
/** | ||
* Image props object for rendering an image item | ||
|
@@ -85,6 +94,10 @@ export interface GridListItemProps { | |
* Should content be align to start (default is center) | ||
*/ | ||
alignToStart?: boolean; | ||
/** | ||
* Content horizontal alignment (default is center) | ||
*/ | ||
horizontalAlignment?: ItemHorizontalAlignmentProp; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this prop? Why the user can't just pass 'alignItems' in the containerStyle prop, and 'textAlign' in the style prop? |
||
/** | ||
* Custom container style | ||
*/ | ||
|
@@ -121,7 +134,8 @@ class GridListItem extends Component<GridListItemProps> { | |
static displayName = 'GridListItem'; | ||
|
||
static defaultProps = { | ||
itemSize: 48 | ||
itemSize: 48, | ||
horizontalAlignment: ItemHorizontalAlignment.center | ||
}; | ||
|
||
state = {}; | ||
|
@@ -139,14 +153,33 @@ class GridListItem extends Component<GridListItemProps> { | |
return {width: itemSize as number, height: itemSize as number}; | ||
} | ||
|
||
getHorizontalAlignmentStyle = memoize(horizontalAlignment => { | ||
switch (horizontalAlignment) { | ||
case ItemHorizontalAlignment.left: | ||
return {contentStyle: styles.contentAlignedToStart, containerStyle: styles.containerAlignedToStart}; | ||
case ItemHorizontalAlignment.right: | ||
return {contentStyle: styles.contentAlignedToEnd, containerStyle: styles.containerAlignedToEnd}; | ||
default: | ||
return {contentStyle: styles.contentAlignedToCenter, containerStyle: styles.containerAlignedToCenter}; | ||
} | ||
}); | ||
|
||
renderContent({text, typography, color, numberOfLines = 1, style, testID}: RenderContentType) { | ||
const {alignToStart} = this.props; | ||
const {alignToStart, horizontalAlignment} = this.props; | ||
const horizontalAlignmentStyle = this.getHorizontalAlignmentStyle(horizontalAlignment); | ||
if (text) { | ||
return ( | ||
<Text | ||
testID={testID} | ||
// @ts-ignore | ||
style={[style, Typography[typography], color && {color}, alignToStart && styles.contentAlignedToStart]} | ||
style={[ | ||
style, | ||
//@ts-ignore | ||
Typography[typography], | ||
color && {color}, | ||
alignToStart && styles.contentAlignedToStart, | ||
horizontalAlignmentStyle.contentStyle | ||
]} | ||
numberOfLines={numberOfLines} | ||
> | ||
{text} | ||
|
@@ -160,6 +193,7 @@ class GridListItem extends Component<GridListItemProps> { | |
testID, | ||
imageProps, | ||
alignToStart, | ||
horizontalAlignment, | ||
containerStyle, | ||
containerProps = {}, | ||
renderCustomItem, | ||
|
@@ -190,22 +224,30 @@ class GridListItem extends Component<GridListItemProps> { | |
const textContainerStyle = overlayText ? {style: [styles.overlayText, overlayTextContainerStyle]} : null; | ||
const imageBorderRadius = imageProps?.borderRadius; | ||
const {hitSlop, ...otherContainerProps} = containerProps; // eslint-disable-line | ||
const horizontalAlignmentStyle = this.getHorizontalAlignmentStyle(horizontalAlignment); | ||
|
||
return ( | ||
<Container | ||
style={[styles.container, alignToStart && styles.containerAlignedToStart, {width}, containerStyle]} | ||
style={[ | ||
styles.container, | ||
alignToStart ? styles.containerAlignedToStart : horizontalAlignmentStyle.containerStyle, | ||
{width}, | ||
containerStyle | ||
]} | ||
{...otherContainerProps} | ||
onPress={hasPress ? this.onItemPress : undefined} | ||
accessible={renderCustomItem ? true : undefined} | ||
{...Modifiers.extractAccessibilityProps(this.props)} | ||
> | ||
{imageProps && ( | ||
<View style={[{borderRadius: imageBorderRadius}, imageStyle]}> | ||
<View style={[{borderRadius: imageBorderRadius}, horizontalAlignmentStyle.containerStyle, imageStyle]}> | ||
<Image {...imageProps} style={[imageStyle, imageProps?.style]}/> | ||
{children} | ||
</View> | ||
)} | ||
{!_.isNil(renderCustomItem) && <View style={{width}}>{renderCustomItem()}</View>} | ||
{!_.isNil(renderCustomItem) && ( | ||
<View style={[horizontalAlignmentStyle.containerStyle, {width}]}>{renderCustomItem()}</View> | ||
)} | ||
{hasOverlay && <View style={[styles.overlay, this.getItemSizeObj()]}>{renderOverlay?.()}</View>} | ||
<TextContainer {...textContainerStyle}> | ||
{this.renderContent({ | ||
|
@@ -246,6 +288,12 @@ const styles = StyleSheet.create({ | |
containerAlignedToStart: { | ||
alignItems: 'flex-start' | ||
}, | ||
containerAlignedToCenter: { | ||
alignItems: 'center' | ||
}, | ||
containerAlignedToEnd: { | ||
alignItems: 'flex-end' | ||
}, | ||
title: { | ||
marginTop: Spacings.s1, | ||
textAlign: 'center', | ||
|
@@ -262,6 +310,12 @@ const styles = StyleSheet.create({ | |
contentAlignedToStart: { | ||
textAlign: 'left' | ||
}, | ||
contentAlignedToCenter: { | ||
textAlign: 'center' | ||
}, | ||
contentAlignedToEnd: { | ||
textAlign: 'right' | ||
}, | ||
overlay: { | ||
position: 'absolute', | ||
left: 0, | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a good name...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I don't see why we need to declare this type