UNPKG

4.48 kBJavaScriptView Raw
1/*
2 * Copyright 2015 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { __assign, __extends } from "tslib";
17import classNames from "classnames";
18import * as React from "react";
19import { Boundary } from "../../common/boundary";
20import * as Classes from "../../common/classes";
21import * as Errors from "../../common/errors";
22import { Position } from "../../common/position";
23import { DISPLAYNAME_PREFIX } from "../../common/props";
24import { isElementOfType } from "../../common/utils";
25import { Menu } from "../menu/menu";
26import { MenuItem } from "../menu/menuItem";
27import { Popover } from "../popover/popover";
28/** @deprecated use `<OverflowList>` for automatic overflow based on available space. */
29var CollapsibleList = /** @class */ (function (_super) {
30 __extends(CollapsibleList, _super);
31 function CollapsibleList() {
32 return _super !== null && _super.apply(this, arguments) || this;
33 }
34 CollapsibleList.prototype.render = function () {
35 var _this = this;
36 var collapseFrom = this.props.collapseFrom;
37 var childrenLength = React.Children.count(this.props.children);
38 var _a = this.partitionChildren(), visibleChildren = _a[0], collapsedChildren = _a[1];
39 var visibleItems = visibleChildren.map(function (child, index) {
40 var absoluteIndex = collapseFrom === Boundary.START ? childrenLength - 1 - index : index;
41 return (React.createElement("li", { className: _this.props.visibleItemClassName, key: absoluteIndex }, _this.props.visibleItemRenderer(child.props, absoluteIndex)));
42 });
43 if (collapseFrom === Boundary.START) {
44 // reverse START list so separators appear before items
45 visibleItems.reverse();
46 }
47 // construct dropdown menu for collapsed items
48 var collapsedPopover;
49 if (collapsedChildren.length > 0) {
50 var position = collapseFrom === Boundary.END ? Position.BOTTOM_RIGHT : Position.BOTTOM_LEFT;
51 /* eslint-disable deprecation/deprecation */
52 collapsedPopover = (React.createElement("li", { className: this.props.visibleItemClassName },
53 React.createElement(Popover, __assign({ content: React.createElement(Menu, null, collapsedChildren), position: position }, this.props.dropdownProps), this.props.dropdownTarget)));
54 /* eslint-enable deprecation/deprecation */
55 }
56 return (React.createElement("ul", { className: classNames(Classes.COLLAPSIBLE_LIST, this.props.className) },
57 collapseFrom === Boundary.START ? collapsedPopover : null,
58 visibleItems,
59 collapseFrom === Boundary.END ? collapsedPopover : null));
60 };
61 // splits the list of children into two arrays: visible and collapsed
62 CollapsibleList.prototype.partitionChildren = function () {
63 var childrenArray = React.Children.map(this.props.children, function (child, index) {
64 if (!isElementOfType(child, MenuItem)) {
65 throw new Error(Errors.COLLAPSIBLE_LIST_INVALID_CHILD);
66 }
67 return React.cloneElement(child, { key: "visible-".concat(index) });
68 });
69 if (childrenArray == null) {
70 return [[], []];
71 }
72 if (this.props.collapseFrom === Boundary.START) {
73 // reverse START list so we can always slice visible items from the front of the list
74 childrenArray.reverse();
75 }
76 var visibleItemCount = this.props.visibleItemCount;
77 return [childrenArray.slice(0, visibleItemCount), childrenArray.slice(visibleItemCount)];
78 };
79 CollapsibleList.displayName = "".concat(DISPLAYNAME_PREFIX, ".CollapsibleList");
80 CollapsibleList.defaultProps = {
81 collapseFrom: Boundary.START,
82 visibleItemCount: 3,
83 };
84 return CollapsibleList;
85}(React.Component));
86export { CollapsibleList };
87//# sourceMappingURL=collapsibleList.js.map
\No newline at end of file