This repository was archived by the owner on Oct 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
[feat] Masonry component with just url image, need to pass image path #49
Open
aupous
wants to merge
13
commits into
master
Choose a base branch
from
masonry
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0ab647f
[feat] Masonry component with just url image, need to pass image path
aupous a442751
[fix] Merge origin and fix conflict
aupous 1c2fbee
[fix] Remove unsed code
aupous e0b9531
[feat] Add Masonry clickable image demo
aupous 4166875
[fix] Remove unused code
aupous e617b5a
Merge branch 'master' into masonry
hieuhani 9bf4ac8
[fix] Change list of demo image
aupous b34a355
[fix] Comment on use of hard code value
aupous 5e96ea0
[fix] Pass number of columns and image gap as props
aupous 4fe9977
[fix] Use react-native StyleSheet
aupous 5e5ebe1
[fix] Merge origin and fix conflict
aupous fc867ec
[fix] Simplify demo of Masonry, add demo code, change props name padd…
aupous 4124058
[fix] Merge origin and fix conflict
aupous 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,84 @@ | ||
| import * as React from 'react' | ||
| import MyMom from '.' | ||
| import { Image, StyleSheet, Text, View } from 'react-native' | ||
| import { Masonry, Heading5, Modal } from 'react-silk-ui' | ||
|
|
||
| const images = [ | ||
| 'https://images.unsplash.com/photo-1542838077-7fb322b21b80?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60', | ||
| 'https://images.unsplash.com/photo-1550483135-4a6b4cbc6469?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60', | ||
| 'https://images.unsplash.com/photo-1550481585-67143479c9bd?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60', | ||
| 'https://images.unsplash.com/photo-1550475966-70af27831597?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60', | ||
| 'https://images.unsplash.com/photo-1550456915-749f38312cbe?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60', | ||
| 'https://images.unsplash.com/photo-1550431703-109e2c9f3e3d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60', | ||
| 'https://images.unsplash.com/photo-1550433728-99a10e2bd20d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60', | ||
| 'https://images.unsplash.com/photo-1550434605-d1a3cb21158e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60', | ||
| 'https://images.unsplash.com/photo-1550411335-cab6040bb5b1?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60', | ||
| 'https://images.unsplash.com/photo-1550359542-5c8b880dabdf?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60', | ||
| ] | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| heading: { | ||
| paddingTop: 15, | ||
| }, | ||
| textDemo: { | ||
| paddingVertical: 12, | ||
| backgroundColor: '#282c34', | ||
| color: '#fff', | ||
| marginVertical: 12, | ||
| paddingLeft: 30, | ||
| borderRadius: 6, | ||
| }, | ||
| }) | ||
|
|
||
| export default class MasonryDemo extends React.Component { | ||
| state = { | ||
| toggle: false, | ||
| url: '', | ||
| width: 0, | ||
| height: 0, | ||
| } | ||
|
|
||
| setModalImg = (img: { url: string; height: number; width: number }) => { | ||
| this.setState({ | ||
| toggle: !this.state.toggle, | ||
| ...img, | ||
| }) | ||
| } | ||
|
|
||
| render() { | ||
| const { toggle, url, width, height } = this.state | ||
| return ( | ||
| <MyMom> | ||
| <View> | ||
| <Heading5 style={styles.heading}>Masonry default</Heading5> | ||
| <Text style={styles.textDemo}>{'<Masonry images={images} />'}</Text> | ||
| <Masonry images={images} /> | ||
| <Heading5 style={styles.heading}> | ||
| Masonry with number of columns is set to 4 | ||
| </Heading5> | ||
| <Text style={styles.textDemo}> | ||
| {'<Masonry images={images} col={4} />'} | ||
| </Text> | ||
| <Masonry images={images} col={4} /> | ||
| <Heading5 style={styles.heading}> | ||
| Masonry with image gap is set to 30 | ||
| </Heading5> | ||
| <Text style={styles.textDemo}> | ||
| {'<Masonry images={images} gap={30} />'} | ||
| </Text> | ||
| <Masonry images={images} gap={30} /> | ||
| <Heading5 style={styles.heading}>Clickable Masonry</Heading5> | ||
| <Masonry images={images} onPress={this.setModalImg}> | ||
| <Modal | ||
| toggle={() => this.setState({ toggle: !toggle })} | ||
| isOpen={toggle} | ||
| style={{}} | ||
| > | ||
| <Image source={{ uri: url }} style={{ width, height }} /> | ||
| </Modal> | ||
| </Masonry> | ||
| </View> | ||
| </MyMom> | ||
| ) | ||
| } | ||
| } |
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,133 @@ | ||
| import * as React from 'react' | ||
| import { View, Image, TouchableWithoutFeedback, StyleSheet } from 'react-native' | ||
|
|
||
| export interface Props { | ||
| images: string[] | ||
| col?: number | ||
| gap?: number | ||
| onPress?: (arg0: { url: string; height: number; width: number }) => void | ||
| } | ||
|
|
||
| const defaultProps = { | ||
| col: 3, | ||
| gap: 10, | ||
| onPress: null, | ||
| } | ||
|
|
||
| interface State { | ||
| width: number | ||
| columns: { | ||
| height: number | ||
| images: { url: string; height: number; width: number }[] | ||
| }[] | ||
| } | ||
|
|
||
| const style = StyleSheet.create({ | ||
| masonryWrapper: { | ||
| flexDirection: 'row', | ||
| }, | ||
| column: { | ||
| flex: 1, | ||
| }, | ||
| }) | ||
|
|
||
| const COLUMN_WIDTH = 300 // on component didmount, layout wontbe loaded so it's width = 0, cannot get column width => define a value to calculate standard image height | ||
|
|
||
| export class Masonry extends React.Component<Props, State> { | ||
| static defaultProps = defaultProps | ||
|
|
||
| constructor(props: Props) { | ||
| super(props) | ||
| this.state = { | ||
| width: 0, | ||
| columns: [], | ||
| } | ||
| } | ||
|
|
||
| async componentDidMount() { | ||
| const columns = [] | ||
| const col = this.props.col! | ||
| for (let i = 0; i < col; i++) { | ||
| columns.push({ | ||
| height: 0, | ||
| images: [], | ||
| }) | ||
| } | ||
| await this.setState({ columns }) | ||
| await this.props.images.forEach(img => { | ||
| Image.getSize( | ||
| img, | ||
| (width, height) => { | ||
| const index = this.minColIndex() | ||
| const cols = this.state.columns | ||
| const imgHeight = (height * COLUMN_WIDTH) / width | ||
| cols[index].height = cols[index].height + imgHeight | ||
| cols[index].images.push({ | ||
| url: img, | ||
| height: imgHeight, | ||
| width: COLUMN_WIDTH, | ||
| }) | ||
| this.setState({ columns: cols }) | ||
| }, | ||
| err => console.log(err), | ||
| ) | ||
| }) | ||
| } | ||
|
|
||
| minColIndex() { | ||
| return this.state.columns.reduce( | ||
| (minIndex, current, index, arr) => | ||
| current.height < arr[minIndex].height ? index : minIndex, | ||
| 0, | ||
| ) | ||
| } | ||
|
|
||
| onLayout = (event: any) => { | ||
| const { width } = event.nativeEvent.layout | ||
| this.setState({ width }) | ||
| } | ||
|
|
||
| columnWidth() { | ||
| const col = this.props.col! | ||
| const gap = this.props.gap! | ||
| return this.state.width / col - gap | ||
| } | ||
|
|
||
| render() { | ||
| const onPress = this.props.onPress! | ||
| const gap = this.props.gap! | ||
| const colWidth = this.columnWidth() | ||
| return ( | ||
| <View | ||
| onLayout={this.onLayout} | ||
| style={[style.masonryWrapper, { marginHorizontal: -gap / 2 }]} | ||
| > | ||
| {this.state.columns.map((col, index) => ( | ||
| <View style={style.column} key={`column${index}`}> | ||
| {col.images.map((img, id) => { | ||
| const height = (img.height * colWidth) / COLUMN_WIDTH | ||
| return onPress !== null ? ( | ||
| <TouchableWithoutFeedback | ||
| onPress={() => onPress(img)} | ||
| key={`col${index}_img${id}`} | ||
| > | ||
| <Image | ||
| source={{ uri: img.url }} | ||
| style={{ width: colWidth, height, margin: gap / 2 }} | ||
| /> | ||
| </TouchableWithoutFeedback> | ||
| ) : ( | ||
| <Image | ||
| source={{ uri: img.url }} | ||
| style={{ width: colWidth, height, margin: gap / 2 }} | ||
| key={`col${index}_img${id}`} | ||
| /> | ||
| ) | ||
| })} | ||
| </View> | ||
| ))} | ||
| {this.props.children} | ||
| </View> | ||
| ) | ||
| } | ||
| } | ||
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
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.