Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/elements/common/nav-button/BackButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as React from 'react';
import classNames from 'classnames';
import { FormattedMessage } from 'react-intl';
import { Route } from 'react-router-dom';
import type { Location } from 'history';
import IconNavigateLeft from '../../../icons/general/IconNavigateLeft';
import PlainButton from '../../../components/plain-button';
import messages from '../messages';
import { ButtonType } from '../../../components/button';
import './BackButton.scss';

export interface BackButtonProps {
className?: string;
to?: Location;
}

const BackButton = ({ className, to, ...rest }: BackButtonProps) => (
<Route>
{({ history }) => (
<PlainButton
className={classNames('bdl-BackButton', className)}
onClick={() => (to ? history.push(to) : history.goBack())}
type={ButtonType.BUTTON}
{...rest}
>
<IconNavigateLeft height={24} width={24} />
<span className="accessibility-hidden">
<FormattedMessage {...messages.back} />
</span>
</PlainButton>
)}
</Route>
);

export default BackButton;
77 changes: 77 additions & 0 deletions src/elements/common/nav-button/NavButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import * as React from 'react';
import classNames from 'classnames';
import { Route } from 'react-router-dom';
import type { match } from 'react-router';
import type { Location } from 'history';
import PlainButton, { PlainButtonProps } from '../../../components/plain-button';
import { isLeftClick } from '../../../utils/dom';

export interface NavButtonProps {
activeClassName?: string;
children: React.ReactNode;
className?: string;
component?: React.ComponentType<PlainButtonProps & { ref?: React.Ref<HTMLButtonElement> }>;
exact?: boolean;
isActive?: (match: match, location: Location) => boolean;
isDisabled?: boolean;
onClick?: (event: React.SyntheticEvent) => void;
replace?: boolean;
strict?: boolean;
to: string | Location;
}

const NavButton = React.forwardRef<HTMLButtonElement, NavButtonProps>(
(props: NavButtonProps, ref: React.Ref<HTMLButtonElement>) => {
const {
activeClassName = 'bdl-is-active',
children,
className = 'bdl-NavButton',
component: Component = PlainButton,
exact,
isActive,
isDisabled,
onClick,
replace,
strict,
to,
...rest
} = props;
const path = typeof to === 'object' ? to.pathname : to;

const disabledClassName = 'bdl-is-disabled';

return (
<Route exact={exact} path={path} strict={strict}>
{({ history, location, match }) => {
const isActiveValue = !!(isActive ? isActive(match, location) : match);

return (
<Component
className={classNames(className, {
[activeClassName]: isActiveValue,
[disabledClassName]: isDisabled,
})}
isDisabled={isDisabled}
onClick={event => {
if (onClick) {
onClick(event);
}

if (!event.defaultPrevented && isLeftClick(event)) {
const method = replace ? history.replace : history.push;
method(to);
}
}}
ref={ref}
{...rest}
>
{children}
</Component>
);
}}
</Route>
);
},
);

export default NavButton;
2 changes: 2 additions & 0 deletions src/elements/common/nav-button/index.js.flow
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as BackButton } from './BackButton';
export { default } from './NavButton';
4 changes: 4 additions & 0 deletions src/elements/common/nav-button/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { default as BackButton } from './BackButton';
export { default } from './NavButton';
export type { BackButtonProps } from './BackButton';
export type { NavButtonProps } from './NavButton';