Properly handle navigating to previous page if 'backHref' is undefined

This commit is contained in:
Justin 2022-03-21 15:23:57 -04:00
parent b4d677dfde
commit e02d5142e5
2 changed files with 18 additions and 4 deletions

View file

@ -1,10 +1,11 @@
import React from 'react'; import React from 'react';
import { RouteComponentProps, withRouter } from 'react-router-dom';
import Helmet from 'soapbox/components/helmet'; import Helmet from 'soapbox/components/helmet';
import { Card, CardBody, CardHeader, CardTitle } from '../card/card'; import { Card, CardBody, CardHeader, CardTitle } from '../card/card';
interface IColumn { interface IColumn extends RouteComponentProps {
backHref?: string, backHref?: string,
label?: string, label?: string,
transparent?: boolean, transparent?: boolean,
@ -12,7 +13,20 @@ interface IColumn {
} }
const Column: React.FC<IColumn> = React.forwardRef((props, ref: React.ForwardedRef<HTMLDivElement>): JSX.Element => { const Column: React.FC<IColumn> = React.forwardRef((props, ref: React.ForwardedRef<HTMLDivElement>): JSX.Element => {
const { backHref, children, label, transparent = false, withHeader = true } = props; const { backHref, children, label, history, transparent = false, withHeader = true } = props;
const handleBackClick = () => {
if (backHref) {
history.push(backHref);
return;
}
if (history.length === 1) {
history.push('/');
} else {
history.goBack();
}
};
const renderChildren = () => { const renderChildren = () => {
if (transparent) { if (transparent) {
@ -22,7 +36,7 @@ const Column: React.FC<IColumn> = React.forwardRef((props, ref: React.ForwardedR
return ( return (
<Card variant='rounded'> <Card variant='rounded'>
{withHeader ? ( {withHeader ? (
<CardHeader backHref={backHref}> <CardHeader onBackClick={handleBackClick}>
<CardTitle title={label} /> <CardTitle title={label} />
</CardHeader> </CardHeader>
) : null} ) : null}
@ -43,4 +57,4 @@ const Column: React.FC<IColumn> = React.forwardRef((props, ref: React.ForwardedR
); );
}); });
export default Column; export default withRouter(Column);