UNPKG

2.11 kBTypeScriptView Raw
1import * as React from 'react';
2import { PropTypes } from 'react';
3import { Menu } from 'semantic-ui-react';
4import * as _ from 'lodash';
5
6interface PaginationProps {
7 numRows: number;
8 pageSize: number;
9}
10
11interface PaginationState {
12 activePage: number;
13}
14
15class Pagination extends React.Component<PaginationProps, PaginationState> {
16 static contextTypes = {
17 searchState: PropTypes.object,
18 transition: PropTypes.func
19 };
20
21 constructor() {
22 super();
23 }
24
25 handlePaging(idx: number) {
26 return () => {
27 this.context.transition({start: (idx - 1) * this.props.pageSize});
28 };
29 }
30
31 render() {
32 const self = this;
33 const pageSize = self.props.pageSize || 10;
34 const numRows = self.props.numRows || 0;
35 let activePage = 1 + self.context.searchState.start / pageSize;
36
37 if (numRows < pageSize) {
38 return null;
39 }
40
41 const maxPage = Math.ceil(numRows / pageSize);
42 if (maxPage < activePage) {
43 activePage = maxPage;
44 }
45
46 const pages: number[] =
47 maxPage > 0 ?
48 _.takeRight(
49 _.takeWhile(
50 _.range(1, Math.max(maxPage, 1)),
51 (value: number, index: number, array: number[]) =>
52 index < activePage + 2
53 ),
54 5) :
55 [1];
56
57 if (pages[0] !== 1) {
58 pages.unshift(-1);
59 pages.unshift(1);
60 }
61
62 if (pages[pages.length - 1] < maxPage) {
63 pages.push(-1);
64 pages.push(maxPage);
65 }
66
67 const menuItems = (
68 pages.map(
69 (idx: number, row: number) =>
70 idx === -1 ?
71 (
72 <Menu.Item key={row} disabled={true}>...</Menu.Item>
73 ) : (
74 <Menu.Item
75 key={row}
76 name={idx + ''}
77 active={activePage === idx}
78 onClick={this.handlePaging(idx)}
79 />
80 )
81 )
82 );
83
84 return (
85 <Menu pagination={true}>
86 {menuItems}
87 </Menu>
88 );
89 }
90}
91
92export {
93 PaginationProps,
94 PaginationState,
95 Pagination
96};
\No newline at end of file