@itwin/itwinui-react 1.30.0 → 1.32.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.
Files changed (66) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/cjs/core/Badge/Badge.js +2 -2
  3. package/cjs/core/Checkbox/Checkbox.d.ts +13 -0
  4. package/cjs/core/Checkbox/Checkbox.js +15 -22
  5. package/cjs/core/ColorPicker/ColorBuilder.js +7 -8
  6. package/cjs/core/ColorPicker/ColorSwatch.d.ts +1 -1
  7. package/cjs/core/ColorPicker/ColorSwatch.js +2 -2
  8. package/cjs/core/ComboBox/ComboBox.d.ts +11 -0
  9. package/cjs/core/ComboBox/ComboBox.js +93 -55
  10. package/cjs/core/ExpandableBlock/ExpandableBlock.d.ts +6 -0
  11. package/cjs/core/ExpandableBlock/ExpandableBlock.js +3 -2
  12. package/cjs/core/Menu/Menu.js +3 -3
  13. package/cjs/core/Menu/MenuItem.js +1 -1
  14. package/cjs/core/Radio/Radio.d.ts +13 -0
  15. package/cjs/core/Radio/Radio.js +7 -8
  16. package/cjs/core/Select/Select.js +23 -8
  17. package/cjs/core/Table/TablePaginator.js +7 -9
  18. package/cjs/core/Table/filters/DateRangeFilter/DatePickerInput.js +1 -1
  19. package/cjs/core/Tile/Tile.js +4 -4
  20. package/cjs/core/Tree/Tree.d.ts +123 -0
  21. package/cjs/core/Tree/Tree.js +177 -0
  22. package/cjs/core/Tree/TreeContext.d.ts +25 -0
  23. package/cjs/core/Tree/TreeContext.js +20 -0
  24. package/cjs/core/Tree/TreeNode.d.ts +87 -0
  25. package/cjs/core/Tree/TreeNode.js +169 -0
  26. package/cjs/core/Tree/TreeNodeExpander.d.ts +8 -0
  27. package/cjs/core/Tree/TreeNodeExpander.js +46 -0
  28. package/cjs/core/Tree/index.d.ts +6 -0
  29. package/cjs/core/Tree/index.js +13 -0
  30. package/cjs/core/index.d.ts +2 -0
  31. package/cjs/core/index.js +5 -1
  32. package/cjs/core/utils/components/Popover.js +1 -1
  33. package/cjs/core/utils/functions/focusable.js +6 -2
  34. package/esm/core/Badge/Badge.js +2 -2
  35. package/esm/core/Checkbox/Checkbox.d.ts +13 -0
  36. package/esm/core/Checkbox/Checkbox.js +15 -22
  37. package/esm/core/ColorPicker/ColorBuilder.js +7 -8
  38. package/esm/core/ColorPicker/ColorSwatch.d.ts +1 -1
  39. package/esm/core/ColorPicker/ColorSwatch.js +2 -2
  40. package/esm/core/ComboBox/ComboBox.d.ts +11 -0
  41. package/esm/core/ComboBox/ComboBox.js +94 -56
  42. package/esm/core/ExpandableBlock/ExpandableBlock.d.ts +6 -0
  43. package/esm/core/ExpandableBlock/ExpandableBlock.js +3 -2
  44. package/esm/core/Menu/Menu.js +3 -3
  45. package/esm/core/Menu/MenuItem.js +1 -1
  46. package/esm/core/Radio/Radio.d.ts +13 -0
  47. package/esm/core/Radio/Radio.js +7 -8
  48. package/esm/core/Select/Select.js +23 -8
  49. package/esm/core/Table/TablePaginator.js +7 -9
  50. package/esm/core/Table/filters/DateRangeFilter/DatePickerInput.js +1 -1
  51. package/esm/core/Tile/Tile.js +4 -4
  52. package/esm/core/Tree/Tree.d.ts +123 -0
  53. package/esm/core/Tree/Tree.js +170 -0
  54. package/esm/core/Tree/TreeContext.d.ts +25 -0
  55. package/esm/core/Tree/TreeContext.js +13 -0
  56. package/esm/core/Tree/TreeNode.d.ts +87 -0
  57. package/esm/core/Tree/TreeNode.js +162 -0
  58. package/esm/core/Tree/TreeNodeExpander.d.ts +8 -0
  59. package/esm/core/Tree/TreeNodeExpander.js +39 -0
  60. package/esm/core/Tree/index.d.ts +6 -0
  61. package/esm/core/Tree/index.js +7 -0
  62. package/esm/core/index.d.ts +2 -0
  63. package/esm/core/index.js +1 -0
  64. package/esm/core/utils/components/Popover.js +1 -1
  65. package/esm/core/utils/functions/focusable.js +6 -2
  66. package/package.json +2 -2
@@ -37,6 +37,7 @@ require("@itwin/itwinui-css/css/inputs.css");
37
37
  /**
38
38
  * Basic radio input component
39
39
  * @example
40
+ * <Radio />
40
41
  * <Radio label='Radio' />
41
42
  * <Radio disabled={true} label='Radio' />
42
43
  * <Radio status='positive' label='Positive' />
@@ -44,8 +45,8 @@ require("@itwin/itwinui-css/css/inputs.css");
44
45
  * <Radio status='negative' label='Negative' />
45
46
  */
46
47
  exports.Radio = react_1.default.forwardRef(function (props, ref) {
47
- var _a;
48
- var className = props.className, _b = props.disabled, disabled = _b === void 0 ? false : _b, label = props.label, status = props.status, style = props.style, checkmarkClassName = props.checkmarkClassName, checkmarkStyle = props.checkmarkStyle, _c = props.setFocus, setFocus = _c === void 0 ? false : _c, rest = __rest(props, ["className", "disabled", "label", "status", "style", "checkmarkClassName", "checkmarkStyle", "setFocus"]);
48
+ var _a, _b;
49
+ var className = props.className, _c = props.disabled, disabled = _c === void 0 ? false : _c, label = props.label, status = props.status, style = props.style, checkmarkClassName = props.checkmarkClassName, checkmarkStyle = props.checkmarkStyle, _d = props.setFocus, setFocus = _d === void 0 ? false : _d, rest = __rest(props, ["className", "disabled", "label", "status", "style", "checkmarkClassName", "checkmarkStyle", "setFocus"]);
49
50
  (0, utils_1.useTheme)();
50
51
  var inputElementRef = react_1.default.useRef(null);
51
52
  var refs = (0, utils_1.useMergedRefs)(inputElementRef, ref);
@@ -54,11 +55,9 @@ exports.Radio = react_1.default.forwardRef(function (props, ref) {
54
55
  inputElementRef.current.focus();
55
56
  }
56
57
  }, [setFocus]);
57
- return (react_1.default.createElement("label", { className: (0, classnames_1.default)('iui-radio', (_a = { 'iui-disabled': disabled }, _a["iui-" + status] = !!status, _a), className), style: style },
58
- react_1.default.createElement("input", __assign({ disabled: disabled, type: 'radio', ref: refs }, rest)),
59
- react_1.default.createElement("span", { className: (0, classnames_1.default)('iui-radio-dot', checkmarkClassName), style: checkmarkStyle },
60
- react_1.default.createElement("svg", { viewBox: '0 0 16 16', "aria-hidden": 'true', focusable: 'false' },
61
- react_1.default.createElement("circle", { cx: '8', cy: '8', r: '4' }))),
62
- label && react_1.default.createElement("span", { className: 'iui-label' }, label)));
58
+ var radio = (react_1.default.createElement("input", __assign({ className: (0, classnames_1.default)('iui-radio', className && (_a = {}, _a[className] = !label, _a), checkmarkClassName), style: __assign(__assign({}, (!label && style)), checkmarkStyle), disabled: disabled, type: 'radio', ref: refs }, rest)));
59
+ return !label ? (radio) : (react_1.default.createElement("label", { className: (0, classnames_1.default)('iui-radio-wrapper', (_b = { 'iui-disabled': disabled }, _b["iui-" + status] = !!status, _b), className), style: style },
60
+ radio,
61
+ label && react_1.default.createElement("span", { className: 'iui-radio-label' }, label)));
63
62
  });
64
63
  exports.default = exports.Radio;
@@ -36,6 +36,7 @@ var DropdownMenu_1 = require("../DropdownMenu");
36
36
  var MenuItem_1 = require("../Menu/MenuItem");
37
37
  var utils_1 = require("../utils");
38
38
  require("@itwin/itwinui-css/css/inputs.css");
39
+ var CaretDownSmall_1 = __importDefault(require("@itwin/itwinui-icons-react/cjs/icons/CaretDownSmall"));
39
40
  /**
40
41
  * Select component to select value from options.
41
42
  * Generic type is used for value. It prevents you from mistakenly using other types in `options`, `value` and `onChange`.
@@ -97,6 +98,7 @@ var Select = function (props) {
97
98
  var _f = react_1.default.useState(0), minWidth = _f[0], setMinWidth = _f[1];
98
99
  var toggle = function () { return setIsOpen(function (open) { return !open; }); };
99
100
  var selectRef = react_1.default.useRef(null);
101
+ var toggleButtonRef = react_1.default.useRef(null);
100
102
  var onShowHandler = react_1.default.useCallback(function (instance) {
101
103
  setIsOpen(true);
102
104
  onShow === null || onShow === void 0 ? void 0 : onShow(instance);
@@ -143,13 +145,20 @@ var Select = function (props) {
143
145
  }
144
146
  return options.find(function (option) { return option.value === value; });
145
147
  }, [options, value]);
146
- return (react_1.default.createElement("div", __assign({ className: (0, classnames_1.default)('iui-select', (_a = {}, _a["iui-" + size] = !!size, _a), className), "aria-expanded": isOpen, "aria-haspopup": 'listbox', style: style }, rest),
147
- react_1.default.createElement(DropdownMenu_1.DropdownMenu, __assign({ menuItems: menuItems, placement: 'bottom-start', className: (0, classnames_1.default)('iui-scroll', menuClassName), style: __assign({ minWidth: minWidth, maxWidth: "min(" + minWidth * 2 + "px, 90vw)", maxHeight: "300px" }, menuStyle), role: 'listbox', onShow: onShowHandler, onHide: onHideHandler, disabled: disabled }, popoverProps, { visible: isOpen }),
148
- react_1.default.createElement("div", { ref: selectRef, className: (0, classnames_1.default)('iui-select-button', {
149
- 'iui-placeholder': !selectedItem && !!placeholder,
150
- 'iui-disabled': disabled,
151
- 'iui-active': isOpen,
152
- }), onClick: function () { return !disabled && toggle(); }, onKeyDown: function (e) { return !disabled && onKeyDown(e, toggle); }, tabIndex: !disabled ? 0 : undefined },
148
+ return (react_1.default.createElement("div", __assign({ className: (0, classnames_1.default)('iui-input-with-icon', className), "aria-expanded": isOpen, "aria-haspopup": 'listbox', style: style }, rest),
149
+ react_1.default.createElement(DropdownMenu_1.DropdownMenu, __assign({ menuItems: menuItems, placement: 'bottom-start', className: (0, classnames_1.default)('iui-scroll', menuClassName), style: __assign({ minWidth: minWidth, maxWidth: "min(" + minWidth * 2 + "px, 90vw)", maxHeight: "300px" }, menuStyle), role: 'listbox', onShow: onShowHandler, onHide: onHideHandler, disabled: disabled }, popoverProps, { visible: isOpen, onClickOutside: function (_, _a) {
150
+ var _b;
151
+ var target = _a.target;
152
+ if (!((_b = toggleButtonRef.current) === null || _b === void 0 ? void 0 : _b.contains(target))) {
153
+ setIsOpen(false);
154
+ }
155
+ } }),
156
+ react_1.default.createElement("div", { ref: selectRef, className: (0, classnames_1.default)('iui-select-button', (_a = {
157
+ 'iui-placeholder': !selectedItem && !!placeholder,
158
+ 'iui-disabled': disabled
159
+ },
160
+ _a["iui-" + size] = !!size,
161
+ _a)), onClick: function () { return !disabled && toggle(); }, onKeyDown: function (e) { return !disabled && onKeyDown(e, toggle); }, tabIndex: !disabled ? 0 : undefined },
153
162
  !selectedItem && react_1.default.createElement("span", { className: 'iui-content' }, placeholder),
154
163
  selectedItem &&
155
164
  selectedItemRenderer &&
@@ -159,7 +168,13 @@ var Select = function (props) {
159
168
  react_1.default.cloneElement(selectedItem.icon, {
160
169
  className: (0, classnames_1.default)(selectedItem === null || selectedItem === void 0 ? void 0 : selectedItem.icon.props.className, 'iui-icon'),
161
170
  }),
162
- react_1.default.createElement("span", { className: 'iui-content' }, selectedItem.label)))))));
171
+ react_1.default.createElement("span", { className: 'iui-content' }, selectedItem.label))))),
172
+ react_1.default.createElement("span", { ref: toggleButtonRef, className: (0, classnames_1.default)('iui-end-icon', {
173
+ 'iui-actionable': !disabled,
174
+ 'iui-disabled': disabled,
175
+ 'iui-open': isOpen,
176
+ }), onClick: function () { return !disabled && toggle(); } },
177
+ react_1.default.createElement(CaretDownSmall_1.default, { "aria-hidden": true }))));
163
178
  };
164
179
  exports.Select = Select;
165
180
  exports.default = exports.Select;
@@ -85,11 +85,10 @@ var TablePaginator = function (props) {
85
85
  var buttonSize = size != 'default' ? 'small' : undefined;
86
86
  var pageButton = react_1.default.useCallback(function (index, tabIndex) {
87
87
  if (tabIndex === void 0) { tabIndex = index === focusedIndex ? 0 : -1; }
88
- return (react_1.default.createElement("div", { key: index },
89
- react_1.default.createElement("button", { className: (0, classnames_1.default)('iui-paginator-page-button', {
90
- 'iui-active': index === currentPage,
91
- 'iui-paginator-page-button-small': buttonSize === 'small',
92
- }), onClick: function () { return onPageChange(index); }, "aria-current": index === currentPage, "aria-label": localization.goToPageLabel(index + 1), tabIndex: tabIndex }, index + 1)));
88
+ return (react_1.default.createElement("button", { key: index, className: (0, classnames_1.default)('iui-paginator-page-button', {
89
+ 'iui-active': index === currentPage,
90
+ 'iui-paginator-page-button-small': buttonSize === 'small',
91
+ }), onClick: function () { return onPageChange(index); }, "aria-current": index === currentPage, "aria-label": localization.goToPageLabel(index + 1), tabIndex: tabIndex }, index + 1));
93
92
  }, [focusedIndex, currentPage, localization, buttonSize, onPageChange]);
94
93
  var totalPagesCount = Math.ceil(totalRowsCount / pageSize);
95
94
  var pageList = react_1.default.useMemo(function () {
@@ -151,10 +150,9 @@ var TablePaginator = function (props) {
151
150
  var hasNoRows = totalPagesCount === 0;
152
151
  var showPagesList = totalPagesCount > 1 || isLoading;
153
152
  var showPageSizeList = pageSizeList && onPageSizeChange && !!totalRowsCount;
154
- var ellipsis = (react_1.default.createElement("div", null,
155
- react_1.default.createElement("span", { className: (0, classnames_1.default)('iui-paginator-ellipsis', {
156
- 'iui-paginator-ellipsis-small': size === 'small',
157
- }) }, "\u2026")));
153
+ var ellipsis = (react_1.default.createElement("span", { className: (0, classnames_1.default)('iui-paginator-ellipsis', {
154
+ 'iui-paginator-ellipsis-small': size === 'small',
155
+ }) }, "\u2026"));
158
156
  var noRowsContent = (react_1.default.createElement(react_1.default.Fragment, null, isLoading ? (react_1.default.createElement(ProgressIndicators_1.ProgressRadial, { indeterminate: true, size: 'small' })) : (react_1.default.createElement(Buttons_1.Button, { styleType: 'borderless', disabled: true, size: buttonSize }, "1"))));
159
157
  if (!showPagesList && !showPageSizeList) {
160
158
  return null;
@@ -68,7 +68,7 @@ var DatePickerInput = function (props) {
68
68
  if (!((_a = buttonRef.current) === null || _a === void 0 ? void 0 : _a.contains(e.target))) {
69
69
  close();
70
70
  }
71
- } },
71
+ }, appendTo: 'parent' },
72
72
  react_1.default.createElement(LabeledInput_1.LabeledInput, __assign({ displayStyle: 'inline', value: inputValue, onChange: onInputChange, onClick: close, svgIcon: react_1.default.createElement(Buttons_1.IconButton, { styleType: 'borderless', onClick: function () { return setIsVisible(function (v) { return !v; }); }, ref: buttonRef },
73
73
  react_1.default.createElement(Calendar_1.default, null)) }, rest))));
74
74
  };
@@ -88,7 +88,6 @@ var Tile = function (props) {
88
88
  moreOptions && (react_1.default.createElement(DropdownMenu_1.DropdownMenu, { onShow: showMenu, onHide: hideMenu, menuItems: function (close) {
89
89
  return moreOptions.map(function (option) {
90
90
  return react_1.default.cloneElement(option, {
91
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
92
91
  onClick: function (value) {
93
92
  var _a, _b;
94
93
  close();
@@ -97,10 +96,11 @@ var Tile = function (props) {
97
96
  });
98
97
  });
99
98
  } },
100
- react_1.default.createElement(Buttons_1.IconButton, { styleType: 'borderless', size: 'small', className: (0, classnames_1.default)('iui-tile-more-options', {
99
+ react_1.default.createElement("div", { className: (0, classnames_1.default)('iui-tile-more-options', {
101
100
  'iui-visible': isMenuVisible,
102
- }), "aria-label": 'More options' },
103
- react_1.default.createElement(More_1.default, null)))),
101
+ }) },
102
+ react_1.default.createElement(Buttons_1.IconButton, { styleType: 'borderless', size: 'small', "aria-label": 'More options' },
103
+ react_1.default.createElement(More_1.default, null))))),
104
104
  children),
105
105
  buttons && react_1.default.createElement("div", { className: 'iui-tile-buttons' }, buttons)));
106
106
  };
@@ -0,0 +1,123 @@
1
+ /// <reference types="react" />
2
+ import { CommonProps } from '../utils';
3
+ import '@itwin/itwinui-css/css/tree.css';
4
+ export declare type NodeData<T> = {
5
+ /**
6
+ * Array of the child nodes contained in the node.
7
+ */
8
+ subNodes?: Array<T>;
9
+ /**
10
+ * Unique id of the node.
11
+ */
12
+ nodeId: string;
13
+ /**
14
+ * Custom type used to map type `T` to `NodeData`
15
+ */
16
+ node: T;
17
+ /**
18
+ * Flag whether the node is expanded.
19
+ */
20
+ isExpanded?: boolean;
21
+ /**
22
+ * Flag whether the node is disabled.
23
+ */
24
+ isDisabled?: boolean;
25
+ /**
26
+ * Flag whether the node is selected.
27
+ */
28
+ isSelected?: boolean;
29
+ /**
30
+ * Flag whether the node has sub-nodes.
31
+ * Used to determine if node should be expandable.
32
+ */
33
+ hasSubNodes: boolean;
34
+ };
35
+ export declare type NodeRenderProps<T> = Omit<NodeData<T>, 'subNodes'>;
36
+ export declare type TreeProps<T> = {
37
+ /**
38
+ * Render function that should return the node element.
39
+ * Recommended to use `TreeNode` component.
40
+ * Must be memoized.
41
+ * @example
42
+ * const nodeRenderer = React.useCallback(({ node, ...rest }: NodeRenderProps<DataType>) => (
43
+ * <TreeNode
44
+ * label={node.label}
45
+ * onNodeExpanded={onNodeExpanded}
46
+ * {...rest}
47
+ * />
48
+ * ), [onNodeExpanded])
49
+ */
50
+ nodeRenderer: (props: NodeRenderProps<T>) => JSX.Element;
51
+ /**
52
+ * Array of custom data used for `TreeNodes` inside `Tree`.
53
+ */
54
+ data: T[];
55
+ /**
56
+ * Function that maps your `data` entry to `NodeData` that has all info about the node state.
57
+ * It will be used to render a tree node in `nodeRenderer`.
58
+ * Must be memoized.
59
+ * @example
60
+ * const getNode = React.useCallback((node: DemoData): NodeData<DemoData> => {
61
+ * return {
62
+ * subNodes: node.subItems,
63
+ * nodeId: node.id,
64
+ * node,
65
+ * isExpanded: expandedNodes[node.id],
66
+ * hasSubNodes: node.subItems.length > 0,
67
+ * };
68
+ * }, [expandedNodes]);
69
+ */
70
+ getNode: (node: T) => NodeData<T>;
71
+ } & Omit<CommonProps, 'title'>;
72
+ /**
73
+ * Tree component used to display a hierarchical structure of `TreeNodes`.
74
+ * User should control state of expanded, selected and disabled nodes using `getNode` prop.
75
+ * @example
76
+ type DemoData = {
77
+ id: string;
78
+ label: string;
79
+ subItems: DemoData[];
80
+ };
81
+
82
+ const data: Array<DemoData> = [
83
+ {
84
+ id: 'Node-1',
85
+ label: 'Facility 1',
86
+ subItems: [{ id: 'Node-1-1', label: 'Unit 1', subItems: [] }],
87
+ },
88
+ {
89
+ id: 'Node-2',
90
+ label: 'Facility 2',
91
+ subItems: [{ id: 'Node-2-1', label: 'Unit 2', subItems: [] }],
92
+ },
93
+ ];
94
+
95
+ const [expandedNodes, setExpandedNodes] = React.useState<Record<string, boolean>>({});
96
+ const onNodeExpanded = React.useCallback((nodeId: string, isExpanded: boolean) => {
97
+ setExpandedNodes((oldExpanded) => ({ ...oldExpanded, [nodeId]: isExpanded }));
98
+ }, []);
99
+
100
+ const getNode = React.useCallback((node: DemoData): NodeData<DemoData> => {
101
+ return {
102
+ subNodes: node.subItems,
103
+ nodeId: node.id,
104
+ node,
105
+ isExpanded: expandedNodes[node.id],
106
+ hasSubNodes: node.subItems.length > 0,
107
+ };
108
+ }, [expandedNodes]);
109
+
110
+ <Tree<DemoData>
111
+ data={data}
112
+ getNode={getNode}
113
+ nodeRenderer={React.useCallback(({ node, ...rest }) => (
114
+ <TreeNode
115
+ label={node.label}
116
+ onNodeExpanded={onNodeExpanded}
117
+ {...rest}
118
+ />
119
+ ), [onNodeExpanded])}
120
+ />
121
+ */
122
+ export declare const Tree: <T>(props: TreeProps<T>) => JSX.Element;
123
+ export default Tree;
@@ -0,0 +1,177 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __rest = (this && this.__rest) || function (s, e) {
14
+ var t = {};
15
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
16
+ t[p] = s[p];
17
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
18
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
19
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
20
+ t[p[i]] = s[p[i]];
21
+ }
22
+ return t;
23
+ };
24
+ var __importDefault = (this && this.__importDefault) || function (mod) {
25
+ return (mod && mod.__esModule) ? mod : { "default": mod };
26
+ };
27
+ Object.defineProperty(exports, "__esModule", { value: true });
28
+ exports.Tree = void 0;
29
+ /*---------------------------------------------------------------------------------------------
30
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
31
+ * See LICENSE.md in the project root for license terms and full copyright notice.
32
+ *--------------------------------------------------------------------------------------------*/
33
+ var react_1 = __importDefault(require("react"));
34
+ var utils_1 = require("../utils");
35
+ require("@itwin/itwinui-css/css/tree.css");
36
+ var classnames_1 = __importDefault(require("classnames"));
37
+ var TreeContext_1 = require("./TreeContext");
38
+ /**
39
+ * Tree component used to display a hierarchical structure of `TreeNodes`.
40
+ * User should control state of expanded, selected and disabled nodes using `getNode` prop.
41
+ * @example
42
+ type DemoData = {
43
+ id: string;
44
+ label: string;
45
+ subItems: DemoData[];
46
+ };
47
+
48
+ const data: Array<DemoData> = [
49
+ {
50
+ id: 'Node-1',
51
+ label: 'Facility 1',
52
+ subItems: [{ id: 'Node-1-1', label: 'Unit 1', subItems: [] }],
53
+ },
54
+ {
55
+ id: 'Node-2',
56
+ label: 'Facility 2',
57
+ subItems: [{ id: 'Node-2-1', label: 'Unit 2', subItems: [] }],
58
+ },
59
+ ];
60
+
61
+ const [expandedNodes, setExpandedNodes] = React.useState<Record<string, boolean>>({});
62
+ const onNodeExpanded = React.useCallback((nodeId: string, isExpanded: boolean) => {
63
+ setExpandedNodes((oldExpanded) => ({ ...oldExpanded, [nodeId]: isExpanded }));
64
+ }, []);
65
+
66
+ const getNode = React.useCallback((node: DemoData): NodeData<DemoData> => {
67
+ return {
68
+ subNodes: node.subItems,
69
+ nodeId: node.id,
70
+ node,
71
+ isExpanded: expandedNodes[node.id],
72
+ hasSubNodes: node.subItems.length > 0,
73
+ };
74
+ }, [expandedNodes]);
75
+
76
+ <Tree<DemoData>
77
+ data={data}
78
+ getNode={getNode}
79
+ nodeRenderer={React.useCallback(({ node, ...rest }) => (
80
+ <TreeNode
81
+ label={node.label}
82
+ onNodeExpanded={onNodeExpanded}
83
+ {...rest}
84
+ />
85
+ ), [onNodeExpanded])}
86
+ />
87
+ */
88
+ var Tree = function (props) {
89
+ var data = props.data, className = props.className, nodeRenderer = props.nodeRenderer, getNode = props.getNode, rest = __rest(props, ["data", "className", "nodeRenderer", "getNode"]);
90
+ (0, utils_1.useTheme)();
91
+ var treeRef = react_1.default.useRef(null);
92
+ var focusedIndex = react_1.default.useRef(0);
93
+ react_1.default.useEffect(function () {
94
+ focusedIndex.current = 0;
95
+ }, [data]);
96
+ var getFocusableNodes = react_1.default.useCallback(function () {
97
+ var focusableItems = (0, utils_1.getFocusableElements)(treeRef.current);
98
+ // Filter out focusable elements that are inside each node, e.g. checkbox
99
+ return focusableItems.filter(function (i) { return !focusableItems.some(function (p) { return p.contains(i.parentElement); }); });
100
+ }, []);
101
+ var handleKeyDown = function (event) {
102
+ var items = getFocusableNodes();
103
+ if (!(items === null || items === void 0 ? void 0 : items.length)) {
104
+ return;
105
+ }
106
+ var activeIndex = items.findIndex(function (el) { var _a; return el.contains((_a = treeRef.current) === null || _a === void 0 ? void 0 : _a.ownerDocument.activeElement); });
107
+ var currentIndex = activeIndex > -1 ? activeIndex : 0;
108
+ switch (event.key) {
109
+ case 'ArrowUp': {
110
+ event.preventDefault();
111
+ var newIndex = Math.max(0, currentIndex - 1);
112
+ items[newIndex].focus();
113
+ focusedIndex.current = newIndex;
114
+ break;
115
+ }
116
+ case 'ArrowDown': {
117
+ event.preventDefault();
118
+ var newIndex = Math.min(items.length - 1, currentIndex + 1);
119
+ items[newIndex].focus();
120
+ focusedIndex.current = newIndex;
121
+ break;
122
+ }
123
+ default:
124
+ break;
125
+ }
126
+ };
127
+ var _a = react_1.default.useMemo(function () {
128
+ var flatList = [];
129
+ var firstLevelNodes = [];
130
+ var flattenNodes = function (nodes, depth, parentNode) {
131
+ if (nodes === void 0) { nodes = []; }
132
+ if (depth === void 0) { depth = 0; }
133
+ var nodeIdList = Array();
134
+ nodes.forEach(function (element, index) {
135
+ var _a = getNode(element), subNodes = _a.subNodes, nodeProps = __rest(_a, ["subNodes"]);
136
+ var flatNode = {
137
+ nodeProps: nodeProps,
138
+ depth: depth,
139
+ parentNode: parentNode,
140
+ indexInGroup: index,
141
+ };
142
+ nodeIdList.push(flatNode.nodeProps.nodeId);
143
+ flatList.push(flatNode);
144
+ if (depth === 0) {
145
+ firstLevelNodes.push(flatNode);
146
+ }
147
+ if (flatNode.nodeProps.isExpanded) {
148
+ var subNodeIds = flattenNodes(subNodes, depth + 1, flatNode);
149
+ flatNode.subNodeIds = subNodeIds;
150
+ }
151
+ });
152
+ return nodeIdList;
153
+ };
154
+ flattenNodes(data);
155
+ return [flatList, firstLevelNodes];
156
+ }, [data, getNode]), flatNodesList = _a[0], firstLevelNodesList = _a[1];
157
+ return (react_1.default.createElement("ul", __assign({ className: (0, classnames_1.default)('iui-tree', className), role: 'tree', onKeyDown: handleKeyDown, ref: treeRef, tabIndex: 0, onFocus: function () {
158
+ var _a;
159
+ var items = getFocusableNodes();
160
+ if (items.length > 0) {
161
+ (_a = items[focusedIndex.current]) === null || _a === void 0 ? void 0 : _a.focus();
162
+ }
163
+ } }, rest), flatNodesList.map(function (flatNode) {
164
+ var _a, _b, _c, _d;
165
+ return (react_1.default.createElement(TreeContext_1.TreeContext.Provider, { key: flatNode.nodeProps.nodeId, value: {
166
+ nodeDepth: flatNode.depth,
167
+ subNodeIds: flatNode.subNodeIds,
168
+ groupSize: flatNode.depth === 0
169
+ ? firstLevelNodesList.length
170
+ : (_c = (_b = (_a = flatNode.parentNode) === null || _a === void 0 ? void 0 : _a.subNodeIds) === null || _b === void 0 ? void 0 : _b.length) !== null && _c !== void 0 ? _c : 0,
171
+ indexInGroup: flatNode.indexInGroup,
172
+ parentNodeId: (_d = flatNode.parentNode) === null || _d === void 0 ? void 0 : _d.nodeProps.nodeId,
173
+ } }, nodeRenderer(flatNode.nodeProps)));
174
+ })));
175
+ };
176
+ exports.Tree = Tree;
177
+ exports.default = exports.Tree;
@@ -0,0 +1,25 @@
1
+ import React from 'react';
2
+ export declare type TreeContextProps = {
3
+ /**
4
+ * Depth of the node.
5
+ */
6
+ nodeDepth: number;
7
+ /**
8
+ * List of sub-node IDs. Used for an accessibility attribute and keyboard navigation.
9
+ */
10
+ subNodeIds?: string[];
11
+ /**
12
+ * ID of the parent node. Used for keyboard navigation.
13
+ */
14
+ parentNodeId?: string;
15
+ /**
16
+ * Number of nodes that are under the same parent node or in the root. Used for an accessibility attribute.
17
+ */
18
+ groupSize: number;
19
+ /**
20
+ * Node index in the list of nodes under the same parent node or in the root. Used for an accessibility attribute.
21
+ */
22
+ indexInGroup: number;
23
+ };
24
+ export declare const TreeContext: React.Context<TreeContextProps | undefined>;
25
+ export declare const useTreeContext: () => TreeContextProps;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.useTreeContext = exports.TreeContext = void 0;
7
+ /*---------------------------------------------------------------------------------------------
8
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
9
+ * See LICENSE.md in the project root for license terms and full copyright notice.
10
+ *--------------------------------------------------------------------------------------------*/
11
+ var react_1 = __importDefault(require("react"));
12
+ exports.TreeContext = react_1.default.createContext(undefined);
13
+ var useTreeContext = function () {
14
+ var context = react_1.default.useContext(exports.TreeContext);
15
+ if (context == undefined) {
16
+ throw new Error('TreeContext must be used within a TreeContext.Provider');
17
+ }
18
+ return context;
19
+ };
20
+ exports.useTreeContext = useTreeContext;
@@ -0,0 +1,87 @@
1
+ import React from 'react';
2
+ import { CommonProps } from '../utils';
3
+ import '@itwin/itwinui-css/css/tree.css';
4
+ export declare type TreeNodeProps = {
5
+ /**
6
+ * Unique id of the node.
7
+ * It has to be compatible with HTML id attribute.
8
+ */
9
+ nodeId: string;
10
+ /**
11
+ * The main text displayed on the node.
12
+ */
13
+ label: React.ReactNode;
14
+ /**
15
+ * Small note displayed below main label.
16
+ */
17
+ sublabel?: React.ReactNode;
18
+ /**
19
+ * Icon shown before label and sublabel content.
20
+ */
21
+ icon?: JSX.Element;
22
+ /**
23
+ * Flag whether the node has child sub-nodes. It is used to show expander icon.
24
+ * @default false
25
+ */
26
+ hasSubNodes?: boolean;
27
+ /**
28
+ * Flag whether the node is disabled.
29
+ * @default false
30
+ */
31
+ isDisabled?: boolean;
32
+ /**
33
+ * Flag whether the node is expanded.
34
+ * @default false
35
+ */
36
+ isExpanded?: boolean;
37
+ /**
38
+ * Flag whether the node is selected.
39
+ * @default false
40
+ */
41
+ isSelected?: boolean;
42
+ /**
43
+ * Callback fired when expanding or closing a TreeNode.
44
+ * Gives nodeId and new isExpanded value of specified node.
45
+ */
46
+ onExpanded: (nodeId: string, isExpanded: boolean) => void;
47
+ /**
48
+ * Callback fired when selecting a TreeNode.
49
+ * Gives nodeId and new isSelected value of specified node.
50
+ */
51
+ onSelected?: (nodeId: string, isSelected: boolean) => void;
52
+ /**
53
+ * Checkbox to be shown at the very beginning of the node.
54
+ * If undefined, checkbox will not be shown.
55
+ * Recommended to use `Checkbox` component.
56
+ */
57
+ checkbox?: React.ReactNode;
58
+ /**
59
+ * Custom expander element. If `hasSubNodes` is false, it won't be shown.
60
+ */
61
+ expander?: React.ReactNode;
62
+ /**
63
+ * Content shown after `TreeNode`.
64
+ */
65
+ children?: React.ReactNode;
66
+ } & Omit<CommonProps, 'id'>;
67
+ /**
68
+ * `TreeNode` component to display node content within a `Tree`.
69
+ * Must be used inside `Tree` component to correctly set node `depth` and `subNodes`.
70
+ * @example
71
+ <TreeNode
72
+ nodeId={props.nodeId}
73
+ label={props.node.label}
74
+ sublabel={props.node.sublabel}
75
+ onExpanded={onExpanded}
76
+ onSelected={onSelectedNodeChange}
77
+ isDisabled={props.isDisabled}
78
+ isExpanded={props.isExpanded}
79
+ isSelected={props.isSelected}
80
+ checkbox={
81
+ <Checkbox variant='eyeball' disabled={props.isDisabled} />
82
+ }
83
+ icon={<SvgPlaceholder />}
84
+ />
85
+ */
86
+ export declare const TreeNode: (props: TreeNodeProps) => JSX.Element;
87
+ export default TreeNode;