2020-08-17 21:07:16 +01:00
|
|
|
import React from 'react'
|
2020-08-17 21:59:29 +01:00
|
|
|
import PropTypes from 'prop-types'
|
2020-04-29 23:32:49 +01:00
|
|
|
import { withRouter } from 'react-router-dom'
|
|
|
|
import { CX } from '../constants'
|
|
|
|
import Button from './button'
|
|
|
|
import Text from './text'
|
|
|
|
|
2020-08-17 21:07:16 +01:00
|
|
|
class PillItem extends React.PureComponent {
|
2020-04-29 23:32:49 +01:00
|
|
|
|
|
|
|
state = {
|
2020-05-01 06:50:27 +01:00
|
|
|
isCurrent: false,
|
2020-04-29 23:32:49 +01:00
|
|
|
}
|
|
|
|
|
2020-07-25 02:18:07 +01:00
|
|
|
componentDidMount() {
|
|
|
|
this.checkIfCurrent()
|
|
|
|
}
|
|
|
|
|
2020-04-29 23:32:49 +01:00
|
|
|
componentDidUpdate(prevProps) {
|
2020-07-25 02:18:07 +01:00
|
|
|
if (prevProps.location !== this.props.location) {
|
|
|
|
this.checkIfCurrent()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkIfCurrent() {
|
2020-04-29 23:32:49 +01:00
|
|
|
// If user navigates to different page, ensure tab bar item
|
|
|
|
// with this.props.to that is on location is set to active.
|
2020-07-25 02:18:07 +01:00
|
|
|
const isCurrent = this.props.to === this.props.location.pathname && !this.props.location.search
|
2020-04-29 23:32:49 +01:00
|
|
|
|
2020-07-25 02:18:07 +01:00
|
|
|
if (this.state.isCurrent !== isCurrent) {
|
|
|
|
this.setState({ isCurrent })
|
2020-04-29 23:32:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
title,
|
|
|
|
to,
|
|
|
|
onClick,
|
|
|
|
location,
|
|
|
|
isActive,
|
|
|
|
} = this.props
|
|
|
|
const { isCurrent } = this.state
|
|
|
|
|
|
|
|
// Combine state, props, location to make absolutely
|
|
|
|
// sure of active status.
|
2020-07-02 03:39:43 +01:00
|
|
|
const active = isActive || (to === location.pathname && !location.search) || isCurrent
|
2020-04-29 23:32:49 +01:00
|
|
|
|
|
|
|
const containerClasses = CX({
|
2020-08-18 21:49:11 +01:00
|
|
|
d: 1,
|
2020-04-29 23:32:49 +01:00
|
|
|
noUnderline: 1,
|
|
|
|
text: 1,
|
2020-08-18 21:43:06 +01:00
|
|
|
aiCenter: 1,
|
|
|
|
jcCenter: 1,
|
2020-04-29 23:32:49 +01:00
|
|
|
py5: 1,
|
|
|
|
outlineNone: 1,
|
|
|
|
cursorPointer: 1,
|
|
|
|
circle: 1,
|
|
|
|
bgSecondary: !active,
|
|
|
|
bgSecondaryDark_onHover: !active,
|
|
|
|
bgBrand: active,
|
|
|
|
mr5: 1,
|
|
|
|
mb5: 1,
|
|
|
|
})
|
|
|
|
|
|
|
|
const textParentClasses = CX({
|
2020-08-18 21:49:11 +01:00
|
|
|
d: 1,
|
2020-08-18 21:43:06 +01:00
|
|
|
h100PC: 1,
|
|
|
|
aiCenter: 1,
|
|
|
|
jcCenter: 1,
|
2020-04-29 23:32:49 +01:00
|
|
|
py2: 1,
|
|
|
|
px15: 1,
|
|
|
|
})
|
|
|
|
|
|
|
|
const textOptions = {
|
|
|
|
size: 'small',
|
|
|
|
color: active ? 'white' : 'secondary',
|
|
|
|
weight: active ? 'bold' : 'medium',
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
onClick={onClick}
|
|
|
|
className={containerClasses}
|
|
|
|
to={to || undefined}
|
|
|
|
noClasses
|
|
|
|
>
|
|
|
|
<span className={textParentClasses}>
|
|
|
|
<Text {...textOptions}>
|
|
|
|
{title}
|
|
|
|
</Text>
|
|
|
|
</span>
|
|
|
|
</Button>
|
|
|
|
)
|
|
|
|
}
|
2020-08-18 01:57:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PillItem.propTypes = {
|
|
|
|
icon: PropTypes.string,
|
|
|
|
isActive: PropTypes.bool,
|
|
|
|
onClick: PropTypes.func,
|
|
|
|
title: PropTypes.string,
|
|
|
|
to: PropTypes.string,
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withRouter(PillItem)
|