@dhis2-ui/pagination 8.1.9 → 8.2.0

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.
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.noTotalAtLastPage = exports.noTotal = exports.atLastPage = exports.atTenthPage = exports.atFirstPage = void 0;
6
+ exports.noTotalAtLastPage = exports.noTotal = exports.atPageBeforeLast = exports.atLastPage = exports.atTenthPage = exports.atSecondPage = exports.atFirstPage = void 0;
7
7
  const atFirstPage = {
8
8
  page: 1,
9
9
  pageCount: 21,
@@ -11,6 +11,13 @@ const atFirstPage = {
11
11
  pageSize: 50
12
12
  };
13
13
  exports.atFirstPage = atFirstPage;
14
+ const atSecondPage = {
15
+ page: 2,
16
+ pageCount: 21,
17
+ total: 1035,
18
+ pageSize: 50
19
+ };
20
+ exports.atSecondPage = atSecondPage;
14
21
  const atTenthPage = {
15
22
  page: 10,
16
23
  pageCount: 21,
@@ -25,8 +32,15 @@ const atLastPage = {
25
32
  pageSize: 50
26
33
  };
27
34
  exports.atLastPage = atLastPage;
35
+ const atPageBeforeLast = {
36
+ page: 20,
37
+ pageCount: 21,
38
+ total: 1035,
39
+ pageSize: 50
40
+ };
41
+ exports.atPageBeforeLast = atPageBeforeLast;
28
42
  const noTotal = {
29
- page: 2,
43
+ page: 3,
30
44
  pageSize: 50,
31
45
  isLastPage: false
32
46
  };
@@ -23,8 +23,8 @@ describe('getItemRange', () => {
23
23
  pageSize,
24
24
  total
25
25
  });
26
- expect(firstItem).toEqual(451);
27
- expect(lastItem).toEqual(500);
26
+ expect(firstItem).toBe(451);
27
+ expect(lastItem).toBe(500);
28
28
  });
29
29
  it('returns 0 for firstItem and lastItem if the total is 0', () => {
30
30
  const {
@@ -35,8 +35,8 @@ describe('getItemRange', () => {
35
35
  pageSize: 50,
36
36
  total: 0
37
37
  });
38
- expect(firstItem).toEqual(0);
39
- expect(lastItem).toEqual(0);
38
+ expect(firstItem).toBe(0);
39
+ expect(lastItem).toBe(0);
40
40
  });
41
41
  it('uses the total count as lastItem when the last page is reached', () => {
42
42
  const {
@@ -51,7 +51,7 @@ describe('getItemRange', () => {
51
51
  pageSize,
52
52
  total
53
53
  });
54
- expect(lastItem).toEqual(total);
54
+ expect(lastItem).toBe(total);
55
55
  });
56
56
  it('handles pagers without totals', () => {
57
57
  const {
@@ -61,8 +61,8 @@ describe('getItemRange', () => {
61
61
  page: 3,
62
62
  pageSize: 50
63
63
  });
64
- expect(firstItem).toEqual(101);
65
- expect(lastItem).toEqual(150);
64
+ expect(firstItem).toBe(101);
65
+ expect(lastItem).toBe(150);
66
66
  });
67
67
  it('bases the lastItem on the pageLength for pagers without total when the last page is reached', () => {
68
68
  const {
@@ -73,7 +73,7 @@ describe('getItemRange', () => {
73
73
  pageLength: 21,
74
74
  isLastPage: true
75
75
  });
76
- expect(lastItem).toEqual(121);
76
+ expect(lastItem).toBe(121);
77
77
  });
78
78
  it('sets lastItem to NaN when on the last page and there is no total or pageLength', () => {
79
79
  const {
@@ -83,6 +83,6 @@ describe('getItemRange', () => {
83
83
  pageSize: 50,
84
84
  isLastPage: true
85
85
  });
86
- expect(lastItem).toEqual(NaN);
86
+ expect(lastItem).toBe(NaN);
87
87
  });
88
88
  });
@@ -25,24 +25,24 @@ describe('<PageControls />', () => {
25
25
  it('renders without errors', () => {
26
26
  (0, _enzyme.shallow)( /*#__PURE__*/_react.default.createElement(_pageControls.PageControls, props));
27
27
  });
28
- it('disables no buttons on a page between first and last', () => {
28
+ it('enables all buttons on by default', () => {
29
29
  const wrapper = (0, _enzyme.shallow)( /*#__PURE__*/_react.default.createElement(_pageControls.PageControls, props));
30
- expect(wrapper.find('.button-previous').getElement().props.disabled).toEqual(false);
31
- expect(wrapper.find('.button-next').getElement().props.disabled).toEqual(false);
30
+ expect(wrapper.find('.button-previous').getElement().props.disabled).toBeFalsy();
31
+ expect(wrapper.find('.button-next').getElement().props.disabled).toBeFalsy();
32
32
  });
33
- it('disables the previous page button on the first page', () => {
33
+ it('disables the previous page button when isPreviousDisabled is true', () => {
34
34
  const wrapper = (0, _enzyme.shallow)( /*#__PURE__*/_react.default.createElement(_pageControls.PageControls, _extends({}, props, {
35
- page: 1
35
+ isPreviousDisabled: true
36
36
  })));
37
- expect(wrapper.find('.button-previous').getElement().props.disabled).toEqual(true);
38
- expect(wrapper.find('.button-next').getElement().props.disabled).toEqual(false);
37
+ expect(wrapper.find('.button-previous').getElement().props.disabled).toBe(true);
38
+ expect(wrapper.find('.button-next').getElement().props.disabled).toBeFalsy();
39
39
  });
40
- it('disables the next page button on the last page', () => {
40
+ it('disables the next page button when isNextDisabled is true', () => {
41
41
  const wrapper = (0, _enzyme.shallow)( /*#__PURE__*/_react.default.createElement(_pageControls.PageControls, _extends({}, props, {
42
- isLastPage: true
42
+ isNextDisabled: true
43
43
  })));
44
- expect(wrapper.find('.button-previous').getElement().props.disabled).toEqual(false);
45
- expect(wrapper.find('.button-next').getElement().props.disabled).toEqual(true);
44
+ expect(wrapper.find('.button-previous').getElement().props.disabled).toBeFalsy();
45
+ expect(wrapper.find('.button-next').getElement().props.disabled).toBe(true);
46
46
  });
47
47
  it('calls the onClick handler with the value for the next page when next is clicked', () => {
48
48
  const wrapper = (0, _enzyme.shallow)( /*#__PURE__*/_react.default.createElement(_pageControls.PageControls, props));
@@ -35,7 +35,7 @@ describe('<PageSummary />', () => {
35
35
  total: 224
36
36
  })));
37
37
  const expectedString = 'Page 2 of 5, items 51-100 of 224';
38
- expect(wrapper.find('span').text()).toEqual(expectedString);
38
+ expect(wrapper.find('span').text()).toBe(expectedString);
39
39
  });
40
40
  it('renders the correct message when only lastItem is provided', () => {
41
41
  const wrapper = (0, _enzyme.shallow)( /*#__PURE__*/_react.default.createElement(_pageSummary.PageSummary, _extends({}, props, {
@@ -45,7 +45,7 @@ describe('<PageSummary />', () => {
45
45
  pageCount: 5
46
46
  })));
47
47
  const expectedString = 'Page 2, items 51-100';
48
- expect(wrapper.find('span').text()).toEqual(expectedString);
48
+ expect(wrapper.find('span').text()).toBe(expectedString);
49
49
  });
50
50
  it('renders the correct message when total is missing and lastItem is not a number', () => {
51
51
  const wrapper = (0, _enzyme.shallow)( /*#__PURE__*/_react.default.createElement(_pageSummary.PageSummary, _extends({}, props, {
@@ -55,6 +55,6 @@ describe('<PageSummary />', () => {
55
55
  pageCount: 5
56
56
  })));
57
57
  const expectedString = 'Page 2';
58
- expect(wrapper.find('span').text()).toEqual(expectedString);
58
+ expect(wrapper.find('span').text()).toBe(expectedString);
59
59
  });
60
60
  });
@@ -1,14 +1,12 @@
1
1
  "use strict";
2
2
 
3
- var _enzyme = require("enzyme");
3
+ require("@testing-library/dom");
4
4
 
5
- var _react = _interopRequireDefault(require("react"));
5
+ var _react = require("@testing-library/react");
6
6
 
7
- var mockPagers = _interopRequireWildcard(require("../__fixtures__/index.js"));
8
-
9
- var _pageSelect = require("../page-select.js");
7
+ var _react2 = _interopRequireDefault(require("react"));
10
8
 
11
- var _pageSizeSelect = require("../page-size-select.js");
9
+ var mockPagers = _interopRequireWildcard(require("../__fixtures__/index.js"));
12
10
 
13
11
  var _pagination = require("../pagination.js");
14
12
 
@@ -27,69 +25,163 @@ describe('<Pagination />', () => {
27
25
  onPageSizeChange: () => {}
28
26
  };
29
27
  it('renders without errors', () => {
30
- (0, _enzyme.shallow)( /*#__PURE__*/_react.default.createElement(_pagination.Pagination, props));
28
+ (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_pagination.Pagination, props));
31
29
  });
32
30
  it('renders a PageSelect and PageSizeSelect by default', () => {
33
- const wrapper = (0, _enzyme.shallow)( /*#__PURE__*/_react.default.createElement(_pagination.Pagination, props));
34
- expect(wrapper.find(_pageSelect.PageSelect).length).toEqual(1);
35
- expect(wrapper.find(_pageSizeSelect.PageSizeSelect).length).toEqual(1);
31
+ const {
32
+ getByTestId
33
+ } = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_pagination.Pagination, props));
34
+ expect(getByTestId('dhis2-uiwidgets-pagination-page-select')).toBeInTheDocument();
35
+ expect(getByTestId('dhis2-uiwidgets-pagination-pagesize')).toBeInTheDocument();
36
36
  });
37
37
  it('renders without a PageSelect when hidePageSelect is true', () => {
38
- const wrapper = (0, _enzyme.shallow)( /*#__PURE__*/_react.default.createElement(_pagination.Pagination, _extends({}, props, {
38
+ const {
39
+ getByTestId,
40
+ queryByTestId
41
+ } = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_pagination.Pagination, _extends({}, props, {
39
42
  hidePageSelect: true
40
43
  })));
41
- expect(wrapper.find(_pageSelect.PageSelect).length).toEqual(0);
42
- expect(wrapper.find(_pageSizeSelect.PageSizeSelect).length).toEqual(1);
44
+ expect(queryByTestId('dhis2-uiwidgets-pagination-page-select')).not.toBeInTheDocument();
45
+ expect(getByTestId('dhis2-uiwidgets-pagination-pagesize')).toBeInTheDocument();
43
46
  });
44
47
  it('renders without a PageSelect when pageCount is not provided', () => {
45
- const wrapper = (0, _enzyme.shallow)( /*#__PURE__*/_react.default.createElement(_pagination.Pagination, _extends({}, props, {
48
+ const {
49
+ getByTestId,
50
+ queryByTestId
51
+ } = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_pagination.Pagination, _extends({}, props, {
46
52
  pageCount: undefined
47
53
  })));
48
- expect(wrapper.find(_pageSelect.PageSelect).length).toEqual(0);
49
- expect(wrapper.find(_pageSizeSelect.PageSizeSelect).length).toEqual(1);
54
+ expect(queryByTestId('dhis2-uiwidgets-pagination-page-select')).not.toBeInTheDocument();
55
+ expect(getByTestId('dhis2-uiwidgets-pagination-pagesize')).toBeInTheDocument();
50
56
  });
51
57
  it('renders without a PageSelect when pageCount is 1', () => {
52
- const wrapper = (0, _enzyme.shallow)( /*#__PURE__*/_react.default.createElement(_pagination.Pagination, _extends({}, props, {
58
+ const {
59
+ getByTestId,
60
+ queryByTestId
61
+ } = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_pagination.Pagination, _extends({}, props, {
53
62
  pageCount: 1
54
63
  })));
55
- expect(wrapper.find(_pageSelect.PageSelect).length).toEqual(0);
56
- expect(wrapper.find(_pageSizeSelect.PageSizeSelect).length).toEqual(1);
64
+ expect(queryByTestId('dhis2-uiwidgets-pagination-page-select')).not.toBeInTheDocument();
65
+ expect(getByTestId('dhis2-uiwidgets-pagination-pagesize')).toBeInTheDocument();
57
66
  });
58
67
  it('renders without a PageSelect when pageCount is over 2000', () => {
59
- const wrapper = (0, _enzyme.shallow)( /*#__PURE__*/_react.default.createElement(_pagination.Pagination, _extends({}, props, {
68
+ const {
69
+ getByTestId,
70
+ queryByTestId
71
+ } = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_pagination.Pagination, _extends({}, props, {
60
72
  pageCount: 2001
61
73
  })));
62
- expect(wrapper.find(_pageSelect.PageSelect).length).toEqual(0);
63
- expect(wrapper.find(_pageSizeSelect.PageSizeSelect).length).toEqual(1);
74
+ expect(queryByTestId('dhis2-uiwidgets-pagination-page-select')).not.toBeInTheDocument();
75
+ expect(getByTestId('dhis2-uiwidgets-pagination-pagesize')).toBeInTheDocument();
64
76
  });
65
77
  it('renders without a PageSizeSelect when hidePageSizeSelect is true', () => {
66
- const wrapper = (0, _enzyme.shallow)( /*#__PURE__*/_react.default.createElement(_pagination.Pagination, _extends({}, props, {
78
+ const {
79
+ getByTestId,
80
+ queryByTestId
81
+ } = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_pagination.Pagination, _extends({}, props, {
67
82
  hidePageSizeSelect: true
68
83
  })));
69
- expect(wrapper.find(_pageSelect.PageSelect).length).toEqual(1);
70
- expect(wrapper.find(_pageSizeSelect.PageSizeSelect).length).toEqual(0);
84
+ expect(queryByTestId('dhis2-uiwidgets-pagination-pagesize')).not.toBeInTheDocument();
85
+ expect(getByTestId('dhis2-uiwidgets-pagination-page-select')).toBeInTheDocument();
71
86
  });
72
87
  it('renders without PageSelect and PageSizeSelect when both are true', () => {
73
- const wrapper = (0, _enzyme.shallow)( /*#__PURE__*/_react.default.createElement(_pagination.Pagination, _extends({}, props, {
88
+ const {
89
+ queryByTestId
90
+ } = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_pagination.Pagination, _extends({}, props, {
74
91
  hidePageSelect: true,
75
92
  hidePageSizeSelect: true
76
93
  })));
77
- expect(wrapper.find(_pageSelect.PageSelect).length).toEqual(0);
78
- expect(wrapper.find(_pageSizeSelect.PageSizeSelect).length).toEqual(0);
94
+ expect(queryByTestId('dhis2-uiwidgets-pagination-pagesize')).not.toBeInTheDocument();
95
+ expect(queryByTestId('dhis2-uiwidgets-pagination-page-select')).not.toBeInTheDocument();
96
+ });
97
+ it('enables all elements when disabled is falsy', () => {
98
+ const {
99
+ getByTestId,
100
+ getByRole
101
+ } = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_pagination.Pagination, props));
102
+ expect(getByTestId('dhis2-uiwidgets-pagination-pagesize').querySelector('.select .root')).not.toHaveClass('disabled');
103
+ expect(getByTestId('dhis2-uiwidgets-pagination-summary').querySelector('span')).not.toHaveClass('inactive');
104
+ expect(getByTestId('dhis2-uiwidgets-pagination-page-select').querySelector('.select .root')).not.toHaveClass('disabled');
105
+ expect(getByRole('button', {
106
+ name: 'Previous'
107
+ })).not.toBeDisabled();
108
+ expect(getByRole('button', {
109
+ name: 'Next'
110
+ })).not.toBeDisabled();
111
+ });
112
+ it('disables all elements when disabled is true', () => {
113
+ const {
114
+ getByTestId,
115
+ getByRole
116
+ } = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_pagination.Pagination, _extends({}, props, {
117
+ disabled: true
118
+ })));
119
+ expect(getByTestId('dhis2-uiwidgets-pagination-pagesize').querySelector('.select .root')).toHaveClass('disabled');
120
+ expect(getByTestId('dhis2-uiwidgets-pagination-summary').querySelector('span')).toHaveClass('inactive');
121
+ expect(getByTestId('dhis2-uiwidgets-pagination-page-select').querySelector('.select .root')).toHaveClass('disabled');
122
+ expect(getByRole('button', {
123
+ name: 'Previous'
124
+ })).toBeDisabled();
125
+ expect(getByRole('button', {
126
+ name: 'Next'
127
+ })).toBeDisabled();
128
+ });
129
+ it('disables previous button when on the first page', () => {
130
+ const {
131
+ getByRole
132
+ } = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_pagination.Pagination, _extends({}, mockPagers.atFirstPage, {
133
+ onPageChange: () => {},
134
+ onPageSizeChange: () => {}
135
+ })));
136
+ expect(getByRole('button', {
137
+ name: 'Previous'
138
+ })).toBeDisabled();
139
+ expect(getByRole('button', {
140
+ name: 'Next'
141
+ })).not.toBeDisabled();
142
+ });
143
+ it('disables next button when on the last page', () => {
144
+ const {
145
+ getByRole
146
+ } = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_pagination.Pagination, _extends({}, mockPagers.atLastPage, {
147
+ onPageChange: () => {},
148
+ onPageSizeChange: () => {}
149
+ })));
150
+ expect(getByRole('button', {
151
+ name: 'Previous'
152
+ })).not.toBeDisabled();
153
+ expect(getByRole('button', {
154
+ name: 'Next'
155
+ })).toBeDisabled();
79
156
  });
80
157
  });
81
158
  describe('Pagination without total and totalPages', () => {
82
159
  const props = { ...mockPagers.noTotal,
160
+ pageLength: mockPagers.noTotal.pageSize,
83
161
  onPageChange: () => {},
84
162
  onPageSizeChange: () => {}
85
163
  };
86
164
  it('renders without errors', () => {
87
- (0, _enzyme.shallow)( /*#__PURE__*/_react.default.createElement(_pagination.Pagination, props));
165
+ (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_pagination.Pagination, props));
88
166
  });
89
167
  it('renders with a PageSizeSelect but without a PageSelect', () => {
90
- const wrapper = (0, _enzyme.shallow)( /*#__PURE__*/_react.default.createElement(_pagination.Pagination, props));
91
- expect(wrapper.find(_pageSizeSelect.PageSizeSelect).length).toEqual(1);
92
- expect(wrapper.find(_pageSelect.PageSelect).length).toEqual(0);
168
+ const {
169
+ queryByTestId,
170
+ getByTestId
171
+ } = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_pagination.Pagination, props));
172
+ expect(getByTestId('dhis2-uiwidgets-pagination-pagesize')).toBeInTheDocument();
173
+ expect(queryByTestId('dhis2-uiwidgets-pagination-page-select')).not.toBeInTheDocument();
174
+ });
175
+ it('prevents forward navigation when on the last page', () => {
176
+ const {
177
+ getByRole
178
+ } = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_pagination.Pagination, _extends({}, props, mockPagers.noTotalAtLastPage)));
179
+ expect(getByRole('button', {
180
+ name: 'Previous'
181
+ })).not.toBeDisabled();
182
+ expect(getByRole('button', {
183
+ name: 'Next'
184
+ })).toBeDisabled();
93
185
  });
94
186
  });
95
187
  });
@@ -19,7 +19,6 @@ var _react = _interopRequireDefault(require("react"));
19
19
 
20
20
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
21
 
22
- // TODO: i18n translate
23
22
  const translate = (prop, interpolationObject) => {
24
23
  if (typeof prop === 'function') {
25
24
  return prop(interpolationObject);
@@ -30,11 +29,12 @@ const translate = (prop, interpolationObject) => {
30
29
 
31
30
  const PageControls = ({
32
31
  dataTest,
33
- isLastPage,
34
- onClick,
32
+ isNextDisabled,
33
+ isPreviousDisabled,
35
34
  nextPageText,
36
35
  page,
37
- previousPageText
36
+ previousPageText,
37
+ onClick
38
38
  }) => /*#__PURE__*/_react.default.createElement("div", {
39
39
  "data-test": `${dataTest}-pagecontrols`,
40
40
  className: _style.default.dynamic([["2407365506", [_uiConstants.spacers.dp4]]])
@@ -42,14 +42,14 @@ const PageControls = ({
42
42
  secondary: true,
43
43
  className: "button-previous",
44
44
  small: true,
45
- disabled: page === 1,
45
+ disabled: isPreviousDisabled,
46
46
  onClick: () => onClick(page - 1),
47
47
  dataTest: `${dataTest}-page-previous`
48
48
  }, /*#__PURE__*/_react.default.createElement(_uiIcons.IconChevronLeft16, null), translate(previousPageText)), /*#__PURE__*/_react.default.createElement(_button.Button, {
49
49
  secondary: true,
50
50
  className: "button-next",
51
51
  small: true,
52
- disabled: !!isLastPage,
52
+ disabled: isNextDisabled,
53
53
  onClick: () => onClick(page + 1),
54
54
  dataTest: `${dataTest}-page-next`
55
55
  }, translate(nextPageText), /*#__PURE__*/_react.default.createElement(_uiIcons.IconChevronRight16, null)), /*#__PURE__*/_react.default.createElement(_style.default, {
@@ -64,5 +64,6 @@ PageControls.propTypes = {
64
64
  page: _propTypes.default.number.isRequired,
65
65
  previousPageText: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.func]).isRequired,
66
66
  onClick: _propTypes.default.func.isRequired,
67
- isLastPage: _propTypes.default.bool
67
+ isNextDisabled: _propTypes.default.bool,
68
+ isPreviousDisabled: _propTypes.default.bool
68
69
  };
@@ -34,6 +34,7 @@ exports.createAvailablePages = createAvailablePages;
34
34
 
35
35
  const PageSelect = ({
36
36
  dataTest,
37
+ disabled,
37
38
  pageSelectText,
38
39
  onChange,
39
40
  page,
@@ -43,6 +44,7 @@ const PageSelect = ({
43
44
  className: _style.default.dynamic([["485717002", [_uiConstants.spacers.dp12]]])
44
45
  }, /*#__PURE__*/_react.default.createElement(_select.SingleSelect, {
45
46
  dense: true,
47
+ disabled: disabled,
46
48
  selected: page.toString(),
47
49
  onChange: ({
48
50
  selected
@@ -65,5 +67,6 @@ PageSelect.propTypes = {
65
67
  page: _propTypes.default.number.isRequired,
66
68
  pageCount: _propTypes.default.number.isRequired,
67
69
  pageSelectText: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.func]).isRequired,
68
- onChange: _propTypes.default.func.isRequired
70
+ onChange: _propTypes.default.func.isRequired,
71
+ disabled: _propTypes.default.bool
69
72
  };
@@ -28,6 +28,7 @@ const translate = (prop, interpolationObject) => {
28
28
 
29
29
  const PageSizeSelect = ({
30
30
  dataTest,
31
+ disabled,
31
32
  pageSizeSelectText,
32
33
  pageSize,
33
34
  pageSizes,
@@ -37,6 +38,7 @@ const PageSizeSelect = ({
37
38
  className: _style.default.dynamic([["1934482915", [_uiConstants.spacers.dp12]]])
38
39
  }, /*#__PURE__*/_react.default.createElement(_select.SingleSelect, {
39
40
  dense: true,
41
+ disabled: disabled,
40
42
  selected: pageSize.toString(),
41
43
  onChange: ({
42
44
  selected
@@ -59,5 +61,6 @@ PageSizeSelect.propTypes = {
59
61
  pageSize: _propTypes.default.number.isRequired,
60
62
  pageSizeSelectText: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.func]).isRequired,
61
63
  pageSizes: _propTypes.default.arrayOf(_propTypes.default.string).isRequired,
62
- onChange: _propTypes.default.func.isRequired
64
+ onChange: _propTypes.default.func.isRequired,
65
+ disabled: _propTypes.default.bool
63
66
  };
@@ -9,6 +9,8 @@ var _style = _interopRequireDefault(require("styled-jsx/style"));
9
9
 
10
10
  var _uiConstants = require("@dhis2/ui-constants");
11
11
 
12
+ var _classnames = _interopRequireDefault(require("classnames"));
13
+
12
14
  var _propTypes = _interopRequireDefault(require("prop-types"));
13
15
 
14
16
  var _react = _interopRequireDefault(require("react"));
@@ -26,6 +28,7 @@ const translate = (prop, interpolationObject) => {
26
28
  const PageSummary = ({
27
29
  dataTest,
28
30
  firstItem,
31
+ inactive,
29
32
  lastItem,
30
33
  page,
31
34
  pageCount,
@@ -41,13 +44,15 @@ const PageSummary = ({
41
44
  });
42
45
  return /*#__PURE__*/_react.default.createElement("div", {
43
46
  "data-test": `${dataTest}-summary`,
44
- className: _style.default.dynamic([["3931741314", [_uiConstants.spacers.dp12, _uiConstants.colors.grey700]]])
47
+ className: _style.default.dynamic([["1022808428", [_uiConstants.spacers.dp12, _uiConstants.colors.grey700, _uiConstants.colors.grey500]]])
45
48
  }, /*#__PURE__*/_react.default.createElement("span", {
46
- className: _style.default.dynamic([["3931741314", [_uiConstants.spacers.dp12, _uiConstants.colors.grey700]]])
49
+ className: _style.default.dynamic([["1022808428", [_uiConstants.spacers.dp12, _uiConstants.colors.grey700, _uiConstants.colors.grey500]]]) + " " + ((0, _classnames.default)({
50
+ inactive
51
+ }) || "")
47
52
  }, summary), /*#__PURE__*/_react.default.createElement(_style.default, {
48
- id: "3931741314",
49
- dynamic: [_uiConstants.spacers.dp12, _uiConstants.colors.grey700]
50
- }, [`div.__jsx-style-dynamic-selector{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;min-height:32px;margin-right:${_uiConstants.spacers.dp12};}`, `span.__jsx-style-dynamic-selector{color:${_uiConstants.colors.grey700};font-size:14px;}`]));
53
+ id: "1022808428",
54
+ dynamic: [_uiConstants.spacers.dp12, _uiConstants.colors.grey700, _uiConstants.colors.grey500]
55
+ }, [`div.__jsx-style-dynamic-selector{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;min-height:32px;margin-right:${_uiConstants.spacers.dp12};}`, `span.__jsx-style-dynamic-selector{color:${_uiConstants.colors.grey700};font-size:14px;}`, `span.inactive.__jsx-style-dynamic-selector{color:${_uiConstants.colors.grey500};}`]));
51
56
  };
52
57
 
53
58
  exports.PageSummary = PageSummary;
@@ -56,6 +61,7 @@ PageSummary.propTypes = {
56
61
  page: _propTypes.default.number.isRequired,
57
62
  pageSummaryText: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.func]).isRequired,
58
63
  firstItem: _propTypes.default.number,
64
+ inactive: _propTypes.default.bool,
59
65
  lastItem: _propTypes.default.number,
60
66
  pageCount: _propTypes.default.number,
61
67
  total: _propTypes.default.number
@@ -36,6 +36,7 @@ const MAX_PAGE_COUNT = 2000;
36
36
  const Pagination = ({
37
37
  className,
38
38
  dataTest,
39
+ disabled,
39
40
  hidePageSelect,
40
41
  hidePageSizeSelect,
41
42
  hidePageSummary,
@@ -72,12 +73,14 @@ const Pagination = ({
72
73
  className: "jsx-3647884394" + " " + "spacer"
73
74
  }) : /*#__PURE__*/_react.default.createElement(_pageSizeSelect.PageSizeSelect, {
74
75
  dataTest: dataTest,
76
+ disabled: disabled,
75
77
  pageSize: pageSize,
76
78
  pageSizes: pageSizes,
77
79
  onChange: onPageSizeChange,
78
80
  pageSizeSelectText: pageSizeSelectText
79
81
  }), !hidePageSummary && /*#__PURE__*/_react.default.createElement(_pageSummary.PageSummary, {
80
82
  dataTest: dataTest,
83
+ inactive: disabled,
81
84
  firstItem: firstItem,
82
85
  lastItem: lastItem,
83
86
  page: page,
@@ -88,17 +91,19 @@ const Pagination = ({
88
91
  className: "jsx-3647884394" + " " + "page-navigation"
89
92
  }, showPageSelect && /*#__PURE__*/_react.default.createElement(_pageSelect.PageSelect, {
90
93
  dataTest: dataTest,
94
+ disabled: disabled,
91
95
  pageSelectText: pageSelectText,
92
96
  page: page,
93
97
  pageCount: pageCount,
94
98
  onChange: onPageChange
95
99
  }), /*#__PURE__*/_react.default.createElement(_pageControls.PageControls, {
96
100
  dataTest: dataTest,
97
- isLastPage: isLastPage || page === pageCount,
98
101
  nextPageText: nextPageText,
99
102
  page: page,
100
103
  previousPageText: previousPageText,
101
- onClick: onPageChange
104
+ onClick: onPageChange,
105
+ isNextDisabled: disabled || isLastPage || page === pageCount,
106
+ isPreviousDisabled: disabled || page === 1
102
107
  })), /*#__PURE__*/_react.default.createElement(_style.default, {
103
108
  id: "3647884394"
104
109
  }, [".container.jsx-3647884394{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-wrap:wrap;-ms-flex-wrap:wrap;flex-wrap:wrap;}", ".spacer.jsx-3647884394{-webkit-box-flex:1;-webkit-flex-grow:1;-ms-flex-positive:1;flex-grow:1;}", ".page-navigation.jsx-3647884394{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;min-height:32px;}"]));
@@ -119,6 +124,7 @@ Pagination.propTypes = {
119
124
  pageSize: _propTypes2.default.number.isRequired,
120
125
  className: _propTypes2.default.string,
121
126
  dataTest: _propTypes2.default.string,
127
+ disabled: _propTypes2.default.bool,
122
128
  hidePageSelect: _propTypes2.default.bool,
123
129
  hidePageSizeSelect: _propTypes2.default.bool,
124
130
  hidePageSummary: _propTypes2.default.bool,
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.WithoutAnySelect = exports.WithCustomPageSummary = exports.WithoutPageSummary = exports.WithoutGoToPageSelect = exports.WithoutPageSizeSelect = exports.NoTotalAtLastPageWithoutPageLength = exports.NoTotalAtLastPage = exports.NoTotal = exports.PagerAtLastPage = exports.PagerAtFirstPage = exports.Default = exports.default = void 0;
6
+ exports.WithoutAnySelect = exports.WithCustomPageSummary = exports.WithoutPageSummary = exports.WithoutGoToPageSelect = exports.WithoutPageSizeSelect = exports.NoTotalAtLastPageWithoutPageLength = exports.NoTotalAtLastPage = exports.NoTotal = exports.PagerAtLastPage = exports.PagerAtFirstPage = exports.Disabled = exports.Default = exports.default = void 0;
7
7
 
8
8
  var _d2I18n = _interopRequireDefault(require("@dhis2/d2-i18n"));
9
9
 
@@ -65,6 +65,11 @@ const Default = Template.bind({});
65
65
  exports.Default = Default;
66
66
  Default.args = { ...pagers.atTenthPage
67
67
  };
68
+ const Disabled = Template.bind({});
69
+ exports.Disabled = Disabled;
70
+ Disabled.args = { ...pagers.atTenthPage,
71
+ disabled: true
72
+ };
68
73
  const PagerAtFirstPage = Template.bind({});
69
74
  exports.PagerAtFirstPage = PagerAtFirstPage;
70
75
  PagerAtFirstPage.args = { ...pagers.atFirstPage
@@ -4,6 +4,12 @@ export const atFirstPage = {
4
4
  total: 1035,
5
5
  pageSize: 50
6
6
  };
7
+ export const atSecondPage = {
8
+ page: 2,
9
+ pageCount: 21,
10
+ total: 1035,
11
+ pageSize: 50
12
+ };
7
13
  export const atTenthPage = {
8
14
  page: 10,
9
15
  pageCount: 21,
@@ -16,8 +22,14 @@ export const atLastPage = {
16
22
  total: 1035,
17
23
  pageSize: 50
18
24
  };
25
+ export const atPageBeforeLast = {
26
+ page: 20,
27
+ pageCount: 21,
28
+ total: 1035,
29
+ pageSize: 50
30
+ };
19
31
  export const noTotal = {
20
- page: 2,
32
+ page: 3,
21
33
  pageSize: 50,
22
34
  isLastPage: false
23
35
  };
@@ -15,8 +15,8 @@ describe('getItemRange', () => {
15
15
  pageSize,
16
16
  total
17
17
  });
18
- expect(firstItem).toEqual(451);
19
- expect(lastItem).toEqual(500);
18
+ expect(firstItem).toBe(451);
19
+ expect(lastItem).toBe(500);
20
20
  });
21
21
  it('returns 0 for firstItem and lastItem if the total is 0', () => {
22
22
  const {
@@ -27,8 +27,8 @@ describe('getItemRange', () => {
27
27
  pageSize: 50,
28
28
  total: 0
29
29
  });
30
- expect(firstItem).toEqual(0);
31
- expect(lastItem).toEqual(0);
30
+ expect(firstItem).toBe(0);
31
+ expect(lastItem).toBe(0);
32
32
  });
33
33
  it('uses the total count as lastItem when the last page is reached', () => {
34
34
  const {
@@ -43,7 +43,7 @@ describe('getItemRange', () => {
43
43
  pageSize,
44
44
  total
45
45
  });
46
- expect(lastItem).toEqual(total);
46
+ expect(lastItem).toBe(total);
47
47
  });
48
48
  it('handles pagers without totals', () => {
49
49
  const {
@@ -53,8 +53,8 @@ describe('getItemRange', () => {
53
53
  page: 3,
54
54
  pageSize: 50
55
55
  });
56
- expect(firstItem).toEqual(101);
57
- expect(lastItem).toEqual(150);
56
+ expect(firstItem).toBe(101);
57
+ expect(lastItem).toBe(150);
58
58
  });
59
59
  it('bases the lastItem on the pageLength for pagers without total when the last page is reached', () => {
60
60
  const {
@@ -65,7 +65,7 @@ describe('getItemRange', () => {
65
65
  pageLength: 21,
66
66
  isLastPage: true
67
67
  });
68
- expect(lastItem).toEqual(121);
68
+ expect(lastItem).toBe(121);
69
69
  });
70
70
  it('sets lastItem to NaN when on the last page and there is no total or pageLength', () => {
71
71
  const {
@@ -75,6 +75,6 @@ describe('getItemRange', () => {
75
75
  pageSize: 50,
76
76
  isLastPage: true
77
77
  });
78
- expect(lastItem).toEqual(NaN);
78
+ expect(lastItem).toBe(NaN);
79
79
  });
80
80
  });
@@ -18,24 +18,24 @@ describe('<PageControls />', () => {
18
18
  it('renders without errors', () => {
19
19
  shallow( /*#__PURE__*/React.createElement(PageControls, props));
20
20
  });
21
- it('disables no buttons on a page between first and last', () => {
21
+ it('enables all buttons on by default', () => {
22
22
  const wrapper = shallow( /*#__PURE__*/React.createElement(PageControls, props));
23
- expect(wrapper.find('.button-previous').getElement().props.disabled).toEqual(false);
24
- expect(wrapper.find('.button-next').getElement().props.disabled).toEqual(false);
23
+ expect(wrapper.find('.button-previous').getElement().props.disabled).toBeFalsy();
24
+ expect(wrapper.find('.button-next').getElement().props.disabled).toBeFalsy();
25
25
  });
26
- it('disables the previous page button on the first page', () => {
26
+ it('disables the previous page button when isPreviousDisabled is true', () => {
27
27
  const wrapper = shallow( /*#__PURE__*/React.createElement(PageControls, _extends({}, props, {
28
- page: 1
28
+ isPreviousDisabled: true
29
29
  })));
30
- expect(wrapper.find('.button-previous').getElement().props.disabled).toEqual(true);
31
- expect(wrapper.find('.button-next').getElement().props.disabled).toEqual(false);
30
+ expect(wrapper.find('.button-previous').getElement().props.disabled).toBe(true);
31
+ expect(wrapper.find('.button-next').getElement().props.disabled).toBeFalsy();
32
32
  });
33
- it('disables the next page button on the last page', () => {
33
+ it('disables the next page button when isNextDisabled is true', () => {
34
34
  const wrapper = shallow( /*#__PURE__*/React.createElement(PageControls, _extends({}, props, {
35
- isLastPage: true
35
+ isNextDisabled: true
36
36
  })));
37
- expect(wrapper.find('.button-previous').getElement().props.disabled).toEqual(false);
38
- expect(wrapper.find('.button-next').getElement().props.disabled).toEqual(true);
37
+ expect(wrapper.find('.button-previous').getElement().props.disabled).toBeFalsy();
38
+ expect(wrapper.find('.button-next').getElement().props.disabled).toBe(true);
39
39
  });
40
40
  it('calls the onClick handler with the value for the next page when next is clicked', () => {
41
41
  const wrapper = shallow( /*#__PURE__*/React.createElement(PageControls, props));
@@ -27,7 +27,7 @@ describe('<PageSummary />', () => {
27
27
  total: 224
28
28
  })));
29
29
  const expectedString = 'Page 2 of 5, items 51-100 of 224';
30
- expect(wrapper.find('span').text()).toEqual(expectedString);
30
+ expect(wrapper.find('span').text()).toBe(expectedString);
31
31
  });
32
32
  it('renders the correct message when only lastItem is provided', () => {
33
33
  const wrapper = shallow( /*#__PURE__*/React.createElement(PageSummary, _extends({}, props, {
@@ -37,7 +37,7 @@ describe('<PageSummary />', () => {
37
37
  pageCount: 5
38
38
  })));
39
39
  const expectedString = 'Page 2, items 51-100';
40
- expect(wrapper.find('span').text()).toEqual(expectedString);
40
+ expect(wrapper.find('span').text()).toBe(expectedString);
41
41
  });
42
42
  it('renders the correct message when total is missing and lastItem is not a number', () => {
43
43
  const wrapper = shallow( /*#__PURE__*/React.createElement(PageSummary, _extends({}, props, {
@@ -47,6 +47,6 @@ describe('<PageSummary />', () => {
47
47
  pageCount: 5
48
48
  })));
49
49
  const expectedString = 'Page 2';
50
- expect(wrapper.find('span').text()).toEqual(expectedString);
50
+ expect(wrapper.find('span').text()).toBe(expectedString);
51
51
  });
52
52
  });
@@ -1,10 +1,9 @@
1
1
  function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
2
 
3
- import { shallow } from 'enzyme';
3
+ import '@testing-library/dom';
4
+ import { render } from '@testing-library/react';
4
5
  import React from 'react';
5
6
  import * as mockPagers from '../__fixtures__/index.js';
6
- import { PageSelect } from '../page-select.js';
7
- import { PageSizeSelect } from '../page-size-select.js';
8
7
  import { Pagination } from '../pagination.js';
9
8
  describe('<Pagination />', () => {
10
9
  describe('Pagination with total and totalPages', () => {
@@ -13,69 +12,163 @@ describe('<Pagination />', () => {
13
12
  onPageSizeChange: () => {}
14
13
  };
15
14
  it('renders without errors', () => {
16
- shallow( /*#__PURE__*/React.createElement(Pagination, props));
15
+ render( /*#__PURE__*/React.createElement(Pagination, props));
17
16
  });
18
17
  it('renders a PageSelect and PageSizeSelect by default', () => {
19
- const wrapper = shallow( /*#__PURE__*/React.createElement(Pagination, props));
20
- expect(wrapper.find(PageSelect).length).toEqual(1);
21
- expect(wrapper.find(PageSizeSelect).length).toEqual(1);
18
+ const {
19
+ getByTestId
20
+ } = render( /*#__PURE__*/React.createElement(Pagination, props));
21
+ expect(getByTestId('dhis2-uiwidgets-pagination-page-select')).toBeInTheDocument();
22
+ expect(getByTestId('dhis2-uiwidgets-pagination-pagesize')).toBeInTheDocument();
22
23
  });
23
24
  it('renders without a PageSelect when hidePageSelect is true', () => {
24
- const wrapper = shallow( /*#__PURE__*/React.createElement(Pagination, _extends({}, props, {
25
+ const {
26
+ getByTestId,
27
+ queryByTestId
28
+ } = render( /*#__PURE__*/React.createElement(Pagination, _extends({}, props, {
25
29
  hidePageSelect: true
26
30
  })));
27
- expect(wrapper.find(PageSelect).length).toEqual(0);
28
- expect(wrapper.find(PageSizeSelect).length).toEqual(1);
31
+ expect(queryByTestId('dhis2-uiwidgets-pagination-page-select')).not.toBeInTheDocument();
32
+ expect(getByTestId('dhis2-uiwidgets-pagination-pagesize')).toBeInTheDocument();
29
33
  });
30
34
  it('renders without a PageSelect when pageCount is not provided', () => {
31
- const wrapper = shallow( /*#__PURE__*/React.createElement(Pagination, _extends({}, props, {
35
+ const {
36
+ getByTestId,
37
+ queryByTestId
38
+ } = render( /*#__PURE__*/React.createElement(Pagination, _extends({}, props, {
32
39
  pageCount: undefined
33
40
  })));
34
- expect(wrapper.find(PageSelect).length).toEqual(0);
35
- expect(wrapper.find(PageSizeSelect).length).toEqual(1);
41
+ expect(queryByTestId('dhis2-uiwidgets-pagination-page-select')).not.toBeInTheDocument();
42
+ expect(getByTestId('dhis2-uiwidgets-pagination-pagesize')).toBeInTheDocument();
36
43
  });
37
44
  it('renders without a PageSelect when pageCount is 1', () => {
38
- const wrapper = shallow( /*#__PURE__*/React.createElement(Pagination, _extends({}, props, {
45
+ const {
46
+ getByTestId,
47
+ queryByTestId
48
+ } = render( /*#__PURE__*/React.createElement(Pagination, _extends({}, props, {
39
49
  pageCount: 1
40
50
  })));
41
- expect(wrapper.find(PageSelect).length).toEqual(0);
42
- expect(wrapper.find(PageSizeSelect).length).toEqual(1);
51
+ expect(queryByTestId('dhis2-uiwidgets-pagination-page-select')).not.toBeInTheDocument();
52
+ expect(getByTestId('dhis2-uiwidgets-pagination-pagesize')).toBeInTheDocument();
43
53
  });
44
54
  it('renders without a PageSelect when pageCount is over 2000', () => {
45
- const wrapper = shallow( /*#__PURE__*/React.createElement(Pagination, _extends({}, props, {
55
+ const {
56
+ getByTestId,
57
+ queryByTestId
58
+ } = render( /*#__PURE__*/React.createElement(Pagination, _extends({}, props, {
46
59
  pageCount: 2001
47
60
  })));
48
- expect(wrapper.find(PageSelect).length).toEqual(0);
49
- expect(wrapper.find(PageSizeSelect).length).toEqual(1);
61
+ expect(queryByTestId('dhis2-uiwidgets-pagination-page-select')).not.toBeInTheDocument();
62
+ expect(getByTestId('dhis2-uiwidgets-pagination-pagesize')).toBeInTheDocument();
50
63
  });
51
64
  it('renders without a PageSizeSelect when hidePageSizeSelect is true', () => {
52
- const wrapper = shallow( /*#__PURE__*/React.createElement(Pagination, _extends({}, props, {
65
+ const {
66
+ getByTestId,
67
+ queryByTestId
68
+ } = render( /*#__PURE__*/React.createElement(Pagination, _extends({}, props, {
53
69
  hidePageSizeSelect: true
54
70
  })));
55
- expect(wrapper.find(PageSelect).length).toEqual(1);
56
- expect(wrapper.find(PageSizeSelect).length).toEqual(0);
71
+ expect(queryByTestId('dhis2-uiwidgets-pagination-pagesize')).not.toBeInTheDocument();
72
+ expect(getByTestId('dhis2-uiwidgets-pagination-page-select')).toBeInTheDocument();
57
73
  });
58
74
  it('renders without PageSelect and PageSizeSelect when both are true', () => {
59
- const wrapper = shallow( /*#__PURE__*/React.createElement(Pagination, _extends({}, props, {
75
+ const {
76
+ queryByTestId
77
+ } = render( /*#__PURE__*/React.createElement(Pagination, _extends({}, props, {
60
78
  hidePageSelect: true,
61
79
  hidePageSizeSelect: true
62
80
  })));
63
- expect(wrapper.find(PageSelect).length).toEqual(0);
64
- expect(wrapper.find(PageSizeSelect).length).toEqual(0);
81
+ expect(queryByTestId('dhis2-uiwidgets-pagination-pagesize')).not.toBeInTheDocument();
82
+ expect(queryByTestId('dhis2-uiwidgets-pagination-page-select')).not.toBeInTheDocument();
83
+ });
84
+ it('enables all elements when disabled is falsy', () => {
85
+ const {
86
+ getByTestId,
87
+ getByRole
88
+ } = render( /*#__PURE__*/React.createElement(Pagination, props));
89
+ expect(getByTestId('dhis2-uiwidgets-pagination-pagesize').querySelector('.select .root')).not.toHaveClass('disabled');
90
+ expect(getByTestId('dhis2-uiwidgets-pagination-summary').querySelector('span')).not.toHaveClass('inactive');
91
+ expect(getByTestId('dhis2-uiwidgets-pagination-page-select').querySelector('.select .root')).not.toHaveClass('disabled');
92
+ expect(getByRole('button', {
93
+ name: 'Previous'
94
+ })).not.toBeDisabled();
95
+ expect(getByRole('button', {
96
+ name: 'Next'
97
+ })).not.toBeDisabled();
98
+ });
99
+ it('disables all elements when disabled is true', () => {
100
+ const {
101
+ getByTestId,
102
+ getByRole
103
+ } = render( /*#__PURE__*/React.createElement(Pagination, _extends({}, props, {
104
+ disabled: true
105
+ })));
106
+ expect(getByTestId('dhis2-uiwidgets-pagination-pagesize').querySelector('.select .root')).toHaveClass('disabled');
107
+ expect(getByTestId('dhis2-uiwidgets-pagination-summary').querySelector('span')).toHaveClass('inactive');
108
+ expect(getByTestId('dhis2-uiwidgets-pagination-page-select').querySelector('.select .root')).toHaveClass('disabled');
109
+ expect(getByRole('button', {
110
+ name: 'Previous'
111
+ })).toBeDisabled();
112
+ expect(getByRole('button', {
113
+ name: 'Next'
114
+ })).toBeDisabled();
115
+ });
116
+ it('disables previous button when on the first page', () => {
117
+ const {
118
+ getByRole
119
+ } = render( /*#__PURE__*/React.createElement(Pagination, _extends({}, mockPagers.atFirstPage, {
120
+ onPageChange: () => {},
121
+ onPageSizeChange: () => {}
122
+ })));
123
+ expect(getByRole('button', {
124
+ name: 'Previous'
125
+ })).toBeDisabled();
126
+ expect(getByRole('button', {
127
+ name: 'Next'
128
+ })).not.toBeDisabled();
129
+ });
130
+ it('disables next button when on the last page', () => {
131
+ const {
132
+ getByRole
133
+ } = render( /*#__PURE__*/React.createElement(Pagination, _extends({}, mockPagers.atLastPage, {
134
+ onPageChange: () => {},
135
+ onPageSizeChange: () => {}
136
+ })));
137
+ expect(getByRole('button', {
138
+ name: 'Previous'
139
+ })).not.toBeDisabled();
140
+ expect(getByRole('button', {
141
+ name: 'Next'
142
+ })).toBeDisabled();
65
143
  });
66
144
  });
67
145
  describe('Pagination without total and totalPages', () => {
68
146
  const props = { ...mockPagers.noTotal,
147
+ pageLength: mockPagers.noTotal.pageSize,
69
148
  onPageChange: () => {},
70
149
  onPageSizeChange: () => {}
71
150
  };
72
151
  it('renders without errors', () => {
73
- shallow( /*#__PURE__*/React.createElement(Pagination, props));
152
+ render( /*#__PURE__*/React.createElement(Pagination, props));
74
153
  });
75
154
  it('renders with a PageSizeSelect but without a PageSelect', () => {
76
- const wrapper = shallow( /*#__PURE__*/React.createElement(Pagination, props));
77
- expect(wrapper.find(PageSizeSelect).length).toEqual(1);
78
- expect(wrapper.find(PageSelect).length).toEqual(0);
155
+ const {
156
+ queryByTestId,
157
+ getByTestId
158
+ } = render( /*#__PURE__*/React.createElement(Pagination, props));
159
+ expect(getByTestId('dhis2-uiwidgets-pagination-pagesize')).toBeInTheDocument();
160
+ expect(queryByTestId('dhis2-uiwidgets-pagination-page-select')).not.toBeInTheDocument();
161
+ });
162
+ it('prevents forward navigation when on the last page', () => {
163
+ const {
164
+ getByRole
165
+ } = render( /*#__PURE__*/React.createElement(Pagination, _extends({}, props, mockPagers.noTotalAtLastPage)));
166
+ expect(getByRole('button', {
167
+ name: 'Previous'
168
+ })).not.toBeDisabled();
169
+ expect(getByRole('button', {
170
+ name: 'Next'
171
+ })).toBeDisabled();
79
172
  });
80
173
  });
81
174
  });
@@ -3,7 +3,7 @@ import { Button } from '@dhis2-ui/button';
3
3
  import { spacers } from '@dhis2/ui-constants';
4
4
  import { IconChevronLeft16, IconChevronRight16 } from '@dhis2/ui-icons';
5
5
  import PropTypes from 'prop-types';
6
- import React from 'react'; // TODO: i18n translate
6
+ import React from 'react';
7
7
 
8
8
  const translate = (prop, interpolationObject) => {
9
9
  if (typeof prop === 'function') {
@@ -15,11 +15,12 @@ const translate = (prop, interpolationObject) => {
15
15
 
16
16
  const PageControls = ({
17
17
  dataTest,
18
- isLastPage,
19
- onClick,
18
+ isNextDisabled,
19
+ isPreviousDisabled,
20
20
  nextPageText,
21
21
  page,
22
- previousPageText
22
+ previousPageText,
23
+ onClick
23
24
  }) => /*#__PURE__*/React.createElement("div", {
24
25
  "data-test": `${dataTest}-pagecontrols`,
25
26
  className: _JSXStyle.dynamic([["2407365506", [spacers.dp4]]])
@@ -27,14 +28,14 @@ const PageControls = ({
27
28
  secondary: true,
28
29
  className: "button-previous",
29
30
  small: true,
30
- disabled: page === 1,
31
+ disabled: isPreviousDisabled,
31
32
  onClick: () => onClick(page - 1),
32
33
  dataTest: `${dataTest}-page-previous`
33
34
  }, /*#__PURE__*/React.createElement(IconChevronLeft16, null), translate(previousPageText)), /*#__PURE__*/React.createElement(Button, {
34
35
  secondary: true,
35
36
  className: "button-next",
36
37
  small: true,
37
- disabled: !!isLastPage,
38
+ disabled: isNextDisabled,
38
39
  onClick: () => onClick(page + 1),
39
40
  dataTest: `${dataTest}-page-next`
40
41
  }, translate(nextPageText), /*#__PURE__*/React.createElement(IconChevronRight16, null)), /*#__PURE__*/React.createElement(_JSXStyle, {
@@ -48,6 +49,7 @@ PageControls.propTypes = {
48
49
  page: PropTypes.number.isRequired,
49
50
  previousPageText: PropTypes.oneOfType([PropTypes.string, PropTypes.func]).isRequired,
50
51
  onClick: PropTypes.func.isRequired,
51
- isLastPage: PropTypes.bool
52
+ isNextDisabled: PropTypes.bool,
53
+ isPreviousDisabled: PropTypes.bool
52
54
  };
53
55
  export { PageControls };
@@ -18,6 +18,7 @@ const createAvailablePages = length => Array.from({
18
18
 
19
19
  const PageSelect = ({
20
20
  dataTest,
21
+ disabled,
21
22
  pageSelectText,
22
23
  onChange,
23
24
  page,
@@ -27,6 +28,7 @@ const PageSelect = ({
27
28
  className: _JSXStyle.dynamic([["485717002", [spacers.dp12]]])
28
29
  }, /*#__PURE__*/React.createElement(SingleSelect, {
29
30
  dense: true,
31
+ disabled: disabled,
30
32
  selected: page.toString(),
31
33
  onChange: ({
32
34
  selected
@@ -48,6 +50,7 @@ PageSelect.propTypes = {
48
50
  page: PropTypes.number.isRequired,
49
51
  pageCount: PropTypes.number.isRequired,
50
52
  pageSelectText: PropTypes.oneOfType([PropTypes.string, PropTypes.func]).isRequired,
51
- onChange: PropTypes.func.isRequired
53
+ onChange: PropTypes.func.isRequired,
54
+ disabled: PropTypes.bool
52
55
  };
53
56
  export { PageSelect, createAvailablePages };
@@ -14,6 +14,7 @@ const translate = (prop, interpolationObject) => {
14
14
 
15
15
  const PageSizeSelect = ({
16
16
  dataTest,
17
+ disabled,
17
18
  pageSizeSelectText,
18
19
  pageSize,
19
20
  pageSizes,
@@ -23,6 +24,7 @@ const PageSizeSelect = ({
23
24
  className: _JSXStyle.dynamic([["1934482915", [spacers.dp12]]])
24
25
  }, /*#__PURE__*/React.createElement(SingleSelect, {
25
26
  dense: true,
27
+ disabled: disabled,
26
28
  selected: pageSize.toString(),
27
29
  onChange: ({
28
30
  selected
@@ -44,6 +46,7 @@ PageSizeSelect.propTypes = {
44
46
  pageSize: PropTypes.number.isRequired,
45
47
  pageSizeSelectText: PropTypes.oneOfType([PropTypes.string, PropTypes.func]).isRequired,
46
48
  pageSizes: PropTypes.arrayOf(PropTypes.string).isRequired,
47
- onChange: PropTypes.func.isRequired
49
+ onChange: PropTypes.func.isRequired,
50
+ disabled: PropTypes.bool
48
51
  };
49
52
  export { PageSizeSelect };
@@ -1,5 +1,6 @@
1
1
  import _JSXStyle from "styled-jsx/style";
2
2
  import { colors, spacers } from '@dhis2/ui-constants';
3
+ import cx from 'classnames';
3
4
  import PropTypes from 'prop-types';
4
5
  import React from 'react';
5
6
 
@@ -14,6 +15,7 @@ const translate = (prop, interpolationObject) => {
14
15
  const PageSummary = ({
15
16
  dataTest,
16
17
  firstItem,
18
+ inactive,
17
19
  lastItem,
18
20
  page,
19
21
  pageCount,
@@ -29,13 +31,15 @@ const PageSummary = ({
29
31
  });
30
32
  return /*#__PURE__*/React.createElement("div", {
31
33
  "data-test": `${dataTest}-summary`,
32
- className: _JSXStyle.dynamic([["3931741314", [spacers.dp12, colors.grey700]]])
34
+ className: _JSXStyle.dynamic([["1022808428", [spacers.dp12, colors.grey700, colors.grey500]]])
33
35
  }, /*#__PURE__*/React.createElement("span", {
34
- className: _JSXStyle.dynamic([["3931741314", [spacers.dp12, colors.grey700]]])
36
+ className: _JSXStyle.dynamic([["1022808428", [spacers.dp12, colors.grey700, colors.grey500]]]) + " " + (cx({
37
+ inactive
38
+ }) || "")
35
39
  }, summary), /*#__PURE__*/React.createElement(_JSXStyle, {
36
- id: "3931741314",
37
- dynamic: [spacers.dp12, colors.grey700]
38
- }, [`div.__jsx-style-dynamic-selector{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;min-height:32px;margin-right:${spacers.dp12};}`, `span.__jsx-style-dynamic-selector{color:${colors.grey700};font-size:14px;}`]));
40
+ id: "1022808428",
41
+ dynamic: [spacers.dp12, colors.grey700, colors.grey500]
42
+ }, [`div.__jsx-style-dynamic-selector{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;min-height:32px;margin-right:${spacers.dp12};}`, `span.__jsx-style-dynamic-selector{color:${colors.grey700};font-size:14px;}`, `span.inactive.__jsx-style-dynamic-selector{color:${colors.grey500};}`]));
39
43
  };
40
44
 
41
45
  PageSummary.propTypes = {
@@ -43,6 +47,7 @@ PageSummary.propTypes = {
43
47
  page: PropTypes.number.isRequired,
44
48
  pageSummaryText: PropTypes.oneOfType([PropTypes.string, PropTypes.func]).isRequired,
45
49
  firstItem: PropTypes.number,
50
+ inactive: PropTypes.bool,
46
51
  lastItem: PropTypes.number,
47
52
  pageCount: PropTypes.number,
48
53
  total: PropTypes.number
@@ -15,6 +15,7 @@ const MAX_PAGE_COUNT = 2000;
15
15
  const Pagination = ({
16
16
  className,
17
17
  dataTest,
18
+ disabled,
18
19
  hidePageSelect,
19
20
  hidePageSizeSelect,
20
21
  hidePageSummary,
@@ -51,12 +52,14 @@ const Pagination = ({
51
52
  className: "jsx-3647884394" + " " + "spacer"
52
53
  }) : /*#__PURE__*/React.createElement(PageSizeSelect, {
53
54
  dataTest: dataTest,
55
+ disabled: disabled,
54
56
  pageSize: pageSize,
55
57
  pageSizes: pageSizes,
56
58
  onChange: onPageSizeChange,
57
59
  pageSizeSelectText: pageSizeSelectText
58
60
  }), !hidePageSummary && /*#__PURE__*/React.createElement(PageSummary, {
59
61
  dataTest: dataTest,
62
+ inactive: disabled,
60
63
  firstItem: firstItem,
61
64
  lastItem: lastItem,
62
65
  page: page,
@@ -67,17 +70,19 @@ const Pagination = ({
67
70
  className: "jsx-3647884394" + " " + "page-navigation"
68
71
  }, showPageSelect && /*#__PURE__*/React.createElement(PageSelect, {
69
72
  dataTest: dataTest,
73
+ disabled: disabled,
70
74
  pageSelectText: pageSelectText,
71
75
  page: page,
72
76
  pageCount: pageCount,
73
77
  onChange: onPageChange
74
78
  }), /*#__PURE__*/React.createElement(PageControls, {
75
79
  dataTest: dataTest,
76
- isLastPage: isLastPage || page === pageCount,
77
80
  nextPageText: nextPageText,
78
81
  page: page,
79
82
  previousPageText: previousPageText,
80
- onClick: onPageChange
83
+ onClick: onPageChange,
84
+ isNextDisabled: disabled || isLastPage || page === pageCount,
85
+ isPreviousDisabled: disabled || page === 1
81
86
  })), /*#__PURE__*/React.createElement(_JSXStyle, {
82
87
  id: "3647884394"
83
88
  }, [".container.jsx-3647884394{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-wrap:wrap;-ms-flex-wrap:wrap;flex-wrap:wrap;}", ".spacer.jsx-3647884394{-webkit-box-flex:1;-webkit-flex-grow:1;-ms-flex-positive:1;flex-grow:1;}", ".page-navigation.jsx-3647884394{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;min-height:32px;}"]));
@@ -97,6 +102,7 @@ Pagination.propTypes = {
97
102
  pageSize: PropTypes.number.isRequired,
98
103
  className: PropTypes.string,
99
104
  dataTest: PropTypes.string,
105
+ disabled: PropTypes.bool,
100
106
  hidePageSelect: PropTypes.bool,
101
107
  hidePageSizeSelect: PropTypes.bool,
102
108
  hidePageSummary: PropTypes.bool,
@@ -46,6 +46,10 @@ const Template = args => /*#__PURE__*/React.createElement(Pagination, args);
46
46
  export const Default = Template.bind({});
47
47
  Default.args = { ...pagers.atTenthPage
48
48
  };
49
+ export const Disabled = Template.bind({});
50
+ Disabled.args = { ...pagers.atTenthPage,
51
+ disabled: true
52
+ };
49
53
  export const PagerAtFirstPage = Template.bind({});
50
54
  PagerAtFirstPage.args = { ...pagers.atFirstPage
51
55
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhis2-ui/pagination",
3
- "version": "8.1.9",
3
+ "version": "8.2.0",
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.1.9",
37
- "@dhis2-ui/select": "8.1.9",
38
- "@dhis2/ui-constants": "8.1.9",
39
- "@dhis2/ui-icons": "8.1.9",
36
+ "@dhis2-ui/button": "8.2.0",
37
+ "@dhis2-ui/select": "8.2.0",
38
+ "@dhis2/ui-constants": "8.2.0",
39
+ "@dhis2/ui-icons": "8.2.0",
40
40
  "classnames": "^2.3.1",
41
41
  "prop-types": "^15.7.2"
42
42
  },