2019-10-20 16:59:02 +02:00
|
|
|
import React, { MouseEvent, ReactNode } from "react";
|
|
|
|
|
import classNames from "classnames";
|
|
|
|
|
import { withRouter, RouteComponentProps } from "react-router-dom";
|
|
|
|
|
import Icon from "../Icon";
|
2018-09-03 16:17:36 +02:00
|
|
|
|
|
|
|
|
export type ButtonProps = {
|
2019-10-19 16:38:07 +02:00
|
|
|
label?: string;
|
|
|
|
|
loading?: boolean;
|
|
|
|
|
disabled?: boolean;
|
2019-10-20 16:59:02 +02:00
|
|
|
action?: (event: MouseEvent) => void;
|
2019-10-19 16:38:07 +02:00
|
|
|
link?: string;
|
|
|
|
|
className?: string;
|
|
|
|
|
icon?: string;
|
|
|
|
|
fullWidth?: boolean;
|
|
|
|
|
reducedMobile?: boolean;
|
2019-10-20 16:59:02 +02:00
|
|
|
children?: ReactNode;
|
2018-09-03 16:17:36 +02:00
|
|
|
};
|
|
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
type Props = ButtonProps &
|
|
|
|
|
RouteComponentProps & {
|
2019-11-12 17:30:37 +01:00
|
|
|
title?: string;
|
2019-10-20 16:59:02 +02:00
|
|
|
type?: "button" | "submit" | "reset";
|
|
|
|
|
color?: string;
|
|
|
|
|
};
|
2018-09-03 16:17:36 +02:00
|
|
|
|
|
|
|
|
class Button extends React.Component<Props> {
|
2019-10-20 16:59:02 +02:00
|
|
|
static defaultProps: Partial<Props> = {
|
|
|
|
|
type: "button",
|
|
|
|
|
color: "default"
|
2018-09-03 16:17:36 +02:00
|
|
|
};
|
|
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
onClick = (event: React.MouseEvent) => {
|
2018-10-11 17:29:50 +02:00
|
|
|
const { action, link, history } = this.props;
|
|
|
|
|
if (action) {
|
|
|
|
|
action(event);
|
|
|
|
|
} else if (link) {
|
|
|
|
|
history.push(link);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
2019-11-12 17:30:37 +01:00
|
|
|
const {
|
|
|
|
|
label,
|
|
|
|
|
title,
|
|
|
|
|
loading,
|
|
|
|
|
disabled,
|
|
|
|
|
type,
|
|
|
|
|
color,
|
|
|
|
|
className,
|
|
|
|
|
icon,
|
|
|
|
|
fullWidth,
|
|
|
|
|
reducedMobile,
|
|
|
|
|
children
|
|
|
|
|
} = this.props;
|
2019-10-20 16:59:02 +02:00
|
|
|
const loadingClass = loading ? "is-loading" : "";
|
|
|
|
|
const fullWidthClass = fullWidth ? "is-fullwidth" : "";
|
|
|
|
|
const reducedMobileClass = reducedMobile ? "is-reduced-mobile" : "";
|
2019-09-23 14:12:02 +02:00
|
|
|
if (icon) {
|
|
|
|
|
return (
|
|
|
|
|
<button
|
|
|
|
|
type={type}
|
2019-11-12 17:30:37 +01:00
|
|
|
title={title}
|
2019-09-23 14:12:02 +02:00
|
|
|
disabled={disabled}
|
|
|
|
|
onClick={this.onClick}
|
2019-10-21 10:57:56 +02:00
|
|
|
className={classNames("button", "is-" + color, loadingClass, fullWidthClass, reducedMobileClass, className)}
|
2019-09-23 14:12:02 +02:00
|
|
|
>
|
|
|
|
|
<span className="icon is-medium">
|
|
|
|
|
<Icon name={icon} color="inherit" />
|
|
|
|
|
</span>
|
|
|
|
|
<span>
|
|
|
|
|
{label} {children}
|
|
|
|
|
</span>
|
|
|
|
|
</button>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-03 16:17:36 +02:00
|
|
|
return (
|
|
|
|
|
<button
|
|
|
|
|
type={type}
|
2019-11-12 17:30:37 +01:00
|
|
|
title={title}
|
2018-09-03 16:17:36 +02:00
|
|
|
disabled={disabled}
|
2018-10-11 17:29:50 +02:00
|
|
|
onClick={this.onClick}
|
2019-10-21 10:57:56 +02:00
|
|
|
className={classNames("button", "is-" + color, loadingClass, fullWidthClass, className)}
|
2018-09-03 16:17:36 +02:00
|
|
|
>
|
2019-02-07 09:29:53 +01:00
|
|
|
{label} {children}
|
2018-09-03 16:17:36 +02:00
|
|
|
</button>
|
|
|
|
|
);
|
2019-09-23 14:12:02 +02:00
|
|
|
}
|
2018-09-03 16:17:36 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-11 17:29:50 +02:00
|
|
|
export default withRouter(Button);
|