gab-social/app/javascript/gabsocial/components/tab_bar_item.js

112 lines
2.4 KiB
JavaScript
Raw Normal View History

2020-03-26 03:11:32 +00:00
import { withRouter } from 'react-router-dom'
2020-02-22 23:26:23 +00:00
import classNames from 'classnames/bind'
2020-03-26 03:11:32 +00:00
import Button from './button'
import Icon from './icon'
2020-02-22 23:26:23 +00:00
import Text from './text'
const cx = classNames.bind(_s)
export default
@withRouter
class TabBarItem extends PureComponent {
static propTypes = {
location: PropTypes.object.isRequired,
title: PropTypes.string,
2020-03-26 03:11:32 +00:00
onClick: PropTypes.func,
icon: PropTypes.string,
2020-02-22 23:26:23 +00:00
to: PropTypes.string,
2020-02-28 15:20:47 +00:00
large: PropTypes.bool,
2020-03-26 03:11:32 +00:00
active: PropTypes.bool,
2020-02-22 23:26:23 +00:00
}
state = {
active: -1,
}
componentDidUpdate(prevProps) {
if (this.props.location !== prevProps.location) {
const isCurrent = this.props.to === this.props.location.pathname
if (this.state.active !== isCurrent) {
this.setState({
active: isCurrent,
})
}
}
}
render() {
2020-03-26 03:11:32 +00:00
const {
title,
to,
onClick,
location,
large,
icon,
// active
} = this.props
2020-02-22 23:26:23 +00:00
const { active } = this.state
const isCurrent = active === -1 ? to === location.pathname : active
const containerClasses = cx({
default: 1,
height53PX: 1,
noUnderline: 1,
text: 1,
displayFlex: 1,
alignItemsCenter: 1,
justifyContentCenter: 1,
borderBottom2PX: 1,
2020-03-11 23:56:18 +00:00
py5: 1,
2020-03-26 03:11:32 +00:00
backgroundTransparent: 1,
2020-02-22 23:26:23 +00:00
borderColorTransparent: !isCurrent,
borderColorBrand: isCurrent,
2020-03-11 23:56:18 +00:00
mr5: large,
2020-02-28 15:20:47 +00:00
})
const textParentClasses = cx({
default: 1,
height100PC: 1,
alignItemsCenter: 1,
justifyContentCenter: 1,
radiusSmall: 1,
2020-03-11 23:56:18 +00:00
px10: !large,
px15: large,
2020-02-28 15:20:47 +00:00
backgroundSubtle2Dark_onHover: !isCurrent,
2020-02-22 23:26:23 +00:00
})
const textOptions = {
2020-02-28 15:20:47 +00:00
size: !!large ? 'normal' : 'small',
color: isCurrent ? 'brand' : large ? 'secondary' : 'primary',
weight: isCurrent ? 'bold' : large ? 'medium' : 'normal',
2020-02-22 23:26:23 +00:00
}
2020-03-26 03:11:32 +00:00
const iconOptions = {
id: icon,
width: !!large ? 20 : 14,
height: !!large ? 20 : 14,
}
2020-02-22 23:26:23 +00:00
return (
2020-03-26 03:11:32 +00:00
<Button
onClick={onClick}
to={to}
className={containerClasses}
noClasses
>
2020-02-28 15:20:47 +00:00
<span className={textParentClasses}>
2020-03-26 03:11:32 +00:00
{ !!title &&
2020-02-28 15:20:47 +00:00
<Text {...textOptions}>
{title}
</Text>
2020-03-26 03:11:32 +00:00
}
{ !!icon &&
<Icon {...iconOptions} />
}
2020-02-28 15:20:47 +00:00
</span>
2020-03-26 03:11:32 +00:00
</Button>
2020-02-22 23:26:23 +00:00
)
}
}