2017-06-16 03:39:08 +00:00
|
|
|
// @flow
|
2017-10-23 03:17:54 +00:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { observable, action } from 'mobx';
|
|
|
|
import { observer } from 'mobx-react';
|
2017-07-02 00:02:17 +00:00
|
|
|
import { NavLink } from 'react-router-dom';
|
2017-10-27 05:42:08 +00:00
|
|
|
import { color, fontWeight } from 'shared/styles/constants';
|
2017-06-16 03:39:08 +00:00
|
|
|
import styled from 'styled-components';
|
2017-10-29 22:02:24 +00:00
|
|
|
import Flex from 'shared/components/Flex';
|
2017-10-21 21:05:20 +00:00
|
|
|
import CollapsedIcon from 'components/Icon/CollapsedIcon';
|
2017-09-27 07:21:40 +00:00
|
|
|
|
2017-06-16 03:39:08 +00:00
|
|
|
const activeStyle = {
|
2017-09-09 05:06:47 +00:00
|
|
|
color: color.black,
|
2017-11-08 06:02:07 +00:00
|
|
|
fontWeight: fontWeight.medium,
|
2017-06-16 03:39:08 +00:00
|
|
|
};
|
|
|
|
|
2017-10-21 21:05:20 +00:00
|
|
|
const StyledGoTo = styled(CollapsedIcon)`
|
2017-10-21 23:21:43 +00:00
|
|
|
margin-bottom: -4px;
|
2017-11-01 04:35:17 +00:00
|
|
|
margin-left: 1px;
|
|
|
|
margin-right: -3px;
|
2017-10-21 23:21:43 +00:00
|
|
|
${({ expanded }) => !expanded && 'transform: rotate(-90deg);'}
|
2017-10-21 21:05:20 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
const IconWrapper = styled.span`
|
|
|
|
margin-left: -4px;
|
|
|
|
margin-right: 4px;
|
|
|
|
height: 24px;
|
|
|
|
`;
|
|
|
|
|
2017-10-31 07:03:17 +00:00
|
|
|
const StyledNavLink = styled(NavLink)`
|
2017-10-16 01:43:26 +00:00
|
|
|
display: flex;
|
2017-09-27 07:21:40 +00:00
|
|
|
width: 100%;
|
2017-10-21 21:05:20 +00:00
|
|
|
position: relative;
|
2017-09-09 05:06:47 +00:00
|
|
|
overflow: hidden;
|
|
|
|
text-overflow: ellipsis;
|
2017-10-21 21:05:20 +00:00
|
|
|
padding: 4px 0;
|
2017-09-27 07:21:40 +00:00
|
|
|
margin-left: ${({ hasChildren }) => (hasChildren ? '-20px;' : '0')};
|
2017-07-09 05:12:14 +00:00
|
|
|
color: ${color.slateDark};
|
2017-09-04 15:32:31 +00:00
|
|
|
font-size: 15px;
|
2017-09-18 06:45:40 +00:00
|
|
|
cursor: pointer;
|
2017-07-02 00:02:17 +00:00
|
|
|
|
2017-07-09 05:12:14 +00:00
|
|
|
&:hover {
|
2017-09-09 05:06:47 +00:00
|
|
|
color: ${color.text};
|
2017-06-16 03:39:08 +00:00
|
|
|
}
|
2017-09-27 07:21:40 +00:00
|
|
|
|
2017-10-21 21:05:20 +00:00
|
|
|
&.active {
|
|
|
|
svg {
|
|
|
|
fill: ${activeStyle.color}
|
|
|
|
}
|
2017-09-27 07:21:40 +00:00
|
|
|
}
|
2017-06-16 03:39:08 +00:00
|
|
|
`;
|
|
|
|
|
2017-10-31 07:03:17 +00:00
|
|
|
const StyledDiv = StyledNavLink.withComponent('div');
|
|
|
|
|
2017-10-21 21:05:20 +00:00
|
|
|
type Props = {
|
|
|
|
to?: string,
|
|
|
|
onClick?: SyntheticEvent => *,
|
|
|
|
children?: React$Element<*>,
|
|
|
|
icon?: React$Element<*>,
|
2017-10-23 03:17:54 +00:00
|
|
|
expand?: boolean,
|
|
|
|
expandedContent?: React$Element<*>,
|
2017-10-21 21:05:20 +00:00
|
|
|
};
|
|
|
|
|
2017-10-23 03:17:54 +00:00
|
|
|
@observer class SidebarLink extends Component {
|
|
|
|
props: Props;
|
2017-10-31 07:03:17 +00:00
|
|
|
@observable expanded: boolean = false;
|
2017-10-23 03:17:54 +00:00
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
if (this.props.expand) this.handleExpand();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidReceiveProps(nextProps: Props) {
|
|
|
|
if (nextProps.expand) this.handleExpand();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action handleClick = (event: SyntheticEvent) => {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
this.expanded = !this.expanded;
|
|
|
|
};
|
|
|
|
|
|
|
|
@action handleExpand = () => {
|
|
|
|
this.expanded = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { icon, children, expandedContent, ...rest } = this.props;
|
2017-10-31 07:03:17 +00:00
|
|
|
const Component = rest.to ? StyledNavLink : StyledDiv;
|
2017-10-23 03:17:54 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Flex column>
|
|
|
|
<Component
|
|
|
|
exact
|
|
|
|
activeStyle={activeStyle}
|
|
|
|
hasChildren={expandedContent}
|
|
|
|
{...rest}
|
|
|
|
>
|
|
|
|
{icon && <IconWrapper>{icon}</IconWrapper>}
|
|
|
|
{expandedContent &&
|
|
|
|
<StyledGoTo expanded={this.expanded} onClick={this.handleClick} />}
|
|
|
|
<Content onClick={this.handleExpand}>{children}</Content>
|
|
|
|
</Component>
|
|
|
|
{this.expanded && expandedContent}
|
|
|
|
</Flex>
|
|
|
|
);
|
|
|
|
}
|
2017-09-18 06:45:40 +00:00
|
|
|
}
|
|
|
|
|
2017-10-16 05:15:15 +00:00
|
|
|
const Content = styled.div`
|
|
|
|
width: 100%;
|
|
|
|
`;
|
|
|
|
|
2017-07-02 00:02:17 +00:00
|
|
|
export default SidebarLink;
|