@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.
- package/build/cjs/__e2e__/common.js +134 -92
- package/build/cjs/__e2e__/controlled_expanded.stories.e2e.js +90 -0
- package/build/cjs/__e2e__/path_based_filtering.stories.e2e.js +0 -1
- package/build/cjs/__e2e__/single_selection.stories.e2e.js +4 -4
- package/build/cjs/__e2e__/tree_api.stories.e2e.js +6 -8
- package/build/cjs/__stories__/collapsed.js +22 -0
- package/build/cjs/__stories__/custom-expanded-imperative-open.js +184 -0
- package/build/cjs/__stories__/custom-node-label.js +33 -0
- package/build/cjs/__stories__/development-stories.js +96 -0
- package/build/cjs/__stories__/expanded.js +23 -0
- package/build/cjs/__stories__/filtered-root.js +25 -0
- package/build/cjs/__stories__/filtered.js +24 -0
- package/build/cjs/__stories__/force-reload-all.js +58 -0
- package/build/cjs/__stories__/force-reload-one-unit.js +37 -0
- package/build/cjs/__stories__/highlighted.js +24 -0
- package/build/cjs/__stories__/indeterminate.js +24 -0
- package/build/cjs/__stories__/loading-error-grandchild.js +44 -0
- package/build/cjs/__stories__/loading.js +39 -0
- package/build/cjs/__stories__/multiple-roots.js +24 -0
- package/build/cjs/__stories__/no-selection.js +26 -0
- package/build/cjs/__stories__/replace-roots.js +28 -0
- package/build/cjs/__stories__/root-error.js +48 -0
- package/build/cjs/__stories__/root-loading.js +48 -0
- package/build/cjs/__stories__/selected-multiple.js +25 -0
- package/build/cjs/__stories__/shared.js +140 -0
- package/build/cjs/__stories__/single-selection.js +26 -0
- package/build/cjs/features/controlled_expanded/index.js +60 -0
- package/build/cjs/features/controlled_expanded.feature +11 -0
- package/build/cjs/features/tree_api/index.js +1 -3
- package/build/cjs/get-all-expanded-paths/get-all-expanded-paths.js +40 -0
- package/build/cjs/get-all-expanded-paths/get-all-expanded-paths.test.js +17 -0
- package/build/cjs/get-all-expanded-paths/index.js +13 -0
- package/build/cjs/index.js +9 -1
- package/build/cjs/organisation-unit-node/use-open-state.js +11 -16
- package/build/cjs/organisation-unit-node/use-open-state.test.js +59 -101
- package/build/cjs/organisation-unit-tree/organisation-unit-tree.js +35 -20
- package/build/cjs/organisation-unit-tree/organisation-unit-tree.test.js +63 -0
- package/build/cjs/organisation-unit-tree/use-expanded/{helpers.js → create-expand-handlers.js} +1 -30
- package/build/cjs/organisation-unit-tree/use-expanded/{helpers.test.js → create-expand-handlers.test.js} +5 -18
- package/build/cjs/organisation-unit-tree/use-expanded/use-expanded.js +23 -4
- package/build/cjs/organisation-unit-tree/use-expanded/use-expanded.test.js +42 -8
- package/build/cjs/organisation-unit-tree.stories.js +191 -534
- package/build/es/__e2e__/common.js +121 -90
- package/build/es/__e2e__/controlled_expanded.stories.e2e.js +68 -0
- package/build/es/__e2e__/path_based_filtering.stories.e2e.js +0 -1
- package/build/es/__e2e__/single_selection.stories.e2e.js +4 -4
- package/build/es/__e2e__/tree_api.stories.e2e.js +6 -7
- package/build/es/__stories__/collapsed.js +8 -0
- package/build/es/__stories__/custom-expanded-imperative-open.js +166 -0
- package/build/es/__stories__/custom-node-label.js +19 -0
- package/build/es/__stories__/development-stories.js +73 -0
- package/build/es/__stories__/expanded.js +9 -0
- package/build/es/__stories__/filtered-root.js +11 -0
- package/build/es/__stories__/filtered.js +10 -0
- package/build/es/__stories__/force-reload-all.js +38 -0
- package/build/es/__stories__/force-reload-one-unit.js +25 -0
- package/build/es/__stories__/highlighted.js +10 -0
- package/build/es/__stories__/indeterminate.js +10 -0
- package/build/es/__stories__/loading-error-grandchild.js +29 -0
- package/build/es/__stories__/loading.js +24 -0
- package/build/es/__stories__/multiple-roots.js +10 -0
- package/build/es/__stories__/no-selection.js +12 -0
- package/build/es/__stories__/replace-roots.js +16 -0
- package/build/es/__stories__/root-error.js +33 -0
- package/build/es/__stories__/root-loading.js +33 -0
- package/build/es/__stories__/selected-multiple.js +11 -0
- package/build/es/__stories__/shared.js +120 -0
- package/build/es/__stories__/single-selection.js +12 -0
- package/build/es/features/controlled_expanded/index.js +57 -0
- package/build/es/features/controlled_expanded.feature +11 -0
- package/build/es/features/tree_api/index.js +1 -3
- package/build/es/get-all-expanded-paths/get-all-expanded-paths.js +31 -0
- package/build/es/get-all-expanded-paths/get-all-expanded-paths.test.js +14 -0
- package/build/es/get-all-expanded-paths/index.js +1 -0
- package/build/es/index.js +2 -1
- package/build/es/organisation-unit-node/use-open-state.js +11 -16
- package/build/es/organisation-unit-node/use-open-state.test.js +59 -94
- package/build/es/organisation-unit-tree/organisation-unit-tree.js +15 -1
- package/build/es/organisation-unit-tree/organisation-unit-tree.test.js +55 -0
- package/build/es/organisation-unit-tree/use-expanded/{helpers.js → create-expand-handlers.js} +0 -23
- package/build/es/organisation-unit-tree/use-expanded/{helpers.test.js → create-expand-handlers.test.js} +1 -14
- package/build/es/organisation-unit-tree/use-expanded/use-expanded.js +21 -3
- package/build/es/organisation-unit-tree/use-expanded/use-expanded.test.js +40 -7
- package/build/es/organisation-unit-tree.stories.js +23 -465
- package/package.json +5 -5
|
@@ -5,13 +5,15 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.OrganisationUnitTree = void 0;
|
|
7
7
|
|
|
8
|
-
var _propTypes =
|
|
8
|
+
var _propTypes = require("@dhis2/prop-types");
|
|
9
|
+
|
|
10
|
+
var _propTypes2 = _interopRequireDefault(require("prop-types"));
|
|
9
11
|
|
|
10
12
|
var _react = _interopRequireWildcard(require("react"));
|
|
11
13
|
|
|
12
14
|
var _index = require("../organisation-unit-node/index.js");
|
|
13
15
|
|
|
14
|
-
var
|
|
16
|
+
var _propTypes3 = require("../prop-types.js");
|
|
15
17
|
|
|
16
18
|
var _index2 = require("./default-render-node-label/index.js");
|
|
17
19
|
|
|
@@ -48,6 +50,9 @@ const OrganisationUnitTree = ({
|
|
|
48
50
|
selected,
|
|
49
51
|
singleSelection,
|
|
50
52
|
suppressAlphabeticalSorting,
|
|
53
|
+
expanded: expandedControlled,
|
|
54
|
+
handleExpand: handleExpandControlled,
|
|
55
|
+
handleCollapse: handleCollapseControlled,
|
|
51
56
|
onExpand,
|
|
52
57
|
onCollapse,
|
|
53
58
|
onChildrenLoaded
|
|
@@ -68,7 +73,14 @@ const OrganisationUnitTree = ({
|
|
|
68
73
|
expanded,
|
|
69
74
|
handleExpand,
|
|
70
75
|
handleCollapse
|
|
71
|
-
} = (0, _index3.useExpanded)(
|
|
76
|
+
} = (0, _index3.useExpanded)({
|
|
77
|
+
initiallyExpanded,
|
|
78
|
+
onExpand,
|
|
79
|
+
onCollapse,
|
|
80
|
+
expandedControlled,
|
|
81
|
+
handleExpandControlled,
|
|
82
|
+
handleCollapseControlled
|
|
83
|
+
});
|
|
72
84
|
(0, _react.useEffect)(() => {
|
|
73
85
|
// do not refetch on initial render
|
|
74
86
|
if (refetch && reloadId > 0 && reloadId !== prevReloadId) {
|
|
@@ -115,27 +127,30 @@ const OrganisationUnitTree = ({
|
|
|
115
127
|
exports.OrganisationUnitTree = OrganisationUnitTree;
|
|
116
128
|
OrganisationUnitTree.propTypes = {
|
|
117
129
|
/** Root org unit ID(s) */
|
|
118
|
-
roots:
|
|
130
|
+
roots: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.arrayOf(_propTypes2.default.string)]).isRequired,
|
|
119
131
|
|
|
120
132
|
/** Will be called with the following object:
|
|
121
133
|
* `{ id: string, displayName: string, path: string, checked: boolean, selected: string[] }` */
|
|
122
|
-
onChange:
|
|
134
|
+
onChange: _propTypes2.default.func.isRequired,
|
|
123
135
|
|
|
124
136
|
/** When set, the error when loading children fails will be shown automatically */
|
|
125
|
-
autoExpandLoadingError:
|
|
126
|
-
dataTest:
|
|
137
|
+
autoExpandLoadingError: _propTypes2.default.bool,
|
|
138
|
+
dataTest: _propTypes2.default.string,
|
|
127
139
|
|
|
128
140
|
/** When set to true, no unit can be selected */
|
|
129
|
-
disableSelection:
|
|
141
|
+
disableSelection: _propTypes2.default.bool,
|
|
142
|
+
expanded: (0, _propTypes.requiredIf)(props => !!props.handleExpand || !!props.handleCollapse, _propTypes2.default.arrayOf(_propTypes2.default.string)),
|
|
130
143
|
|
|
131
144
|
/**
|
|
132
145
|
* All organisation units with a path that includes the provided paths will be shown.
|
|
133
146
|
* All others will not be rendered. When not provided, all org units will be shown.
|
|
134
147
|
*/
|
|
135
|
-
filter:
|
|
148
|
+
filter: _propTypes2.default.arrayOf(_propTypes3.orgUnitPathPropType),
|
|
136
149
|
|
|
137
150
|
/** When true, everything will be reloaded. In order to load it again after reloading, `forceReload` has to be set to `false` and then to `true` again */
|
|
138
|
-
forceReload:
|
|
151
|
+
forceReload: _propTypes2.default.bool,
|
|
152
|
+
handleCollapse: (0, _propTypes.requiredIf)(props => !!props.expanded || !!props.handleExpand, _propTypes2.default.func),
|
|
153
|
+
handleExpand: (0, _propTypes.requiredIf)(props => !!props.expanded || !!props.handleCollapse, _propTypes2.default.func),
|
|
139
154
|
|
|
140
155
|
/**
|
|
141
156
|
* All units provided to "highlighted" as path will be visually
|
|
@@ -145,7 +160,7 @@ OrganisationUnitTree.propTypes = {
|
|
|
145
160
|
* * searchResults
|
|
146
161
|
* * highlightSearchResults
|
|
147
162
|
*/
|
|
148
|
-
highlighted:
|
|
163
|
+
highlighted: _propTypes2.default.arrayOf(_propTypes3.orgUnitPathPropType),
|
|
149
164
|
|
|
150
165
|
/**
|
|
151
166
|
* An array of OU paths that will be expanded automatically
|
|
@@ -154,10 +169,10 @@ OrganisationUnitTree.propTypes = {
|
|
|
154
169
|
* and all its parent OUs separated by slashes (/)
|
|
155
170
|
* Note: This replaces "openFirstLevel" as that's redundant
|
|
156
171
|
*/
|
|
157
|
-
initiallyExpanded:
|
|
172
|
+
initiallyExpanded: _propTypes2.default.arrayOf(_propTypes3.orgUnitPathPropType),
|
|
158
173
|
|
|
159
174
|
/** When provided, the 'isUserDataViewFallback' option will be sent when requesting the org units */
|
|
160
|
-
isUserDataViewFallback:
|
|
175
|
+
isUserDataViewFallback: _propTypes2.default.bool,
|
|
161
176
|
|
|
162
177
|
/** Renders the actual node component for each leaf, can be used to
|
|
163
178
|
* customize the node. The default function just returns the node's
|
|
@@ -187,25 +202,25 @@ OrganisationUnitTree.propTypes = {
|
|
|
187
202
|
* disableSelection: boolean,
|
|
188
203
|
* }
|
|
189
204
|
* ``` */
|
|
190
|
-
renderNodeLabel:
|
|
205
|
+
renderNodeLabel: _propTypes2.default.func,
|
|
191
206
|
|
|
192
207
|
/** An array of paths of selected OUs. The path of an OU is the UIDs of the OU and all its parent OUs separated by slashes (`/`) */
|
|
193
|
-
selected:
|
|
208
|
+
selected: _propTypes2.default.arrayOf(_propTypes3.orgUnitPathPropType),
|
|
194
209
|
|
|
195
210
|
/** When set, no checkboxes will be displayed and only the first selected path in `selected` will be highlighted */
|
|
196
|
-
singleSelection:
|
|
211
|
+
singleSelection: _propTypes2.default.bool,
|
|
197
212
|
|
|
198
213
|
/** Turns off alphabetical sorting of units */
|
|
199
|
-
suppressAlphabeticalSorting:
|
|
214
|
+
suppressAlphabeticalSorting: _propTypes2.default.bool,
|
|
200
215
|
|
|
201
216
|
/** Called with the children's data that was loaded */
|
|
202
|
-
onChildrenLoaded:
|
|
217
|
+
onChildrenLoaded: _propTypes2.default.func,
|
|
203
218
|
|
|
204
219
|
/** Called with `{ path: string }` with the path of the parent of the level closed */
|
|
205
|
-
onCollapse:
|
|
220
|
+
onCollapse: _propTypes2.default.func,
|
|
206
221
|
|
|
207
222
|
/** Called with `{ path: string }` with the path of the parent of the level opened */
|
|
208
|
-
onExpand:
|
|
223
|
+
onExpand: _propTypes2.default.func
|
|
209
224
|
/**
|
|
210
225
|
* All units with ids (not paths!) provided
|
|
211
226
|
* to "idsThatShouldBeReloaded" will be reloaded
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _appRuntime = require("@dhis2/app-runtime");
|
|
4
|
+
|
|
5
|
+
var _enzyme = require("enzyme");
|
|
6
|
+
|
|
7
|
+
var _react = _interopRequireDefault(require("react"));
|
|
8
|
+
|
|
9
|
+
var _organisationUnitTree = require("./organisation-unit-tree.js");
|
|
10
|
+
|
|
11
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
12
|
+
|
|
13
|
+
describe('OrganisationUnitTree', () => {
|
|
14
|
+
const origError = console.error.bind(console);
|
|
15
|
+
const errorMock = jest.fn();
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
console.error = errorMock;
|
|
18
|
+
});
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
console.error = origError;
|
|
21
|
+
errorMock.mockClear();
|
|
22
|
+
});
|
|
23
|
+
describe('Controlled expanded props', () => {
|
|
24
|
+
describe('Missing props', () => {
|
|
25
|
+
it('should throw a prop-types error when "handleCollapse" is missing', () => {
|
|
26
|
+
(0, _enzyme.shallow)( /*#__PURE__*/_react.default.createElement(_appRuntime.CustomDataProvider, {
|
|
27
|
+
data: {}
|
|
28
|
+
}, /*#__PURE__*/_react.default.createElement(_organisationUnitTree.OrganisationUnitTree, {
|
|
29
|
+
roots: "/A001",
|
|
30
|
+
expanded: [],
|
|
31
|
+
onChange: () => {},
|
|
32
|
+
handleExpand: () => {}
|
|
33
|
+
})));
|
|
34
|
+
expect(errorMock).toHaveBeenCalledTimes(1);
|
|
35
|
+
expect(errorMock.mock.calls[0][0]).toMatch(/^Warning: Failed prop type: Invalid prop `handleCollapse` supplied to `OrganisationUnitTree`/);
|
|
36
|
+
});
|
|
37
|
+
it('should throw a prop-types error when "handleExpand" is missing', () => {
|
|
38
|
+
(0, _enzyme.shallow)( /*#__PURE__*/_react.default.createElement(_appRuntime.CustomDataProvider, {
|
|
39
|
+
data: {}
|
|
40
|
+
}, /*#__PURE__*/_react.default.createElement(_organisationUnitTree.OrganisationUnitTree, {
|
|
41
|
+
roots: "/A001",
|
|
42
|
+
expanded: [],
|
|
43
|
+
onChange: () => {},
|
|
44
|
+
handleCollapse: () => {}
|
|
45
|
+
})));
|
|
46
|
+
expect(errorMock).toHaveBeenCalledTimes(1);
|
|
47
|
+
expect(errorMock.mock.calls[0][0]).toMatch(/^Warning: Failed prop type: Invalid prop `handleExpand` supplied to `OrganisationUnitTree`/);
|
|
48
|
+
});
|
|
49
|
+
it('should throw a prop-types error when "expanded" is missing', () => {
|
|
50
|
+
(0, _enzyme.shallow)( /*#__PURE__*/_react.default.createElement(_appRuntime.CustomDataProvider, {
|
|
51
|
+
data: {}
|
|
52
|
+
}, /*#__PURE__*/_react.default.createElement(_organisationUnitTree.OrganisationUnitTree, {
|
|
53
|
+
roots: "/A001",
|
|
54
|
+
onChange: () => {},
|
|
55
|
+
handleCollapse: () => {},
|
|
56
|
+
handleExpand: () => {}
|
|
57
|
+
})));
|
|
58
|
+
expect(errorMock).toHaveBeenCalledTimes(1);
|
|
59
|
+
expect(errorMock.mock.calls[0][0]).toMatch(/^Warning: Failed prop type: Invalid prop `expanded` supplied to `OrganisationUnitTree`/);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
});
|
package/build/cjs/organisation-unit-tree/use-expanded/{helpers.js → create-expand-handlers.js}
RENAMED
|
@@ -3,33 +3,8 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.createExpandHandlers =
|
|
6
|
+
exports.createExpandHandlers = void 0;
|
|
7
7
|
|
|
8
|
-
/**
|
|
9
|
-
* @param {string} path
|
|
10
|
-
* @returns {string[]}
|
|
11
|
-
*/
|
|
12
|
-
const extractAllPathsFromPath = path => {
|
|
13
|
-
// remove leading slash and split by path delimiter/slashes
|
|
14
|
-
const segments = path.replace(/^\//, '').split('/');
|
|
15
|
-
const withSubPaths = segments.map((segment, index) => {
|
|
16
|
-
// take all segments from 0 to index and join them with the delimiter
|
|
17
|
-
return `/${segments.slice(0, index + 1).join('/')}`;
|
|
18
|
-
});
|
|
19
|
-
return withSubPaths;
|
|
20
|
-
};
|
|
21
|
-
/**
|
|
22
|
-
* @param {string[]} initiallyExpanded
|
|
23
|
-
* @returns {string[]}
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
exports.extractAllPathsFromPath = extractAllPathsFromPath;
|
|
28
|
-
|
|
29
|
-
const getInitiallyExpandedPaths = initiallyExpanded => initiallyExpanded.reduce((all, curPath) => {
|
|
30
|
-
const allPathsInCurPath = extractAllPathsFromPath(curPath);
|
|
31
|
-
return [...all, ...allPathsInCurPath];
|
|
32
|
-
}, []);
|
|
33
8
|
/**
|
|
34
9
|
* @param {Object} args
|
|
35
10
|
* @param {string[]} args.expanded
|
|
@@ -38,10 +13,6 @@ const getInitiallyExpandedPaths = initiallyExpanded => initiallyExpanded.reduce(
|
|
|
38
13
|
* @param {Function} [args.onCollapse]
|
|
39
14
|
* @returns {{ handleExpand: Function, handleCollapse: Function }}
|
|
40
15
|
*/
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
exports.getInitiallyExpandedPaths = getInitiallyExpandedPaths;
|
|
44
|
-
|
|
45
16
|
const createExpandHandlers = ({
|
|
46
17
|
expanded,
|
|
47
18
|
setExpanded,
|
|
@@ -1,20 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _createExpandHandlers = require("./create-expand-handlers.js");
|
|
4
4
|
|
|
5
|
-
describe('OrganisationUnitTree - useExpanded - getInitiallyExpandedPaths', () => {
|
|
6
|
-
const initiallyExpanded = ['/foo/bar/baz', '/foobar/barbaz/bazfoo'];
|
|
7
|
-
it('should include all initiallyExpanded paths in the returned expanded array', () => {
|
|
8
|
-
const actual = (0, _helpers.getInitiallyExpandedPaths)(initiallyExpanded);
|
|
9
|
-
const expected = expect.arrayContaining(initiallyExpanded);
|
|
10
|
-
expect(actual).toEqual(expected);
|
|
11
|
-
});
|
|
12
|
-
it('should include all sub paths of the paths in initiallyExpanded in the returned exanded array', () => {
|
|
13
|
-
const actual = (0, _helpers.getInitiallyExpandedPaths)(initiallyExpanded);
|
|
14
|
-
const expected = expect.arrayContaining(['/foo', '/foo/bar', '/foobar', '/foobar/barbaz']);
|
|
15
|
-
expect(actual).toEqual(expected);
|
|
16
|
-
});
|
|
17
|
-
});
|
|
18
5
|
describe('OrganisationUnitTree - useExpanded - createExpandHandlers', () => {
|
|
19
6
|
const initiallyExpanded = ['/foo/bar/baz', '/foobar/barbaz/bazfoo'];
|
|
20
7
|
const onExpand = jest.fn();
|
|
@@ -28,7 +15,7 @@ describe('OrganisationUnitTree - useExpanded - createExpandHandlers', () => {
|
|
|
28
15
|
const expanded = initiallyExpanded;
|
|
29
16
|
const {
|
|
30
17
|
handleExpand
|
|
31
|
-
} = (0,
|
|
18
|
+
} = (0, _createExpandHandlers.createExpandHandlers)({
|
|
32
19
|
expanded,
|
|
33
20
|
setExpanded
|
|
34
21
|
});
|
|
@@ -42,7 +29,7 @@ describe('OrganisationUnitTree - useExpanded - createExpandHandlers', () => {
|
|
|
42
29
|
const expanded = [...initiallyExpanded, '/a/new/path'];
|
|
43
30
|
const {
|
|
44
31
|
handleExpand
|
|
45
|
-
} = (0,
|
|
32
|
+
} = (0, _createExpandHandlers.createExpandHandlers)({
|
|
46
33
|
expanded,
|
|
47
34
|
setExpanded
|
|
48
35
|
});
|
|
@@ -56,7 +43,7 @@ describe('OrganisationUnitTree - useExpanded - createExpandHandlers', () => {
|
|
|
56
43
|
const expanded = [...initiallyExpanded, '/a/new/path'];
|
|
57
44
|
const {
|
|
58
45
|
handleCollapse
|
|
59
|
-
} = (0,
|
|
46
|
+
} = (0, _createExpandHandlers.createExpandHandlers)({
|
|
60
47
|
expanded,
|
|
61
48
|
setExpanded
|
|
62
49
|
});
|
|
@@ -70,7 +57,7 @@ describe('OrganisationUnitTree - useExpanded - createExpandHandlers', () => {
|
|
|
70
57
|
const expanded = initiallyExpanded;
|
|
71
58
|
const {
|
|
72
59
|
handleCollapse
|
|
73
|
-
} = (0,
|
|
60
|
+
} = (0, _createExpandHandlers.createExpandHandlers)({
|
|
74
61
|
expanded,
|
|
75
62
|
setExpanded
|
|
76
63
|
});
|
|
@@ -7,7 +7,9 @@ exports.useExpanded = void 0;
|
|
|
7
7
|
|
|
8
8
|
var _react = require("react");
|
|
9
9
|
|
|
10
|
-
var
|
|
10
|
+
var _index = require("../../get-all-expanded-paths/index.js");
|
|
11
|
+
|
|
12
|
+
var _createExpandHandlers = require("./create-expand-handlers.js");
|
|
11
13
|
|
|
12
14
|
/**
|
|
13
15
|
* @param {string[]} initiallyExpanded
|
|
@@ -15,13 +17,30 @@ var _helpers = require("./helpers.js");
|
|
|
15
17
|
* @param {Function} [onCollapse]
|
|
16
18
|
* @returns {{ expanded: string[], handleExpand: Function, handleCollapse: Function }}
|
|
17
19
|
*/
|
|
18
|
-
const useExpanded = (
|
|
19
|
-
|
|
20
|
+
const useExpanded = ({
|
|
21
|
+
initiallyExpanded,
|
|
22
|
+
onExpand,
|
|
23
|
+
onCollapse,
|
|
24
|
+
expandedControlled,
|
|
25
|
+
handleExpandControlled,
|
|
26
|
+
handleCollapseControlled
|
|
27
|
+
}) => {
|
|
28
|
+
const isControlled = !!expandedControlled;
|
|
29
|
+
const allInitiallyExpandedPaths = isControlled ? [] : (0, _index.getAllExpandedPaths)(initiallyExpanded);
|
|
20
30
|
const [expanded, setExpanded] = (0, _react.useState)(allInitiallyExpandedPaths);
|
|
31
|
+
|
|
32
|
+
if (isControlled) {
|
|
33
|
+
return {
|
|
34
|
+
expanded: expandedControlled,
|
|
35
|
+
handleExpand: handleExpandControlled,
|
|
36
|
+
handleCollapse: handleCollapseControlled
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
21
40
|
const {
|
|
22
41
|
handleExpand,
|
|
23
42
|
handleCollapse
|
|
24
|
-
} = (0,
|
|
43
|
+
} = (0, _createExpandHandlers.createExpandHandlers)({
|
|
25
44
|
expanded,
|
|
26
45
|
setExpanded,
|
|
27
46
|
onExpand,
|
|
@@ -2,28 +2,38 @@
|
|
|
2
2
|
|
|
3
3
|
var _react = require("react");
|
|
4
4
|
|
|
5
|
-
var
|
|
5
|
+
var _index = require("../../get-all-expanded-paths/index.js");
|
|
6
|
+
|
|
7
|
+
var _createExpandHandlers = require("./create-expand-handlers.js");
|
|
6
8
|
|
|
7
9
|
var _useExpanded = require("./use-expanded.js");
|
|
8
10
|
|
|
9
11
|
jest.mock('react', () => ({
|
|
10
12
|
useState: jest.fn(initialValue => [initialValue, () => null])
|
|
11
13
|
}));
|
|
12
|
-
jest.mock('
|
|
13
|
-
|
|
14
|
+
jest.mock('../../get-all-expanded-paths/index.js', () => ({
|
|
15
|
+
getAllExpandedPaths: jest.fn(input => input)
|
|
16
|
+
}));
|
|
17
|
+
jest.mock('./create-expand-handlers.js', () => ({
|
|
14
18
|
createExpandHandlers: jest.fn(() => ({
|
|
15
19
|
handleCollapse: () => null,
|
|
16
20
|
handleExpand: () => null
|
|
17
21
|
}))
|
|
18
22
|
}));
|
|
19
23
|
describe('OrganisationUnitTree - useExpanded hook', () => {
|
|
20
|
-
|
|
21
|
-
|
|
24
|
+
const onExpand = jest.fn();
|
|
25
|
+
const onCollapse = jest.fn();
|
|
26
|
+
it('should use the getAllExpandedPaths helper to determine the initial state', () => {
|
|
27
|
+
_index.getAllExpandedPaths.mockImplementationOnce(input => [...input, '/foo/bar/baz']);
|
|
22
28
|
|
|
23
29
|
const expected = ['/foo', '/foo/bar', '/foo/bar/baz'];
|
|
24
30
|
const {
|
|
25
31
|
expanded: actual
|
|
26
|
-
} = (0, _useExpanded.useExpanded)(
|
|
32
|
+
} = (0, _useExpanded.useExpanded)({
|
|
33
|
+
initiallyExpanded: ['/foo', '/foo/bar'],
|
|
34
|
+
onExpand,
|
|
35
|
+
onCollapse
|
|
36
|
+
});
|
|
27
37
|
expect(actual).toEqual(expected);
|
|
28
38
|
});
|
|
29
39
|
it('should pass the setExpanded function from seState to createExpandHandlers', () => {
|
|
@@ -31,9 +41,33 @@ describe('OrganisationUnitTree - useExpanded hook', () => {
|
|
|
31
41
|
|
|
32
42
|
_react.useState.mockImplementationOnce(() => [[], setExpanded]);
|
|
33
43
|
|
|
34
|
-
(0, _useExpanded.useExpanded)(
|
|
35
|
-
|
|
44
|
+
(0, _useExpanded.useExpanded)({
|
|
45
|
+
initiallyExpanded: [],
|
|
46
|
+
onExpand,
|
|
47
|
+
onCollapse
|
|
48
|
+
});
|
|
49
|
+
expect(_createExpandHandlers.createExpandHandlers).toHaveBeenCalledWith(expect.objectContaining({
|
|
36
50
|
setExpanded
|
|
37
51
|
}));
|
|
38
52
|
});
|
|
53
|
+
it('should return the controlled values if all of them are being provided', () => {
|
|
54
|
+
const expandedControlled = ['/foo'];
|
|
55
|
+
const handleExpandControlled = jest.fn();
|
|
56
|
+
const handleCollapseControlled = jest.fn();
|
|
57
|
+
const {
|
|
58
|
+
expanded,
|
|
59
|
+
handleExpand,
|
|
60
|
+
handleCollapse
|
|
61
|
+
} = (0, _useExpanded.useExpanded)({
|
|
62
|
+
initiallyExpanded: [],
|
|
63
|
+
onExpand,
|
|
64
|
+
onCollapse,
|
|
65
|
+
expandedControlled,
|
|
66
|
+
handleExpandControlled,
|
|
67
|
+
handleCollapseControlled
|
|
68
|
+
});
|
|
69
|
+
expect(expanded).toBe(expandedControlled);
|
|
70
|
+
expect(handleExpand).toBe(handleExpandControlled);
|
|
71
|
+
expect(handleCollapse).toBe(handleCollapseControlled);
|
|
72
|
+
});
|
|
39
73
|
});
|