Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit 5cf474d

Browse files
author
Shammamah Hossain
committed
Allow for direct navigation to page.
1 parent c6aa878 commit 5cf474d

File tree

3 files changed

+106
-7
lines changed

3 files changed

+106
-7
lines changed

src/dash-table/components/ControlledTable/index.tsx

+21-3
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,12 @@ export default class ControlledTable extends PureComponent<ControlledTableProps>
652652
return paginator.lastPage;
653653
}
654654

655+
goToPage = (page_number: string) => {
656+
const { paginator } = this.props;
657+
658+
paginator.goToPage(page_number);
659+
}
660+
655661
applyStyle = () => {
656662
const {
657663
fixed_columns,
@@ -887,9 +893,21 @@ export default class ControlledTable extends PureComponent<ControlledTableProps>
887893
<FontAwesomeIcon icon='angle-left' />
888894
</button>
889895

890-
<div className='current-page'>
891-
{this.props.page_current + 1} {lastPage !== undefined ? ' / ' : ''}
892-
{lastPage !== undefined ? lastPage + 1 : ''}
896+
<div className='page-number'>
897+
<input
898+
type='text'
899+
className='current-page'
900+
onBlur={event => { this.goToPage(event.target.value); event.target.value = ''; }}
901+
placeholder={(this.props.page_current + 1).toString()}
902+
defaultValue=''
903+
>
904+
</input>
905+
906+
{lastPage !== undefined ? ' / ' : ''}
907+
908+
<div className='last-page'>
909+
{lastPage !== undefined ? lastPage + 1 : ''}
910+
</div>
893911
</div>
894912

895913
<button

src/dash-table/components/Table/Table.less

+30-1
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,37 @@ button.previous-page, button.next-page, .current-page {
152152
margin-right: 5px;
153153
}
154154

155-
.current-page {
155+
.page-number {
156156
font-family: monospace;
157+
display: inline-block;
158+
}
159+
160+
input.current-page, input.current-page::placeholder {
161+
color: black;
162+
border: none;
163+
width:30px;
164+
text-align:right;
165+
font-family: monospace;
166+
font-size: 10pt;
167+
margin: 0.5em;
168+
}
169+
170+
.page-number > .last-page {
171+
min-width: 30px;
172+
display: inline-block;
173+
margin: 0.5em;
174+
}
175+
176+
.last-page {
177+
width: 30px;
178+
display: inline-block;
179+
}
180+
181+
input.current-page:focus {
182+
outline: none;
183+
}
184+
input.current-page:focus::placeholder {
185+
opacity: 0;
157186
}
158187

159188
button.previous-page, button.next-page, button.first-page, button.last-page {

src/dash-table/derived/paginator.ts

+55-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface IPaginator {
1414
loadFirst(): void;
1515
loadLast(): void;
1616
lastPage: number | undefined;
17+
goToPage(page_number: string): void;
1718
}
1819

1920
export function lastPage(data: Data, page_size: number) {
@@ -48,7 +49,32 @@ function getBackEndPagination(
4849
setProps({ page_current, ...clearSelection });
4950
}
5051
},
51-
lastPage: max_page_count
52+
lastPage: max_page_count,
53+
goToPage: (page_number: string) => {
54+
55+
let page = parseInt(page_number, 10);
56+
57+
if (isNaN(page)) {
58+
return;
59+
}
60+
61+
page--;
62+
63+
if (page < 0) {
64+
page_current = 0;
65+
setProps({ page_current, ...clearSelection });
66+
return;
67+
}
68+
69+
if (max_page_count !== undefined && page > max_page_count) {
70+
page_current = max_page_count;
71+
setProps({ page_current, ...clearSelection });
72+
return;
73+
}
74+
75+
page_current = page;
76+
setProps({ page_current, ...clearSelection });
77+
}
5278
};
5379
}
5480

@@ -85,7 +111,32 @@ function getFrontEndPagination(
85111
page_current = lastPage(data, page_size);
86112
setProps({ page_current, ...clearSelection });
87113
},
88-
lastPage: lastPage(data, page_size)
114+
lastPage: lastPage(data, page_size),
115+
goToPage: (page_number: string) => {
116+
117+
let page = parseInt(page_number, 10);
118+
119+
if (isNaN(page)) {
120+
return;
121+
}
122+
123+
page--;
124+
125+
if (page < 0) {
126+
page_current = 0;
127+
setProps({ page_current, ...clearSelection });
128+
return;
129+
}
130+
131+
if (page > lastPage(data, page_size)) {
132+
page_current = lastPage(data, page_size);
133+
setProps({ page_current, ...clearSelection });
134+
return;
135+
}
136+
137+
page_current = page;
138+
setProps({ page_current, ...clearSelection });
139+
}
89140
};
90141
}
91142

@@ -95,7 +146,8 @@ function getNoPagination() {
95146
loadPrevious: () => { },
96147
loadFirst: () => { },
97148
loadLast: () => { },
98-
lastPage: 0
149+
lastPage: 0,
150+
goToPage: () => { }
99151
};
100152
}
101153

0 commit comments

Comments
 (0)