import React, { useState } from 'react'; import styled from 'styled-components'; import ISVGIcon from '../Icon/SVGIcon'; const SVGIcon = styled(ISVGIcon)` margin: 0; transform: translateY(2px); `; const PageButtonContainer = styled.div` margin: 24px auto; display: inline-block; width: 100%; text-align: center; `; const PageButton = styled.button` background: #fefefe; border-radius: 6px; min-width: 32px; height: 32px; transition: 100ms ease; margin-right: 10px; font-size: 11pt; ${ props => props.isCurrent ? ` color: royalblue; font-weight: 600; border: 1px solid royalblue; ` : '' } &:hover { background: #f2f2f2; } &:active { background: #eeefee; } `; const FastButton = styled(PageButton)` background: url(/images/icons/dots.svg) no-repeat; background-position: center; background-size: 20px; transform: translateY(-5px); &:hover, &:active { background: url(/images/icons/caret-double-blue.svg) no-repeat; background-position: center; background-size: 14px; transform: translateY(-5px); } `; const range = (start, stop) => { if (stop < start) throw 'Stop value can\'t be smaller than start value.'; let a = []; for (let i = start; i != stop; i++) a.push(i); return a; } // TODO: Needs some work class Pagination extends React.Component { static defaultProps = { onChange: () => {}, disabled: false, sideButtons: true, value: 0, styles: {}, viewAllButton: false, onViewAll: () => {} } /** * @type {{ * pages: number, * * onChange?: (event: number) => void, * disabled?: boolean, * sideButtons?: boolean, * value?: number, * styles?: React.CSSProperties, * viewAllButton?: boolean, * onViewAll?: () => void * }} */ props; constructor(props) { super(props); this.state = { value: props.value } this.value_controlled = props.onChange != undefined && props.value != undefined; this.updateValue = this.updateValue.bind(this); } updateValue(new_val) { if (new_val < 0) new_val = 0; if (new_val > this.props.pages) new_val = this.props.pages; this.props.onChange(new_val); if (this.value_controlled) return; this.setState({value: new_val}); } render() { const { pages } = this.props; let value = this.state.value; if (this.value_controlled) value = this.props.value; return ( { this.props.viewAllButton ? Показать все : null } { this.props.sideButtons ? this.updateValue(value - 1)}> : null } { range(0, pages).map((el, i) => { if ( !( i == 0 | i == pages - 1) ) if ( i < value - 2 | i > value + 2 ) { if ( i == value - 3 | i == value + 3 ) { return ( { if (i == value - 3) { this.updateValue(value - 3); } else this.updateValue(value + 3); }} > ); } return null; } return this.updateValue(i)}>{i + 1} }) } { this.props.sideButtons ? this.updateValue(value + 1)}> : null } ); } } export default Pagination;