@@ -7,12 +7,47 @@ import Component from '@glimmer/component';
7
7
import { tracked } from '@glimmer/tracking' ;
8
8
import { action } from '@ember/object' ;
9
9
import { assert } from '@ember/debug' ;
10
+ import { HdsPaginationDirectionValues } from '../types.ts' ;
11
+
12
+ import type {
13
+ HdsPaginationRoutingProps ,
14
+ HdsPaginationDirections ,
15
+ } from '../types' ;
16
+ import type { HdsInteractiveSignature } from '../../interactive' ;
17
+
18
+ type HdsInteractiveQuery = HdsInteractiveSignature [ 'Args' ] [ 'query' ] ;
19
+
20
+ type HdsPaginationCompactRoutingQueryProps = HdsPaginationRoutingProps & {
21
+ queryNext ?: HdsInteractiveQuery ;
22
+ queryPrev ?: HdsInteractiveQuery ;
23
+ } ;
24
+
25
+ interface HdsPaginationCompactArgs {
26
+ ariaLabel ?: string ;
27
+ showLabels ?: boolean ;
28
+ isDisabledPrev ?: boolean ;
29
+ isDisabledNext ?: boolean ;
30
+ showSizeSelector ?: boolean ;
31
+ sizeSelectorLabel ?: string ;
32
+ pageSizes ?: number [ ] ;
33
+ currentPageSize ?: number ;
34
+ queryFunction ?: (
35
+ page : HdsPaginationDirections ,
36
+ pageSize ?: number
37
+ ) => HdsInteractiveQuery ;
38
+ onPageChange ?: ( page : HdsPaginationDirections ) => void ;
39
+ onPageSizeChange ?: ( pageSize : number ) => void ;
40
+ }
41
+
42
+ interface HdsPaginationCompactSignature {
43
+ Args : HdsPaginationCompactArgs & HdsPaginationRoutingProps ;
44
+ Element : HTMLDivElement ;
45
+ }
10
46
11
47
// for context about the decision to use these values, see:
12
48
// https://hashicorp.slack.com/archives/C03A0N1QK8S/p1673546329082759
13
49
export const DEFAULT_PAGE_SIZES = [ 10 , 30 , 50 ] ;
14
-
15
- export default class HdsPaginationCompactIndexComponent extends Component {
50
+ export default class HdsPaginationCompactComponent extends Component < HdsPaginationCompactSignature > {
16
51
// This private variable is used to differentiate between
17
52
// "uncontrolled" component (where the state is handled internally) and
18
53
// "controlled" component (where the state is handled externally, by the consumer's code).
@@ -27,10 +62,10 @@ export default class HdsPaginationCompactIndexComponent extends Component {
27
62
showLabels = this . args . showLabels ?? true ; // if the labels for the "prev/next" controls are visible
28
63
showSizeSelector = this . args . showSizeSelector ?? false ; // if the "size selector" block is visible
29
64
30
- constructor ( ) {
31
- super ( ... arguments ) ;
65
+ constructor ( owner : unknown , args : HdsPaginationCompactSignature [ 'Args' ] ) {
66
+ super ( owner , args ) ;
32
67
33
- let { queryFunction } = this . args ;
68
+ const { queryFunction } = this . args ;
34
69
35
70
// This component works in two different ways, depending if we need to support
36
71
// routing through links (`LinkTo`) for the "navigation controls", or not.
@@ -51,12 +86,7 @@ export default class HdsPaginationCompactIndexComponent extends Component {
51
86
}
52
87
}
53
88
54
- /**
55
- * @param ariaLabel
56
- * @type {string }
57
- * @default 'Pagination'
58
- */
59
- get ariaLabel ( ) {
89
+ get ariaLabel ( ) : string {
60
90
return this . args . ariaLabel ?? 'Pagination' ;
61
91
}
62
92
@@ -73,14 +103,13 @@ export default class HdsPaginationCompactIndexComponent extends Component {
73
103
// is *always* determined by the component's internal logic (and updated according to the user interaction with it).
74
104
// For this reason the "get" and "set" methods always read from or write to the private internal state (_variable).
75
105
76
- get currentPageSize ( ) {
106
+ get currentPageSize ( ) : number | undefined {
77
107
if ( this . isControlled ) {
78
108
return this . args . currentPageSize ;
79
109
} else {
80
110
return this . _currentPageSize ;
81
111
}
82
112
}
83
-
84
113
set currentPageSize ( value ) {
85
114
if ( this . isControlled ) {
86
115
// noop
@@ -89,33 +118,31 @@ export default class HdsPaginationCompactIndexComponent extends Component {
89
118
}
90
119
}
91
120
92
- /**
93
- * @param pageSizes
94
- * @type {array of numbers }
95
- * @description Set the page sizes users can select from.
96
- * @default [10, 30, 50]
97
- */
98
- get pageSizes ( ) {
99
- let { pageSizes = DEFAULT_PAGE_SIZES } = this . args ;
121
+ get pageSizes ( ) : number [ ] {
122
+ const { pageSizes = DEFAULT_PAGE_SIZES } = this . args ;
100
123
101
124
assert (
102
125
`pageSizes argument must be an array. Received: ${ pageSizes } ` ,
103
- Array . isArray ( pageSizes ) === true
126
+ Array . isArray ( pageSizes ) === true && pageSizes . length > 0
104
127
) ;
105
128
106
129
return pageSizes ;
107
130
}
108
131
109
- buildQueryParamsObject ( page , pageSize ) {
132
+ buildQueryParamsObject (
133
+ page : HdsPaginationDirections ,
134
+ pageSize ?: number
135
+ ) : HdsInteractiveQuery {
110
136
if ( this . isControlled ) {
111
- return this . args . queryFunction ( page , pageSize ) ;
137
+ // if the component is controlled, we can assert that the queryFunction is defined
138
+ return this . args . queryFunction ! ( page , pageSize ) ;
112
139
} else {
113
140
return { } ;
114
141
}
115
142
}
116
143
117
- get routing ( ) {
118
- let routing = {
144
+ get routing ( ) : HdsPaginationCompactRoutingQueryProps {
145
+ const routing : HdsPaginationCompactRoutingQueryProps = {
119
146
route : this . args . route ?? undefined ,
120
147
model : this . args . model ?? undefined ,
121
148
models : this . args . models ?? undefined ,
@@ -125,11 +152,11 @@ export default class HdsPaginationCompactIndexComponent extends Component {
125
152
// the "query" is dynamic and needs to be calculated
126
153
if ( this . isControlled ) {
127
154
routing . queryPrev = this . buildQueryParamsObject (
128
- 'prev' ,
155
+ HdsPaginationDirectionValues . Prev ,
129
156
this . currentPageSize
130
157
) ;
131
158
routing . queryNext = this . buildQueryParamsObject (
132
- 'next' ,
159
+ HdsPaginationDirectionValues . Next ,
133
160
this . currentPageSize
134
161
) ;
135
162
} else {
@@ -141,19 +168,17 @@ export default class HdsPaginationCompactIndexComponent extends Component {
141
168
}
142
169
143
170
@action
144
- onPageChange ( newPage ) {
145
- this . currentPage = newPage ;
146
-
147
- let { onPageChange } = this . args ;
171
+ onPageChange ( newPage : HdsPaginationDirections ) : void {
172
+ const { onPageChange } = this . args ;
148
173
149
174
if ( typeof onPageChange === 'function' ) {
150
175
onPageChange ( newPage ) ;
151
176
}
152
177
}
153
178
154
179
@action
155
- onPageSizeChange ( newPageSize ) {
156
- let { onPageSizeChange } = this . args ;
180
+ onPageSizeChange ( newPageSize : number ) : void {
181
+ const { onPageSizeChange } = this . args ;
157
182
158
183
// invoke the callback function
159
184
if ( typeof onPageSizeChange === 'function' ) {
0 commit comments