import React, { createRef, isValidElement, useEffect, useState } from 'react'; import PropTypes from 'prop-types'; import { FormattedMessage } from 'react-intl'; import { isEmpty } from 'lodash'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import matchSorter from 'match-sorter'; import Wrapper from './Wrapper'; import List from './List'; import Search from './Search'; import LeftMenuLink from '../LeftMenuLink'; import LeftMenuSubList from '../LeftMenuSubList'; function LeftMenuList({ customLink, links, title }) { const [search, setSearch] = useState(''); const [showSearch, setShowSearch] = useState(false); const ref = createRef(); useEffect(() => { if (showSearch && ref.current) { ref.current.focus(); } }, [ref, showSearch]); const { Component, componentProps } = customLink || { Component: null, componentProps: {}, }; const toggleSearch = () => setShowSearch(!showSearch); const handleClose = () => { clearSearch(); toggleSearch(); }; const clearSearch = () => { setSearch(''); }; const handleChange = ({ target: { value } }) => { setSearch(value); }; const hasChildObject = () => links.some(link => !isEmpty(link.links)); const getCount = () => { if (hasChildObject()) { return links.reduce((acc, current) => { return acc + current.links.length; }, 0); } return links.length; }; const getList = () => { if (hasChildObject()) { return links.map(link => { return { ...link, links: matchSorter(link.links, search, { keys: ['title'] }), }; }); } return matchSorter(links, search, { keys: ['title'] }); }; const getTitle = () => getCount() > 1 ? `${title.id}plural` : `${title.id}singular`; const renderCompo = (link, i) => { const { links, name, title, ...rest } = link; if (links) { const isSearching = !isEmpty(search); return ( ); } return (
  • {title}
  • ); }; return (
    {!showSearch ? (

       {getCount()}

    ) : (
    )}
    {getList().map((link, i) => renderCompo(link, i))} {Component && isValidElement() && ( )}
    ); } LeftMenuList.defaultProps = { customLink: null, links: [], title: 'models', }; LeftMenuList.propTypes = { customLink: PropTypes.shape({ Component: PropTypes.object, componentProps: PropTypes.shape({ id: PropTypes.string, onClick: PropTypes.func, }), }), links: PropTypes.array, title: PropTypes.string, }; export default LeftMenuList;