UNPKG

@findify/react-components

Version:
149 lines (129 loc) 6.33 kB
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } /** * @module components/search/DesktopFacets */ import Branch from "../../common/Branch"; import MapArray from "../../common/MapArray"; import Facet from "../../Facet"; import Sticky from "../../common/Sticky"; import Title from "./Title"; import { useFacets } from '@findify/react-connect'; import { memo, useCallback, useMemo, useState } from 'react'; import useTranslations from "../../../helpers/useTranslations"; import { useEvents, emit } from "../../../helpers/emmiter"; import { jsx as _jsx } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; var styles = { "root": "findify-components-search--desktop-facets", "container": "findify-components-search--desktop-facets__container", "sticky": "findify-components-search--desktop-facets__sticky", "horizontal": "findify-components-search--desktop-facets__horizontal", "header": "findify-components-search--desktop-facets__header", "title": "findify-components-search--desktop-facets__title", "icon": "findify-components-search--desktop-facets__icon", "reset": "findify-components-search--desktop-facets__reset", "hide": "findify-components-search--desktop-facets__hide" }; var DefaultContent = function DefaultContent(_ref) { var theme = _ref.theme, children = _ref.children, title = _ref.title; return /*#__PURE__*/_jsx("section", { className: theme.root, role: "region", "aria-label": title, tabIndex: 0, children: /*#__PURE__*/_jsx("div", { className: theme.container, children: children }) }); }; export default /*#__PURE__*/memo(function (_ref2) { var _ref2$theme = _ref2.theme, theme = _ref2$theme === void 0 ? styles : _ref2$theme; var _useFacets = useFacets(), facets = _useFacets.facets, meta = _useFacets.meta, onReset = _useFacets.onReset, config = _useFacets.config; var translate = useTranslations(); var _useMemo = useMemo(function () { return [config.getIn(['facets', 'position']) === 'top', config.getIn(['facets', 'hidable']), config.getIn(['facets', 'sticky']), config.getIn(['facets', 'accordion'])]; }, []), _useMemo2 = _slicedToArray(_useMemo, 4), isHorizontal = _useMemo2[0], isHidable = _useMemo2[1], isSticky = _useMemo2[2], isAccordion = _useMemo2[3]; var _useState = useState(true), _useState2 = _slicedToArray(_useState, 2), visible = _useState2[0], setVisible = _useState2[1]; /** Generate initial list of opened facets */ var _useState3 = useState(config.getIn(['facets', 'filters']).filter(function (f) { return !f.get('initiallyCollapsed'); }).keySeq().toArray()), _useState4 = _slicedToArray(_useState3, 2), openFacets = _useState4[0], setFacetsStates = _useState4[1]; var _toggleFacet = function toggleFacet(name) { return setFacetsStates(function (facets) { var isOpen = facets.includes(name); if (isAccordion) return isOpen ? [] : [name]; return isOpen ? _toConsumableArray(facets.filter(function (k) { return k !== name; })) : [].concat(_toConsumableArray(facets), [name]); }); }; useEvents({ showFacets: function showFacets() { return setVisible(true); }, toggleFacet: function toggleFacet(name) { return _toggleFacet(name); } }); var hideFacets = useCallback(function () { emit('hideFacets'); setVisible(false); }, []); return !isHidable || visible ? /*#__PURE__*/_jsxs(Branch, { theme: { container: theme.container, root: isHorizontal ? theme.horizontal : theme.root }, condition: isSticky, title: translate('facets.filters'), left: Sticky, right: DefaultContent, stickToTop: isHorizontal, offset: isHorizontal ? 0 : 25, children: [/*#__PURE__*/_jsx(Title, { meta: meta, theme: theme, onReset: onReset, onHide: hideFacets, hidable: isHidable }), /*#__PURE__*/_jsx(MapArray, { array: facets, factory: Facet, config: config, isHorizontal: isHorizontal, onToggle: _toggleFacet, openFacets: openFacets, keyAccessor: function keyAccessor(i) { return i.get('name'); } })] }) : null; });