UNPKG

@brizy/ui

Version:
70 lines (69 loc) 4.64 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Dropdown = void 0; const classNamesFn_1 = require("../../classNamesFn"); const fuzzy_search_1 = __importDefault(require("fuzzy-search")); const react_1 = __importStar(require("react")); const react_popper_1 = require("react-popper"); const LegacyScrollbar_1 = require("../../LegacyScrollbar"); const Portal_1 = require("../../Portal"); const utils_1 = require("../utils"); const DropdownItem_1 = require("./DropdownItem"); const constants_1 = require("../../constants"); const useTranslation_1 = require("../../utils/localization/useTranslation"); const Dropdown = ({ className: _className, options: _options, value, isMultiple, isFixed, itemHeight = 30, minItems = 1, maxItems = 5, isOpen, inputValue, highlightedIndex, selectedItem, getItemProps, getMenuProps, selectRef, }) => { const { t } = (0, useTranslation_1.useTranslation)(); const container = selectRef.current; const menuWidth = container === null || container === void 0 ? void 0 : container.getBoundingClientRect().width; const node = container === null || container === void 0 ? void 0 : container.ownerDocument.body; const strategy = isFixed ? "fixed" : "absolute"; const className = (0, classNamesFn_1.classNames)(_className)("select2-portal"); const options = (0, react_1.useMemo)(() => Array.isArray(value) && isMultiple ? _options.filter(option => value.every(tagValue => tagValue !== option.value)) : _options, [_options, value, isMultiple]); const searcher = (0, react_1.useMemo)(() => new fuzzy_search_1.default(options, ["value"]), [options]); const getItemStyles = (0, react_1.useCallback)((style) => (Object.assign(Object.assign({}, style), { width: menuWidth })), [menuWidth]); const items = searcher.search(inputValue).map((item, index) => (react_1.default.createElement(DropdownItem_1.DropdownItem, Object.assign({ key: item.value, value: item.value, isSelected: selectedItem === item.value || highlightedIndex === index }, getItemProps({ index, item }))))); const scrollbarHeight = (0, react_1.useMemo)(() => (0, utils_1.getDropdownHeight)(items.length, itemHeight, minItems, maxItems), [items.length, itemHeight, minItems, maxItems]); return node && isOpen ? (react_1.default.createElement(Portal_1.Portal, { node: node, className: className }, react_1.default.createElement(react_popper_1.Popper, { placement: "bottom", strategy: strategy }, ({ ref, style, placement }) => (react_1.default.createElement("div", { ref: ref, style: getItemStyles(style), className: `${constants_1.BRZ_PREFIX}-control__select2-menu`, "data-placement": placement }, react_1.default.createElement(LegacyScrollbar_1.LegacyScrollBar, { autoHeightMax: scrollbarHeight, theme: "dark" }, react_1.default.createElement("ul", Object.assign({ className: `${constants_1.BRZ_PREFIX}-ul` }, getMenuProps()), items.length > 0 ? (items) : (react_1.default.createElement("li", { className: `${constants_1.BRZ_PREFIX}-control__select2-option` }, t("Nothing found")))))))))) : null; }; exports.Dropdown = Dropdown;