@@ -7,12 +7,47 @@ import Component from '@glimmer/component';
77import { tracked } from '@glimmer/tracking' ;
88import { action } from '@ember/object' ;
99import { 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+ }
1046
1147// for context about the decision to use these values, see:
1248// https://hashicorp.slack.com/archives/C03A0N1QK8S/p1673546329082759
1349export const DEFAULT_PAGE_SIZES = [ 10 , 30 , 50 ] ;
14-
15- export default class HdsPaginationCompactIndexComponent extends Component {
50+ export default class HdsPaginationCompactComponent extends Component < HdsPaginationCompactSignature > {
1651 // This private variable is used to differentiate between
1752 // "uncontrolled" component (where the state is handled internally) and
1853 // "controlled" component (where the state is handled externally, by the consumer's code).
@@ -27,10 +62,10 @@ export default class HdsPaginationCompactIndexComponent extends Component {
2762 showLabels = this . args . showLabels ?? true ; // if the labels for the "prev/next" controls are visible
2863 showSizeSelector = this . args . showSizeSelector ?? false ; // if the "size selector" block is visible
2964
30- constructor ( ) {
31- super ( ... arguments ) ;
65+ constructor ( owner : unknown , args : HdsPaginationCompactSignature [ 'Args' ] ) {
66+ super ( owner , args ) ;
3267
33- let { queryFunction } = this . args ;
68+ const { queryFunction } = this . args ;
3469
3570 // This component works in two different ways, depending if we need to support
3671 // routing through links (`LinkTo`) for the "navigation controls", or not.
@@ -51,12 +86,7 @@ export default class HdsPaginationCompactIndexComponent extends Component {
5186 }
5287 }
5388
54- /**
55- * @param ariaLabel
56- * @type {string }
57- * @default 'Pagination'
58- */
59- get ariaLabel ( ) {
89+ get ariaLabel ( ) : string {
6090 return this . args . ariaLabel ?? 'Pagination' ;
6191 }
6292
@@ -73,14 +103,13 @@ export default class HdsPaginationCompactIndexComponent extends Component {
73103 // is *always* determined by the component's internal logic (and updated according to the user interaction with it).
74104 // For this reason the "get" and "set" methods always read from or write to the private internal state (_variable).
75105
76- get currentPageSize ( ) {
106+ get currentPageSize ( ) : number | undefined {
77107 if ( this . isControlled ) {
78108 return this . args . currentPageSize ;
79109 } else {
80110 return this . _currentPageSize ;
81111 }
82112 }
83-
84113 set currentPageSize ( value ) {
85114 if ( this . isControlled ) {
86115 // noop
@@ -89,33 +118,31 @@ export default class HdsPaginationCompactIndexComponent extends Component {
89118 }
90119 }
91120
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 ;
100123
101124 assert (
102125 `pageSizes argument must be an array. Received: ${ pageSizes } ` ,
103- Array . isArray ( pageSizes ) === true
126+ Array . isArray ( pageSizes ) === true && pageSizes . length > 0
104127 ) ;
105128
106129 return pageSizes ;
107130 }
108131
109- buildQueryParamsObject ( page , pageSize ) {
132+ buildQueryParamsObject (
133+ page : HdsPaginationDirections ,
134+ pageSize ?: number
135+ ) : HdsInteractiveQuery {
110136 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 ) ;
112139 } else {
113140 return { } ;
114141 }
115142 }
116143
117- get routing ( ) {
118- let routing = {
144+ get routing ( ) : HdsPaginationCompactRoutingQueryProps {
145+ const routing : HdsPaginationCompactRoutingQueryProps = {
119146 route : this . args . route ?? undefined ,
120147 model : this . args . model ?? undefined ,
121148 models : this . args . models ?? undefined ,
@@ -125,11 +152,11 @@ export default class HdsPaginationCompactIndexComponent extends Component {
125152 // the "query" is dynamic and needs to be calculated
126153 if ( this . isControlled ) {
127154 routing . queryPrev = this . buildQueryParamsObject (
128- 'prev' ,
155+ HdsPaginationDirectionValues . Prev ,
129156 this . currentPageSize
130157 ) ;
131158 routing . queryNext = this . buildQueryParamsObject (
132- 'next' ,
159+ HdsPaginationDirectionValues . Next ,
133160 this . currentPageSize
134161 ) ;
135162 } else {
@@ -141,19 +168,17 @@ export default class HdsPaginationCompactIndexComponent extends Component {
141168 }
142169
143170 @action
144- onPageChange ( newPage ) {
145- this . currentPage = newPage ;
146-
147- let { onPageChange } = this . args ;
171+ onPageChange ( newPage : HdsPaginationDirections ) : void {
172+ const { onPageChange } = this . args ;
148173
149174 if ( typeof onPageChange === 'function' ) {
150175 onPageChange ( newPage ) ;
151176 }
152177 }
153178
154179 @action
155- onPageSizeChange ( newPageSize ) {
156- let { onPageSizeChange } = this . args ;
180+ onPageSizeChange ( newPageSize : number ) : void {
181+ const { onPageSizeChange } = this . args ;
157182
158183 // invoke the callback function
159184 if ( typeof onPageSizeChange === 'function' ) {
0 commit comments