UNPKG

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