Created
November 3, 2015 14:01
-
-
Save johndavedecano/06a040249a7029c117a8 to your computer and use it in GitHub Desktop.
ReactJS SemanticUI Pagination
This file contains 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
import React, { Component } from 'react'; | |
class Pagination extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
currentPage: 1, | |
items: [] | |
} | |
this.goTo = this.goTo.bind(this); | |
this.onClickNext = this.onClickNext.bind(this); | |
this.onClickPrev = this.onClickPrev.bind(this); | |
} | |
componentDidUpdate(prevProps, prevState) { | |
if (prevState.currentPage !== this.state.currentPage) { | |
this.props.onChange(this.state.currentPage); | |
} | |
} | |
goTo(page, e) { | |
if (e) { | |
e.preventDefault(); | |
} | |
this.setState({currentPage: page}); | |
} | |
onClickNext(e) { | |
e.preventDefault(); | |
let page = this.state.currentPage; | |
if (page < this.props.max) { | |
this.goTo(page + 1); | |
} | |
} | |
onClickPrev(e) { | |
e.preventDefault(); | |
if (this.state.currentPage > 1) { | |
this.goTo(this.state.currentPage - 1); | |
} | |
} | |
render() { | |
let className = this.props.className || '', | |
p = this.props, | |
s = this.state, | |
skip = 0; | |
if (s.currentPage > p.maxVisible - 1 && s.currentPage < p.max) { | |
skip = s.currentPage - p.maxVisible + 1; | |
} else if (s.currentPage === p.max) { | |
skip = s.currentPage - p.maxVisible; | |
} | |
let iterator = Array.apply(null, Array(p.maxVisible)).map(function(v, i) { | |
return skip + i + 1; | |
}); | |
return ( | |
React.createElement("div", {className:'ui pagination menu bottom center aligned'}, | |
React.createElement("a", {className: s.currentPage === 1 ? 'item disabled' : 'item', href: "#", onClick: this.onClickPrev}, | |
React.createElement("i", {className : 'chevron left icon'}, ""), | |
), | |
iterator.map(function(page) { | |
return ( | |
React.createElement("li", {key: page, | |
onClick: this.goTo.bind(this, page), | |
href: "#", | |
className: s.currentPage === page ? 'active item' : 'item'}, | |
page | |
) | |
); | |
}, this), | |
React.createElement("a", {className: s.currentPage === p.max ? 'disabled item' : 'item', href: "#", onClick: this.onClickNext}, | |
React.createElement("i", {className : 'chevron right icon'}, ""), | |
) | |
) | |
); | |
} | |
} | |
Pagination.propTypes = { | |
max: React.PropTypes.number.isRequired, | |
maxVisible: React.PropTypes.number, | |
onChange: React.PropTypes.func.isRequired | |
}; | |
Pagination.defaultProps = { | |
maxVisible: 5 | |
}; | |
export default Pagination; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment