@@ -6,152 +6,154 @@ import * as telemetry from '../../common/telemetry';
6
6
import { Stack } from "office-ui-fabric-react/lib/Stack" ;
7
7
8
8
export interface IPaginationProps {
9
- /**
10
- * The page initial selected
11
- */
12
- currentPage : number ;
13
- /**
14
- * The total of page that you want to show on control
15
- */
16
- totalPages : number ;
17
- /**
18
- * When the page number change send the page number selected
19
- */
20
- onChange : ( page : number ) => void ;
21
- /**
22
- * The number of pages showing before the icon
23
- */
24
- limiter ?: number ;
25
- /**
26
- * Hide the quick jump to the first page
27
- */
28
- hideFirstPageJump ?: boolean ;
29
- /**
30
- * Hide the quick jump to the last page
31
- */
32
- hideLastPageJump ?: boolean ;
33
- /**
34
- * Limitir icon, by default is More icon
35
- */
36
- limiterIcon ?: string ;
9
+ /**
10
+ * The page initial selected
11
+ */
12
+ currentPage : number ;
13
+ /**
14
+ * The total of page that you want to show on control
15
+ */
16
+ totalPages : number ;
17
+ /**
18
+ * When the page number change send the page number selected
19
+ */
20
+ onChange : ( page : number ) => void ;
21
+ /**
22
+ * The number of pages showing before the icon
23
+ */
24
+ limiter ?: number ;
25
+ /**
26
+ * Hide the quick jump to the first page
27
+ */
28
+ hideFirstPageJump ?: boolean ;
29
+ /**
30
+ * Hide the quick jump to the last page
31
+ */
32
+ hideLastPageJump ?: boolean ;
33
+ /**
34
+ * Limitir icon, by default is More icon
35
+ */
36
+ limiterIcon ?: string ;
37
37
}
38
38
39
39
export interface IPaginationState {
40
- currentPage : number ;
41
- paginationElements : number [ ] ;
42
- limiter : number ;
40
+ currentPage : number ;
41
+ paginationElements : number [ ] ;
42
+ limiter : number ;
43
43
}
44
44
45
45
export class Pagination extends React . Component < IPaginationProps , IPaginationState > {
46
- constructor ( props : Readonly < IPaginationProps > ) {
47
- super ( props ) ;
46
+ constructor ( props : Readonly < IPaginationProps > ) {
47
+ super ( props ) ;
48
48
49
- telemetry . track ( 'ReactPagination' ) ;
49
+ telemetry . track ( 'ReactPagination' ) ;
50
50
51
- const paginationElements = this . preparePaginationElements ( props . totalPages ) ;
51
+ const paginationElements = this . preparePaginationElements ( props . totalPages ) ;
52
52
53
- this . state = {
54
- currentPage : props . currentPage ,
55
- paginationElements,
56
- limiter : props . limiter ? props . limiter : 3 ,
57
- } ;
58
- }
59
-
60
- public componentDidUpdate ( prevProps : IPaginationProps ) {
61
- let { currentPage, paginationElements } = this . state ;
53
+ this . state = {
54
+ currentPage : props . currentPage ,
55
+ paginationElements,
56
+ limiter : props . limiter ? props . limiter : 3 ,
57
+ } ;
58
+ }
62
59
63
- if ( prevProps . totalPages !== this . props . totalPages ) {
64
- paginationElements = this . preparePaginationElements ( this . props . totalPages ) ;
65
- currentPage = this . state . currentPage > this . props . totalPages ? this . props . totalPages : this . state . currentPage ;
66
- }
67
- if ( this . props . currentPage !== prevProps . currentPage ) {
68
- currentPage = this . props . currentPage > this . props . totalPages ? this . props . totalPages : this . props . currentPage ;
69
- }
60
+ public componentDidUpdate ( prevProps : IPaginationProps ) : void {
61
+ let { currentPage, paginationElements } = this . state ;
70
62
71
- if ( ! isEqual ( this . state . currentPage , currentPage ) || ! isEqual ( this . state . paginationElements , paginationElements ) ) {
72
- this . setState ( {
73
- paginationElements,
74
- currentPage
75
- } ) ;
76
- }
63
+ if ( prevProps . totalPages !== this . props . totalPages ) {
64
+ paginationElements = this . preparePaginationElements ( this . props . totalPages ) ;
65
+ currentPage = this . state . currentPage > this . props . totalPages ? this . props . totalPages : this . state . currentPage ;
77
66
}
78
-
79
- public render ( ) : React . ReactElement < IPaginationProps > {
80
- return (
81
- < div className = { `${ styles . pagination } pagination-container` } >
82
- < Stack horizontal horizontalAlign = "center" verticalAlign = "center" tokens = { { childrenGap :1 } } wrap >
83
- { ! this . props . hideFirstPageJump &&
84
- < DefaultButton
85
- className = { `${ styles . buttonStyle } pagination-button pagination-button_first` }
86
- onClick = { ( ) => this . onClick ( 1 ) }
87
- iconProps = { { iconName : "DoubleChevronLeft" } } >
88
- </ DefaultButton >
89
- }
90
-
91
- { this . state . paginationElements . map ( ( pageNumber ) => this . renderPageNumber ( pageNumber ) ) }
92
-
93
- { ! this . props . hideLastPageJump &&
94
- < DefaultButton
95
- className = { `${ styles . buttonStyle } pagination-button pagination-button_last` }
96
- onClick = { ( ) => this . onClick ( this . props . totalPages ) }
97
- iconProps = { { iconName : "DoubleChevronRight" } } >
98
- </ DefaultButton >
99
- }
100
- </ Stack >
101
- </ div >
102
- ) ;
67
+ if ( this . props . currentPage !== prevProps . currentPage ) {
68
+ currentPage = this . props . currentPage > this . props . totalPages ? this . props . totalPages : this . props . currentPage ;
103
69
}
104
70
105
- private preparePaginationElements = ( totalPages : number ) => {
106
- let paginationElementsArray = [ ] ;
107
- for ( let i = 0 ; i < totalPages ; i ++ ) {
108
- paginationElementsArray . push ( i + 1 ) ;
109
- }
110
- return paginationElementsArray ;
71
+ if ( ! isEqual ( this . state . currentPage , currentPage ) || ! isEqual ( this . state . paginationElements , paginationElements ) ) {
72
+ this . setState ( {
73
+ paginationElements,
74
+ currentPage
75
+ } ) ;
111
76
}
77
+ }
112
78
113
- private onClick = ( page : number ) => {
114
- this . setState ( { currentPage : page } ) ;
115
- this . props . onChange ( page ) ;
79
+ public render ( ) : React . ReactElement < IPaginationProps > {
80
+ return (
81
+ < div className = { `${ styles . pagination } pagination-container` } >
82
+ < Stack horizontal horizontalAlign = "center" verticalAlign = "center" tokens = { { childrenGap : 1 } } wrap >
83
+ { ! this . props . hideFirstPageJump &&
84
+ < DefaultButton
85
+ className = { `${ styles . buttonStyle } pagination-button pagination-button_first` }
86
+ onClick = { ( ) => this . onClick ( 1 ) }
87
+ iconProps = { { iconName : "DoubleChevronLeft" } } />
88
+ }
89
+
90
+ { this . state . paginationElements . map ( ( pageNumber ) => this . renderPageNumber ( pageNumber ) ) }
91
+
92
+ { ! this . props . hideLastPageJump &&
93
+ < DefaultButton
94
+ className = { `${ styles . buttonStyle } pagination-button pagination-button_last` }
95
+ onClick = { ( ) => this . onClick ( this . props . totalPages ) }
96
+ iconProps = { { iconName : "DoubleChevronRight" } } />
97
+ }
98
+ </ Stack >
99
+ </ div >
100
+ ) ;
101
+ }
102
+
103
+ private preparePaginationElements = ( totalPages : number ) : number [ ] => {
104
+ const paginationElementsArray : number [ ] = [ ] ;
105
+ for ( let i = 0 ; i < totalPages ; i ++ ) {
106
+ paginationElementsArray . push ( i + 1 ) ;
116
107
}
108
+ return paginationElementsArray ;
109
+ }
110
+
111
+ private onClick = ( page : number ) : void => {
112
+ this . setState ( { currentPage : page } ) ;
113
+ this . props . onChange ( page ) ;
114
+ }
117
115
118
- private renderPageNumber ( pageNumber ) {
119
- if ( pageNumber === this . state . currentPage ) {
120
- return (
121
- < PrimaryButton
122
- className = { styles . buttonStyle }
123
- onClick = { ( ) => this . onClick ( pageNumber ) }
124
- text = { pageNumber } >
125
- </ PrimaryButton >
126
- ) ;
127
- } else {
128
- if ( ! ( pageNumber < this . state . currentPage - this . state . limiter || pageNumber > this . state . currentPage + this . state . limiter ) ) {
129
- return (
130
- < DefaultButton
131
- className = { styles . buttonStyle }
132
- onClick = { ( ) => this . onClick ( pageNumber ) }
133
- text = { pageNumber } >
134
- </ DefaultButton > ) ;
135
- }
136
- else if ( ! ( pageNumber < this . state . currentPage - this . state . limiter - 1 || pageNumber > this . state . currentPage + this . state . limiter + 1 ) ) {
137
- if ( this . props . limiterIcon ) {
138
- return ( < DefaultButton
139
- className = { styles . buttonStyle }
140
- onClick = { ( ) => this . onClick ( pageNumber ) }
141
- iconProps = { { iconName : this . props . limiterIcon ? this . props . limiterIcon : "More" } } >
142
- </ DefaultButton > ) ;
143
- }
144
- else {
145
- return ( < DefaultButton
146
- className = { styles . buttonStyle }
147
- onClick = { ( ) => this . onClick ( pageNumber ) }
148
- iconProps = { { iconName : this . props . limiterIcon ? this . props . limiterIcon : "More" } } >
149
- </ DefaultButton > ) ;
150
- }
151
- }
152
- else {
153
- return ;
154
- }
116
+ private renderPageNumber ( pageNumber ) : JSX . Element {
117
+ const {
118
+ limiterIcon
119
+ } = this . props ;
120
+
121
+ const {
122
+ currentPage,
123
+ limiter
124
+ } = this . state ;
125
+ if ( pageNumber === currentPage ) {
126
+ return (
127
+ < PrimaryButton
128
+ className = { styles . buttonStyle }
129
+ onClick = { ( ) => this . onClick ( pageNumber ) }
130
+ text = { pageNumber } />
131
+ ) ;
132
+ } else {
133
+ if ( ! ( pageNumber < currentPage - limiter || pageNumber > currentPage + limiter ) ) {
134
+ return (
135
+ < DefaultButton
136
+ className = { styles . buttonStyle }
137
+ onClick = { ( ) => this . onClick ( pageNumber ) }
138
+ text = { pageNumber } /> ) ;
139
+ }
140
+ else if ( ! ( pageNumber < currentPage - limiter - 1 || pageNumber > currentPage + limiter + 1 ) ) {
141
+ if ( limiterIcon ) {
142
+ return ( < DefaultButton
143
+ className = { styles . buttonStyle }
144
+ onClick = { ( ) => this . onClick ( pageNumber ) }
145
+ iconProps = { { iconName : limiterIcon ? limiterIcon : "More" } } /> ) ;
146
+ }
147
+ else {
148
+ return ( < DefaultButton
149
+ className = { styles . buttonStyle }
150
+ onClick = { ( ) => this . onClick ( pageNumber ) }
151
+ iconProps = { { iconName : limiterIcon ? limiterIcon : "More" } } /> ) ;
155
152
}
153
+ }
154
+ else {
155
+ return ;
156
+ }
156
157
}
158
+ }
157
159
}
0 commit comments