Skip to content

Commit 579dfcd

Browse files
zamooreshleewhite
andauthored
Pagination TS conversion (#2174)
Co-authored-by: Lee White <leewhite128@gmail.com>
1 parent b26d701 commit 579dfcd

File tree

21 files changed

+460
-250
lines changed

21 files changed

+460
-250
lines changed

Diff for: .changeset/hungry-cycles-drum.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hashicorp/design-system-components": minor
3+
---
4+
5+
`Hds::Pagination` - Converted component to Typescript

Diff for: packages/components/src/components.ts

+14
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ import HdsModalBodyComponent from './components/hds/modal/body.ts';
8686
import HdsModalFooter from './components/hds/modal/footer.ts';
8787
import HdsModalHeader from './components/hds/modal/header.ts';
8888
import HdsPageHeader from './components/hds/page-header/index.ts';
89+
import HdsPaginationCompactComponent from './components/hds/pagination/compact/index.ts';
90+
import HdsPaginationControlInfoComponent from './components/hds/pagination/info/index.ts';
91+
import HdsPaginationControlArrowComponent from './components/hds/pagination/nav/arrow.ts';
92+
import HdsPaginationControlEllipsisComponent from './components/hds/pagination/nav/ellipsis.ts';
93+
import HdsPaginationControlNumberComponent from './components/hds/pagination/nav/number.ts';
94+
import HdsPaginationNumberedComponent from './components/hds/pagination/numbered/index.ts';
95+
import HdsPaginationSizeSelectorComponent from './components/hds/pagination/size-selector/index.ts';
8996
import HdsPopoverPrimitive from './components/hds/popover-primitive/index.ts';
9097
import HdsReveal from './components/hds/reveal/index.ts';
9198
import HdsRichTooltip from './components/hds/rich-tooltip/index.ts';
@@ -201,6 +208,13 @@ export {
201208
HdsModalFooter,
202209
HdsModalHeader,
203210
HdsPageHeader,
211+
HdsPaginationCompactComponent,
212+
HdsPaginationControlInfoComponent,
213+
HdsPaginationControlArrowComponent,
214+
HdsPaginationControlEllipsisComponent,
215+
HdsPaginationControlNumberComponent,
216+
HdsPaginationNumberedComponent,
217+
HdsPaginationSizeSelectorComponent,
204218
HdsPopoverPrimitive,
205219
HdsReveal,
206220
HdsRichTooltip,

Diff for: packages/components/src/components/hds/pagination/compact/index.hbs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
{{! @glint-nocheck: not typesafe yet }}
21
{{!
32
Copyright (c) HashiCorp, Inc.
43
SPDX-License-Identifier: MPL-2.0

Diff for: packages/components/src/components/hds/pagination/compact/index.js renamed to packages/components/src/components/hds/pagination/compact/index.ts

+59-34
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,47 @@ import Component from '@glimmer/component';
77
import { tracked } from '@glimmer/tracking';
88
import { action } from '@ember/object';
99
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+
}
1046

1147
// for context about the decision to use these values, see:
1248
// https://hashicorp.slack.com/archives/C03A0N1QK8S/p1673546329082759
1349
export 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') {

Diff for: packages/components/src/components/hds/pagination/info/index.hbs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
{{! @glint-nocheck: not typesafe yet }}
21
{{!
32
Copyright (c) HashiCorp, Inc.
43
SPDX-License-Identifier: MPL-2.0

Diff for: packages/components/src/components/hds/pagination/info/index.js

-17
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (c) HashiCorp, Inc.
3+
* SPDX-License-Identifier: MPL-2.0
4+
*/
5+
6+
import Component from '@glimmer/component';
7+
import type { HdsPaginationNumberedSignature } from '../numbered/index';
8+
import type { HdsTextBodySignature } from '../../text/body';
9+
interface HdsPaginationInfoSignature {
10+
Args: {
11+
itemsRangeStart: number;
12+
itemsRangeEnd: number;
13+
showTotalItems?: HdsPaginationNumberedSignature['Args']['showTotalItems'];
14+
totalItems: HdsPaginationNumberedSignature['Args']['totalItems'];
15+
};
16+
Element: HdsTextBodySignature['Element'];
17+
}
18+
19+
export default class HdsPaginationInfoComponent extends Component<HdsPaginationInfoSignature> {
20+
get showTotalItems(): boolean {
21+
return this.args.showTotalItems ?? true;
22+
}
23+
}

Diff for: packages/components/src/components/hds/pagination/nav/arrow.hbs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
{{! @glint-nocheck: not typesafe yet }}
21
{{!
32
Copyright (c) HashiCorp, Inc.
43
SPDX-License-Identifier: MPL-2.0

Diff for: packages/components/src/components/hds/pagination/nav/arrow.js

-77
This file was deleted.

0 commit comments

Comments
 (0)