UNPKG

@eccenca/gui-elements

Version:

GUI elements based on other libraries, usable in React application, written in Typescript.

106 lines 6.49 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; import React from "react"; import { compute } from "compute-scroll-into-view"; import { CLASSPREFIX as eccgui } from "../../configuration/constants.js"; import { Highlighter, Menu, MenuItem, OverflowText, OverviewItem, OverviewItemDescription, OverviewItemLine, Spacing, Spinner, Tooltip, } from "./../../index.js"; var ListItem = function (_a, ref) { var item = _a.item; var listItem = (React.createElement(OverviewItem, { densityHigh: true }, React.createElement(OverviewItemDescription, null, React.createElement(OverviewItemLine, null, React.createElement(OverflowText, { ellipsis: "reverse" }, React.createElement(Highlighter, { label: item.value, searchValue: item.query }))), item.label ? (React.createElement(OverviewItemLine, { small: true }, React.createElement(OverflowText, null, React.createElement(Highlighter, { label: item.label, searchValue: item.query })))) : null, item.description ? (React.createElement(OverviewItemLine, { small: true }, React.createElement(OverflowText, null, React.createElement(Highlighter, { label: item.description, searchValue: item.query })))) : null))); return (React.createElement("div", { ref: ref }, !!item.description && item.description.length > 50 ? (React.createElement(Tooltip, { content: item.description }, listItem)) : (React.createElement(React.Fragment, null, listItem)))); }; var Item = React.forwardRef(ListItem); /** A drop-down-like list that can be used in combination with other components to show and select items. */ export var AutoSuggestionList = function (_a) { var _b, _c; var isOpen = _a.isOpen, options = _a.options, loading = _a.loading, onItemSelectionChange = _a.onItemSelectionChange, currentlyFocusedIndex = _a.currentlyFocusedIndex, itemToHighlight = _a.itemToHighlight, style = _a.style, offsetValues = _a.offsetValues, otherDivProps = __rest(_a, ["isOpen", "options", "loading", "onItemSelectionChange", "currentlyFocusedIndex", "itemToHighlight", "style", "offsetValues"]); var _d = __read(React.useState(undefined), 2), hoveredItem = _d[0], setHoveredItem = _d[1]; // Refs of list items var _e = __read(React.useState([]), 1), refs = _e[0]; var dropdownRef = React.useRef(null); var generateRef = function (index) { if (!refs[index]) { refs[index] = React.createRef(); } return refs[index]; }; React.useEffect(function () { var listIndexNode = refs[currentlyFocusedIndex]; if ((dropdownRef === null || dropdownRef === void 0 ? void 0 : dropdownRef.current) && (listIndexNode === null || listIndexNode === void 0 ? void 0 : listIndexNode.current)) { var actions = compute(listIndexNode.current, { boundary: dropdownRef.current, block: "nearest", scrollMode: "if-needed", }); actions.forEach(function (_a) { var el = _a.el, top = _a.top, left = _a.left; el.scrollTop = top; el.scrollLeft = left; }); } }, [currentlyFocusedIndex, refs]); var focusedItem = options[currentlyFocusedIndex]; // Decide which item to highlight React.useEffect(function () { itemToHighlight(!isOpen ? undefined : hoveredItem || focusedItem); }, [currentlyFocusedIndex, itemToHighlight, focusedItem, isOpen, hoveredItem]); var Loader = (React.createElement(OverviewItem, { hasSpacing: true }, React.createElement(OverviewItemLine, null, "Fetching suggestions"), React.createElement(Spacing, { size: "tiny", vertical: true }), React.createElement(Spinner, { position: "inline" }))); var loadingOrHasSuggestions = loading || options.length; if (!loadingOrHasSuggestions || !isOpen) return null; return (React.createElement("div", __assign({}, otherDivProps, { className: "".concat(eccgui, "-autosuggestion__dropdown"), style: __assign(__assign({}, style), { left: (_b = offsetValues === null || offsetValues === void 0 ? void 0 : offsetValues.x) !== null && _b !== void 0 ? _b : 0, top: (_c = offsetValues === null || offsetValues === void 0 ? void 0 : offsetValues.y) !== null && _c !== void 0 ? _c : 0 }), ref: dropdownRef }), loading ? (Loader) : (React.createElement(Menu, null, options.map(function (item, index) { return (React.createElement(MenuItem, { key: index, active: currentlyFocusedIndex === index, onMouseDown: function (e) { return e.preventDefault(); }, onClick: function () { return onItemSelectionChange(item); }, text: React.createElement(Item, { ref: generateRef(index), item: item }), onMouseEnter: function () { return setHoveredItem(item); }, onMouseLeave: function () { return setHoveredItem(undefined); }, onMouseOver: function () { if (item.value !== (hoveredItem === null || hoveredItem === void 0 ? void 0 : hoveredItem.value)) { setHoveredItem(item); } } })); }))))); }; //# sourceMappingURL=AutoSuggestionList.js.map