import React, { useState } from 'react'; import PropTypes from 'prop-types'; import classnames from 'classnames'; import { useTranslation } from 'react-i18next'; import { Button, Icon } from '../'; const borderSize = 4; const expandedWidth = 248; const collapsedWidth = 56; const baseStyle = { maxWidth: `${expandedWidth}px`, width: `${expandedWidth}px`, }; const collapsedHideWidth = expandedWidth - collapsedWidth - borderSize; const styleMap = { open: { left: { marginLeft: '0px' }, right: { marginRight: '0px' }, }, closed: { left: { marginLeft: `-${collapsedHideWidth}px` }, right: { marginRight: `-${collapsedHideWidth}px` }, }, }; const baseClasses = 'transition-all duration-300 ease-in-out h-100 bg-primary-dark border-black flex flex-col justify-start box-content'; const classesMap = { open: { left: `border-r-4`, right: `border-l-4`, }, closed: { left: `border-r-4 items-end`, right: `border-l-4 items-start`, }, }; const openIconName = { left: 'push-left', right: 'push-right', }; const position = { left: { right: 5, }, right: { left: 5, }, }; const getChildComponent = (childComponents, componentOpen) => { if (Array.isArray(childComponents)) { return childComponents.find( _childComponent => _childComponent.name === componentOpen ); } else { return childComponents; } }; const SidePanel = ({ side, className, defaultComponentOpen, childComponents, }) => { const { t } = useTranslation('SidePanel'); const [componentOpen, setComponentOpen] = useState(defaultComponentOpen); const openStatus = componentOpen ? 'open' : 'closed'; const style = Object.assign({}, styleMap[openStatus][side], baseStyle); const childComponent = getChildComponent(childComponents, componentOpen); const getPanelButtons = () => { const _childComponents = Array.isArray(childComponents) ? childComponents : [childComponents]; return _childComponents.map((childComponent, i) => { return ( ); }); }; return (
{componentOpen ? (
) : ( {getPanelButtons()} )}
); }; SidePanel.defaultProps = { defaultComponentOpen: null, }; SidePanel.propTypes = { side: PropTypes.oneOf(['left', 'right']).isRequired, className: PropTypes.string, defaultComponentOpen: PropTypes.string, childComponents: PropTypes.oneOfType([ PropTypes.shape({ iconName: PropTypes.string.isRequired, iconLabel: PropTypes.string.isRequired, name: PropTypes.string.isRequired, label: PropTypes.string.isRequired, content: PropTypes.func, // TODO: Should be node, but it keeps complaining? }), PropTypes.arrayOf( PropTypes.shape({ iconName: PropTypes.string.isRequired, iconLabel: PropTypes.string.isRequired, name: PropTypes.string.isRequired, label: PropTypes.string.isRequired, content: PropTypes.func, // TODO: Should be node, but it keeps complaining? }) ), ]), }; export default SidePanel;