114 lines
2.5 KiB
JavaScript
Raw Normal View History

2019-12-16 21:53:16 +01:00
import React, { isValidElement, useState } from 'react';
2019-10-23 15:49:03 +02:00
import PropTypes from 'prop-types';
import { isEmpty } from 'lodash';
import matchSorter from 'match-sorter';
2019-10-16 16:56:44 +02:00
import Wrapper from './Wrapper';
2019-10-23 15:49:03 +02:00
import List from './List';
2019-10-16 16:56:44 +02:00
2019-10-24 14:41:06 +02:00
import LeftMenuLink from '../LeftMenuLink';
2019-10-23 15:49:03 +02:00
import LeftMenuSubList from '../LeftMenuSubList';
2019-12-16 21:53:16 +01:00
import LeftMenuHeader from '../LeftMenuHeader';
2019-10-16 16:56:44 +02:00
2019-12-16 21:53:16 +01:00
function LeftMenuList({ customLink, links, title, searchable }) {
2019-10-23 15:49:03 +02:00
const [search, setSearch] = useState('');
2019-10-30 17:27:58 +01:00
const { Component, componentProps } = customLink || {
Component: null,
componentProps: {},
};
2019-10-16 16:56:44 +02:00
2019-10-30 17:27:58 +01:00
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;
2019-10-23 15:49:03 +02:00
};
const getList = () => {
if (hasChildObject()) {
return links.map(link => {
return {
...link,
2019-12-06 16:40:50 +01:00
links: matchSorter(link.links, search, { keys: ['title'] }),
2019-10-23 15:49:03 +02:00
};
});
}
2019-12-06 16:40:50 +01:00
return matchSorter(links, search, { keys: ['title'] });
2019-10-23 15:49:03 +02:00
};
const renderCompo = (link, i) => {
const { links, name, title, ...rest } = link;
2019-10-23 15:49:03 +02:00
if (links) {
2019-10-30 17:27:58 +01:00
const isSearching = !isEmpty(search);
2019-10-23 15:49:03 +02:00
return (
<LeftMenuSubList
key={name}
{...rest}
2019-10-23 15:49:03 +02:00
{...link}
2019-10-30 17:27:58 +01:00
isSearching={isSearching}
2019-10-23 15:49:03 +02:00
isFirstItem={i === 0}
/>
);
}
return (
<li key={name}>
2019-10-24 14:41:06 +02:00
<LeftMenuLink {...link}>{title}</LeftMenuLink>
2019-10-23 15:49:03 +02:00
</li>
);
};
2019-10-16 16:56:44 +02:00
2019-12-16 21:53:16 +01:00
const headerProps = {
count: getCount(),
search,
searchable,
setSearch,
title,
};
2019-10-16 16:56:44 +02:00
return (
2019-10-23 15:49:03 +02:00
<Wrapper>
2019-10-16 16:56:44 +02:00
<div className="list-header">
2019-12-16 21:53:16 +01:00
<LeftMenuHeader {...headerProps} />
2019-10-23 15:49:03 +02:00
</div>
<div>
<List>{getList().map((link, i) => renderCompo(link, i))}</List>
2019-10-30 17:27:58 +01:00
{Component && isValidElement(<Component />) && (
<Component {...componentProps} />
)}
2019-10-16 16:56:44 +02:00
</div>
</Wrapper>
);
}
2019-10-23 15:49:03 +02:00
LeftMenuList.defaultProps = {
2019-10-30 17:27:58 +01:00
customLink: null,
2019-10-23 15:49:03 +02:00
links: [],
2019-12-12 17:11:28 +01:00
title: null,
2019-12-16 21:53:16 +01:00
searchable: false,
2019-10-23 15:49:03 +02:00
};
LeftMenuList.propTypes = {
customLink: PropTypes.shape({
Component: PropTypes.object,
componentProps: PropTypes.shape({
id: PropTypes.string,
onClick: PropTypes.func,
}),
}),
links: PropTypes.array,
2019-12-16 21:53:16 +01:00
title: PropTypes.shape({
id: PropTypes.string,
}),
searchable: PropTypes.bool,
2019-10-23 15:49:03 +02:00
};
2019-10-16 16:56:44 +02:00
export default LeftMenuList;