@dhis2-ui/organisation-unit-tree 7.2.8 → 7.4.2

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 (85) hide show
  1. package/build/cjs/__e2e__/common.js +134 -92
  2. package/build/cjs/__e2e__/controlled_expanded.stories.e2e.js +90 -0
  3. package/build/cjs/__e2e__/path_based_filtering.stories.e2e.js +0 -1
  4. package/build/cjs/__e2e__/single_selection.stories.e2e.js +4 -4
  5. package/build/cjs/__e2e__/tree_api.stories.e2e.js +6 -8
  6. package/build/cjs/__stories__/collapsed.js +22 -0
  7. package/build/cjs/__stories__/custom-expanded-imperative-open.js +184 -0
  8. package/build/cjs/__stories__/custom-node-label.js +33 -0
  9. package/build/cjs/__stories__/development-stories.js +96 -0
  10. package/build/cjs/__stories__/expanded.js +23 -0
  11. package/build/cjs/__stories__/filtered-root.js +25 -0
  12. package/build/cjs/__stories__/filtered.js +24 -0
  13. package/build/cjs/__stories__/force-reload-all.js +58 -0
  14. package/build/cjs/__stories__/force-reload-one-unit.js +37 -0
  15. package/build/cjs/__stories__/highlighted.js +24 -0
  16. package/build/cjs/__stories__/indeterminate.js +24 -0
  17. package/build/cjs/__stories__/loading-error-grandchild.js +44 -0
  18. package/build/cjs/__stories__/loading.js +39 -0
  19. package/build/cjs/__stories__/multiple-roots.js +24 -0
  20. package/build/cjs/__stories__/no-selection.js +26 -0
  21. package/build/cjs/__stories__/replace-roots.js +28 -0
  22. package/build/cjs/__stories__/root-error.js +48 -0
  23. package/build/cjs/__stories__/root-loading.js +48 -0
  24. package/build/cjs/__stories__/selected-multiple.js +25 -0
  25. package/build/cjs/__stories__/shared.js +140 -0
  26. package/build/cjs/__stories__/single-selection.js +26 -0
  27. package/build/cjs/features/controlled_expanded/index.js +60 -0
  28. package/build/cjs/features/controlled_expanded.feature +11 -0
  29. package/build/cjs/features/tree_api/index.js +1 -3
  30. package/build/cjs/get-all-expanded-paths/get-all-expanded-paths.js +40 -0
  31. package/build/cjs/get-all-expanded-paths/get-all-expanded-paths.test.js +17 -0
  32. package/build/cjs/get-all-expanded-paths/index.js +13 -0
  33. package/build/cjs/index.js +9 -1
  34. package/build/cjs/organisation-unit-node/use-open-state.js +11 -16
  35. package/build/cjs/organisation-unit-node/use-open-state.test.js +59 -101
  36. package/build/cjs/organisation-unit-tree/organisation-unit-tree.js +35 -20
  37. package/build/cjs/organisation-unit-tree/organisation-unit-tree.test.js +63 -0
  38. package/build/cjs/organisation-unit-tree/use-expanded/{helpers.js → create-expand-handlers.js} +1 -30
  39. package/build/cjs/organisation-unit-tree/use-expanded/{helpers.test.js → create-expand-handlers.test.js} +5 -18
  40. package/build/cjs/organisation-unit-tree/use-expanded/use-expanded.js +23 -4
  41. package/build/cjs/organisation-unit-tree/use-expanded/use-expanded.test.js +42 -8
  42. package/build/cjs/organisation-unit-tree.stories.js +191 -534
  43. package/build/es/__e2e__/common.js +121 -90
  44. package/build/es/__e2e__/controlled_expanded.stories.e2e.js +68 -0
  45. package/build/es/__e2e__/path_based_filtering.stories.e2e.js +0 -1
  46. package/build/es/__e2e__/single_selection.stories.e2e.js +4 -4
  47. package/build/es/__e2e__/tree_api.stories.e2e.js +6 -7
  48. package/build/es/__stories__/collapsed.js +8 -0
  49. package/build/es/__stories__/custom-expanded-imperative-open.js +166 -0
  50. package/build/es/__stories__/custom-node-label.js +19 -0
  51. package/build/es/__stories__/development-stories.js +73 -0
  52. package/build/es/__stories__/expanded.js +9 -0
  53. package/build/es/__stories__/filtered-root.js +11 -0
  54. package/build/es/__stories__/filtered.js +10 -0
  55. package/build/es/__stories__/force-reload-all.js +38 -0
  56. package/build/es/__stories__/force-reload-one-unit.js +25 -0
  57. package/build/es/__stories__/highlighted.js +10 -0
  58. package/build/es/__stories__/indeterminate.js +10 -0
  59. package/build/es/__stories__/loading-error-grandchild.js +29 -0
  60. package/build/es/__stories__/loading.js +24 -0
  61. package/build/es/__stories__/multiple-roots.js +10 -0
  62. package/build/es/__stories__/no-selection.js +12 -0
  63. package/build/es/__stories__/replace-roots.js +16 -0
  64. package/build/es/__stories__/root-error.js +33 -0
  65. package/build/es/__stories__/root-loading.js +33 -0
  66. package/build/es/__stories__/selected-multiple.js +11 -0
  67. package/build/es/__stories__/shared.js +120 -0
  68. package/build/es/__stories__/single-selection.js +12 -0
  69. package/build/es/features/controlled_expanded/index.js +57 -0
  70. package/build/es/features/controlled_expanded.feature +11 -0
  71. package/build/es/features/tree_api/index.js +1 -3
  72. package/build/es/get-all-expanded-paths/get-all-expanded-paths.js +31 -0
  73. package/build/es/get-all-expanded-paths/get-all-expanded-paths.test.js +14 -0
  74. package/build/es/get-all-expanded-paths/index.js +1 -0
  75. package/build/es/index.js +2 -1
  76. package/build/es/organisation-unit-node/use-open-state.js +11 -16
  77. package/build/es/organisation-unit-node/use-open-state.test.js +59 -94
  78. package/build/es/organisation-unit-tree/organisation-unit-tree.js +15 -1
  79. package/build/es/organisation-unit-tree/organisation-unit-tree.test.js +55 -0
  80. package/build/es/organisation-unit-tree/use-expanded/{helpers.js → create-expand-handlers.js} +0 -23
  81. package/build/es/organisation-unit-tree/use-expanded/{helpers.test.js → create-expand-handlers.test.js} +1 -14
  82. package/build/es/organisation-unit-tree/use-expanded/use-expanded.js +21 -3
  83. package/build/es/organisation-unit-tree/use-expanded/use-expanded.test.js +40 -7
  84. package/build/es/organisation-unit-tree.stories.js +23 -465
  85. package/package.json +5 -5
@@ -3,7 +3,9 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.StatefulMultiSelectionWrapper = exports.dataProviderData = exports.delayResponse = exports.namespace = void 0;
6
+ exports.createDecoratorCustomDataProvider = exports.createDecoratorStatefulMultiSelection = exports.StatefulMultiSelectionWrapper = exports.dataProviderData = exports.getOrganisationUnitData = exports.delayResponse = exports.namespace = void 0;
7
+
8
+ var _appRuntime = require("@dhis2/app-runtime");
7
9
 
8
10
  var _propTypes = _interopRequireDefault(require("prop-types"));
9
11
 
@@ -15,131 +17,171 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
15
17
 
16
18
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
19
 
20
+ /* eslint-disable react/display-name */
18
21
  const namespace = 'OrganisationUnitTree';
19
22
  exports.namespace = namespace;
20
23
 
21
24
  const delayResponse = (delay, response) => () => new Promise(resolve => setTimeout(() => resolve(response), delay));
22
25
 
23
26
  exports.delayResponse = delayResponse;
24
- const dataProviderData = {
25
- organisationUnits: (type, {
26
- id
27
- }) => {
28
- if (id === 'A0000000000') {
29
- return {
30
- id: 'A0000000000',
31
- path: '/A0000000000',
32
- displayName: 'Org Unit 1',
33
- children: [{
34
- id: 'A0000000001',
35
- path: '/A0000000000/A0000000001',
36
- children: [{
37
- id: 'A0000000003'
38
- }, {
39
- id: 'A0000000004'
40
- }],
41
- displayName: 'Org Unit 2'
42
- }, {
43
- id: 'A0000000002',
44
- path: '/A0000000000/A0000000002',
45
- children: [],
46
- displayName: 'Org Unit 3'
47
- }, {
48
- id: 'A0000000006',
49
- path: '/A0000000000/A0000000006',
50
- children: [],
51
- displayName: 'Org Unit 7'
52
- }]
53
- };
54
- }
55
27
 
56
- if (id === 'A0000000001') {
57
- return {
28
+ const getOrganisationUnitData = id => {
29
+ let data;
30
+
31
+ if (id === 'A0000000000') {
32
+ data = {
33
+ id: 'A0000000000',
34
+ path: '/A0000000000',
35
+ displayName: 'Org Unit 1',
36
+ children: [{
58
37
  id: 'A0000000001',
59
38
  path: '/A0000000000/A0000000001',
60
- displayName: 'Org Unit 2',
61
39
  children: [{
62
- id: 'A0000000003',
63
- path: '/A0000000000/A0000000001/A0000000003',
64
- children: [],
65
- displayName: 'Org Unit 4'
40
+ id: 'A0000000003'
66
41
  }, {
67
- id: 'A0000000004',
68
- path: '/A0000000000/A0000000001/A0000000004',
69
- children: [],
70
- displayName: 'Org Unit 5'
71
- }]
72
- };
73
- }
74
-
75
- if (id === 'A0000000002') {
76
- return {
77
- displayName: 'Org Unit 3',
42
+ id: 'A0000000004'
43
+ }],
44
+ displayName: 'Org Unit 2'
45
+ }, {
78
46
  id: 'A0000000002',
79
47
  path: '/A0000000000/A0000000002',
80
- children: []
81
- };
82
- }
48
+ children: [],
49
+ displayName: 'Org Unit 3'
50
+ }, {
51
+ id: 'A0000000006',
52
+ path: '/A0000000000/A0000000006',
53
+ children: [],
54
+ displayName: 'Org Unit 7'
55
+ }]
56
+ };
57
+ }
83
58
 
84
- if (id === 'A0000000003') {
85
- return {
86
- displayName: 'Org Unit 4',
59
+ if (id === 'A0000000001') {
60
+ data = {
61
+ id: 'A0000000001',
62
+ path: '/A0000000000/A0000000001',
63
+ displayName: 'Org Unit 2',
64
+ children: [{
87
65
  id: 'A0000000003',
88
66
  path: '/A0000000000/A0000000001/A0000000003',
89
- children: []
90
- };
91
- }
92
-
93
- if (id === 'A0000000004') {
94
- return {
95
- displayName: 'Org Unit 5',
67
+ children: [],
68
+ displayName: 'Org Unit 4'
69
+ }, {
96
70
  id: 'A0000000004',
97
71
  path: '/A0000000000/A0000000001/A0000000004',
98
- children: []
99
- };
100
- }
72
+ children: [],
73
+ displayName: 'Org Unit 5'
74
+ }]
75
+ };
76
+ }
101
77
 
102
- if (id === 'A0000000006') {
103
- return {
104
- displayName: 'Org Unit 7',
105
- id: 'A0000000006',
106
- path: '/A0000000000/A0000000006',
107
- children: []
108
- };
109
- }
78
+ if (id === 'A0000000002') {
79
+ data = {
80
+ displayName: 'Org Unit 3',
81
+ id: 'A0000000002',
82
+ path: '/A0000000000/A0000000002',
83
+ children: []
84
+ };
85
+ }
86
+
87
+ if (id === 'A0000000003') {
88
+ data = {
89
+ displayName: 'Org Unit 4',
90
+ id: 'A0000000003',
91
+ path: '/A0000000000/A0000000001/A0000000003',
92
+ children: [{
93
+ id: 'A0000000007',
94
+ path: '/A0000000000/A0000000001/A0000000003/A0000000007',
95
+ children: [],
96
+ displayName: 'Org Unit 8'
97
+ }]
98
+ };
99
+ }
100
+
101
+ if (id === 'A0000000004') {
102
+ data = {
103
+ displayName: 'Org Unit 5',
104
+ id: 'A0000000004',
105
+ path: '/A0000000000/A0000000001/A0000000004',
106
+ children: []
107
+ };
108
+ }
110
109
 
111
- return Promise.reject(`No org unit with id "${id}"`);
110
+ if (id === 'A0000000006') {
111
+ data = {
112
+ displayName: 'Org Unit 7',
113
+ id: 'A0000000006',
114
+ path: '/A0000000000/A0000000006',
115
+ children: []
116
+ };
112
117
  }
118
+
119
+ if (id === 'A0000000007') {
120
+ data = {
121
+ displayName: 'Org Unit 8',
122
+ id: 'A0000000007',
123
+ path: '/A0000000000/A0000000001/A0000000003/A0000000007',
124
+ children: []
125
+ };
126
+ }
127
+
128
+ return data;
113
129
  };
114
- exports.dataProviderData = dataProviderData;
115
130
 
116
- const onChange = (selected, setSelected) => ({
117
- selected: newSelected
118
- }) => {
119
- setSelected(newSelected);
120
- return newSelected;
131
+ exports.getOrganisationUnitData = getOrganisationUnitData;
132
+ const dataProviderData = {
133
+ organisationUnits: (_, {
134
+ id
135
+ }) => {
136
+ const data = getOrganisationUnitData(id);
137
+
138
+ if (!data) {
139
+ return Promise.reject(new Error('404 - Org unit not found'));
140
+ }
141
+
142
+ return Promise.resolve(data);
143
+ }
121
144
  };
145
+ exports.dataProviderData = dataProviderData;
122
146
 
123
147
  const StatefulMultiSelectionWrapper = ({
124
148
  children,
125
- onSelectionChange
149
+ onSelectionChange = () => null
126
150
  }) => {
127
151
  const [selected, setSelected] = (0, _react.useState)([]);
128
- const onChangeHandler = onChange(selected, setSelected, false);
129
- return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, children({
152
+ return children({
130
153
  selected,
131
- onChange: (...args) => {
132
- const newSelection = onChangeHandler(...args);
133
-
134
- if (onSelectionChange) {
135
- onSelectionChange(newSelection);
136
- }
154
+ onChange: ({
155
+ selected: newSelected
156
+ }) => {
157
+ setSelected(newSelected);
158
+ onSelectionChange(newSelected);
137
159
  }
138
- }));
160
+ });
139
161
  };
140
162
 
141
163
  exports.StatefulMultiSelectionWrapper = StatefulMultiSelectionWrapper;
142
164
  StatefulMultiSelectionWrapper.propTypes = {
143
165
  children: _propTypes.default.func.isRequired,
144
166
  onSelectionChange: _propTypes.default.func
145
- };
167
+ };
168
+
169
+ const createDecoratorStatefulMultiSelection = args => {
170
+ return fn => /*#__PURE__*/_react.default.createElement(StatefulMultiSelectionWrapper, {
171
+ onSelectionChange: args === null || args === void 0 ? void 0 : args.onSelectionChange
172
+ }, fn);
173
+ };
174
+
175
+ exports.createDecoratorStatefulMultiSelection = createDecoratorStatefulMultiSelection;
176
+
177
+ const createDecoratorCustomDataProvider = args => {
178
+ const data = (args === null || args === void 0 ? void 0 : args.data) || dataProviderData;
179
+ return fn => {
180
+ window.dataProviderData = data;
181
+ return /*#__PURE__*/_react.default.createElement(_appRuntime.CustomDataProvider, {
182
+ data: data
183
+ }, fn());
184
+ };
185
+ };
186
+
187
+ exports.createDecoratorCustomDataProvider = createDecoratorCustomDataProvider;
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.MissingProps = exports.Controlled = exports.default = void 0;
7
+
8
+ var _button = require("@dhis2-ui/button");
9
+
10
+ var _react = _interopRequireWildcard(require("react"));
11
+
12
+ var _index = require("../index.js");
13
+
14
+ var _common = require("./common.js");
15
+
16
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
17
+
18
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
19
+
20
+ window.initiallyExpandedPaths = ['/A0000000000/A0000000001'];
21
+ window.orgUnitPathToExpand = '/A0000000000/A0000000001';
22
+ var _default = {
23
+ title: _common.namespace,
24
+ decorators: [(0, _common.createDecoratorCustomDataProvider)()]
25
+ };
26
+ exports.default = _default;
27
+
28
+ const Controlled = () => {
29
+ const initiallyExpanded = (0, _index.getAllExpandedOrgUnitPaths)(window.initiallyExpandedPaths);
30
+ const [expanded, setExpanded] = (0, _react.useState)(initiallyExpanded);
31
+
32
+ const handleExpand = ({
33
+ path
34
+ }) => {
35
+ if (!expanded.includes(path)) {
36
+ setExpanded([...expanded, path]);
37
+ }
38
+ };
39
+
40
+ const handleCollapse = ({
41
+ path
42
+ }) => {
43
+ const pathIndex = expanded.indexOf(path);
44
+
45
+ if (pathIndex !== -1) {
46
+ const updatedExpanded = pathIndex === 0 ? expanded.slice(1) : [...expanded.slice(0, pathIndex), ...expanded.slice(pathIndex + 1)];
47
+ setExpanded(updatedExpanded);
48
+ }
49
+ };
50
+
51
+ const imperativeToggle = () => {
52
+ if (!expanded.includes('/A0000000000/A0000000001')) {
53
+ // Make sure that all required sub paths are included as well
54
+ const nextPaths = (0, _index.getAllExpandedOrgUnitPaths)([...expanded, window.orgUnitPathToExpand]);
55
+ return setExpanded(nextPaths);
56
+ }
57
+
58
+ setExpanded(expanded.filter(v => v !== '/A0000000000/A0000000001'));
59
+ };
60
+
61
+ return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_index.OrganisationUnitTree, {
62
+ onChange: () => null,
63
+ name: "Root org unit",
64
+ roots: ['A0000000000'],
65
+ expanded: expanded,
66
+ handleExpand: handleExpand,
67
+ handleCollapse: handleCollapse
68
+ }), /*#__PURE__*/_react.default.createElement("br", null), /*#__PURE__*/_react.default.createElement(_button.Button, {
69
+ dataTest: "org-unit-toggle",
70
+ onClick: imperativeToggle
71
+ }, "Toggle Org Unit 2"));
72
+ };
73
+
74
+ exports.Controlled = Controlled;
75
+
76
+ const MissingProps = () => {
77
+ const expanded = [];
78
+
79
+ const handleExpand = () => null;
80
+
81
+ return /*#__PURE__*/_react.default.createElement(_index.OrganisationUnitTree, {
82
+ onChange: () => null,
83
+ name: "Root org unit",
84
+ roots: ['A0000000000'],
85
+ expanded: expanded,
86
+ handleExpand: handleExpand
87
+ });
88
+ };
89
+
90
+ exports.MissingProps = MissingProps;
@@ -12,7 +12,6 @@ var _common = require("./common.js");
12
12
 
13
13
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
14
 
15
- window.dataProviderData = _common.dataProviderData;
16
15
  window.allUnits = [];
17
16
 
18
17
  for (let i = 0; i < 7; ++i) {
@@ -19,16 +19,16 @@ const data = {
19
19
  }] = args;
20
20
 
21
21
  if (id === 'A0000000000') {
22
- return { ..._common.dataProviderData.organisationUnits(...args),
22
+ return _common.dataProviderData.organisationUnits(...args).then(response => ({ ...response,
23
23
  children: []
24
- };
24
+ }));
25
25
  }
26
26
 
27
27
  if (id === 'A0000000001') {
28
- return { ..._common.dataProviderData.organisationUnits(...args),
28
+ return _common.dataProviderData.organisationUnits(...args).then(response => ({ ...response,
29
29
  path: '/A0000000001',
30
30
  children: []
31
- };
31
+ }));
32
32
  }
33
33
 
34
34
  return Promise.resolve({});
@@ -12,25 +12,23 @@ var _common = require("./common.js");
12
12
 
13
13
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
14
 
15
- window.dataProviderData = {
15
+ const customizedDataProviderData = {
16
16
  organisationUnits: (...args) => {
17
17
  const [, {
18
18
  id
19
19
  }] = args;
20
20
 
21
21
  if (id === 'A0000000000') {
22
- const data = _common.dataProviderData.organisationUnits(...args);
23
-
24
- return { ...data,
22
+ return _common.dataProviderData.organisationUnits(...args).then(data => ({ ...data,
25
23
  children: data.children.slice(0, 1)
26
- };
24
+ }));
27
25
  }
28
26
 
29
27
  if (id === 'A0000000001') {
30
- return { ..._common.dataProviderData.organisationUnits(...args),
28
+ return _common.dataProviderData.organisationUnits(...args).then(data => ({ ...data,
31
29
  path: '/A0000000001',
32
30
  children: []
33
- };
31
+ }));
34
32
  }
35
33
 
36
34
  return Promise.resolve({});
@@ -41,7 +39,7 @@ window.onCollapse = window.Cypress && window.Cypress.cy.stub();
41
39
  window.onExpand = window.Cypress && window.Cypress.cy.stub();
42
40
  window.onChildrenLoaded = window.Cypress && window.Cypress.cy.stub();
43
41
  (0, _react.storiesOf)(_common.namespace, module).add('Events', () => /*#__PURE__*/_react2.default.createElement(_appRuntime.CustomDataProvider, {
44
- data: window.dataProviderData
42
+ data: customizedDataProviderData
45
43
  }, /*#__PURE__*/_react2.default.createElement(_common.StatefulMultiSelectionWrapper, null, ({
46
44
  selected,
47
45
  onChange
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.Collapsed = void 0;
7
+
8
+ var _react = _interopRequireDefault(require("react"));
9
+
10
+ var _index = require("../index.js");
11
+
12
+ var _shared = require("./shared.js");
13
+
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+
16
+ const Collapsed = () => /*#__PURE__*/_react.default.createElement(_index.OrganisationUnitTree, {
17
+ onChange: _shared.onChange,
18
+ name: 'Root org unit',
19
+ roots: ['A0000000000']
20
+ });
21
+
22
+ exports.Collapsed = Collapsed;
@@ -0,0 +1,184 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.CustomExpandedImperativeOpen = void 0;
7
+
8
+ var _button = require("@dhis2-ui/button");
9
+
10
+ var _react = _interopRequireWildcard(require("react"));
11
+
12
+ var _index = require("../index.js");
13
+
14
+ var _shared = require("./shared.js");
15
+
16
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
17
+
18
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
19
+
20
+ const CustomExpandedImperativeOpen = () => {
21
+ const orgUnit2Path = '/A0000000000/A0000000001';
22
+ const initiallyExpanded = (0, _index.getAllExpandedOrgUnitPaths)([]);
23
+ const [expanded, setExpanded] = (0, _react.useState)(initiallyExpanded);
24
+
25
+ const handleExpand = ({
26
+ path
27
+ }) => {
28
+ if (!expanded.includes(path)) {
29
+ setExpanded([...expanded, path]);
30
+ }
31
+ };
32
+
33
+ const handleCollapse = ({
34
+ path
35
+ }) => {
36
+ const pathIndex = expanded.indexOf(path);
37
+
38
+ if (pathIndex !== -1) {
39
+ const updatedExpanded = pathIndex === 0 ? expanded.slice(1) : [...expanded.slice(0, pathIndex), ...expanded.slice(pathIndex + 1)];
40
+ setExpanded(updatedExpanded);
41
+ }
42
+ };
43
+
44
+ const imperativeToggle = () => {
45
+ if (!expanded.includes('/A0000000000/A0000000001')) {
46
+ // Make sure that all required sub paths are included as well
47
+ const nextPaths = (0, _index.getAllExpandedOrgUnitPaths)([...expanded, orgUnit2Path]);
48
+ return setExpanded(nextPaths);
49
+ }
50
+
51
+ setExpanded(expanded.filter(v => v !== '/A0000000000/A0000000001'));
52
+ };
53
+
54
+ return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_index.OrganisationUnitTree, {
55
+ onChange: _shared.onChange,
56
+ name: "Root org unit",
57
+ roots: ['A0000000000'],
58
+ expanded: expanded,
59
+ handleExpand: handleExpand,
60
+ handleCollapse: handleCollapse
61
+ }), /*#__PURE__*/_react.default.createElement("br", null), /*#__PURE__*/_react.default.createElement(_button.Button, {
62
+ onClick: imperativeToggle
63
+ }, "Toggle Org Unit 2"));
64
+ }; // Do we need this story?
65
+ // export const CustomExpandedKeepUserExpanded = () => {
66
+ // /**
67
+ // * @param {string} path
68
+ // * @returns {string[]}
69
+ // */
70
+ // const extractAllPathsFromPath = path => {
71
+ // // remove leading slash and split by path delimiter/slashes
72
+ // const segments = path.replace(/^\//, '').split('/')
73
+ //
74
+ // const withSubPaths = segments.map((segment, index) => {
75
+ // // take all segments from 0 to index and join them with the delimiter
76
+ // return `/${segments.slice(0, index + 1).join('/')}`
77
+ // })
78
+ //
79
+ // return withSubPaths
80
+ // }
81
+ //
82
+ // /**
83
+ // * @param {string[]} initiallyExpanded
84
+ // * @returns {string[]}
85
+ // */
86
+ // const getInitiallyExpandedPaths = paths =>
87
+ // paths.reduce((all, curPath) => {
88
+ // const allPathsInCurPath = extractAllPathsFromPath(curPath)
89
+ // return [...all, ...allPathsInCurPath]
90
+ // }, [])
91
+ //
92
+ // const [useOriginal, setUseOriginal] = useState(true)
93
+ // const initiallyExpanded = getInitiallyExpandedPaths(
94
+ // ['/A0000000000/A0000000001']
95
+ // )
96
+ // const alternativeInitiallyExpanded = getInitiallyExpandedPaths(
97
+ // ['/A0000000000']
98
+ // )
99
+ //
100
+ // const [expandedConfig, setExpandedConfig] = useState(
101
+ // () => initiallyExpanded.map(path => ({
102
+ // userExpanded: false,
103
+ // path,
104
+ // }))
105
+ // )
106
+ //
107
+ // const expanded = useMemo(
108
+ // () => expandedConfig.map(({ path }) => path),
109
+ // [expandedConfig]
110
+ // )
111
+ //
112
+ // const switchAutomaticallyExpended = () => {
113
+ // // Necessary due to the nature of the async state setter of the hook
114
+ // const nextUseOriginal = !useOriginal
115
+ // const allUserExpandedPaths = getInitiallyExpandedPaths(
116
+ // expandedConfig
117
+ // .filter(({ userExpanded }) => userExpanded)
118
+ // .map(({ path }) => path)
119
+ // )
120
+ //
121
+ // const allUserExpanded = allUserExpandedPaths.map(path => ({
122
+ // userExpanded: true,
123
+ // path,
124
+ // }))
125
+ //
126
+ // const actualInitiallyExpended = nextUseOriginal ? initiallyExpanded : alternativeInitiallyExpanded
127
+ // const missingInitiallyExpanded = actualInitiallyExpended.filter(path => {
128
+ // return !allUserExpandedPaths.find(curPath => curPath.includes(path))
129
+ // })
130
+ //
131
+ // const nextExpandedConfig = [
132
+ // ...allUserExpanded,
133
+ // ...missingInitiallyExpanded.map(path => ({
134
+ // userExpanded: false,
135
+ // path,
136
+ // }))
137
+ // ]
138
+ //
139
+ // setExpandedConfig(nextExpandedConfig)
140
+ // setUseOriginal(nextUseOriginal)
141
+ // }
142
+ //
143
+ // const handleExpand = ({ path }) => {
144
+ // if (!expandedConfig.find(config => config.path === path)) {
145
+ // setExpandedConfig([...expandedConfig, { userExpanded: true, path }])
146
+ // }
147
+ // }
148
+ //
149
+ // const handleCollapse = ({ path }) => {
150
+ // const pathIndex = expandedConfig.findIndex(config => config.path === path)
151
+ //
152
+ // if (pathIndex !== -1) {
153
+ // const updatedExpandedConfig =
154
+ // pathIndex === 0
155
+ // ? expandedConfig.slice(1)
156
+ // : [
157
+ // ...expandedConfig.slice(0, pathIndex),
158
+ // ...expandedConfig.slice(pathIndex + 1),
159
+ // ]
160
+ //
161
+ // setExpandedConfig(updatedExpandedConfig)
162
+ // }
163
+ // }
164
+ //
165
+ // return (
166
+ // <>
167
+ // <OrganisationUnitTree
168
+ // onChange={onChange}
169
+ // name="Root org unit"
170
+ // roots={['A0000000000']}
171
+ // expanded={expanded}
172
+ // handleExpand={handleExpand}
173
+ // handleCollapse={handleCollapse}
174
+ // />
175
+ // <br />
176
+ // <Button onClick={switchAutomaticallyExpended}>
177
+ // Switch automatically expanded
178
+ // </Button>
179
+ // </>
180
+ // )
181
+ // }
182
+
183
+
184
+ exports.CustomExpandedImperativeOpen = CustomExpandedImperativeOpen;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.CustomNodeLabel = void 0;
7
+
8
+ var _react = _interopRequireDefault(require("react"));
9
+
10
+ var _index = require("../index.js");
11
+
12
+ var _shared = require("./shared.js");
13
+
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+
16
+ const CustomNodeLabel = () => /*#__PURE__*/_react.default.createElement(_index.OrganisationUnitTree, {
17
+ name: "Root org unit",
18
+ roots: "A0000000000",
19
+ initiallyExpanded: ['/A0000000000/A0000000001'],
20
+ renderNodeLabel: ({
21
+ label,
22
+ node
23
+ }) => {
24
+ if (node.path !== '/A0000000000/A0000000001') {
25
+ return label;
26
+ }
27
+
28
+ return /*#__PURE__*/_react.default.createElement("span", null, "--- ", label);
29
+ },
30
+ onChange: _shared.onChange
31
+ });
32
+
33
+ exports.CustomNodeLabel = CustomNodeLabel;