UNPKG

@up-group/react-controls

Version:

We know that there are a ton of react UI library projects to choose from. Our hope with this one is to provide the next generation of react components that you can use to bootstrap your next project, or as a reference for building a UIKit. Read on to get

266 lines 12.9 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var React = require("react"); var classname = require("classnames"); var Grid_1 = require("../../Containers/Grid"); var Select_1 = require("../../Inputs/Select"); var typestyle_1 = require("typestyle"); var paginationStyle = typestyle_1.style({ margin: "0px 4px", listStyle: "none", display: "inline-block", paddingLeft: "0", borderRadius: "4px", }); var firstChild = { marginLeft: 0, borderTopLeftRadius: "4px", borderBottomLeftRadius: "4px" }; var lastChild = { borderTopRightRadius: "4px", borderBottomRightRadius: "4px" }; var itemHover = { color: "#23527C", backgroundColor: "#eee", borderColor: "#ddd" }; var itemActive = { color: "#fff", backgroundColor: "#337ab7", borderColor: "#337ab7", cursor: "not-allowed" }; var itemDisabled = { color: "#777", cursor: "not-allowed", backgroundColor: "#fff", borderColor: "#ddd" }; var paginationItemStyle = typestyle_1.style({ display: "inline", $nest: { '> a': { minWidth: 41, textAlign: "center", position: "relative", float: "left", padding: "6px 3px", marginLeft: "-1px", lineHeight: "1.43", color: "#337ab7", textDecoration: "none", backgroundColor: "#fff", border: "1px solid #ddd" }, "&:first-child a": firstChild, "&:first-child span": firstChild, "&:last-child a": lastChild, "&:last-child span": lastChild, 'a:hover': itemHover, 'a:focus': itemHover, 'span:hover': itemHover, 'span:focus': itemHover, "&.active > a": itemActive, "&.active > span": itemActive, "&.active > a:hover": itemActive, "&.active > span:hover": itemActive, "&.active > a:focus": itemActive, "&.active > span:focus": itemActive, "&.disabled > a": itemDisabled, "&.disabled > span": itemDisabled, "&.disabled > a:hover": itemDisabled, "&.disabled > span:hover": itemDisabled, "&.disabled > a:focus": itemDisabled, "&.disabled > span:focus": itemDisabled } }); var paginationCounterStyle = typestyle_1.style({ margin: "0px 0px", color: "#2a6496", backgroundColor: "#eee", borderColor: "#ddd", borderRadius: "4px", padding: "6px 12px", lineHeight: "1.43", textDecoration: "none", border: "1px solid #ddd", float: "right", cursor: "pointer", }); var UpPagination = (function (_super) { __extends(UpPagination, _super); function UpPagination(props, context) { var _this = _super.call(this, props) || this; _this.goToPreviousPage = function () { if (_this.state.page > 1) { var previousPage = _this.state.page - 1; var newState = { page: previousPage, skip: (previousPage - 1) * _this.state.take }; _this.setState(newState, function () { _this.props.onPageChange(_this.state.page, _this.state.take, _this.state.skip); }); } }; _this.getMaxPage = function () { var maxPage = Math.ceil(_this.props.total / _this.state.take); return maxPage; }; _this.goToNextPage = function () { if (_this.state.page < _this.getMaxPage()) { var nextPage = _this.state.page + 1; var newState = { page: nextPage, skip: (nextPage - 1) * _this.state.take }; _this.setState(newState, function () { _this.props.onPageChange(_this.state.page, _this.state.take, _this.state.skip); }); } }; _this.goTo = function (page) { var newState = { page: page, skip: (page - 1) * _this.state.take }; _this.setState(newState, function () { _this.props.onPageChange(_this.state.page, _this.state.take, _this.state.skip); }); }; _this.onTakeChange = function (data) { if (data && data.id != _this.state.take) { var newTake = data.id; var newPage = _this.getPage(newTake, _this.state.skip); var newSkip = newTake * (newPage - 1); var newState = { take: newTake, page: newPage, skip: newSkip }; _this.setState(newState, function () { this.props.onPageChange(this.state.page, this.state.take, this.state.skip); }); } }; _this.getPage = function (take, skip) { if (take >= _this.props.total) { return 1; } return Math.floor((skip + take) / take); }; _this.state = { page: _this.getPage(_this.props.defaultTake, _this.props.defaultSkip), skip: _this.props.defaultSkip, take: _this.props.defaultTake }; return _this; } UpPagination.prototype.componentWillReceiveProps = function (nextProps) { var newState = { take: nextProps.defaultTake, skip: nextProps.defaultSkip, page: this.getPage(nextProps.defaultTake, nextProps.defaultSkip) }; this.setState(newState); }; UpPagination.prototype.inRange = function (curPage, pagevalue, distance) { var absRange = Math.abs(curPage - pagevalue); if (absRange < distance) { return true; } return false; }; UpPagination.prototype.render = function () { var _this = this; var pages = []; var currentPage = 1; var maxPage = this.getMaxPage(); while (currentPage <= maxPage) { pages.push(currentPage++); } var from = this.state.skip + 1; var to = from + this.state.take - 1; if (to > this.props.total) to = this.props.total; var takes = this.props.takes; var pageNumberNavigation = React.createElement("span", null); if (pages.length >= 2) { var PageNumber = React.createElement("span", null); if (pages.length <= 15) { PageNumber = pages.map(function (value, index) { return React.createElement("li", { key: "page-" + value, className: classname(_this.state.page == value ? "active" : "", paginationItemStyle), onClick: _this.goTo.bind(_this, value) }, React.createElement("a", { onClick: function (e) { return e.preventDefault(); }, href: "#" }, value)); }); } else { PageNumber = pages.map(function (value, index, array) { if (value < 4 || array.length - 3 < value) { return React.createElement("li", { key: "page-" + value, className: classname(_this.state.page == value ? "active" : "", paginationItemStyle), onClick: _this.goTo.bind(_this, value) }, React.createElement("a", { onClick: function (e) { return e.preventDefault(); }, href: "#" }, value)); } else if (_this.inRange(_this.state.page, value, 4)) { return React.createElement("li", { key: "page-" + value, className: classname(_this.state.page == value ? "active" : "", paginationItemStyle), onClick: _this.goTo.bind(_this, value) }, React.createElement("a", { onClick: function (e) { return e.preventDefault(); }, href: "#" }, value)); } else if (_this.state.page < 8 && _this.inRange(7, value, 5)) { return React.createElement("li", { key: "page-" + value, className: classname(_this.state.page == value ? "active" : "", paginationItemStyle), onClick: _this.goTo.bind(_this, value) }, React.createElement("a", { onClick: function (e) { return e.preventDefault(); }, href: "#" }, value)); } else if (array.length - _this.state.page < 8 && _this.inRange(array.length - 6, value, 5)) { return React.createElement("li", { key: "page-" + value, className: classname(_this.state.page == value ? "active" : "", paginationItemStyle), onClick: _this.goTo.bind(_this, value) }, React.createElement("a", { onClick: function (e) { return e.preventDefault(); }, href: "#" }, value)); } else if (value === 4 || array.length - 3 === value) { return React.createElement("li", { key: "page-" + value, className: classname(_this.state.page == value ? "active" : "", paginationItemStyle) }, React.createElement("a", { onClick: function (e) { return e.preventDefault(); }, href: "#" }, "..")); } else { return null; } }).filter(function (value) { return value !== null; }); } var pageNumberNavigation = React.createElement("nav", null, React.createElement("ul", { className: paginationStyle }, React.createElement("li", { key: "page-prev", className: classname(this.state.page == 1 ? "disabled" : "", paginationItemStyle), onClick: this.goToPreviousPage }, React.createElement("a", { onClick: function (e) { return e.preventDefault(); }, href: "#", "aria-label": "Previous" }, React.createElement("span", { "aria-hidden": "true" }, "\u00AB"))), PageNumber, React.createElement("li", { key: "page-next", className: classname(this.state.page == maxPage ? "disabled" : "", paginationItemStyle), onClick: this.goToNextPage }, React.createElement("a", { href: "#", "aria-label": "Next", onClick: function (e) { return e.preventDefault(); } }, React.createElement("span", { "aria-hidden": "true" }, "\u00BB"))))); } return (React.createElement(Grid_1.UpGrid, null, React.createElement(Grid_1.UpRow, null, React.createElement(Grid_1.UpCol, { span: 16 }, pageNumberNavigation), React.createElement(Grid_1.UpCol, { span: 3 }, React.createElement(Select_1.default, { placeholder: this.props.nbByPageMessage, width: "small", default: { id: this.props.defaultTake, text: this.props.defaultTake }, data: takes, onChange: this.onTakeChange })), React.createElement(Grid_1.UpCol, { span: 5 }, React.createElement("span", { className: paginationCounterStyle }, maxPage == 0 && React.createElement("span", null, this.props.noResultMessage), maxPage != 0 && React.createElement("span", null, React.createElement("span", null, "R\u00E9sultat(s)\u00A0"), React.createElement("span", null, from), React.createElement("span", null, " \u00E0 "), React.createElement("span", null, to), React.createElement("span", null, " sur "), React.createElement("span", null, this.props.total))))))); }; UpPagination.defaultProps = { noResultMessage: "Aucun résultat", nbByPageMessage: "Par page", isTakeChangeEnable: true, isExtraInfoDisplay: true, takes: [{ id: 20, text: "20" }, { id: 50, text: "50" }, { id: 100, text: "100" }, { id: 200, text: "200" }], total: 0, defaultPage: 1, defaultSkip: 0, defaultTake: 50, onPageChange: function (page, take, skip) { } }; return UpPagination; }(React.Component)); exports.default = UpPagination; //# sourceMappingURL=UpPagination.js.map