Skip to content
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

Add the option to supply your own handleClick function #37

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"react-dom": "16.8.1"
},
"dependencies": {
"es-abstract": "1.14.1",
"lodash.debounce": "^4.0.8",
"lodash.throttle": "^4.1.1",
"react-spring": "^8.0.5"
Expand Down
31 changes: 18 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import computeLayout from './computeLayout'
import computeLayoutGroups from './computeLayoutGroups'
import getUrl from './utils/getUrl'
import sortByDate from './utils/sortByDate'
import groupByDate from './utils/groupByDate'
import getScrollSpeed from './utils/getScrollSpeed'

import styles from './styles.css'
Expand All @@ -25,6 +24,9 @@ export default class Pig extends Component {
// if getUrl has been provided as a prop, use it. otherwise use the default getUrl from /utils
this.getUrl = props.getUrl || getUrl

// if handleClick has been provided as a prop, use it. otherwise use the default handleClick from /utils
this.handleClick = props.handleClick || this.defaultHandleClick

this.imageData = props.imageData

// if sortFunc has been provided as a prop, use it
Expand Down Expand Up @@ -123,6 +125,20 @@ export default class Pig extends Component {
this.windowHeight = window.innerHeight
}

defaultHandleClick = (item) => {
// if an image is already the width of the container, don't expand it on click
if (item.style.width >= this.containerWidth) {
this.setState({ activeTileUrl: null })
return
}

this.setState({
// if Tile is already active, deactivate it
activeTileUrl: item.url !== this.state.activeTileUrl ? item.url : null
})
}


getUpdatedImageLayout() {
const wrapperWidth = this.container.offsetWidth

Expand Down Expand Up @@ -185,18 +201,7 @@ export default class Pig extends Component {
item={item}
gridGap={this.settings.gridGap}
getUrl={this.getUrl}
handleClick={item => {
// if an image is already the width of the container, don't expand it on click
if (item.style.width >= this.containerWidth) {
this.setState({ activeTileUrl: null })
return
}

this.setState({
// if Tile is already active, deactivate it
activeTileUrl: item.url !== this.state.activeTileUrl ? item.url : null
})
}}
handleClick={this.handleClick}
activeTileUrl={this.state.activeTileUrl}
settings={this.settings}
thumbnailSize={this.props.thumbnailSize}
Expand Down
Loading