@dhis2-ui/organisation-unit-tree 10.16.3 → 10.16.5

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.
Files changed (114) hide show
  1. package/build/cjs/__e2e__/children_as_child_nodes.js +8 -11
  2. package/build/cjs/__e2e__/common.js +19 -26
  3. package/build/cjs/__e2e__/controlled_expanded.js +7 -10
  4. package/build/cjs/__e2e__/displaying_loading_error.js +1 -4
  5. package/build/cjs/__e2e__/expanded.js +25 -34
  6. package/build/cjs/__e2e__/force_reload.js +15 -22
  7. package/build/cjs/__e2e__/get-organisation-unit-data.js +25 -29
  8. package/build/cjs/__e2e__/highlight.js +7 -10
  9. package/build/cjs/__e2e__/loading_state.js +7 -13
  10. package/build/cjs/__e2e__/multi_selection.js +8 -11
  11. package/build/cjs/__e2e__/no_selection.js +15 -21
  12. package/build/cjs/__e2e__/path_based_filtering.js +16 -22
  13. package/build/cjs/__e2e__/single_selection.js +10 -16
  14. package/build/cjs/__e2e__/sub_unit_as_root.js +5 -7
  15. package/build/cjs/__e2e__/tree_api.js +15 -21
  16. package/build/cjs/__stories__/custom-expanded-imperative-open.js +7 -10
  17. package/build/cjs/__stories__/custom-node-label.js +4 -5
  18. package/build/cjs/__stories__/development-stories.js +6 -8
  19. package/build/cjs/__stories__/force-reload-all.js +1 -2
  20. package/build/cjs/__stories__/loading-error-grandchild.js +1 -4
  21. package/build/cjs/__stories__/loading.js +1 -4
  22. package/build/cjs/__stories__/multiple-roots.js +9 -12
  23. package/build/cjs/__stories__/root-error.js +1 -4
  24. package/build/cjs/__stories__/root-loading.js +1 -4
  25. package/build/cjs/__stories__/shared.js +46 -79
  26. package/build/cjs/features/controlled_expanded/index.js +3 -6
  27. package/build/cjs/locales/index.js +49 -27
  28. package/build/cjs/organisation-unit-node/error-message.js +10 -13
  29. package/build/cjs/organisation-unit-node/label/disabled-selection-label.js +9 -12
  30. package/build/cjs/organisation-unit-node/label/icon-empty.js +19 -22
  31. package/build/cjs/organisation-unit-node/label/icon-folder-closed.js +25 -28
  32. package/build/cjs/organisation-unit-node/label/icon-folder-open.js +34 -37
  33. package/build/cjs/organisation-unit-node/label/icon-single.js +28 -31
  34. package/build/cjs/organisation-unit-node/label/icon.js +6 -7
  35. package/build/cjs/organisation-unit-node/label/iconized-checkbox.js +12 -13
  36. package/build/cjs/organisation-unit-node/label/label-container.js +13 -16
  37. package/build/cjs/organisation-unit-node/label/label.js +27 -30
  38. package/build/cjs/organisation-unit-node/label/single-selection-label.js +18 -21
  39. package/build/cjs/organisation-unit-node/organisation-unit-node-children.js +27 -29
  40. package/build/cjs/organisation-unit-node/organisation-unit-node.js +22 -23
  41. package/build/cjs/organisation-unit-node/use-open-state.js +8 -9
  42. package/build/cjs/organisation-unit-node/use-org-children.js +14 -21
  43. package/build/cjs/organisation-unit-node/use-org-children.test.js +40 -59
  44. package/build/cjs/organisation-unit-node/use-org-data/use-org-data.js +13 -20
  45. package/build/cjs/organisation-unit-node/use-org-data/use-org-data.test.js +17 -27
  46. package/build/cjs/organisation-unit-tree/default-render-node-label/default-render-node-label.js +3 -6
  47. package/build/cjs/organisation-unit-tree/organisation-unit-tree-root-error.js +9 -12
  48. package/build/cjs/organisation-unit-tree/organisation-unit-tree-root-loading.js +10 -13
  49. package/build/cjs/organisation-unit-tree/organisation-unit-tree.js +41 -35
  50. package/build/cjs/organisation-unit-tree/organisation-unit-tree.test.js +78 -7
  51. package/build/cjs/organisation-unit-tree/use-expanded/create-expand-handlers.js +14 -17
  52. package/build/cjs/organisation-unit-tree/use-expanded/use-expanded.js +8 -9
  53. package/build/cjs/organisation-unit-tree/use-root-org-data/patch-missing-display-name.js +1 -2
  54. package/build/cjs/organisation-unit-tree/use-root-org-data/use-root-org-data.js +10 -14
  55. package/build/cjs/organisation-unit-tree/use-root-org-data/use-root-org-unit.test.js +32 -46
  56. package/build/es/__e2e__/children_as_child_nodes.js +8 -11
  57. package/build/es/__e2e__/common.js +18 -24
  58. package/build/es/__e2e__/controlled_expanded.js +6 -8
  59. package/build/es/__e2e__/displaying_loading_error.js +1 -4
  60. package/build/es/__e2e__/expanded.js +25 -34
  61. package/build/es/__e2e__/force_reload.js +14 -20
  62. package/build/es/__e2e__/get-organisation-unit-data.js +25 -29
  63. package/build/es/__e2e__/highlight.js +7 -10
  64. package/build/es/__e2e__/loading_state.js +7 -13
  65. package/build/es/__e2e__/multi_selection.js +8 -11
  66. package/build/es/__e2e__/no_selection.js +15 -21
  67. package/build/es/__e2e__/path_based_filtering.js +16 -22
  68. package/build/es/__e2e__/single_selection.js +10 -16
  69. package/build/es/__e2e__/sub_unit_as_root.js +4 -5
  70. package/build/es/__e2e__/tree_api.js +15 -21
  71. package/build/es/__stories__/custom-expanded-imperative-open.js +6 -8
  72. package/build/es/__stories__/custom-node-label.js +4 -5
  73. package/build/es/__stories__/development-stories.js +5 -6
  74. package/build/es/__stories__/loading-error-grandchild.js +1 -4
  75. package/build/es/__stories__/loading.js +1 -4
  76. package/build/es/__stories__/multiple-roots.js +9 -12
  77. package/build/es/__stories__/root-error.js +1 -4
  78. package/build/es/__stories__/root-loading.js +1 -4
  79. package/build/es/__stories__/shared.js +45 -77
  80. package/build/es/features/controlled_expanded/index.js +3 -6
  81. package/build/es/locales/index.js +40 -18
  82. package/build/es/organisation-unit-node/error-message.js +10 -13
  83. package/build/es/organisation-unit-node/label/disabled-selection-label.js +9 -12
  84. package/build/es/organisation-unit-node/label/icon-empty.js +19 -22
  85. package/build/es/organisation-unit-node/label/icon-folder-closed.js +25 -28
  86. package/build/es/organisation-unit-node/label/icon-folder-open.js +34 -37
  87. package/build/es/organisation-unit-node/label/icon-single.js +28 -31
  88. package/build/es/organisation-unit-node/label/icon.js +6 -7
  89. package/build/es/organisation-unit-node/label/iconized-checkbox.js +12 -13
  90. package/build/es/organisation-unit-node/label/label-container.js +13 -16
  91. package/build/es/organisation-unit-node/label/label.js +27 -30
  92. package/build/es/organisation-unit-node/label/single-selection-label.js +18 -21
  93. package/build/es/organisation-unit-node/organisation-unit-node-children.js +27 -29
  94. package/build/es/organisation-unit-node/organisation-unit-node.js +22 -23
  95. package/build/es/organisation-unit-node/use-open-state.js +8 -9
  96. package/build/es/organisation-unit-node/use-org-children.js +14 -21
  97. package/build/es/organisation-unit-node/use-org-children.test.js +40 -59
  98. package/build/es/organisation-unit-node/use-org-data/use-org-data.js +13 -20
  99. package/build/es/organisation-unit-node/use-org-data/use-org-data.test.js +17 -27
  100. package/build/es/organisation-unit-tree/default-render-node-label/default-render-node-label.js +3 -6
  101. package/build/es/organisation-unit-tree/organisation-unit-tree-root-error.js +9 -12
  102. package/build/es/organisation-unit-tree/organisation-unit-tree-root-loading.js +10 -13
  103. package/build/es/organisation-unit-tree/organisation-unit-tree.js +34 -27
  104. package/build/es/organisation-unit-tree/organisation-unit-tree.test.js +71 -0
  105. package/build/es/organisation-unit-tree/use-expanded/create-expand-handlers.js +14 -17
  106. package/build/es/organisation-unit-tree/use-expanded/use-expanded.js +8 -9
  107. package/build/es/organisation-unit-tree/use-root-org-data/patch-missing-display-name.js +1 -2
  108. package/build/es/organisation-unit-tree/use-root-org-data/use-root-org-data.js +10 -14
  109. package/build/es/organisation-unit-tree/use-root-org-data/use-root-org-unit.test.js +32 -46
  110. package/package.json +6 -6
  111. package/src/locales/index.js +57 -18
  112. package/src/organisation-unit-tree/organisation-unit-tree.js +14 -3
  113. package/src/organisation-unit-tree/organisation-unit-tree.test.js +64 -0
  114. package/src/organisation-unit-tree/use-root-org-data/use-root-org-unit.test.js +14 -14
@@ -7,10 +7,9 @@ var _useOrgChildren = require("./use-org-children.js");
7
7
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
8
8
  describe('OrganisationUnitTree - useOrgChildren', () => {
9
9
  const dataProviderData = {
10
- organisationUnits: jest.fn((type, _ref) => {
11
- let {
12
- id
13
- } = _ref;
10
+ organisationUnits: jest.fn((type, {
11
+ id
12
+ }) => {
14
13
  if (id === 'A0000000000') {
15
14
  return {
16
15
  children: [{
@@ -24,14 +23,11 @@ describe('OrganisationUnitTree - useOrgChildren', () => {
24
23
  return Promise.reject(`No org unit with id "${id}"`);
25
24
  })
26
25
  };
27
- const wrapper = _ref2 => {
28
- let {
29
- children
30
- } = _ref2;
31
- return /*#__PURE__*/_react2.default.createElement(_appRuntime.CustomDataProvider, {
32
- data: dataProviderData
33
- }, children);
34
- };
26
+ const wrapper = ({
27
+ children
28
+ }) => /*#__PURE__*/_react2.default.createElement(_appRuntime.CustomDataProvider, {
29
+ data: dataProviderData
30
+ }, children);
35
31
  const node = {
36
32
  id: 'A0000000000',
37
33
  path: '/A0000000000',
@@ -81,18 +77,15 @@ describe('OrganisationUnitTree - useOrgChildren', () => {
81
77
  });
82
78
  });
83
79
  it('should provide the error', async () => {
84
- const errorWrapper = _ref3 => {
85
- let {
86
- children
87
- } = _ref3;
88
- return /*#__PURE__*/_react2.default.createElement(_appRuntime.CustomDataProvider, {
89
- data: {
90
- organisationUnits: async () => {
91
- throw new Error('Error message');
92
- }
80
+ const errorWrapper = ({
81
+ children
82
+ }) => /*#__PURE__*/_react2.default.createElement(_appRuntime.CustomDataProvider, {
83
+ data: {
84
+ organisationUnits: async () => {
85
+ throw new Error('Error message');
93
86
  }
94
- }, children);
95
- };
87
+ }
88
+ }, children);
96
89
  const {
97
90
  result
98
91
  } = (0, _react.renderHook)(() => (0, _useOrgChildren.useOrgChildren)({
@@ -134,10 +127,9 @@ describe('OrganisationUnitTree - useOrgChildren', () => {
134
127
  });
135
128
  it("should sort the node's children alphabetically by default", async () => {
136
129
  const dataProviderDataWithUnsortedChildren = {
137
- organisationUnits: jest.fn((type, _ref4) => {
138
- let {
139
- id
140
- } = _ref4;
130
+ organisationUnits: jest.fn((type, {
131
+ id
132
+ }) => {
141
133
  if (id === 'A0000000000') {
142
134
  return {
143
135
  children: [{
@@ -156,14 +148,11 @@ describe('OrganisationUnitTree - useOrgChildren', () => {
156
148
  return Promise.reject(`No org unit with id "${id}"`);
157
149
  })
158
150
  };
159
- const wrapperWithUnsortedChildren = _ref5 => {
160
- let {
161
- children
162
- } = _ref5;
163
- return /*#__PURE__*/_react2.default.createElement(_appRuntime.CustomDataProvider, {
164
- data: dataProviderDataWithUnsortedChildren
165
- }, children);
166
- };
151
+ const wrapperWithUnsortedChildren = ({
152
+ children
153
+ }) => /*#__PURE__*/_react2.default.createElement(_appRuntime.CustomDataProvider, {
154
+ data: dataProviderDataWithUnsortedChildren
155
+ }, children);
167
156
  const {
168
157
  result
169
158
  } = (0, _react.renderHook)(() => (0, _useOrgChildren.useOrgChildren)({
@@ -195,10 +184,9 @@ describe('OrganisationUnitTree - useOrgChildren', () => {
195
184
  });
196
185
  it(`should not sort the node's children alphabetically when "suppressAlphabeticalSorting" is true`, async () => {
197
186
  const dataProviderDataWithUnsortedChildren = {
198
- organisationUnits: jest.fn((type, _ref6) => {
199
- let {
200
- id
201
- } = _ref6;
187
+ organisationUnits: jest.fn((type, {
188
+ id
189
+ }) => {
202
190
  if (id === 'A0000000000') {
203
191
  return {
204
192
  children: [{
@@ -217,14 +205,11 @@ describe('OrganisationUnitTree - useOrgChildren', () => {
217
205
  return Promise.reject(`No org unit with id "${id}"`);
218
206
  })
219
207
  };
220
- const wrapperWithUnsortedChildren = _ref7 => {
221
- let {
222
- children
223
- } = _ref7;
224
- return /*#__PURE__*/_react2.default.createElement(_appRuntime.CustomDataProvider, {
225
- data: dataProviderDataWithUnsortedChildren
226
- }, children);
227
- };
208
+ const wrapperWithUnsortedChildren = ({
209
+ children
210
+ }) => /*#__PURE__*/_react2.default.createElement(_appRuntime.CustomDataProvider, {
211
+ data: dataProviderDataWithUnsortedChildren
212
+ }, children);
228
213
  const options = {
229
214
  node: {
230
215
  ...node,
@@ -258,10 +243,9 @@ describe('OrganisationUnitTree - useOrgChildren', () => {
258
243
  });
259
244
  it(`should not sort the node's children alphabetically when "suppressAlphabeticalSorting" is true`, async () => {
260
245
  const dataProviderDataWithUnsortedChildren = {
261
- organisationUnits: jest.fn((type, _ref8) => {
262
- let {
263
- id
264
- } = _ref8;
246
+ organisationUnits: jest.fn((type, {
247
+ id
248
+ }) => {
265
249
  if (id === 'A0000000000') {
266
250
  return {
267
251
  children: [{
@@ -280,14 +264,11 @@ describe('OrganisationUnitTree - useOrgChildren', () => {
280
264
  return Promise.reject(`No org unit with id "${id}"`);
281
265
  })
282
266
  };
283
- const wrapperWithUnsortedChildren = _ref9 => {
284
- let {
285
- children
286
- } = _ref9;
287
- return /*#__PURE__*/_react2.default.createElement(_appRuntime.CustomDataProvider, {
288
- data: dataProviderDataWithUnsortedChildren
289
- }, children);
290
- };
267
+ const wrapperWithUnsortedChildren = ({
268
+ children
269
+ }) => /*#__PURE__*/_react2.default.createElement(_appRuntime.CustomDataProvider, {
270
+ data: dataProviderDataWithUnsortedChildren
271
+ }, children);
291
272
  const options = {
292
273
  node: {
293
274
  ...node,
@@ -8,21 +8,15 @@ var _appRuntime = require("@dhis2/app-runtime");
8
8
  const ORG_DATA_QUERY = {
9
9
  orgUnit: {
10
10
  resource: `organisationUnits`,
11
- id: _ref => {
12
- let {
13
- id
14
- } = _ref;
15
- return id;
16
- },
17
- params: _ref2 => {
18
- let {
19
- isUserDataViewFallback
20
- } = _ref2;
21
- return {
22
- isUserDataViewFallback,
23
- fields: ['path', 'children::size']
24
- };
25
- }
11
+ id: ({
12
+ id
13
+ }) => id,
14
+ params: ({
15
+ isUserDataViewFallback
16
+ }) => ({
17
+ isUserDataViewFallback,
18
+ fields: ['path', 'children::size']
19
+ })
26
20
  }
27
21
  };
28
22
 
@@ -33,11 +27,10 @@ const ORG_DATA_QUERY = {
33
27
  * @param {bool} options.isUserDataViewFallback
34
28
  * @returns {Object}
35
29
  */
36
- const useOrgData = (id, _ref3) => {
37
- let {
38
- displayName,
39
- isUserDataViewFallback
40
- } = _ref3;
30
+ const useOrgData = (id, {
31
+ displayName,
32
+ isUserDataViewFallback
33
+ }) => {
41
34
  if (!displayName) {
42
35
  throw new Error('"displayName" is required');
43
36
  }
@@ -8,10 +8,7 @@ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e
8
8
  describe('OrganisationUnitTree - useOrgData', () => {
9
9
  // @TODO: Figure out why this is necessary at all...
10
10
  const origError = console.error;
11
- jest.spyOn(console, 'error').mockImplementation(function () {
12
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
13
- args[_key] = arguments[_key];
14
- }
11
+ jest.spyOn(console, 'error').mockImplementation((...args) => {
15
12
  const [err] = args;
16
13
  if (!err.toString().match(/^Warning: An update to/)) {
17
14
  origError(...args);
@@ -21,10 +18,9 @@ describe('OrganisationUnitTree - useOrgData', () => {
21
18
  console.error.mockRestore();
22
19
  });
23
20
  const dataProviderData = {
24
- organisationUnits: jest.fn((type, _ref) => {
25
- let {
26
- id
27
- } = _ref;
21
+ organisationUnits: jest.fn((type, {
22
+ id
23
+ }) => {
28
24
  if (id === 'A0000000000') {
29
25
  return {
30
26
  id: 'A0000000000',
@@ -35,14 +31,11 @@ describe('OrganisationUnitTree - useOrgData', () => {
35
31
  return Promise.reject(`No org unit with id "${id}"`);
36
32
  })
37
33
  };
38
- const wrapper = _ref2 => {
39
- let {
40
- children
41
- } = _ref2;
42
- return /*#__PURE__*/_react2.default.createElement(_appRuntime.CustomDataProvider, {
43
- data: dataProviderData
44
- }, children);
45
- };
34
+ const wrapper = ({
35
+ children
36
+ }) => /*#__PURE__*/_react2.default.createElement(_appRuntime.CustomDataProvider, {
37
+ data: dataProviderData
38
+ }, children);
46
39
  it('should respond with `loading: true`, `error: null` and `data: { displayName, id }` initially', () => {
47
40
  const {
48
41
  result
@@ -81,18 +74,15 @@ describe('OrganisationUnitTree - useOrgData', () => {
81
74
  });
82
75
  });
83
76
  it('should provide the error', async () => {
84
- const errorWrapper = _ref3 => {
85
- let {
86
- children
87
- } = _ref3;
88
- return /*#__PURE__*/_react2.default.createElement(_appRuntime.CustomDataProvider, {
89
- data: {
90
- organisationUnits: async () => {
91
- throw new Error('Error message');
92
- }
77
+ const errorWrapper = ({
78
+ children
79
+ }) => /*#__PURE__*/_react2.default.createElement(_appRuntime.CustomDataProvider, {
80
+ data: {
81
+ organisationUnits: async () => {
82
+ throw new Error('Error message');
93
83
  }
94
- }, children);
95
- };
84
+ }
85
+ }, children);
96
86
  const {
97
87
  result
98
88
  } = (0, _react.renderHook)(() => (0, _useOrgData.useOrgData)('A0000000000', {
@@ -4,10 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.defaultRenderNodeLabel = void 0;
7
- const defaultRenderNodeLabel = _ref => {
8
- let {
9
- label
10
- } = _ref;
11
- return label;
12
- };
7
+ const defaultRenderNodeLabel = ({
8
+ label
9
+ }) => label;
13
10
  exports.defaultRenderNodeLabel = defaultRenderNodeLabel;
@@ -8,18 +8,15 @@ var _propTypes = _interopRequireDefault(require("prop-types"));
8
8
  var _react = _interopRequireDefault(require("react"));
9
9
  var _index = _interopRequireDefault(require("../locales/index.js"));
10
10
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
- const OrganisationUnitTreeRootError = _ref => {
12
- let {
13
- dataTest = 'dhis2-uiwidgets-orgunittree-error',
14
- error
15
- } = _ref;
16
- return /*#__PURE__*/_react.default.createElement("div", {
17
- "data-test": dataTest
18
- }, _index.default.t('Error: {{ ERRORMESSAGE }}', {
19
- ERRORMESSAGE: error,
20
- nsSeparator: '>'
21
- }));
22
- };
11
+ const OrganisationUnitTreeRootError = ({
12
+ dataTest = 'dhis2-uiwidgets-orgunittree-error',
13
+ error
14
+ }) => /*#__PURE__*/_react.default.createElement("div", {
15
+ "data-test": dataTest
16
+ }, _index.default.t('Error: {{ ERRORMESSAGE }}', {
17
+ ERRORMESSAGE: error,
18
+ nsSeparator: '>'
19
+ }));
23
20
  exports.OrganisationUnitTreeRootError = OrganisationUnitTreeRootError;
24
21
  OrganisationUnitTreeRootError.propTypes = {
25
22
  error: _propTypes.default.string.isRequired,
@@ -9,19 +9,16 @@ var _loader = require("@dhis2-ui/loader");
9
9
  var _propTypes = _interopRequireDefault(require("prop-types"));
10
10
  var _react = _interopRequireDefault(require("react"));
11
11
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
12
- const OrganisationUnitTreeRootLoading = _ref => {
13
- let {
14
- dataTest = 'dhis2-uiwidgets-orgunittree-loading'
15
- } = _ref;
16
- return /*#__PURE__*/_react.default.createElement("div", {
17
- "data-test": dataTest,
18
- className: "jsx-814846266"
19
- }, /*#__PURE__*/_react.default.createElement(_loader.CircularLoader, {
20
- small: true
21
- }), /*#__PURE__*/_react.default.createElement(_style.default, {
22
- id: "814846266"
23
- }, ["div.jsx-814846266{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-box-pack:center;-webkit-justify-content:center;-ms-flex-pack:center;justify-content:center;}"]));
24
- };
12
+ const OrganisationUnitTreeRootLoading = ({
13
+ dataTest = 'dhis2-uiwidgets-orgunittree-loading'
14
+ }) => /*#__PURE__*/_react.default.createElement("div", {
15
+ "data-test": dataTest,
16
+ className: "jsx-814846266"
17
+ }, /*#__PURE__*/_react.default.createElement(_loader.CircularLoader, {
18
+ small: true
19
+ }), /*#__PURE__*/_react.default.createElement(_style.default, {
20
+ id: "814846266"
21
+ }, ["div.jsx-814846266{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-box-pack:center;-webkit-justify-content:center;-ms-flex-pack:center;justify-content:center;}"]));
25
22
  exports.OrganisationUnitTreeRootLoading = OrganisationUnitTreeRootLoading;
26
23
  OrganisationUnitTreeRootLoading.propTypes = {
27
24
  dataTest: _propTypes.default.string
@@ -7,44 +7,43 @@ exports.OrganisationUnitTree = void 0;
7
7
  var _propTypes = require("@dhis2/prop-types");
8
8
  var _propTypes2 = _interopRequireDefault(require("prop-types"));
9
9
  var _react = _interopRequireWildcard(require("react"));
10
- var _index = require("../organisation-unit-node/index.js");
10
+ var _index = require("../helpers/index.js");
11
+ var _index2 = require("../organisation-unit-node/index.js");
11
12
  var _propTypes3 = require("../prop-types.js");
12
- var _index2 = require("./default-render-node-label/index.js");
13
+ var _index3 = require("./default-render-node-label/index.js");
13
14
  var _filterRootIds = require("./filter-root-ids.js");
14
15
  var _organisationUnitTreeRootError = require("./organisation-unit-tree-root-error.js");
15
16
  var _organisationUnitTreeRootLoading = require("./organisation-unit-tree-root-loading.js");
16
- var _index3 = require("./use-expanded/index.js");
17
+ var _index4 = require("./use-expanded/index.js");
17
18
  var _useForceReload = require("./use-force-reload.js");
18
- var _index4 = require("./use-root-org-data/index.js");
19
- function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
20
- function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
19
+ var _index5 = require("./use-root-org-data/index.js");
20
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
21
21
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
22
22
  // A stable object to reference
23
23
  const staticArray = [];
24
- const OrganisationUnitTree = _ref => {
25
- let {
26
- onChange,
27
- roots,
28
- autoExpandLoadingError,
29
- dataTest = 'dhis2-uiwidgets-orgunittree',
30
- disableSelection,
31
- displayProperty = 'displayName',
32
- forceReload,
33
- highlighted = staticArray,
34
- isUserDataViewFallback,
35
- initiallyExpanded = staticArray,
36
- filter = staticArray,
37
- renderNodeLabel = _index2.defaultRenderNodeLabel,
38
- selected = staticArray,
39
- singleSelection,
40
- suppressAlphabeticalSorting,
41
- expanded: expandedControlled,
42
- handleExpand: handleExpandControlled,
43
- handleCollapse: handleCollapseControlled,
44
- onExpand,
45
- onCollapse,
46
- onChildrenLoaded
47
- } = _ref;
24
+ const OrganisationUnitTree = ({
25
+ onChange,
26
+ roots,
27
+ autoExpandLoadingError,
28
+ dataTest = 'dhis2-uiwidgets-orgunittree',
29
+ disableSelection,
30
+ displayProperty = 'displayName',
31
+ forceReload,
32
+ highlighted = staticArray,
33
+ isUserDataViewFallback,
34
+ initiallyExpanded = staticArray,
35
+ filter = staticArray,
36
+ renderNodeLabel = _index3.defaultRenderNodeLabel,
37
+ selected = staticArray,
38
+ singleSelection,
39
+ suppressAlphabeticalSorting,
40
+ expanded: expandedControlled,
41
+ handleExpand: handleExpandControlled,
42
+ handleCollapse: handleCollapseControlled,
43
+ onExpand,
44
+ onCollapse,
45
+ onChildrenLoaded
46
+ }) => {
48
47
  const rootIds = (0, _filterRootIds.filterRootIds)(filter, Array.isArray(roots) ? roots : [roots]);
49
48
  const reloadId = (0, _useForceReload.useForceReload)(forceReload);
50
49
  const [prevReloadId, setPrevReloadId] = (0, _react.useState)(reloadId);
@@ -54,7 +53,7 @@ const OrganisationUnitTree = _ref => {
54
53
  error,
55
54
  data,
56
55
  refetch
57
- } = (0, _index4.useRootOrgData)(rootIds, {
56
+ } = (0, _index5.useRootOrgData)(rootIds, {
58
57
  isUserDataViewFallback,
59
58
  suppressAlphabeticalSorting,
60
59
  displayProperty
@@ -63,7 +62,7 @@ const OrganisationUnitTree = _ref => {
63
62
  expanded,
64
63
  handleExpand,
65
64
  handleCollapse
66
- } = (0, _index3.useExpanded)({
65
+ } = (0, _index4.useExpanded)({
67
66
  initiallyExpanded,
68
67
  onExpand,
69
68
  onCollapse,
@@ -80,13 +79,20 @@ const OrganisationUnitTree = _ref => {
80
79
  return () => console.warn('@TODO: Why does this component unmount after a force reload?');
81
80
  }, [reloadId, prevReloadId, refetch]);
82
81
  const isLoading = !called || loading;
82
+ const sortedRoots = (0, _react.useMemo)(() => {
83
+ if (!data) {
84
+ return staticArray;
85
+ }
86
+ const rootNodes = rootIds.map(rootId => data[rootId]);
87
+ return suppressAlphabeticalSorting ? rootNodes : (0, _index.sortNodeChildrenAlphabetically)(rootNodes);
88
+ }, [data, rootIds, suppressAlphabeticalSorting]);
83
89
  return /*#__PURE__*/_react.default.createElement("div", {
84
90
  "data-test": dataTest
85
91
  }, isLoading && /*#__PURE__*/_react.default.createElement(_organisationUnitTreeRootLoading.OrganisationUnitTreeRootLoading, null), error && /*#__PURE__*/_react.default.createElement(_organisationUnitTreeRootError.OrganisationUnitTreeRootError, {
86
92
  error: error
87
- }), !error && !isLoading && rootIds.map(rootId => {
88
- const rootNode = data[rootId];
89
- return /*#__PURE__*/_react.default.createElement(_index.OrganisationUnitNode, {
93
+ }), !error && !isLoading && sortedRoots.map(rootNode => {
94
+ const rootId = rootNode.id;
95
+ return /*#__PURE__*/_react.default.createElement(_index2.OrganisationUnitNode, {
90
96
  key: rootNode.path,
91
97
  rootId: rootId,
92
98
  autoExpandLoadingError: autoExpandLoadingError,
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
 
3
3
  var _appRuntime = require("@dhis2/app-runtime");
4
+ var _react = require("@testing-library/react");
4
5
  var _enzyme = require("enzyme");
5
- var _react = _interopRequireDefault(require("react"));
6
+ var _react2 = _interopRequireDefault(require("react"));
6
7
  var _organisationUnitTree = require("./organisation-unit-tree.js");
7
8
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
8
9
  describe('OrganisationUnitTree', () => {
@@ -18,9 +19,9 @@ describe('OrganisationUnitTree', () => {
18
19
  describe('Controlled expanded props', () => {
19
20
  describe('Missing props', () => {
20
21
  it('should throw a prop-types error when "handleCollapse" is missing', () => {
21
- (0, _enzyme.shallow)(/*#__PURE__*/_react.default.createElement(_appRuntime.CustomDataProvider, {
22
+ (0, _enzyme.shallow)(/*#__PURE__*/_react2.default.createElement(_appRuntime.CustomDataProvider, {
22
23
  data: {}
23
- }, /*#__PURE__*/_react.default.createElement(_organisationUnitTree.OrganisationUnitTree, {
24
+ }, /*#__PURE__*/_react2.default.createElement(_organisationUnitTree.OrganisationUnitTree, {
24
25
  roots: "/A001",
25
26
  expanded: [],
26
27
  onChange: () => {},
@@ -30,9 +31,9 @@ describe('OrganisationUnitTree', () => {
30
31
  expect(errorMock.mock.calls[0][2]).toMatch(/Invalid prop `handleCollapse` supplied to `OrganisationUnitTree`/, {});
31
32
  });
32
33
  it('should throw a prop-types error when "handleExpand" is missing', () => {
33
- (0, _enzyme.shallow)(/*#__PURE__*/_react.default.createElement(_appRuntime.CustomDataProvider, {
34
+ (0, _enzyme.shallow)(/*#__PURE__*/_react2.default.createElement(_appRuntime.CustomDataProvider, {
34
35
  data: {}
35
- }, /*#__PURE__*/_react.default.createElement(_organisationUnitTree.OrganisationUnitTree, {
36
+ }, /*#__PURE__*/_react2.default.createElement(_organisationUnitTree.OrganisationUnitTree, {
36
37
  roots: "/A001",
37
38
  expanded: [],
38
39
  onChange: () => {},
@@ -42,9 +43,9 @@ describe('OrganisationUnitTree', () => {
42
43
  expect(errorMock.mock.calls[0][2]).toMatch(/Invalid prop `handleExpand` supplied to `OrganisationUnitTree`/);
43
44
  });
44
45
  it('should throw a prop-types error when "expanded" is missing', () => {
45
- (0, _enzyme.shallow)(/*#__PURE__*/_react.default.createElement(_appRuntime.CustomDataProvider, {
46
+ (0, _enzyme.shallow)(/*#__PURE__*/_react2.default.createElement(_appRuntime.CustomDataProvider, {
46
47
  data: {}
47
- }, /*#__PURE__*/_react.default.createElement(_organisationUnitTree.OrganisationUnitTree, {
48
+ }, /*#__PURE__*/_react2.default.createElement(_organisationUnitTree.OrganisationUnitTree, {
48
49
  roots: "/A001",
49
50
  onChange: () => {},
50
51
  handleCollapse: () => {},
@@ -55,4 +56,74 @@ describe('OrganisationUnitTree', () => {
55
56
  });
56
57
  });
57
58
  });
59
+ describe('Root node sorting', () => {
60
+ // displayNames intentionally out of alphabetical order relative to ids
61
+ const rootNodes = {
62
+ A001: {
63
+ id: 'A001',
64
+ path: '/A001',
65
+ displayName: 'Bravo'
66
+ },
67
+ A002: {
68
+ id: 'A002',
69
+ path: '/A002',
70
+ displayName: 'Alpha'
71
+ },
72
+ A003: {
73
+ id: 'A003',
74
+ path: '/A003',
75
+ displayName: 'Charlie'
76
+ }
77
+ };
78
+ const dataProvider = {
79
+ organisationUnits: (type, {
80
+ id
81
+ }) => {
82
+ const node = rootNodes[id];
83
+ if (node) {
84
+ // children: 0 so nodes don't try to load descendants
85
+ return {
86
+ ...node,
87
+ children: 0
88
+ };
89
+ }
90
+ return Promise.reject(`No org unit with id "${id}"`);
91
+ }
92
+ };
93
+ it('sorts root nodes alphabetically by displayName by default', async () => {
94
+ const {
95
+ container,
96
+ findByText
97
+ } = (0, _react.render)(/*#__PURE__*/_react2.default.createElement(_appRuntime.CustomDataProvider, {
98
+ data: dataProvider
99
+ }, /*#__PURE__*/_react2.default.createElement(_organisationUnitTree.OrganisationUnitTree, {
100
+ roots: ['A001', 'A002', 'A003'],
101
+ onChange: () => {}
102
+ })));
103
+ await findByText('Alpha');
104
+ const {
105
+ textContent
106
+ } = container;
107
+ expect(textContent.indexOf('Alpha')).toBeLessThan(textContent.indexOf('Bravo'));
108
+ expect(textContent.indexOf('Bravo')).toBeLessThan(textContent.indexOf('Charlie'));
109
+ });
110
+ it('keeps the provided roots order when suppressAlphabeticalSorting is true', async () => {
111
+ const {
112
+ container,
113
+ findByText
114
+ } = (0, _react.render)(/*#__PURE__*/_react2.default.createElement(_appRuntime.CustomDataProvider, {
115
+ data: dataProvider
116
+ }, /*#__PURE__*/_react2.default.createElement(_organisationUnitTree.OrganisationUnitTree, {
117
+ roots: ['A003', 'A001', 'A002'],
118
+ suppressAlphabeticalSorting: true,
119
+ onChange: () => {}
120
+ })));
121
+ await findByText('Charlie');
122
+ const {
123
+ textContent
124
+ } = container;
125
+ expect(textContent.indexOf('Charlie')).toBeLessThan(textContent.indexOf('Bravo'));
126
+ expect(textContent.indexOf('Bravo')).toBeLessThan(textContent.indexOf('Alpha'));
127
+ });
128
+ });
58
129
  });
@@ -12,18 +12,16 @@ exports.createExpandHandlers = void 0;
12
12
  * @param {Function} [args.onCollapse]
13
13
  * @returns {{ handleExpand: Function, handleCollapse: Function }}
14
14
  */
15
- const createExpandHandlers = _ref => {
16
- let {
17
- expanded,
18
- setExpanded,
19
- onExpand,
20
- onCollapse
21
- } = _ref;
22
- const handleExpand = _ref2 => {
23
- let {
24
- path,
25
- ...rest
26
- } = _ref2;
15
+ const createExpandHandlers = ({
16
+ expanded,
17
+ setExpanded,
18
+ onExpand,
19
+ onCollapse
20
+ }) => {
21
+ const handleExpand = ({
22
+ path,
23
+ ...rest
24
+ }) => {
27
25
  if (!expanded.includes(path)) {
28
26
  setExpanded([...expanded, path]);
29
27
  if (onExpand) {
@@ -34,11 +32,10 @@ const createExpandHandlers = _ref => {
34
32
  }
35
33
  }
36
34
  };
37
- const handleCollapse = _ref3 => {
38
- let {
39
- path,
40
- ...rest
41
- } = _ref3;
35
+ const handleCollapse = ({
36
+ path,
37
+ ...rest
38
+ }) => {
42
39
  const pathIndex = expanded.indexOf(path);
43
40
  if (pathIndex !== -1) {
44
41
  const updatedExpanded = pathIndex === 0 ? expanded.slice(1) : [...expanded.slice(0, pathIndex), ...expanded.slice(pathIndex + 1)];
@@ -13,15 +13,14 @@ var _createExpandHandlers = require("./create-expand-handlers.js");
13
13
  * @param {Function} [onCollapse]
14
14
  * @returns {{ expanded: string[], handleExpand: Function, handleCollapse: Function }}
15
15
  */
16
- const useExpanded = _ref => {
17
- let {
18
- initiallyExpanded,
19
- onExpand,
20
- onCollapse,
21
- expandedControlled,
22
- handleExpandControlled,
23
- handleCollapseControlled
24
- } = _ref;
16
+ const useExpanded = ({
17
+ initiallyExpanded,
18
+ onExpand,
19
+ onCollapse,
20
+ expandedControlled,
21
+ handleExpandControlled,
22
+ handleCollapseControlled
23
+ }) => {
25
24
  const isControlled = !!expandedControlled;
26
25
  const allInitiallyExpandedPaths = isControlled ? [] : (0, _index.getAllExpandedPaths)(initiallyExpanded);
27
26
  const [expanded, setExpanded] = (0, _react.useState)(allInitiallyExpandedPaths);