UNPKG

3.05 kBJavaScriptView Raw
1/*
2 * Copyright 2016 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 { AbstractPureComponent2, Classes, DISPLAYNAME_PREFIX } from "../../common";
20import { HOTKEYS_HOTKEY_CHILDREN } from "../../common/errors";
21import { isElementOfType, isReactChildrenElementOrElements } from "../../common/utils";
22import { H4 } from "../html/html";
23import { Hotkey } from "./hotkey";
24var Hotkeys = /** @class */ (function (_super) {
25 __extends(Hotkeys, _super);
26 function Hotkeys() {
27 return _super !== null && _super.apply(this, arguments) || this;
28 }
29 Hotkeys.prototype.render = function () {
30 if (!isReactChildrenElementOrElements(this.props.children)) {
31 return null;
32 }
33 var hotkeys = React.Children.map(this.props.children, function (child) { return child.props; });
34 // sort by group label alphabetically, prioritize globals
35 hotkeys.sort(function (a, b) {
36 if (a.global === b.global && a.group && b.group) {
37 return a.group.localeCompare(b.group);
38 }
39 return a.global ? -1 : 1;
40 });
41 var lastGroup;
42 var elems = [];
43 for (var _i = 0, hotkeys_1 = hotkeys; _i < hotkeys_1.length; _i++) {
44 var hotkey = hotkeys_1[_i];
45 var groupLabel = hotkey.group;
46 if (groupLabel !== lastGroup) {
47 elems.push(React.createElement(H4, { key: "group-".concat(elems.length) }, groupLabel));
48 lastGroup = groupLabel;
49 }
50 elems.push(React.createElement(Hotkey, __assign({ key: elems.length }, hotkey)));
51 }
52 var rootClasses = classNames(Classes.HOTKEY_COLUMN, this.props.className);
53 return React.createElement("div", { className: rootClasses }, elems);
54 };
55 Hotkeys.prototype.validateProps = function (props) {
56 if (!isReactChildrenElementOrElements(props.children)) {
57 return;
58 }
59 React.Children.forEach(props.children, function (child) {
60 if (!isElementOfType(child, Hotkey)) {
61 throw new Error(HOTKEYS_HOTKEY_CHILDREN);
62 }
63 });
64 };
65 Hotkeys.displayName = "".concat(DISPLAYNAME_PREFIX, ".Hotkeys");
66 Hotkeys.defaultProps = {
67 tabIndex: 0,
68 };
69 return Hotkeys;
70}(AbstractPureComponent2));
71export { Hotkeys };
72//# sourceMappingURL=hotkeys.js.map
\No newline at end of file