bigbuffet-rw/app/soapbox/components/column_back_button.js

42 lines
982 B
JavaScript
Raw Normal View History

import PropTypes from 'prop-types';
2020-03-27 13:59:38 -07:00
import React from 'react';
import { FormattedMessage } from 'react-intl';
2022-03-17 18:17:28 -07:00
import { withRouter } from 'react-router-dom';
2020-05-28 15:52:07 -07:00
import Icon from 'soapbox/components/icon';
2020-03-27 13:59:38 -07:00
2022-03-17 18:17:28 -07:00
export default @withRouter
class ColumnBackButton extends React.PureComponent {
2020-03-27 13:59:38 -07:00
static propTypes = {
to: PropTypes.string,
2022-03-17 18:17:28 -07:00
history: PropTypes.object,
2020-03-27 13:59:38 -07:00
};
handleClick = () => {
const { to } = this.props;
if (window.history?.length === 1) {
2022-03-17 18:17:28 -07:00
this.props.history.push(to ? to : '/');
2020-03-27 13:59:38 -07:00
} else {
2022-03-17 18:17:28 -07:00
this.props.history.goBack();
2020-03-27 13:59:38 -07:00
}
}
handleKeyUp = (e) => {
if (e.key === 'Enter') {
this.handleClick();
}
}
render() {
2020-03-27 13:59:38 -07:00
return (
<button onClick={this.handleClick} onKeyUp={this.handleKeyUp} className='column-back-button'>
2020-03-27 13:59:38 -07:00
<Icon id='chevron-left' className='column-back-button__icon' fixedWidth />
<FormattedMessage id='column_back_button.label' defaultMessage='Back' />
</button>
);
}
}