@itwin/itwinui-react 1.29.1 → 1.31.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 (44) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/cjs/core/Badge/Badge.js +2 -2
  3. package/cjs/core/ButtonGroup/ButtonGroup.d.ts +10 -3
  4. package/cjs/core/ButtonGroup/ButtonGroup.js +4 -3
  5. package/cjs/core/ColorPicker/ColorBuilder.js +19 -11
  6. package/cjs/core/ColorPicker/ColorSwatch.js +2 -2
  7. package/cjs/core/ComboBox/ComboBox.js +41 -36
  8. package/cjs/core/Modal/Modal.js +4 -1
  9. package/cjs/core/Radio/Radio.js +1 -1
  10. package/cjs/core/Select/Select.js +23 -8
  11. package/cjs/core/Slider/Slider.js +8 -2
  12. package/cjs/core/Table/Table.d.ts +10 -0
  13. package/cjs/core/Table/Table.js +23 -8
  14. package/cjs/core/Table/TablePaginator.js +7 -9
  15. package/cjs/core/Tile/Tile.js +16 -20
  16. package/cjs/core/utils/components/Popover.d.ts +2 -1
  17. package/cjs/core/utils/components/Popover.js +9 -6
  18. package/cjs/core/utils/components/VirtualScroll.d.ts +42 -0
  19. package/cjs/core/utils/components/VirtualScroll.js +174 -0
  20. package/cjs/core/utils/components/index.d.ts +1 -0
  21. package/cjs/core/utils/components/index.js +1 -0
  22. package/cjs/core/utils/props.d.ts +2 -2
  23. package/esm/core/Badge/Badge.js +2 -2
  24. package/esm/core/ButtonGroup/ButtonGroup.d.ts +10 -3
  25. package/esm/core/ButtonGroup/ButtonGroup.js +4 -3
  26. package/esm/core/ColorPicker/ColorBuilder.js +19 -11
  27. package/esm/core/ColorPicker/ColorSwatch.js +2 -2
  28. package/esm/core/ComboBox/ComboBox.js +41 -36
  29. package/esm/core/Modal/Modal.js +4 -1
  30. package/esm/core/Radio/Radio.js +1 -1
  31. package/esm/core/Select/Select.js +23 -8
  32. package/esm/core/Slider/Slider.js +8 -2
  33. package/esm/core/Table/Table.d.ts +10 -0
  34. package/esm/core/Table/Table.js +23 -8
  35. package/esm/core/Table/TablePaginator.js +7 -9
  36. package/esm/core/Tile/Tile.js +16 -20
  37. package/esm/core/utils/components/Popover.d.ts +2 -1
  38. package/esm/core/utils/components/Popover.js +9 -6
  39. package/esm/core/utils/components/VirtualScroll.d.ts +42 -0
  40. package/esm/core/utils/components/VirtualScroll.js +168 -0
  41. package/esm/core/utils/components/index.d.ts +1 -0
  42. package/esm/core/utils/components/index.js +1 -0
  43. package/esm/core/utils/props.d.ts +2 -2
  44. package/package.json +4 -3
@@ -56,6 +56,7 @@ export declare type TableProps<T extends Record<string, unknown> = Record<string
56
56
  isSelectable?: boolean;
57
57
  /**
58
58
  * Handler for rows selection. Must be memoized.
59
+ * This is triggered only by user initiated actions (i.e. data change will not call it).
59
60
  */
60
61
  onSelect?: (selectedData: T[] | undefined, tableState?: TableState<T>) => void;
61
62
  /**
@@ -160,6 +161,15 @@ export declare type TableProps<T extends Record<string, unknown> = Record<string
160
161
  * @default 'default'
161
162
  */
162
163
  styleType?: 'default' | 'zebra-rows';
164
+ /**
165
+ * Virtualization is used for the scrollable table body.
166
+ * Height on the table is required for virtualization to work.
167
+ * @example
168
+ * <Table enableVirtualization style={{height: 400}} {...} />
169
+ * @default false
170
+ * @beta
171
+ */
172
+ enableVirtualization?: boolean;
163
173
  } & Omit<CommonProps, 'title'>;
164
174
  /**
165
175
  * Table based on [react-table](https://react-table.tanstack.com/docs/api/overview).
@@ -40,6 +40,7 @@ import { useExpanderCell, useSelectionCell, useSubRowFiltering, useSubRowSelecti
40
40
  import { onExpandHandler, onFilterHandler, onSelectHandler, } from './actionHandlers';
41
41
  import { onSingleSelectHandler } from './actionHandlers/selectHandler';
42
42
  import { onTableResizeEnd, onTableResizeStart, } from './actionHandlers/resizeHandler';
43
+ import VirtualScroll from '../utils/components/VirtualScroll';
43
44
  var singleRowSelectedAction = 'singleRowSelected';
44
45
  var tableResizeStartAction = 'tableResizeStart';
45
46
  var tableResizeEndAction = 'tableResizeEnd';
@@ -87,9 +88,9 @@ var tableResizeEndAction = 'tableResizeEnd';
87
88
  */
88
89
  export var Table = function (props) {
89
90
  var _a;
90
- var data = props.data, columns = props.columns, _b = props.isLoading, isLoading = _b === void 0 ? false : _b, emptyTableContent = props.emptyTableContent, className = props.className, style = props.style, id = props.id, _c = props.isSelectable, isSelectable = _c === void 0 ? false : _c, onSelect = props.onSelect, onRowClick = props.onRowClick, _d = props.isSortable, isSortable = _d === void 0 ? false : _d, onSort = props.onSort, stateReducer = props.stateReducer, onBottomReached = props.onBottomReached, onRowInViewport = props.onRowInViewport, _e = props.intersectionMargin, intersectionMargin = _e === void 0 ? 300 : _e, subComponent = props.subComponent, onExpand = props.onExpand, onFilter = props.onFilter, emptyFilteredTableContent = props.emptyFilteredTableContent, filterFunctions = props.filterTypes, expanderCell = props.expanderCell, isRowDisabled = props.isRowDisabled, rowProps = props.rowProps, _f = props.density, density = _f === void 0 ? 'default' : _f, _g = props.selectSubRows, selectSubRows = _g === void 0 ? true : _g, getSubRows = props.getSubRows, _h = props.selectRowOnClick, selectRowOnClick = _h === void 0 ? true : _h, paginatorRenderer = props.paginatorRenderer, _j = props.pageSize, pageSize = _j === void 0 ? 25 : _j, _k = props.isResizable, isResizable = _k === void 0 ? false : _k, _l = props.styleType, styleType = _l === void 0 ? 'default' : _l, rest = __rest(props, ["data", "columns", "isLoading", "emptyTableContent", "className", "style", "id", "isSelectable", "onSelect", "onRowClick", "isSortable", "onSort", "stateReducer", "onBottomReached", "onRowInViewport", "intersectionMargin", "subComponent", "onExpand", "onFilter", "emptyFilteredTableContent", "filterTypes", "expanderCell", "isRowDisabled", "rowProps", "density", "selectSubRows", "getSubRows", "selectRowOnClick", "paginatorRenderer", "pageSize", "isResizable", "styleType"]);
91
+ var data = props.data, columns = props.columns, _b = props.isLoading, isLoading = _b === void 0 ? false : _b, emptyTableContent = props.emptyTableContent, className = props.className, style = props.style, id = props.id, _c = props.isSelectable, isSelectable = _c === void 0 ? false : _c, onSelect = props.onSelect, onRowClick = props.onRowClick, _d = props.isSortable, isSortable = _d === void 0 ? false : _d, onSort = props.onSort, stateReducer = props.stateReducer, onBottomReached = props.onBottomReached, onRowInViewport = props.onRowInViewport, _e = props.intersectionMargin, intersectionMargin = _e === void 0 ? 300 : _e, subComponent = props.subComponent, onExpand = props.onExpand, onFilter = props.onFilter, emptyFilteredTableContent = props.emptyFilteredTableContent, filterFunctions = props.filterTypes, expanderCell = props.expanderCell, isRowDisabled = props.isRowDisabled, rowProps = props.rowProps, _f = props.density, density = _f === void 0 ? 'default' : _f, _g = props.selectSubRows, selectSubRows = _g === void 0 ? true : _g, getSubRows = props.getSubRows, _h = props.selectRowOnClick, selectRowOnClick = _h === void 0 ? true : _h, paginatorRenderer = props.paginatorRenderer, _j = props.pageSize, pageSize = _j === void 0 ? 25 : _j, _k = props.isResizable, isResizable = _k === void 0 ? false : _k, _l = props.styleType, styleType = _l === void 0 ? 'default' : _l, _m = props.enableVirtualization, enableVirtualization = _m === void 0 ? false : _m, rest = __rest(props, ["data", "columns", "isLoading", "emptyTableContent", "className", "style", "id", "isSelectable", "onSelect", "onRowClick", "isSortable", "onSort", "stateReducer", "onBottomReached", "onRowInViewport", "intersectionMargin", "subComponent", "onExpand", "onFilter", "emptyFilteredTableContent", "filterTypes", "expanderCell", "isRowDisabled", "rowProps", "density", "selectSubRows", "getSubRows", "selectRowOnClick", "paginatorRenderer", "pageSize", "isResizable", "styleType", "enableVirtualization"]);
91
92
  useTheme();
92
- var _m = React.useState(), ownerDocument = _m[0], setOwnerDocument = _m[1];
93
+ var _o = React.useState(), ownerDocument = _o[0], setOwnerDocument = _o[1];
93
94
  var defaultColumn = React.useMemo(function () { return ({
94
95
  maxWidth: 0,
95
96
  minWidth: 0,
@@ -227,6 +228,23 @@ export var Table = function (props) {
227
228
  });
228
229
  var headerRef = React.useRef(null);
229
230
  var bodyRef = React.useRef(null);
231
+ var getPreparedRow = React.useCallback(function (row) {
232
+ prepareRow(row);
233
+ return (React.createElement(TableRowMemoized, { row: row, rowProps: rowProps, isLast: row.index === data.length - 1, onRowInViewport: onRowInViewportRef, onBottomReached: onBottomReachedRef, intersectionMargin: intersectionMargin, state: state, key: row.getRowProps().key, onClick: onRowClickHandler, subComponent: subComponent, isDisabled: !!(isRowDisabled === null || isRowDisabled === void 0 ? void 0 : isRowDisabled(row.original)), tableHasSubRows: hasAnySubRows, tableInstance: instance, expanderCell: expanderCell }));
234
+ }, [
235
+ data.length,
236
+ expanderCell,
237
+ hasAnySubRows,
238
+ instance,
239
+ intersectionMargin,
240
+ isRowDisabled,
241
+ onRowClickHandler,
242
+ prepareRow,
243
+ rowProps,
244
+ state,
245
+ subComponent,
246
+ ]);
247
+ var virtualizedItemRenderer = React.useCallback(function (index) { return getPreparedRow(page[index]); }, [getPreparedRow, page]);
230
248
  return (React.createElement(React.Fragment, null,
231
249
  React.createElement("div", __assign({ ref: function (element) {
232
250
  setOwnerDocument(element === null || element === void 0 ? void 0 : element.ownerDocument);
@@ -263,16 +281,13 @@ export var Table = function (props) {
263
281
  className: cx('iui-table-body', {
264
282
  'iui-zebra-striping': styleType === 'zebra-rows',
265
283
  }),
284
+ style: { outline: 0 },
266
285
  }), { ref: bodyRef, onScroll: function () {
267
286
  if (headerRef.current && bodyRef.current) {
268
287
  headerRef.current.scrollLeft = bodyRef.current.scrollLeft;
269
288
  }
270
- } }),
271
- data.length !== 0 &&
272
- page.map(function (row) {
273
- prepareRow(row);
274
- return (React.createElement(TableRowMemoized, { row: row, rowProps: rowProps, isLast: row.index === data.length - 1, onRowInViewport: onRowInViewportRef, onBottomReached: onBottomReachedRef, intersectionMargin: intersectionMargin, state: state, key: row.getRowProps().key, onClick: onRowClickHandler, subComponent: subComponent, isDisabled: !!(isRowDisabled === null || isRowDisabled === void 0 ? void 0 : isRowDisabled(row.original)), tableHasSubRows: hasAnySubRows, tableInstance: instance, expanderCell: expanderCell }));
275
- }),
289
+ }, tabIndex: -1 }),
290
+ data.length !== 0 && (React.createElement(React.Fragment, null, enableVirtualization ? (React.createElement(VirtualScroll, { itemsLength: page.length, itemRenderer: virtualizedItemRenderer })) : (page.map(function (row) { return getPreparedRow(row); })))),
276
291
  isLoading && data.length === 0 && (React.createElement("div", { className: 'iui-table-empty' },
277
292
  React.createElement(ProgressRadial, { indeterminate: true }))),
278
293
  isLoading && data.length !== 0 && (React.createElement("div", { className: 'iui-row' },
@@ -79,11 +79,10 @@ export var TablePaginator = function (props) {
79
79
  var buttonSize = size != 'default' ? 'small' : undefined;
80
80
  var pageButton = React.useCallback(function (index, tabIndex) {
81
81
  if (tabIndex === void 0) { tabIndex = index === focusedIndex ? 0 : -1; }
82
- return (React.createElement("div", { key: index },
83
- React.createElement("button", { className: cx('iui-paginator-page-button', {
84
- 'iui-active': index === currentPage,
85
- 'iui-paginator-page-button-small': buttonSize === 'small',
86
- }), onClick: function () { return onPageChange(index); }, "aria-current": index === currentPage, "aria-label": localization.goToPageLabel(index + 1), tabIndex: tabIndex }, index + 1)));
82
+ return (React.createElement("button", { key: index, className: cx('iui-paginator-page-button', {
83
+ 'iui-active': index === currentPage,
84
+ 'iui-paginator-page-button-small': buttonSize === 'small',
85
+ }), onClick: function () { return onPageChange(index); }, "aria-current": index === currentPage, "aria-label": localization.goToPageLabel(index + 1), tabIndex: tabIndex }, index + 1));
87
86
  }, [focusedIndex, currentPage, localization, buttonSize, onPageChange]);
88
87
  var totalPagesCount = Math.ceil(totalRowsCount / pageSize);
89
88
  var pageList = React.useMemo(function () {
@@ -145,10 +144,9 @@ export var TablePaginator = function (props) {
145
144
  var hasNoRows = totalPagesCount === 0;
146
145
  var showPagesList = totalPagesCount > 1 || isLoading;
147
146
  var showPageSizeList = pageSizeList && onPageSizeChange && !!totalRowsCount;
148
- var ellipsis = (React.createElement("div", null,
149
- React.createElement("span", { className: cx('iui-paginator-ellipsis', {
150
- 'iui-paginator-ellipsis-small': size === 'small',
151
- }) }, "\u2026")));
147
+ var ellipsis = (React.createElement("span", { className: cx('iui-paginator-ellipsis', {
148
+ 'iui-paginator-ellipsis-small': size === 'small',
149
+ }) }, "\u2026"));
152
150
  var noRowsContent = (React.createElement(React.Fragment, null, isLoading ? (React.createElement(ProgressRadial, { indeterminate: true, size: 'small' })) : (React.createElement(Button, { styleType: 'borderless', disabled: true, size: buttonSize }, "1"))));
153
151
  if (!showPagesList && !showPageSizeList) {
154
152
  return null;
@@ -57,32 +57,31 @@ export var Tile = function (props) {
57
57
  var showMenu = React.useCallback(function () { return setIsMenuVisible(true); }, []);
58
58
  var hideMenu = React.useCallback(function () { return setIsMenuVisible(false); }, []);
59
59
  return (React.createElement("div", __assign({ className: cx('iui-tile', { 'iui-folder': variant === 'folder' }, { 'iui-new': isNew }, { 'iui-selected': isSelected }, className) }, rest),
60
- thumbnail && (React.createElement("div", { className: 'iui-thumbnail' },
61
- typeof thumbnail === 'string' ? (React.createElement("div", { className: 'iui-picture', style: { backgroundImage: "url(" + thumbnail + ")" } })) : thumbnail && thumbnail.type === 'img' ? (React.cloneElement(thumbnail, {
62
- className: 'iui-picture',
60
+ thumbnail && (React.createElement("div", { className: 'iui-tile-thumbnail' },
61
+ typeof thumbnail === 'string' ? (React.createElement("div", { className: 'iui-tile-thumbnail-picture', style: { backgroundImage: "url(" + thumbnail + ")" } })) : thumbnail && thumbnail.type === 'img' ? (React.cloneElement(thumbnail, {
62
+ className: 'iui-tile-thumbnail-picture',
63
63
  })) : React.isValidElement(thumbnail) ? (React.cloneElement(thumbnail, {
64
64
  className: cx('iui-thumbnail-icon', thumbnail.props.className),
65
65
  })) : (thumbnail),
66
66
  leftIcon &&
67
67
  React.cloneElement(leftIcon, {
68
- className: 'iui-small iui-type-indicator',
68
+ className: 'iui-small iui-tile-thumbnail-type-indicator',
69
69
  }),
70
70
  rightIcon &&
71
71
  React.cloneElement(rightIcon, {
72
- className: 'iui-small iui-quick-action',
72
+ className: 'iui-small iui-tile-thumbnail-quick-action',
73
73
  }),
74
- badge && React.createElement("div", { className: 'iui-badge-container' }, badge))),
75
- React.createElement("div", { className: 'iui-content' },
76
- React.createElement("div", { className: 'iui-name' },
74
+ badge && (React.createElement("div", { className: 'iui-tile-thumbnail-badge-container' }, badge)))),
75
+ React.createElement("div", { className: 'iui-tile-content' },
76
+ React.createElement("div", { className: 'iui-tile-name' },
77
77
  isSelected && (React.createElement(SvgCheckmark, { className: cx('iui-tile-status-icon', 'iui-informational'), "aria-hidden": true })),
78
78
  isNew && (React.createElement(SvgNew, { className: cx('iui-tile-status-icon', 'iui-positive'), "aria-hidden": true })),
79
- React.createElement("span", { className: 'iui-name-label' }, name)),
80
- description != undefined && (React.createElement("div", { className: 'iui-description' }, description)),
81
- metadata != undefined && (React.createElement("div", { className: 'iui-metadata' }, metadata)),
79
+ React.createElement("span", { className: 'iui-tile-name-label' }, name)),
80
+ description != undefined && (React.createElement("div", { className: 'iui-tile-description' }, description)),
81
+ metadata != undefined && (React.createElement("div", { className: 'iui-tile-metadata' }, metadata)),
82
82
  moreOptions && (React.createElement(DropdownMenu, { onShow: showMenu, onHide: hideMenu, menuItems: function (close) {
83
83
  return moreOptions.map(function (option) {
84
84
  return React.cloneElement(option, {
85
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
86
85
  onClick: function (value) {
87
86
  var _a, _b;
88
87
  close();
@@ -91,15 +90,12 @@ export var Tile = function (props) {
91
90
  });
92
91
  });
93
92
  } },
94
- React.createElement(IconButton, { styleType: 'borderless', size: 'small', className: cx('iui-more-options', {
93
+ React.createElement("div", { className: cx('iui-tile-more-options', {
95
94
  'iui-visible': isMenuVisible,
96
- }), "aria-label": 'More options' },
97
- React.createElement(SvgMore, null)))),
95
+ }) },
96
+ React.createElement(IconButton, { styleType: 'borderless', size: 'small', "aria-label": 'More options' },
97
+ React.createElement(SvgMore, null))))),
98
98
  children),
99
- buttons && (React.createElement("div", { className: 'iui-tile-buttons' }, buttons.map(function (button) {
100
- return React.cloneElement(button, {
101
- className: cx('iui-tile-button', button.props.className),
102
- });
103
- })))));
99
+ buttons && React.createElement("div", { className: 'iui-tile-buttons' }, buttons)));
104
100
  };
105
101
  export default Tile;
@@ -1,7 +1,8 @@
1
1
  import React from 'react';
2
2
  import { TippyProps } from '@tippyjs/react';
3
- import { Placement, Instance } from 'tippy.js';
3
+ import type { Placement, Instance } from 'tippy.js';
4
4
  export declare type PopoverInstance = Instance;
5
+ import '@itwin/itwinui-css/css/popover.css';
5
6
  export declare type PopoverProps = {
6
7
  /**
7
8
  * Controlled flag for whether the popover is visible.
@@ -23,15 +23,18 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
23
23
  return to.concat(ar || Array.prototype.slice.call(from));
24
24
  };
25
25
  import React from 'react';
26
+ import cx from 'classnames';
26
27
  import Tippy from '@tippyjs/react';
27
28
  import { useMergedRefs } from '../hooks/useMergedRefs';
29
+ import '@itwin/itwinui-css/css/popover.css';
28
30
  /**
29
31
  * Wrapper around [tippy.js](https://atomiks.github.io/tippyjs)
30
32
  * with pre-configured props and plugins (e.g. lazy mounting, focus, etc).
31
33
  * @private
32
34
  */
33
35
  export var Popover = React.forwardRef(function (props, ref) {
34
- var _a = React.useState(false), mounted = _a[0], setMounted = _a[1];
36
+ var _a;
37
+ var _b = React.useState(false), mounted = _b[0], setMounted = _b[1];
35
38
  var tippyRef = React.useRef(null);
36
39
  var refs = useMergedRefs(tippyRef, ref);
37
40
  // Plugin to allow lazy mounting. See https://github.com/atomiks/tippyjs-react#lazy-mounting
@@ -50,14 +53,14 @@ export var Popover = React.forwardRef(function (props, ref) {
50
53
  },
51
54
  }); },
52
55
  };
53
- var computedProps = __assign(__assign({ allowHTML: true, animation: false, appendTo: 'parent', arrow: false, duration: 0, interactive: true, popperOptions: {
54
- strategy: 'fixed',
55
- modifiers: [{ name: 'flip' }],
56
- }, role: undefined, offset: [0, 0], maxWidth: '' }, props), { plugins: __spreadArray([
56
+ var computedProps = __assign(__assign({ allowHTML: true, animation: false, appendTo: 'parent', arrow: false, duration: 0, interactive: true, role: undefined, offset: [0, 0], maxWidth: '' }, props), { className: cx('iui-popover', props.className), plugins: __spreadArray([
57
57
  lazyLoad,
58
58
  removeTabIndex,
59
59
  hideOnEscOrTab
60
- ], (props.plugins || []), true) });
60
+ ], (props.plugins || []), true), popperOptions: __assign(__assign({ strategy: 'fixed' }, props.popperOptions), { modifiers: __spreadArray([
61
+ { name: 'flip' },
62
+ { name: 'preventOverflow', options: { padding: 0 } }
63
+ ], (((_a = props.popperOptions) === null || _a === void 0 ? void 0 : _a.modifiers) || []), true) }) });
61
64
  if (props.render) {
62
65
  var render_1 = props.render;
63
66
  computedProps.render = function () {
@@ -0,0 +1,42 @@
1
+ import React from 'react';
2
+ export declare type VirtualScrollProps = {
3
+ /**
4
+ * Length of the items to virtualize.
5
+ */
6
+ itemsLength: number;
7
+ /**
8
+ * Single item render function, which gives index of the item (0 based) in the data array
9
+ * and expects to get the JSX of that element to render.
10
+ * Recommended to memoize the reference of the function.
11
+ */
12
+ itemRenderer: (index: number) => JSX.Element;
13
+ /**
14
+ * Number of items to be rendered at the start and the end.
15
+ * Not recommended to go lower than the visible items in viewport.
16
+ * @default 10
17
+ */
18
+ bufferSize?: number;
19
+ } & React.ComponentPropsWithRef<'div'>;
20
+ /**
21
+ * `VirtualScroll` component is used to render a huge amount of items in the DOM. It renders only the ones which are visible
22
+ * and the amount provided through `bufferSize` prop at the start and the end. Can be used inside other components like `Table`.
23
+ *
24
+ * It has two wrapper elements, so DOM will be changed. One is used for setting full expected height in the scrollable container
25
+ * and other is for transformation (translateY) to show the correct part of the list.
26
+ *
27
+ * Currently it works only with the direct vertically scrollable parent element. It does not work with body scroll.
28
+ * It supports only static (same) height rows virtualization. Expect some issues, if list consists of different height elements.
29
+ * @example
30
+ * const itemRenderer = React.useCallback(() => (
31
+ * <div key={index}>
32
+ * This is my item #{index}
33
+ * </div>
34
+ * ), [])
35
+ * <VirtualScroll
36
+ * itemsLength={1000}
37
+ * itemRenderer={itemRenderer}
38
+ * />
39
+ * @private
40
+ */
41
+ export declare const VirtualScroll: React.ForwardRefExoticComponent<Pick<VirtualScrollProps, "key" | keyof React.HTMLAttributes<HTMLDivElement> | "itemsLength" | "itemRenderer" | "bufferSize"> & React.RefAttributes<HTMLDivElement>>;
42
+ export default VirtualScroll;
@@ -0,0 +1,168 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ var __rest = (this && this.__rest) || function (s, e) {
13
+ var t = {};
14
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
15
+ t[p] = s[p];
16
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
17
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
18
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
19
+ t[p[i]] = s[p[i]];
20
+ }
21
+ return t;
22
+ };
23
+ /*---------------------------------------------------------------------------------------------
24
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
25
+ * See LICENSE.md in the project root for license terms and full copyright notice.
26
+ *--------------------------------------------------------------------------------------------*/
27
+ import React from 'react';
28
+ import { useResizeObserver } from '../hooks/useResizeObserver';
29
+ var getScrollableParent = function (element, ownerDocument) {
30
+ if (ownerDocument === void 0) { ownerDocument = document; }
31
+ if (!element || element === ownerDocument.body) {
32
+ return ownerDocument.body;
33
+ }
34
+ return isElementScrollable(element)
35
+ ? element
36
+ : getScrollableParent(element.parentElement, ownerDocument);
37
+ };
38
+ var isElementScrollable = function (element) {
39
+ return /(auto|scroll|overlay)/.test(getElementStyle(element, 'overflow') +
40
+ getElementStyle(element, 'overflow-y'));
41
+ };
42
+ var getElementStyle = function (element, prop) {
43
+ return getComputedStyle(element, null).getPropertyValue(prop);
44
+ };
45
+ var getElementHeight = function (element) {
46
+ var _a;
47
+ return (_a = element === null || element === void 0 ? void 0 : element.getBoundingClientRect().height) !== null && _a !== void 0 ? _a : 0;
48
+ };
49
+ var getNumberOfNodesInHeight = function (childHeight, totalHeight) {
50
+ if (!childHeight) {
51
+ return 0;
52
+ }
53
+ return Math.floor(totalHeight / childHeight);
54
+ };
55
+ var getTranslateValue = function (childHeight, startIndex) {
56
+ return childHeight * startIndex;
57
+ };
58
+ var getVisibleNodeCount = function (childHeight, startIndex, childrenLength, scrollContainer) {
59
+ return Math.min(childrenLength - startIndex, getNumberOfNodesInHeight(childHeight, getElementHeight(scrollContainer)));
60
+ };
61
+ /**
62
+ * `VirtualScroll` component is used to render a huge amount of items in the DOM. It renders only the ones which are visible
63
+ * and the amount provided through `bufferSize` prop at the start and the end. Can be used inside other components like `Table`.
64
+ *
65
+ * It has two wrapper elements, so DOM will be changed. One is used for setting full expected height in the scrollable container
66
+ * and other is for transformation (translateY) to show the correct part of the list.
67
+ *
68
+ * Currently it works only with the direct vertically scrollable parent element. It does not work with body scroll.
69
+ * It supports only static (same) height rows virtualization. Expect some issues, if list consists of different height elements.
70
+ * @example
71
+ * const itemRenderer = React.useCallback(() => (
72
+ * <div key={index}>
73
+ * This is my item #{index}
74
+ * </div>
75
+ * ), [])
76
+ * <VirtualScroll
77
+ * itemsLength={1000}
78
+ * itemRenderer={itemRenderer}
79
+ * />
80
+ * @private
81
+ */
82
+ export var VirtualScroll = React.forwardRef(function (_a, ref) {
83
+ var itemsLength = _a.itemsLength, itemRenderer = _a.itemRenderer, _b = _a.bufferSize, bufferSize = _b === void 0 ? 10 : _b, style = _a.style, rest = __rest(_a, ["itemsLength", "itemRenderer", "bufferSize", "style"]);
84
+ var _c = React.useState(0), startNode = _c[0], setStartNode = _c[1];
85
+ var _d = React.useState(0), visibleNodeCount = _d[0], setVisibleNodeCount = _d[1];
86
+ var scrollContainer = React.useRef();
87
+ var parentRef = React.useRef(null);
88
+ var childHeight = React.useRef(0);
89
+ var onScrollRef = React.useRef();
90
+ // Used only to recalculate on resize
91
+ var _e = React.useState(0), scrollContainerHeight = _e[0], setScrollContainerHeight = _e[1];
92
+ var onResize = React.useCallback(function (_a) {
93
+ var height = _a.height;
94
+ setScrollContainerHeight(height);
95
+ }, []);
96
+ var resizeRef = useResizeObserver(onResize)[0];
97
+ // Find scrollable parent
98
+ // Needed only on init
99
+ React.useLayoutEffect(function () {
100
+ var _a;
101
+ var scrollableParent = getScrollableParent(parentRef.current, (_a = parentRef.current) === null || _a === void 0 ? void 0 : _a.ownerDocument);
102
+ scrollContainer.current = scrollableParent;
103
+ resizeRef(scrollableParent);
104
+ }, [resizeRef]);
105
+ var visibleChildren = React.useMemo(function () {
106
+ var arr = [];
107
+ var endIndex = Math.min(itemsLength, startNode + visibleNodeCount + bufferSize * 2);
108
+ for (var i = startNode; i < endIndex; i++) {
109
+ arr.push(itemRenderer(i));
110
+ }
111
+ return arr;
112
+ }, [itemsLength, itemRenderer, bufferSize, startNode, visibleNodeCount]);
113
+ // Get child height when children available
114
+ React.useLayoutEffect(function () {
115
+ if (!parentRef.current || !visibleChildren.length) {
116
+ return;
117
+ }
118
+ var firstChild = parentRef.current.children.item(0);
119
+ childHeight.current = Number(getElementHeight(firstChild).toFixed(2));
120
+ }, [visibleChildren.length]);
121
+ var updateVirtualScroll = React.useCallback(function () {
122
+ var _a, _b;
123
+ var scrollableContainer = (_a = scrollContainer.current) !== null && _a !== void 0 ? _a : (_b = parentRef.current) === null || _b === void 0 ? void 0 : _b.ownerDocument.scrollingElement;
124
+ if (!scrollableContainer) {
125
+ return;
126
+ }
127
+ var start = getNumberOfNodesInHeight(childHeight.current, scrollableContainer.scrollTop);
128
+ var startIndex = Math.max(0, start - bufferSize);
129
+ setStartNode(startIndex);
130
+ setVisibleNodeCount(getVisibleNodeCount(childHeight.current, start, itemsLength, scrollableContainer));
131
+ if (!parentRef.current) {
132
+ return;
133
+ }
134
+ parentRef.current.style.transform = "translateY(" + getTranslateValue(childHeight.current, startIndex) + "px)";
135
+ }, [bufferSize, itemsLength]);
136
+ var removeScrollListener = React.useCallback(function () {
137
+ var _a, _b;
138
+ if (!onScrollRef.current) {
139
+ return;
140
+ }
141
+ !scrollContainer.current ||
142
+ scrollContainer.current === ((_a = parentRef.current) === null || _a === void 0 ? void 0 : _a.ownerDocument.body)
143
+ ? (_b = parentRef.current) === null || _b === void 0 ? void 0 : _b.ownerDocument.removeEventListener('scroll', onScrollRef.current)
144
+ : scrollContainer.current.removeEventListener('scroll', onScrollRef.current);
145
+ }, []);
146
+ // Add event listener to the scrollable container.
147
+ React.useLayoutEffect(function () {
148
+ var _a, _b;
149
+ removeScrollListener();
150
+ onScrollRef.current = updateVirtualScroll;
151
+ if (!scrollContainer.current ||
152
+ scrollContainer.current === ((_a = parentRef.current) === null || _a === void 0 ? void 0 : _a.ownerDocument.body)) {
153
+ (_b = parentRef.current) === null || _b === void 0 ? void 0 : _b.ownerDocument.addEventListener('scroll', updateVirtualScroll);
154
+ }
155
+ else {
156
+ scrollContainer.current.addEventListener('scroll', updateVirtualScroll);
157
+ }
158
+ return removeScrollListener;
159
+ }, [updateVirtualScroll, removeScrollListener]);
160
+ React.useLayoutEffect(function () {
161
+ updateVirtualScroll();
162
+ }, [scrollContainerHeight, itemsLength, updateVirtualScroll]);
163
+ return (React.createElement("div", __assign({ style: __assign({ overflow: 'hidden', minHeight: itemsLength * childHeight.current, width: '100%' }, style), ref: ref }, rest),
164
+ React.createElement("div", { style: {
165
+ willChange: 'transform',
166
+ }, ref: parentRef }, visibleChildren)));
167
+ });
168
+ export default VirtualScroll;
@@ -4,3 +4,4 @@ export * from './InputContainer';
4
4
  export * from './icons';
5
5
  export * from './WithCSSTransition';
6
6
  export * from './MiddleTextTruncation';
7
+ export * from './VirtualScroll';
@@ -8,3 +8,4 @@ export * from './InputContainer';
8
8
  export * from './icons';
9
9
  export * from './WithCSSTransition';
10
10
  export * from './MiddleTextTruncation';
11
+ export * from './VirtualScroll';
@@ -42,9 +42,9 @@ export interface PolymorphicForwardRefComponent<T, OwnProps = Record<string, unk
42
42
  as?: T;
43
43
  }>> {
44
44
  <As = T>(props: As extends keyof JSX.IntrinsicElements ? Merge<JSX.IntrinsicElements[As], OwnProps & {
45
- as?: As;
45
+ as: As;
46
46
  }> : As extends React.ComponentType<infer P> ? Merge<P, OwnProps & {
47
- as?: As;
47
+ as: As;
48
48
  }> : never): React.ReactElement | null;
49
49
  }
50
50
  declare type Merge<P1, P2> = Omit<P1, keyof P2> & P2;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@itwin/itwinui-react",
3
- "version": "1.29.1",
3
+ "version": "1.31.0",
4
4
  "author": "Bentley Systems",
5
5
  "license": "MIT",
6
6
  "main": "cjs/index.js",
@@ -40,7 +40,7 @@
40
40
  "build-storybook": "build-storybook"
41
41
  },
42
42
  "dependencies": {
43
- "@itwin/itwinui-css": "^0.44.0",
43
+ "@itwin/itwinui-css": "^0.47.0",
44
44
  "@itwin/itwinui-icons-react": "^1.5.0",
45
45
  "@itwin/itwinui-illustrations-react": "^1.0.1",
46
46
  "@tippyjs/react": "^4.2.5",
@@ -121,7 +121,8 @@
121
121
  "nth-check": "^2.0.1",
122
122
  "ansi-regex": "^5.0.1",
123
123
  "@pmmmwh/react-refresh-webpack-plugin": "^0.5.1",
124
- "postcss": "8"
124
+ "postcss": "8",
125
+ "nanoid": "^3.1.31"
125
126
  },
126
127
  "husky": {
127
128
  "hooks": {