@dhis2-ui/pagination 8.0.1 → 8.1.1

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.
@@ -1,7 +1,7 @@
1
1
  import _JSXStyle from "styled-jsx/style";
2
2
  import { colors, spacers } from '@dhis2/ui-constants';
3
3
  import PropTypes from 'prop-types';
4
- import React from 'react'; // TODO: i18n translate
4
+ import React from 'react';
5
5
 
6
6
  const translate = (prop, interpolationObject) => {
7
7
  if (typeof prop === 'function') {
@@ -11,47 +11,20 @@ const translate = (prop, interpolationObject) => {
11
11
  return prop;
12
12
  };
13
13
 
14
- const getItemRange = (page, pageSize, total) => {
15
- let firstItem, lastItem;
16
-
17
- if (total === 0) {
18
- // if no items are found, the pager total is 0
19
- // and we want to force the first and last item to be 0 too
20
- firstItem = 0;
21
- lastItem = 0;
22
- } else {
23
- // page is 1-based
24
- firstItem = (page - 1) * pageSize + 1;
25
- lastItem = firstItem + pageSize - 1;
26
- }
27
-
28
- if (lastItem > total) {
29
- lastItem = total;
30
- }
31
-
32
- return {
33
- firstItem,
34
- lastItem
35
- };
36
- };
37
-
38
14
  const PageSummary = ({
39
15
  dataTest,
16
+ firstItem,
17
+ lastItem,
40
18
  page,
41
19
  pageCount,
42
- pageSize,
43
20
  pageSummaryText,
44
21
  total
45
22
  }) => {
46
- const {
47
- firstItem,
48
- lastItem
49
- } = getItemRange(page, pageSize, total);
50
23
  const summary = translate(pageSummaryText, {
51
- page,
52
- pageCount,
53
24
  firstItem,
54
25
  lastItem,
26
+ page,
27
+ pageCount,
55
28
  total
56
29
  });
57
30
  return /*#__PURE__*/React.createElement("div", {
@@ -68,9 +41,10 @@ const PageSummary = ({
68
41
  PageSummary.propTypes = {
69
42
  dataTest: PropTypes.string.isRequired,
70
43
  page: PropTypes.number.isRequired,
71
- pageCount: PropTypes.number.isRequired,
72
- pageSize: PropTypes.number.isRequired,
73
44
  pageSummaryText: PropTypes.oneOfType([PropTypes.string, PropTypes.func]).isRequired,
74
- total: PropTypes.number.isRequired
45
+ firstItem: PropTypes.number,
46
+ lastItem: PropTypes.number,
47
+ pageCount: PropTypes.number,
48
+ total: PropTypes.number
75
49
  };
76
- export { PageSummary, getItemRange };
50
+ export { PageSummary };
@@ -1,7 +1,10 @@
1
1
  import _JSXStyle from "styled-jsx/style";
2
+ import { requiredIf } from '@dhis2/prop-types';
2
3
  import cx from 'classnames';
3
4
  import PropTypes from 'prop-types';
4
5
  import React from 'react';
6
+ import { getDefaultPageSummaryText } from './get-default-page-summary-text.js';
7
+ import { getItemRange } from './get-item-range.js';
5
8
  import i18n from './locales/index.js';
6
9
  import { PageControls } from './page-controls.js';
7
10
  import { PageSelect } from './page-select.js';
@@ -11,22 +14,34 @@ import { PageSummary } from './page-summary.js';
11
14
  const Pagination = ({
12
15
  className,
13
16
  dataTest,
14
- hidePageSizeSelect,
15
17
  hidePageSelect,
18
+ hidePageSizeSelect,
16
19
  hidePageSummary,
20
+ isLastPage,
21
+ nextPageText,
22
+ onPageChange,
23
+ onPageSizeChange,
17
24
  page,
18
25
  pageCount,
26
+ pageLength,
27
+ pageSelectText,
19
28
  pageSize,
20
- total,
21
29
  pageSizes,
22
- onPageChange,
23
- onPageSizeChange,
24
- nextPageText,
25
- pageSelectText,
26
30
  pageSizeSelectText,
27
31
  pageSummaryText,
28
- previousPageText
32
+ previousPageText,
33
+ total
29
34
  }) => {
35
+ const {
36
+ firstItem,
37
+ lastItem
38
+ } = getItemRange({
39
+ isLastPage,
40
+ page,
41
+ pageLength,
42
+ pageSize,
43
+ total
44
+ });
30
45
  return /*#__PURE__*/React.createElement("div", {
31
46
  "data-test": dataTest,
32
47
  className: "jsx-3647884394" + " " + (cx('container', className) || "")
@@ -40,14 +55,15 @@ const Pagination = ({
40
55
  pageSizeSelectText: pageSizeSelectText
41
56
  }), !hidePageSummary && /*#__PURE__*/React.createElement(PageSummary, {
42
57
  dataTest: dataTest,
58
+ firstItem: firstItem,
59
+ lastItem: lastItem,
43
60
  page: page,
44
61
  pageCount: pageCount,
45
- pageSize: pageSize,
46
- total: total,
47
- pageSummaryText: pageSummaryText
62
+ pageSummaryText: pageSummaryText,
63
+ total: total
48
64
  }), /*#__PURE__*/React.createElement("div", {
49
65
  className: "jsx-3647884394" + " " + "page-navigation"
50
- }, !hidePageSelect && /*#__PURE__*/React.createElement(PageSelect, {
66
+ }, !hidePageSelect && total && /*#__PURE__*/React.createElement(PageSelect, {
51
67
  dataTest: dataTest,
52
68
  pageSelectText: pageSelectText,
53
69
  page: page,
@@ -55,9 +71,9 @@ const Pagination = ({
55
71
  onChange: onPageChange
56
72
  }), /*#__PURE__*/React.createElement(PageControls, {
57
73
  dataTest: dataTest,
74
+ isLastPage: isLastPage || page === pageCount,
58
75
  nextPageText: nextPageText,
59
76
  page: page,
60
- pageCount: pageCount,
61
77
  previousPageText: previousPageText,
62
78
  onClick: onPageChange
63
79
  })), /*#__PURE__*/React.createElement(_JSXStyle, {
@@ -71,25 +87,27 @@ Pagination.defaultProps = {
71
87
  nextPageText: () => i18n.t('Next'),
72
88
  pageSelectText: () => i18n.t('Page'),
73
89
  pageSizeSelectText: () => i18n.t('Items per page'),
74
- pageSummaryText: interpolationObject => i18n.t('Page {{page}} of {{pageCount}}, items {{firstItem}}-{{lastItem}} of {{total}}', interpolationObject),
90
+ pageSummaryText: getDefaultPageSummaryText,
75
91
  previousPageText: () => i18n.t('Previous')
76
92
  };
77
93
  Pagination.propTypes = {
78
94
  page: PropTypes.number.isRequired,
79
- pageCount: PropTypes.number.isRequired,
80
95
  pageSize: PropTypes.number.isRequired,
81
- total: PropTypes.number.isRequired,
82
96
  className: PropTypes.string,
83
97
  dataTest: PropTypes.string,
84
98
  hidePageSelect: PropTypes.bool,
85
99
  hidePageSizeSelect: PropTypes.bool,
86
100
  hidePageSummary: PropTypes.bool,
101
+ isLastPage: PropTypes.bool,
87
102
  nextPageText: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
103
+ pageCount: PropTypes.number,
104
+ pageLength: requiredIf(props => props.isLastPage, PropTypes.number),
88
105
  pageSelectText: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
89
106
  pageSizeSelectText: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
90
107
  pageSizes: PropTypes.arrayOf(PropTypes.string),
91
108
  pageSummaryText: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
92
109
  previousPageText: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
110
+ total: PropTypes.number,
93
111
  onPageChange: PropTypes.func,
94
112
  onPageSizeChange: PropTypes.func
95
113
  };
@@ -35,40 +35,51 @@ export default {
35
35
  }
36
36
  },
37
37
  // Default args for stories
38
- args: { // Fixes 'defaultProps' errors for storybook
39
- ...Pagination.defaultProps,
38
+ args: {
40
39
  onPageChange: logOnPageChange,
41
- onPageSizeChange: logOnPageSizeChange,
42
- ...pagers.atTenthPage
40
+ onPageSizeChange: logOnPageSizeChange
43
41
  }
44
42
  };
45
43
 
46
44
  const Template = args => /*#__PURE__*/React.createElement(Pagination, args);
47
45
 
48
46
  export const Default = Template.bind({});
47
+ Default.args = { ...pagers.atTenthPage
48
+ };
49
49
  export const PagerAtFirstPage = Template.bind({});
50
50
  PagerAtFirstPage.args = { ...pagers.atFirstPage
51
51
  };
52
52
  export const PagerAtLastPage = Template.bind({});
53
53
  PagerAtLastPage.args = { ...pagers.atLastPage
54
54
  };
55
+ export const NoTotal = Template.bind({});
56
+ NoTotal.args = { ...pagers.noTotal
57
+ };
58
+ export const NoTotalAtLastPage = Template.bind({});
59
+ NoTotalAtLastPage.args = { ...pagers.noTotalAtLastPage,
60
+ pageLength: 26
61
+ };
62
+ export const NoTotalAtLastPageWithoutPageLength = Template.bind({});
63
+ NoTotalAtLastPageWithoutPageLength.args = { ...pagers.noTotalAtLastPage
64
+ };
55
65
  export const WithoutPageSizeSelect = Template.bind({});
56
- WithoutPageSizeSelect.args = {
66
+ WithoutPageSizeSelect.args = { ...pagers.atTenthPage,
57
67
  hidePageSizeSelect: true
58
68
  };
59
69
  export const WithoutGoToPageSelect = Template.bind({});
60
- WithoutGoToPageSelect.args = {
70
+ WithoutGoToPageSelect.args = { ...pagers.atTenthPage,
61
71
  hidePageSelect: true
62
72
  };
63
73
  export const WithoutPageSummary = Template.bind({});
64
- WithoutPageSummary.args = {
74
+ WithoutPageSummary.args = { ...pagers.atTenthPage,
65
75
  hidePageSummary: true
66
76
  };
67
77
  export const WithCustomPageSummary = Template.bind({});
68
- WithCustomPageSummary.args = {
69
- pageSummaryText: interpolationObject => i18n.t('Page nr {{page}} of {{pageCount}} pages, items {{firstItem}}-{{lastItem}}, NO TOTAL', interpolationObject)
78
+ WithCustomPageSummary.args = { ...pagers.atTenthPage,
79
+ pageSummaryText: interpolationObject => i18n.t('You are at page {{page}} showing items {{firstItem}}-{{lastItem}}, but there are {{pageCount}} pages and {{total}} items', interpolationObject)
70
80
  };
71
81
  export const WithoutAnySelect = Template.bind({});
72
- WithoutAnySelect.args = { ...WithoutGoToPageSelect.args,
82
+ WithoutAnySelect.args = { ...pagers.atTenthPage,
83
+ ...WithoutGoToPageSelect.args,
73
84
  ...WithoutPageSizeSelect.args
74
85
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhis2-ui/pagination",
3
- "version": "8.0.1",
3
+ "version": "8.1.1",
4
4
  "description": "UI Pagination",
5
5
  "repository": {
6
6
  "type": "git",
@@ -33,10 +33,10 @@
33
33
  },
34
34
  "dependencies": {
35
35
  "@dhis2/prop-types": "^3.0.0-beta.1",
36
- "@dhis2-ui/button": "8.0.1",
37
- "@dhis2-ui/select": "8.0.1",
38
- "@dhis2/ui-constants": "8.0.1",
39
- "@dhis2/ui-icons": "8.0.1",
36
+ "@dhis2-ui/button": "8.1.1",
37
+ "@dhis2-ui/select": "8.1.1",
38
+ "@dhis2/ui-constants": "8.1.1",
39
+ "@dhis2/ui-icons": "8.1.1",
40
40
  "classnames": "^2.3.1",
41
41
  "prop-types": "^15.7.2"
42
42
  },