@nypl/design-system-react-components 1.0.0 → 1.0.3-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -5
- package/dist/components/Checkbox/Checkbox.d.ts +0 -1
- package/dist/components/ComponentWrapper/ComponentWrapper.d.ts +8 -0
- package/dist/components/Heading/Heading.d.ts +2 -0
- package/dist/components/Logo/Logo.d.ts +1 -1
- package/dist/components/Logo/LogoSvgs.d.ts +4 -0
- package/dist/components/Slider/Slider.d.ts +1 -2
- package/dist/components/TextInput/TextInput.d.ts +2 -1
- package/dist/design-system-react-components.cjs.development.js +1038 -674
- package/dist/design-system-react-components.cjs.development.js.map +1 -1
- package/dist/design-system-react-components.cjs.production.min.js +1 -1
- package/dist/design-system-react-components.cjs.production.min.js.map +1 -1
- package/dist/design-system-react-components.esm.js +1039 -675
- package/dist/design-system-react-components.esm.js.map +1 -1
- package/dist/hooks/{tests → __tests__}/useCarouselStyles.test.d.ts +0 -0
- package/dist/theme/components/button.d.ts +1 -0
- package/dist/theme/components/card.d.ts +98 -13
- package/dist/theme/components/checkboxGroup.d.ts +1 -1
- package/dist/theme/components/global.d.ts +2 -2
- package/dist/theme/components/heading.d.ts +4 -16
- package/dist/theme/components/image.d.ts +6 -0
- package/dist/theme/components/radio.d.ts +8 -0
- package/dist/theme/components/radioGroup.d.ts +1 -1
- package/dist/theme/components/skipNavigation.d.ts +3 -0
- package/dist/theme/components/structuredContent.d.ts +0 -5
- package/dist/theme/components/template.d.ts +4 -1
- package/dist/utils/utils.d.ts +15 -0
- package/package.json +6 -7
- package/CHANGELOG.md +0 -1399
- package/src/__tests__/fileMock.ts +0 -6
- package/src/__tests__/setup.ts +0 -27
- package/src/__tests__/utils/utils.test.ts +0 -18
- package/src/components/AccessibilityGuide/SkipNavigation.stories.mdx +0 -42
- package/src/components/Accordion/Accordion.stories.mdx +0 -361
- package/src/components/Accordion/Accordion.test.tsx +0 -237
- package/src/components/Accordion/Accordion.tsx +0 -136
- package/src/components/Accordion/__snapshots__/Accordion.test.tsx.snap +0 -734
- package/src/components/Autosuggest/Autosuggest.stories.mdx +0 -75
- package/src/components/Autosuggest/Autosuggest.stories.tsx +0 -211
- package/src/components/Autosuggest/_Autosuggest.scss +0 -51
- package/src/components/Breadcrumbs/Breadcrumbs.stories.mdx +0 -231
- package/src/components/Breadcrumbs/Breadcrumbs.test.tsx +0 -144
- package/src/components/Breadcrumbs/Breadcrumbs.tsx +0 -98
- package/src/components/Breadcrumbs/__snapshots__/Breadcrumbs.test.tsx.snap +0 -695
- package/src/components/Button/Button.stories.mdx +0 -320
- package/src/components/Button/Button.test.tsx +0 -184
- package/src/components/Button/Button.tsx +0 -95
- package/src/components/Button/__snapshots__/Button.test.tsx.snap +0 -106
- package/src/components/ButtonGroup/ButtonGroup.stories.mdx +0 -147
- package/src/components/ButtonGroup/ButtonGroup.test.tsx +0 -141
- package/src/components/ButtonGroup/ButtonGroup.tsx +0 -99
- package/src/components/ButtonGroup/__snapshots__/ButtonGroup.test.tsx.snap +0 -117
- package/src/components/Card/Card.stories.mdx +0 -1043
- package/src/components/Card/Card.test.tsx +0 -388
- package/src/components/Card/Card.tsx +0 -345
- package/src/components/Card/__snapshots__/Card.test.tsx.snap +0 -568
- package/src/components/Chakra/Box.stories.mdx +0 -52
- package/src/components/Chakra/Center.stories.mdx +0 -96
- package/src/components/Chakra/Flex.stories.mdx +0 -111
- package/src/components/Chakra/Grid.stories.mdx +0 -89
- package/src/components/Chakra/Stack.stories.mdx +0 -109
- package/src/components/Checkbox/Checkbox.stories.mdx +0 -243
- package/src/components/Checkbox/Checkbox.test.tsx +0 -319
- package/src/components/Checkbox/Checkbox.tsx +0 -166
- package/src/components/Checkbox/__snapshots__/Checkbox.test.tsx.snap +0 -616
- package/src/components/CheckboxGroup/CheckboxGroup.stories.mdx +0 -462
- package/src/components/CheckboxGroup/CheckboxGroup.test.tsx +0 -492
- package/src/components/CheckboxGroup/CheckboxGroup.tsx +0 -173
- package/src/components/CheckboxGroup/__snapshots__/CheckboxGroup.test.tsx.snap +0 -1722
- package/src/components/ComponentWrapper/ComponentWrapper.test.tsx +0 -163
- package/src/components/ComponentWrapper/ComponentWrapper.tsx +0 -67
- package/src/components/ComponentWrapper/__snapshots__/ComponentWrapper.test.tsx.snap +0 -98
- package/src/components/DatePicker/DatePicker.stories.mdx +0 -452
- package/src/components/DatePicker/DatePicker.test.tsx +0 -940
- package/src/components/DatePicker/DatePicker.tsx +0 -450
- package/src/components/DatePicker/_DatePicker.scss +0 -100
- package/src/components/DatePicker/__snapshots__/DatePicker.test.tsx.snap +0 -889
- package/src/components/Fieldset/Fieldset.stories.mdx +0 -96
- package/src/components/Fieldset/Fieldset.test.tsx +0 -155
- package/src/components/Fieldset/Fieldset.tsx +0 -55
- package/src/components/Fieldset/__snapshots__/Fieldset.test.tsx.snap +0 -89
- package/src/components/Form/Form.stories.mdx +0 -426
- package/src/components/Form/Form.test.tsx +0 -234
- package/src/components/Form/Form.tsx +0 -124
- package/src/components/Form/__snapshots__/Form.test.tsx.snap +0 -115
- package/src/components/Grid/SimpleGrid.stories.mdx +0 -338
- package/src/components/Grid/SimpleGrid.test.tsx +0 -79
- package/src/components/Grid/SimpleGrid.tsx +0 -49
- package/src/components/Grid/__snapshots__/SimpleGrid.test.tsx.snap +0 -23
- package/src/components/Heading/Heading.stories.mdx +0 -184
- package/src/components/Heading/Heading.test.tsx +0 -171
- package/src/components/Heading/Heading.tsx +0 -101
- package/src/components/Heading/__snapshots__/Heading.test.tsx.snap +0 -90
- package/src/components/HelperErrorText/HelperErrorText.stories.mdx +0 -172
- package/src/components/HelperErrorText/HelperErrorText.test.tsx +0 -114
- package/src/components/HelperErrorText/HelperErrorText.tsx +0 -62
- package/src/components/HelperErrorText/__snapshots__/HelperErrorText.test.tsx.snap +0 -93
- package/src/components/Hero/Hero.stories.mdx +0 -378
- package/src/components/Hero/Hero.test.tsx +0 -611
- package/src/components/Hero/Hero.tsx +0 -203
- package/src/components/Hero/__snapshots__/Hero.test.tsx.snap +0 -379
- package/src/components/HorizontalRule/HorizontalRule.stories.mdx +0 -73
- package/src/components/HorizontalRule/HorizontalRule.test.tsx +0 -54
- package/src/components/HorizontalRule/HorizontalRule.tsx +0 -27
- package/src/components/HorizontalRule/__snapshots__/HorizontalRule.test.tsx.snap +0 -44
- package/src/components/Icons/Icon.stories.mdx +0 -413
- package/src/components/Icons/Icon.test.tsx +0 -120
- package/src/components/Icons/Icon.tsx +0 -187
- package/src/components/Icons/IconSvgs.tsx +0 -64
- package/src/components/Icons/__snapshots__/Icon.test.tsx.snap +0 -129
- package/src/components/Image/Image.stories.mdx +0 -332
- package/src/components/Image/Image.test.tsx +0 -155
- package/src/components/Image/Image.tsx +0 -171
- package/src/components/Image/__snapshots__/Image.test.tsx.snap +0 -269
- package/src/components/Label/Label.stories.mdx +0 -100
- package/src/components/Label/Label.test.tsx +0 -116
- package/src/components/Label/Label.tsx +0 -55
- package/src/components/Label/__snapshots__/Label.test.tsx.snap +0 -45
- package/src/components/Link/Link.stories.mdx +0 -249
- package/src/components/Link/Link.test.tsx +0 -224
- package/src/components/Link/Link.tsx +0 -178
- package/src/components/Link/__snapshots__/Link.test.tsx.snap +0 -286
- package/src/components/List/List.stories.mdx +0 -393
- package/src/components/List/List.test.tsx +0 -265
- package/src/components/List/List.tsx +0 -156
- package/src/components/List/__snapshots__/List.test.tsx.snap +0 -213
- package/src/components/Logo/Logo.stories.mdx +0 -290
- package/src/components/Logo/Logo.test.tsx +0 -116
- package/src/components/Logo/Logo.tsx +0 -147
- package/src/components/Logo/LogoSvgs.tsx +0 -82
- package/src/components/Logo/__snapshots__/Logo.test.tsx.snap +0 -142
- package/src/components/Modal/Modal.stories.mdx +0 -301
- package/src/components/Modal/Modal.test.tsx +0 -157
- package/src/components/Modal/Modal.tsx +0 -154
- package/src/components/Modal/__snapshots__/Modal.test.tsx.snap +0 -25
- package/src/components/Notification/Notification.stories.mdx +0 -358
- package/src/components/Notification/Notification.test.tsx +0 -279
- package/src/components/Notification/Notification.tsx +0 -224
- package/src/components/Notification/__snapshots__/Notification.test.tsx.snap +0 -525
- package/src/components/Pagination/Pagination.stories.mdx +0 -184
- package/src/components/Pagination/Pagination.test.tsx +0 -419
- package/src/components/Pagination/Pagination.tsx +0 -269
- package/src/components/Pagination/__snapshots__/Pagination.test.tsx.snap +0 -544
- package/src/components/Placeholder/Placeholder.tsx +0 -19
- package/src/components/ProgressIndicator/ProgressIndicator.stories.mdx +0 -360
- package/src/components/ProgressIndicator/ProgressIndicator.test.tsx +0 -298
- package/src/components/ProgressIndicator/ProgressIndicator.tsx +0 -132
- package/src/components/ProgressIndicator/__snapshots__/ProgressIndicator.test.tsx.snap +0 -434
- package/src/components/Radio/Radio.stories.mdx +0 -216
- package/src/components/Radio/Radio.test.tsx +0 -247
- package/src/components/Radio/Radio.tsx +0 -128
- package/src/components/Radio/__snapshots__/Radio.test.tsx.snap +0 -411
- package/src/components/RadioGroup/RadioGroup.stories.mdx +0 -478
- package/src/components/RadioGroup/RadioGroup.test.tsx +0 -471
- package/src/components/RadioGroup/RadioGroup.tsx +0 -170
- package/src/components/RadioGroup/__snapshots__/RadioGroup.test.tsx.snap +0 -1430
- package/src/components/SearchBar/SearchBar.stories.mdx +0 -518
- package/src/components/SearchBar/SearchBar.test.tsx +0 -435
- package/src/components/SearchBar/SearchBar.tsx +0 -210
- package/src/components/SearchBar/__snapshots__/SearchBar.test.tsx.snap +0 -1027
- package/src/components/Select/Select.stories.mdx +0 -439
- package/src/components/Select/Select.test.tsx +0 -358
- package/src/components/Select/Select.tsx +0 -181
- package/src/components/Select/__snapshots__/Select.test.tsx.snap +0 -885
- package/src/components/SkeletonLoader/SkeletonLoader.stories.mdx +0 -142
- package/src/components/SkeletonLoader/SkeletonLoader.test.tsx +0 -116
- package/src/components/SkeletonLoader/SkeletonLoader.tsx +0 -123
- package/src/components/SkeletonLoader/__snapshots__/SkeletonLoader.test.tsx.snap +0 -898
- package/src/components/SkipNavigation/SkipNavigation.stories.mdx +0 -90
- package/src/components/SkipNavigation/SkipNavigation.test.tsx +0 -63
- package/src/components/SkipNavigation/SkipNavigation.tsx +0 -51
- package/src/components/SkipNavigation/__snapshots__/SkipNavigation.test.tsx.snap +0 -130
- package/src/components/Slider/Slider.stories.mdx +0 -628
- package/src/components/Slider/Slider.test.tsx +0 -736
- package/src/components/Slider/Slider.tsx +0 -322
- package/src/components/Slider/__snapshots__/Slider.test.tsx.snap +0 -2153
- package/src/components/StatusBadge/StatusBadge.stories.mdx +0 -109
- package/src/components/StatusBadge/StatusBadge.test.tsx +0 -70
- package/src/components/StatusBadge/StatusBadge.tsx +0 -35
- package/src/components/StatusBadge/__snapshots__/StatusBadge.test.tsx.snap +0 -47
- package/src/components/StructuredContent/StructuredContent.stories.mdx +0 -567
- package/src/components/StructuredContent/StructuredContent.test.tsx +0 -435
- package/src/components/StructuredContent/StructuredContent.tsx +0 -139
- package/src/components/StructuredContent/__snapshots__/StructuredContent.test.tsx.snap +0 -368
- package/src/components/StyleGuide/Bidirectionality.stories.mdx +0 -206
- package/src/components/StyleGuide/Breakpoints.stories.mdx +0 -55
- package/src/components/StyleGuide/Buttons.stories.mdx +0 -194
- package/src/components/StyleGuide/ColorCard.tsx +0 -43
- package/src/components/StyleGuide/Colors.stories.mdx +0 -201
- package/src/components/StyleGuide/DesignTokens.stories.mdx +0 -193
- package/src/components/StyleGuide/Forms.stories.mdx +0 -94
- package/src/components/StyleGuide/Iconography.stories.mdx +0 -142
- package/src/components/StyleGuide/Spacing.stories.mdx +0 -116
- package/src/components/StyleGuide/Typography.stories.mdx +0 -419
- package/src/components/Table/Table.stories.mdx +0 -276
- package/src/components/Table/Table.test.tsx +0 -208
- package/src/components/Table/Table.tsx +0 -131
- package/src/components/Table/__snapshots__/Table.test.tsx.snap +0 -1703
- package/src/components/Tabs/Tabs.stories.mdx +0 -338
- package/src/components/Tabs/Tabs.test.tsx +0 -298
- package/src/components/Tabs/Tabs.tsx +0 -264
- package/src/components/Tabs/__snapshots__/Tabs.test.tsx.snap +0 -584
- package/src/components/Template/Template.stories.mdx +0 -691
- package/src/components/Template/Template.test.tsx +0 -309
- package/src/components/Template/Template.tsx +0 -326
- package/src/components/Template/__snapshots__/Template.test.tsx.snap +0 -342
- package/src/components/Text/Text.stories.mdx +0 -103
- package/src/components/Text/Text.test.tsx +0 -63
- package/src/components/Text/Text.tsx +0 -50
- package/src/components/Text/__snapshots__/Text.test.tsx.snap +0 -50
- package/src/components/TextInput/TextInput.stories.mdx +0 -268
- package/src/components/TextInput/TextInput.test.tsx +0 -451
- package/src/components/TextInput/TextInput.tsx +0 -240
- package/src/components/TextInput/__snapshots__/TextInput.test.tsx.snap +0 -264
- package/src/components/Toggle/Toggle.stories.mdx +0 -237
- package/src/components/Toggle/Toggle.test.tsx +0 -170
- package/src/components/Toggle/Toggle.tsx +0 -126
- package/src/components/Toggle/__snapshots__/Toggle.test.tsx.snap +0 -442
- package/src/components/VideoPlayer/VideoPlayer.stories.mdx +0 -211
- package/src/components/VideoPlayer/VideoPlayer.test.tsx +0 -308
- package/src/components/VideoPlayer/VideoPlayer.tsx +0 -188
- package/src/components/VideoPlayer/__snapshots__/VideoPlayer.test.tsx.snap +0 -187
- package/src/docs/Chakra.stories.mdx +0 -563
- package/src/docs/Welcome.stories.mdx +0 -148
- package/src/helpers/types.ts +0 -1
- package/src/hooks/tests/useCarouselStyles.test.ts +0 -140
- package/src/hooks/tests/useNYPLTheme.test.tsx +0 -36
- package/src/hooks/useCarouselStyles.stories.mdx +0 -30
- package/src/hooks/useCarouselStyles.ts +0 -35
- package/src/hooks/useNYPLTheme.stories.mdx +0 -98
- package/src/hooks/useNYPLTheme.ts +0 -91
- package/src/hooks/useWindowSize.stories.mdx +0 -23
- package/src/hooks/useWindowSize.ts +0 -40
- package/src/index.ts +0 -136
- package/src/resources.scss +0 -6
- package/src/styles/base/_01-breakpoints.scss +0 -27
- package/src/styles/base/_02-mixins.scss +0 -103
- package/src/styles/base/_place-holder.scss +0 -33
- package/src/styles/space/_space-inline.scss +0 -79
- package/src/styles/space/_space-inset.scss +0 -57
- package/src/styles/space/_space-stack.scss +0 -116
- package/src/styles.scss +0 -23
- package/src/theme/components/accordion.ts +0 -25
- package/src/theme/components/breadcrumb.ts +0 -94
- package/src/theme/components/button.ts +0 -132
- package/src/theme/components/buttonGroup.ts +0 -10
- package/src/theme/components/card.ts +0 -231
- package/src/theme/components/checkbox.ts +0 -110
- package/src/theme/components/checkboxGroup.ts +0 -10
- package/src/theme/components/componentWrapper.ts +0 -14
- package/src/theme/components/customTable.ts +0 -77
- package/src/theme/components/datePicker.ts +0 -17
- package/src/theme/components/fieldset.ts +0 -23
- package/src/theme/components/global.ts +0 -91
- package/src/theme/components/globalMixins.ts +0 -24
- package/src/theme/components/heading.ts +0 -79
- package/src/theme/components/helperErrorText.ts +0 -14
- package/src/theme/components/hero.ts +0 -238
- package/src/theme/components/horizontalRule.ts +0 -17
- package/src/theme/components/icon.ts +0 -88
- package/src/theme/components/image.ts +0 -135
- package/src/theme/components/label.ts +0 -15
- package/src/theme/components/link.ts +0 -63
- package/src/theme/components/list.ts +0 -88
- package/src/theme/components/logo.ts +0 -58
- package/src/theme/components/notification.ts +0 -132
- package/src/theme/components/pagination.ts +0 -17
- package/src/theme/components/progressIndicator.ts +0 -67
- package/src/theme/components/radio.ts +0 -103
- package/src/theme/components/radioGroup.ts +0 -10
- package/src/theme/components/searchBar.ts +0 -19
- package/src/theme/components/select.ts +0 -72
- package/src/theme/components/skeletonLoader.ts +0 -113
- package/src/theme/components/skipNavigation.ts +0 -26
- package/src/theme/components/slider.ts +0 -95
- package/src/theme/components/statusBadge.ts +0 -26
- package/src/theme/components/structuredContent.ts +0 -149
- package/src/theme/components/tabs.ts +0 -109
- package/src/theme/components/template.ts +0 -114
- package/src/theme/components/text.ts +0 -38
- package/src/theme/components/textInput.ts +0 -65
- package/src/theme/components/toggle.ts +0 -109
- package/src/theme/components/videoPlayer.ts +0 -47
- package/src/theme/foundations/breakpoints.ts +0 -24
- package/src/theme/foundations/colors.ts +0 -212
- package/src/theme/foundations/global.ts +0 -43
- package/src/theme/foundations/radii.ts +0 -7
- package/src/theme/foundations/shadows.ts +0 -5
- package/src/theme/foundations/spacing.ts +0 -136
- package/src/theme/foundations/typography.ts +0 -107
- package/src/theme/index.ts +0 -131
- package/src/theme/provider.tsx +0 -9
- package/src/theme/types.ts +0 -1
- package/src/utils/componentCategories.ts +0 -152
- package/src/utils/interfaces.ts +0 -5
- package/src/utils/utils.ts +0 -44
|
@@ -1,419 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
import { render, screen, within } from "@testing-library/react";
|
|
3
|
-
import { axe } from "jest-axe";
|
|
4
|
-
import userEvent from "@testing-library/user-event";
|
|
5
|
-
import renderer from "react-test-renderer";
|
|
6
|
-
|
|
7
|
-
import Pagination from "./Pagination";
|
|
8
|
-
|
|
9
|
-
describe("Pagination Accessibility", () => {
|
|
10
|
-
const getPageHref = (page: number) => `page=${page}`;
|
|
11
|
-
|
|
12
|
-
it("passes axe accessibility on the first page", async () => {
|
|
13
|
-
const { container } = render(
|
|
14
|
-
<Pagination pageCount={20} currentPage={1} getPageHref={getPageHref} />
|
|
15
|
-
);
|
|
16
|
-
expect(await axe(container)).toHaveNoViolations();
|
|
17
|
-
});
|
|
18
|
-
it("passes axe accessibility on a middle page", async () => {
|
|
19
|
-
const { container } = render(
|
|
20
|
-
<Pagination pageCount={20} currentPage={10} getPageHref={getPageHref} />
|
|
21
|
-
);
|
|
22
|
-
expect(await axe(container)).toHaveNoViolations();
|
|
23
|
-
});
|
|
24
|
-
it("passes axe accessibility on the last page", async () => {
|
|
25
|
-
const { container } = render(
|
|
26
|
-
<Pagination pageCount={20} currentPage={20} getPageHref={getPageHref} />
|
|
27
|
-
);
|
|
28
|
-
expect(await axe(container)).toHaveNoViolations();
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
describe("Pagination", () => {
|
|
33
|
-
const getPageHref = (page: number) => `page=${page}`;
|
|
34
|
-
|
|
35
|
-
describe("Rendering", () => {
|
|
36
|
-
it("renders a nav element with an unordered list and links", () => {
|
|
37
|
-
render(
|
|
38
|
-
<Pagination pageCount={5} initialPage={3} getPageHref={getPageHref} />
|
|
39
|
-
);
|
|
40
|
-
const nav = screen.getByRole("navigation");
|
|
41
|
-
|
|
42
|
-
expect(nav).toBeInTheDocument();
|
|
43
|
-
expect(nav).toHaveAttribute("aria-label", "Pagination");
|
|
44
|
-
// Chakra renders other lists so we only want the list we
|
|
45
|
-
// created within the nav element.
|
|
46
|
-
const { getByRole } = within(nav);
|
|
47
|
-
expect(getByRole("list")).toBeInTheDocument();
|
|
48
|
-
// Previous/Next links + truncated item list = 7 total items
|
|
49
|
-
expect(screen.getAllByRole("listitem")).toHaveLength(7);
|
|
50
|
-
// Pagination should show:
|
|
51
|
-
// Previous 1 2 3 4 5 Next
|
|
52
|
-
expect(screen.getAllByRole("link")).toHaveLength(7);
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it("does not render the Previous link on the first page", () => {
|
|
56
|
-
render(
|
|
57
|
-
<Pagination pageCount={5} initialPage={1} getPageHref={getPageHref} />
|
|
58
|
-
);
|
|
59
|
-
// Pagination should show:
|
|
60
|
-
// 1 2 3 4 5 Next
|
|
61
|
-
const links = screen.getAllByRole("link");
|
|
62
|
-
|
|
63
|
-
expect(links).toHaveLength(6);
|
|
64
|
-
expect(screen.queryByText("Previous")).not.toBeInTheDocument();
|
|
65
|
-
expect(screen.getByText("Next")).toBeInTheDocument();
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it("does not render the Next link on the last page", () => {
|
|
69
|
-
render(
|
|
70
|
-
<Pagination pageCount={5} initialPage={5} getPageHref={getPageHref} />
|
|
71
|
-
);
|
|
72
|
-
// Pagination should show:
|
|
73
|
-
// Previous 1 2 3 4 5
|
|
74
|
-
const links = screen.getAllByRole("link");
|
|
75
|
-
|
|
76
|
-
expect(links).toHaveLength(6);
|
|
77
|
-
expect(screen.getByText("Previous")).toBeInTheDocument();
|
|
78
|
-
expect(screen.queryByText("Next")).not.toBeInTheDocument();
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it("renders an ellipsis at the end of the list", () => {
|
|
82
|
-
render(
|
|
83
|
-
<Pagination pageCount={10} initialPage={1} getPageHref={getPageHref} />
|
|
84
|
-
);
|
|
85
|
-
// Pagination should show:
|
|
86
|
-
// 1 2 3 4 5 ... 10 Next
|
|
87
|
-
const listitem = screen.getAllByRole("listitem");
|
|
88
|
-
|
|
89
|
-
expect(listitem).toHaveLength(8);
|
|
90
|
-
// The ellipsis is not a link
|
|
91
|
-
expect(screen.getAllByRole("link")).toHaveLength(7);
|
|
92
|
-
expect(listitem[4].textContent).toEqual("5");
|
|
93
|
-
expect(listitem[5].textContent).toEqual("...");
|
|
94
|
-
expect(listitem[6].textContent).toEqual("10");
|
|
95
|
-
expect(screen.getByText("...")).toBeInTheDocument();
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
it("renders an ellipsis at the start of the list", () => {
|
|
99
|
-
render(
|
|
100
|
-
<Pagination pageCount={10} initialPage={10} getPageHref={getPageHref} />
|
|
101
|
-
);
|
|
102
|
-
// Pagination should show:
|
|
103
|
-
// Previous 1 ... 6 7 8 9 10
|
|
104
|
-
const listitem = screen.getAllByRole("listitem");
|
|
105
|
-
|
|
106
|
-
expect(listitem).toHaveLength(8);
|
|
107
|
-
// The ellipsis is not a link
|
|
108
|
-
expect(screen.getAllByRole("link")).toHaveLength(7);
|
|
109
|
-
expect(listitem[1].textContent).toEqual("1");
|
|
110
|
-
expect(listitem[2].textContent).toEqual("...");
|
|
111
|
-
expect(listitem[3].textContent).toEqual("6");
|
|
112
|
-
expect(screen.getByText("...")).toBeInTheDocument();
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it("renders two ellipsis when the current page is in the middle", () => {
|
|
116
|
-
render(
|
|
117
|
-
<Pagination pageCount={10} initialPage={5} getPageHref={getPageHref} />
|
|
118
|
-
);
|
|
119
|
-
// Pagination should show:
|
|
120
|
-
// Previous 1 ... 4 5 6 ... 10 Next
|
|
121
|
-
const listitem = screen.getAllByRole("listitem");
|
|
122
|
-
|
|
123
|
-
expect(listitem).toHaveLength(9);
|
|
124
|
-
// The ellipses are not links
|
|
125
|
-
expect(screen.getAllByRole("link")).toHaveLength(7);
|
|
126
|
-
expect(listitem[0].textContent).toEqual("Previous");
|
|
127
|
-
expect(listitem[1].textContent).toEqual("1");
|
|
128
|
-
expect(listitem[2].textContent).toEqual("...");
|
|
129
|
-
expect(listitem[3].textContent).toEqual("4");
|
|
130
|
-
expect(listitem[4].textContent).toEqual("5");
|
|
131
|
-
expect(listitem[5].textContent).toEqual("6");
|
|
132
|
-
expect(listitem[6].textContent).toEqual("...");
|
|
133
|
-
expect(listitem[7].textContent).toEqual("10");
|
|
134
|
-
expect(listitem[8].textContent).toEqual("Next");
|
|
135
|
-
expect(screen.getAllByText("...")).toHaveLength(2);
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
it("adds aria-current to the active page", () => {
|
|
139
|
-
render(
|
|
140
|
-
<Pagination pageCount={5} initialPage={3} getPageHref={getPageHref} />
|
|
141
|
-
);
|
|
142
|
-
// Pagination should show:
|
|
143
|
-
// Previous 1 2 3 4 5 Next
|
|
144
|
-
const links = screen.getAllByRole("link");
|
|
145
|
-
const page2 = links[2].getAttribute("aria-current");
|
|
146
|
-
const page3 = links[3].getAttribute("aria-current");
|
|
147
|
-
const page4 = links[4].getAttribute("aria-current");
|
|
148
|
-
|
|
149
|
-
expect(page2).toEqual(null);
|
|
150
|
-
// Only the current page has `aria-current="page"` for accessibility.
|
|
151
|
-
expect(page3).toEqual("page");
|
|
152
|
-
expect(page4).toEqual(null);
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
it("When pagination has 1 element, pagination is not shown", () => {
|
|
156
|
-
render(
|
|
157
|
-
<Pagination pageCount={1} initialPage={1} getPageHref={getPageHref} />
|
|
158
|
-
);
|
|
159
|
-
expect(screen.queryByRole("navigation")).not.toBeInTheDocument();
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
it("When pagination has 0 elements, pagination is not shown", () => {
|
|
163
|
-
render(
|
|
164
|
-
<Pagination pageCount={0} initialPage={1} getPageHref={getPageHref} />
|
|
165
|
-
);
|
|
166
|
-
expect(screen.queryByRole("navigation")).not.toBeInTheDocument();
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
it("renders the UI snapshot correctly", () => {
|
|
170
|
-
const firstPage = renderer
|
|
171
|
-
.create(
|
|
172
|
-
<Pagination
|
|
173
|
-
id="firstPage"
|
|
174
|
-
pageCount={10}
|
|
175
|
-
initialPage={1}
|
|
176
|
-
getPageHref={getPageHref}
|
|
177
|
-
/>
|
|
178
|
-
)
|
|
179
|
-
.toJSON();
|
|
180
|
-
const lastPage = renderer
|
|
181
|
-
.create(
|
|
182
|
-
<Pagination
|
|
183
|
-
id="lastPage"
|
|
184
|
-
pageCount={10}
|
|
185
|
-
initialPage={10}
|
|
186
|
-
getPageHref={getPageHref}
|
|
187
|
-
/>
|
|
188
|
-
)
|
|
189
|
-
.toJSON();
|
|
190
|
-
const middlePage = renderer
|
|
191
|
-
.create(
|
|
192
|
-
<Pagination
|
|
193
|
-
id="middlePage"
|
|
194
|
-
pageCount={10}
|
|
195
|
-
initialPage={5}
|
|
196
|
-
getPageHref={getPageHref}
|
|
197
|
-
/>
|
|
198
|
-
)
|
|
199
|
-
.toJSON();
|
|
200
|
-
const withChakraProps = renderer
|
|
201
|
-
.create(
|
|
202
|
-
<Pagination
|
|
203
|
-
id="chakra"
|
|
204
|
-
pageCount={10}
|
|
205
|
-
initialPage={1}
|
|
206
|
-
getPageHref={getPageHref}
|
|
207
|
-
p="20px"
|
|
208
|
-
color="ui.error.primary"
|
|
209
|
-
/>
|
|
210
|
-
)
|
|
211
|
-
.toJSON();
|
|
212
|
-
const withOtherProps = renderer
|
|
213
|
-
.create(
|
|
214
|
-
<Pagination
|
|
215
|
-
id="props"
|
|
216
|
-
pageCount={10}
|
|
217
|
-
initialPage={1}
|
|
218
|
-
getPageHref={getPageHref}
|
|
219
|
-
data-testid="props"
|
|
220
|
-
/>
|
|
221
|
-
)
|
|
222
|
-
.toJSON();
|
|
223
|
-
|
|
224
|
-
expect(firstPage).toMatchSnapshot();
|
|
225
|
-
expect(lastPage).toMatchSnapshot();
|
|
226
|
-
expect(middlePage).toMatchSnapshot();
|
|
227
|
-
expect(withChakraProps).toMatchSnapshot();
|
|
228
|
-
expect(withOtherProps).toMatchSnapshot();
|
|
229
|
-
});
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
describe("Behavior", () => {
|
|
233
|
-
it("navigates to the appropriate page when the Next or Previous links are clicked", () => {
|
|
234
|
-
const onPageChange = (page: number) => (currentPage = page);
|
|
235
|
-
let currentPage = 3;
|
|
236
|
-
|
|
237
|
-
render(
|
|
238
|
-
<Pagination
|
|
239
|
-
pageCount={5}
|
|
240
|
-
initialPage={currentPage}
|
|
241
|
-
onPageChange={onPageChange}
|
|
242
|
-
/>
|
|
243
|
-
);
|
|
244
|
-
|
|
245
|
-
let links = screen.getAllByRole("link");
|
|
246
|
-
|
|
247
|
-
// Previous link
|
|
248
|
-
userEvent.click(links[0]);
|
|
249
|
-
expect(currentPage).toEqual(2);
|
|
250
|
-
|
|
251
|
-
links = screen.getAllByRole("link");
|
|
252
|
-
|
|
253
|
-
// Next link
|
|
254
|
-
userEvent.click(links[links.length - 1]);
|
|
255
|
-
expect(currentPage).toEqual(3);
|
|
256
|
-
});
|
|
257
|
-
|
|
258
|
-
it("updates the links href value when getPageHref is used", () => {
|
|
259
|
-
const getPageHref = (page: number) => `?page=${page}`;
|
|
260
|
-
render(
|
|
261
|
-
<Pagination pageCount={10} initialPage={5} getPageHref={getPageHref} />
|
|
262
|
-
);
|
|
263
|
-
// Pagination should show:
|
|
264
|
-
// Previous 1 ... 4 5 6 ... 10 Next
|
|
265
|
-
const links = screen.getAllByRole("link");
|
|
266
|
-
|
|
267
|
-
expect(screen.getByText("Previous")).toBeInTheDocument();
|
|
268
|
-
expect(screen.getByText("Next")).toBeInTheDocument();
|
|
269
|
-
|
|
270
|
-
// Previous link
|
|
271
|
-
expect(links[0].getAttribute("href")).toEqual("?page=4");
|
|
272
|
-
expect(links[1].getAttribute("href")).toEqual("?page=1");
|
|
273
|
-
expect(links[2].getAttribute("href")).toEqual("?page=4");
|
|
274
|
-
expect(links[3].getAttribute("href")).toEqual("?page=5");
|
|
275
|
-
expect(links[4].getAttribute("href")).toEqual("?page=6");
|
|
276
|
-
expect(links[5].getAttribute("href")).toEqual("?page=10");
|
|
277
|
-
// Next link
|
|
278
|
-
expect(links[6].getAttribute("href")).toEqual("?page=6");
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
// In this scenario, we need to update the current page ourselves
|
|
282
|
-
// since we stay on the same page.
|
|
283
|
-
it("when page item is selected, runs the onPageChange callback", () => {
|
|
284
|
-
const onPageChange = (page: number) => (currentPage = page);
|
|
285
|
-
let currentPage = 5;
|
|
286
|
-
const { rerender } = render(
|
|
287
|
-
<Pagination
|
|
288
|
-
pageCount={10}
|
|
289
|
-
initialPage={currentPage}
|
|
290
|
-
onPageChange={onPageChange}
|
|
291
|
-
/>
|
|
292
|
-
);
|
|
293
|
-
// Pagination should show:
|
|
294
|
-
// Previous 1 ... 4 5 6 ... 10 Next
|
|
295
|
-
let links = screen.getAllByRole("link");
|
|
296
|
-
|
|
297
|
-
// Links all have "#" for their href value.
|
|
298
|
-
// Previous link
|
|
299
|
-
expect(links[0].getAttribute("href")).toEqual("#");
|
|
300
|
-
expect(links[1].getAttribute("href")).toEqual("#");
|
|
301
|
-
expect(links[2].getAttribute("href")).toEqual("#");
|
|
302
|
-
expect(links[3].getAttribute("href")).toEqual("#");
|
|
303
|
-
expect(links[4].getAttribute("href")).toEqual("#");
|
|
304
|
-
expect(links[5].getAttribute("href")).toEqual("#");
|
|
305
|
-
// Next link
|
|
306
|
-
expect(links[6].getAttribute("href")).toEqual("#");
|
|
307
|
-
|
|
308
|
-
// Page 4
|
|
309
|
-
userEvent.click(links[2]);
|
|
310
|
-
expect(currentPage).toEqual(4);
|
|
311
|
-
|
|
312
|
-
rerender(
|
|
313
|
-
<Pagination
|
|
314
|
-
pageCount={10}
|
|
315
|
-
initialPage={currentPage}
|
|
316
|
-
onPageChange={onPageChange}
|
|
317
|
-
/>
|
|
318
|
-
);
|
|
319
|
-
// Pagination should show:
|
|
320
|
-
// Previous 1 2 3 4 5 ... 10 Next
|
|
321
|
-
links = screen.getAllByRole("link");
|
|
322
|
-
|
|
323
|
-
// Previous link
|
|
324
|
-
userEvent.click(links[0]);
|
|
325
|
-
expect(currentPage).toEqual(3);
|
|
326
|
-
|
|
327
|
-
rerender(
|
|
328
|
-
<Pagination
|
|
329
|
-
pageCount={10}
|
|
330
|
-
initialPage={currentPage}
|
|
331
|
-
onPageChange={onPageChange}
|
|
332
|
-
/>
|
|
333
|
-
);
|
|
334
|
-
// Pagination should show:
|
|
335
|
-
// Previous 1 2 3 4 5 ... 10 Next
|
|
336
|
-
links = screen.getAllByRole("link");
|
|
337
|
-
|
|
338
|
-
// Page 10
|
|
339
|
-
userEvent.click(links[6]);
|
|
340
|
-
expect(currentPage).toEqual(10);
|
|
341
|
-
|
|
342
|
-
rerender(
|
|
343
|
-
<Pagination
|
|
344
|
-
pageCount={10}
|
|
345
|
-
initialPage={currentPage}
|
|
346
|
-
onPageChange={onPageChange}
|
|
347
|
-
/>
|
|
348
|
-
);
|
|
349
|
-
// Pagination should show:
|
|
350
|
-
// Previous 1 ... 6 7 8 9 10
|
|
351
|
-
links = screen.getAllByRole("link");
|
|
352
|
-
|
|
353
|
-
// Page 6
|
|
354
|
-
userEvent.click(links[2]);
|
|
355
|
-
expect(currentPage).toEqual(6);
|
|
356
|
-
});
|
|
357
|
-
|
|
358
|
-
it("uses the currentPage prop to update the selected page", () => {
|
|
359
|
-
const onPageChange = (page: number) => console.log(page);
|
|
360
|
-
const { rerender } = render(
|
|
361
|
-
<Pagination currentPage={1} onPageChange={onPageChange} pageCount={5} />
|
|
362
|
-
);
|
|
363
|
-
|
|
364
|
-
let links = screen.getAllByRole("link");
|
|
365
|
-
let page1 = links[0].getAttribute("aria-current");
|
|
366
|
-
let page2 = links[1].getAttribute("aria-current");
|
|
367
|
-
let page3 = links[2].getAttribute("aria-current");
|
|
368
|
-
|
|
369
|
-
// Only the current page has `aria-current="page"` for accessibility.
|
|
370
|
-
expect(page1).toEqual("page");
|
|
371
|
-
expect(page2).toEqual(null);
|
|
372
|
-
expect(page3).toEqual(null);
|
|
373
|
-
|
|
374
|
-
rerender(
|
|
375
|
-
<Pagination currentPage={3} onPageChange={onPageChange} pageCount={5} />
|
|
376
|
-
);
|
|
377
|
-
|
|
378
|
-
links = screen.getAllByRole("link");
|
|
379
|
-
// links[0] is now "Previous"
|
|
380
|
-
page1 = links[1].getAttribute("aria-current");
|
|
381
|
-
page2 = links[2].getAttribute("aria-current");
|
|
382
|
-
page3 = links[3].getAttribute("aria-current");
|
|
383
|
-
|
|
384
|
-
expect(page1).toEqual(null);
|
|
385
|
-
expect(page2).toEqual(null);
|
|
386
|
-
expect(page3).toEqual("page");
|
|
387
|
-
});
|
|
388
|
-
|
|
389
|
-
it("logs a warning if both `getPageHref` and `onPageChange` props are both passed", () => {
|
|
390
|
-
const getPageHref = (page: number) => `page=${page}`;
|
|
391
|
-
const onPageChange = (page: number) => console.log(page);
|
|
392
|
-
const warn = jest.spyOn(console, "warn");
|
|
393
|
-
render(
|
|
394
|
-
<Pagination
|
|
395
|
-
pageCount={10}
|
|
396
|
-
onPageChange={onPageChange}
|
|
397
|
-
getPageHref={getPageHref}
|
|
398
|
-
/>
|
|
399
|
-
);
|
|
400
|
-
expect(warn).toHaveBeenCalledWith(
|
|
401
|
-
"NYPL Reservoir Pagination: Props for both `getPageHref` and " +
|
|
402
|
-
"`onPageChange` are passed. The component will default to using " +
|
|
403
|
-
"`getPageHref`."
|
|
404
|
-
);
|
|
405
|
-
});
|
|
406
|
-
|
|
407
|
-
it("logs a warning if both `getPageHref` and `currentPage` props are both passed", () => {
|
|
408
|
-
const getPageHref = (page: number) => `page=${page}`;
|
|
409
|
-
const warn = jest.spyOn(console, "warn");
|
|
410
|
-
render(
|
|
411
|
-
<Pagination pageCount={10} currentPage={2} getPageHref={getPageHref} />
|
|
412
|
-
);
|
|
413
|
-
expect(warn).toHaveBeenCalledWith(
|
|
414
|
-
"NYPL Reservoir Pagination: The `currentPage` prop does not work with " +
|
|
415
|
-
"the `getPageHref` prop. Use `currentPage` with `onPageChange` instead."
|
|
416
|
-
);
|
|
417
|
-
});
|
|
418
|
-
});
|
|
419
|
-
});
|
|
@@ -1,269 +0,0 @@
|
|
|
1
|
-
import { Box, chakra, useMultiStyleConfig } from "@chakra-ui/react";
|
|
2
|
-
import React, { useState, useRef } from "react";
|
|
3
|
-
|
|
4
|
-
import Link from "../Link/Link";
|
|
5
|
-
import List from "../List/List";
|
|
6
|
-
import { range } from "../../utils/utils";
|
|
7
|
-
|
|
8
|
-
export interface PaginationProps {
|
|
9
|
-
/** Additional className. */
|
|
10
|
-
className?: string;
|
|
11
|
-
/** The currentPage can be used to programatically force the selected page to change
|
|
12
|
-
* without the user explicitly requesting it – for example, if the user should be
|
|
13
|
-
* brought back to the first page of a set of results after a new search. */
|
|
14
|
-
currentPage?: number;
|
|
15
|
-
/** The callback function that takes a page number and returns a string
|
|
16
|
-
* to use for a link's `href` attribute. This is used when the current
|
|
17
|
-
* page should refresh when navigating. */
|
|
18
|
-
getPageHref?: undefined | ((pageNumber: number) => string);
|
|
19
|
-
/** ID that other components can cross reference for accessibility purposes. */
|
|
20
|
-
id?: string;
|
|
21
|
-
/** The initially selected page (default value is 1). */
|
|
22
|
-
initialPage?: number;
|
|
23
|
-
/** The callback function called when an item is selected and the current
|
|
24
|
-
* page should not refresh. */
|
|
25
|
-
onPageChange?: (selected: number) => void;
|
|
26
|
-
/** The total number of pages. */
|
|
27
|
-
pageCount: number;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* A component that provides a navigational list of page items.
|
|
32
|
-
*/
|
|
33
|
-
export const Pagination = chakra((props: PaginationProps) => {
|
|
34
|
-
const {
|
|
35
|
-
className,
|
|
36
|
-
currentPage,
|
|
37
|
-
getPageHref,
|
|
38
|
-
id,
|
|
39
|
-
initialPage = 1,
|
|
40
|
-
onPageChange,
|
|
41
|
-
pageCount,
|
|
42
|
-
...rest
|
|
43
|
-
} = props;
|
|
44
|
-
const refCurrentPage = useRef(currentPage);
|
|
45
|
-
const [selectedPage, setSelectedPage] = useState<number>(initialPage);
|
|
46
|
-
const styles = useMultiStyleConfig("Pagination", {});
|
|
47
|
-
const previousPageNumber = selectedPage - 1;
|
|
48
|
-
const nextPageNumber = selectedPage + 1;
|
|
49
|
-
|
|
50
|
-
// If the parent passes down a new currentPage, and an onPageChange
|
|
51
|
-
// function exists, then set the internal state – selectedPage –
|
|
52
|
-
// to the new currentPage and update the refCurrentPage with that value.
|
|
53
|
-
React.useEffect(() => {
|
|
54
|
-
if (onPageChange && currentPage && currentPage !== refCurrentPage.current) {
|
|
55
|
-
setSelectedPage(currentPage);
|
|
56
|
-
refCurrentPage.current = currentPage;
|
|
57
|
-
}
|
|
58
|
-
}, [currentPage, onPageChange]);
|
|
59
|
-
|
|
60
|
-
// If there are 0 or 1 page, the pagination should not show.
|
|
61
|
-
if (pageCount <= 1) {
|
|
62
|
-
return null;
|
|
63
|
-
}
|
|
64
|
-
if (getPageHref && onPageChange) {
|
|
65
|
-
console.warn(
|
|
66
|
-
"NYPL Reservoir Pagination: Props for both `getPageHref` and " +
|
|
67
|
-
"`onPageChange` are passed. The component will default to using " +
|
|
68
|
-
"`getPageHref`."
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (getPageHref && currentPage) {
|
|
73
|
-
console.warn(
|
|
74
|
-
"NYPL Reservoir Pagination: The `currentPage` prop does not work with " +
|
|
75
|
-
"the `getPageHref` prop. Use `currentPage` with `onPageChange` instead."
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// If `getPageHref` was passed, then links should go to a new page.
|
|
80
|
-
const changeUrls =
|
|
81
|
-
typeof getPageHref !== "undefined" && typeof getPageHref === "function";
|
|
82
|
-
/**
|
|
83
|
-
* This function is only called when clicking on a link should
|
|
84
|
-
* not update the URL or refresh the page.
|
|
85
|
-
*/
|
|
86
|
-
const handlePageClick = (e: Event, clickedPage: number) => {
|
|
87
|
-
e.preventDefault && e.preventDefault();
|
|
88
|
-
if (selectedPage === clickedPage) return;
|
|
89
|
-
setSelectedPage(clickedPage);
|
|
90
|
-
onPageChange && onPageChange(clickedPage);
|
|
91
|
-
};
|
|
92
|
-
// Select the previous page.
|
|
93
|
-
const previousPage = (e: Event) => {
|
|
94
|
-
if (selectedPage > 1) {
|
|
95
|
-
handlePageClick(e, previousPageNumber);
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
// Select the next page.
|
|
99
|
-
const nextPage = (e: Event) => {
|
|
100
|
-
if (selectedPage < pageCount) {
|
|
101
|
-
handlePageClick(e, nextPageNumber);
|
|
102
|
-
}
|
|
103
|
-
};
|
|
104
|
-
/**
|
|
105
|
-
* All `Link` components have similar attributes but we need to differentiate
|
|
106
|
-
* between the "previous", "next", and regular number links.
|
|
107
|
-
* 1. If `getPageHref` is passed, this means that the page refreshes and the
|
|
108
|
-
* URL changes. In this case, the parent component returns the `href` URL
|
|
109
|
-
* and the `onClick` callback is undefined.
|
|
110
|
-
* 2. Otherwise, we stay on the same page by setting the `href` attribute to
|
|
111
|
-
* "#" and call the `onPageChange` prop through the `onClick` callback.
|
|
112
|
-
*/
|
|
113
|
-
const getLinkElement = (
|
|
114
|
-
type: "items" | "previous" | "next",
|
|
115
|
-
item?: number
|
|
116
|
-
) => {
|
|
117
|
-
const isSelectedPage = selectedPage === item;
|
|
118
|
-
// The current page link has different styles.
|
|
119
|
-
const currentStyles = isSelectedPage
|
|
120
|
-
? {
|
|
121
|
-
color: "ui.black",
|
|
122
|
-
pointerEvent: "none",
|
|
123
|
-
}
|
|
124
|
-
: {};
|
|
125
|
-
const allAttrs = {
|
|
126
|
-
items: {
|
|
127
|
-
href: changeUrls ? getPageHref(item as number) : "#",
|
|
128
|
-
attributes: {
|
|
129
|
-
"aria-label": `Page ${item}`,
|
|
130
|
-
"aria-current": isSelectedPage ? "page" : null,
|
|
131
|
-
onClick: changeUrls
|
|
132
|
-
? undefined
|
|
133
|
-
: (e: Event) => handlePageClick(e, item as number),
|
|
134
|
-
},
|
|
135
|
-
text: item,
|
|
136
|
-
},
|
|
137
|
-
previous: {
|
|
138
|
-
href: changeUrls ? getPageHref(previousPageNumber) : "#",
|
|
139
|
-
attributes: {
|
|
140
|
-
"aria-label": "Previous page",
|
|
141
|
-
onClick: changeUrls ? undefined : previousPage,
|
|
142
|
-
},
|
|
143
|
-
text: "Previous",
|
|
144
|
-
},
|
|
145
|
-
next: {
|
|
146
|
-
href: changeUrls ? getPageHref(nextPageNumber) : "#",
|
|
147
|
-
attributes: {
|
|
148
|
-
"aria-label": "Next page",
|
|
149
|
-
onClick: changeUrls ? undefined : nextPage,
|
|
150
|
-
},
|
|
151
|
-
text: "Next",
|
|
152
|
-
},
|
|
153
|
-
};
|
|
154
|
-
const linkAttrs = allAttrs[type];
|
|
155
|
-
return (
|
|
156
|
-
<Link
|
|
157
|
-
href={linkAttrs.href}
|
|
158
|
-
id={`${id}-${linkAttrs.text}`}
|
|
159
|
-
{...linkAttrs.attributes}
|
|
160
|
-
__css={{
|
|
161
|
-
...styles.link,
|
|
162
|
-
...currentStyles,
|
|
163
|
-
}}
|
|
164
|
-
>
|
|
165
|
-
{linkAttrs.text}
|
|
166
|
-
</Link>
|
|
167
|
-
);
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* This function returns an array of `li` elements with numbers based on the
|
|
172
|
-
* total pages that `Pagination` should display. When there are many pages to
|
|
173
|
-
* display, ellipsis are shown to signify that the intermediate page numbers
|
|
174
|
-
* are hidden.
|
|
175
|
-
* For a small number of pages, the array will be simple.
|
|
176
|
-
* For a large number of pages, the resulting array will be in the shape of:
|
|
177
|
-
* [
|
|
178
|
-
* 1, this always displays
|
|
179
|
-
* 2 or an ellipsis,
|
|
180
|
-
* a range of numbers in the middle of the total count,
|
|
181
|
-
* pageCount - 1 or an ellipsis,
|
|
182
|
-
* pageCount, the total number of pages
|
|
183
|
-
* ]
|
|
184
|
-
*/
|
|
185
|
-
const getPaginationNumbers = (selected: number) => {
|
|
186
|
-
// Where should the middle range of numbers start at?
|
|
187
|
-
const middleRangeStart = Math.max(
|
|
188
|
-
// Start at three if the current page is near the beginning.
|
|
189
|
-
3,
|
|
190
|
-
Math.min(
|
|
191
|
-
// If the current page is in the middle, start the range
|
|
192
|
-
// one number before the current page.
|
|
193
|
-
selected - 1,
|
|
194
|
-
// If the current page is near the end, show the last five items.
|
|
195
|
-
pageCount - 4
|
|
196
|
-
)
|
|
197
|
-
);
|
|
198
|
-
// Where should the middle range of numbers end at?
|
|
199
|
-
const middleRangeEnd = Math.min(
|
|
200
|
-
// If the current page is near the end, end the range
|
|
201
|
-
// two place before the last page number.
|
|
202
|
-
pageCount - 2,
|
|
203
|
-
Math.max(
|
|
204
|
-
// If the current page is in the middle, end the range
|
|
205
|
-
// one number after the current page.
|
|
206
|
-
selected + 1,
|
|
207
|
-
// If the current page is near the end, show the first five items.
|
|
208
|
-
5
|
|
209
|
-
)
|
|
210
|
-
);
|
|
211
|
-
const itemList =
|
|
212
|
-
pageCount < 4
|
|
213
|
-
? // Get a short array with 2 or 3 items: [1, 2] or [1, 2, 3]
|
|
214
|
-
Array.from({ length: pageCount }, (_, i) => i + 1)
|
|
215
|
-
: // Otherwise, create the longer array of items.
|
|
216
|
-
[
|
|
217
|
-
// Always display the first page.
|
|
218
|
-
1,
|
|
219
|
-
// The second item will be 2 or an ellipse.
|
|
220
|
-
middleRangeStart > 3 ? "ellipse-start" : 2,
|
|
221
|
-
// The middle range of page numbers to display.
|
|
222
|
-
// Add +1 to the end since range() doesn't include the last number.
|
|
223
|
-
...range(middleRangeStart, middleRangeEnd + 1),
|
|
224
|
-
// The next to last item will be the next to last
|
|
225
|
-
// number or an ellipse.
|
|
226
|
-
middleRangeEnd < pageCount - 2 ? "ellipse-end" : pageCount - 1,
|
|
227
|
-
// Always display the last page.
|
|
228
|
-
pageCount,
|
|
229
|
-
];
|
|
230
|
-
// If it's a number, render an `li` element with a link page item,
|
|
231
|
-
// otherwise return the `li` with the ellipse for a break.
|
|
232
|
-
const pageLiItems = itemList.map((item) => {
|
|
233
|
-
const itemElement =
|
|
234
|
-
typeof item === "number" ? getLinkElement("items", item) : "...";
|
|
235
|
-
return <li key={item}>{itemElement}</li>;
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
return pageLiItems;
|
|
239
|
-
};
|
|
240
|
-
|
|
241
|
-
// Don't display the previous link when you're on the first page.
|
|
242
|
-
const previousLiLink = selectedPage !== 1 && (
|
|
243
|
-
<li key="previous">{getLinkElement("previous")}</li>
|
|
244
|
-
);
|
|
245
|
-
// Don't display the next link when you're on the last page.
|
|
246
|
-
const nextLiLink = selectedPage !== pageCount && (
|
|
247
|
-
<li key="next">{getLinkElement("next")}</li>
|
|
248
|
-
);
|
|
249
|
-
|
|
250
|
-
return (
|
|
251
|
-
<Box
|
|
252
|
-
as="nav"
|
|
253
|
-
id={id}
|
|
254
|
-
aria-label="Pagination"
|
|
255
|
-
role="navigation"
|
|
256
|
-
className={className}
|
|
257
|
-
__css={styles}
|
|
258
|
-
{...rest}
|
|
259
|
-
>
|
|
260
|
-
<List type="ul" inline noStyling id={`${id}-list`}>
|
|
261
|
-
{previousLiLink}
|
|
262
|
-
{getPaginationNumbers(selectedPage)}
|
|
263
|
-
{nextLiLink}
|
|
264
|
-
</List>
|
|
265
|
-
</Box>
|
|
266
|
-
);
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
export default Pagination;
|