Files
SCM-Manager/scm-ui/ui-components/src/buttons/Button.tsx

92 lines
2.1 KiB
TypeScript
Raw Normal View History

import React, { MouseEvent, ReactNode } from "react";
import classNames from "classnames";
import { withRouter, RouteComponentProps } from "react-router-dom";
import Icon from "../Icon";
export type ButtonProps = {
label?: string;
loading?: boolean;
disabled?: boolean;
action?: (event: MouseEvent) => void;
link?: string;
className?: string;
icon?: string;
fullWidth?: boolean;
reducedMobile?: boolean;
children?: ReactNode;
};
type Props = ButtonProps &
RouteComponentProps & {
title?: string;
type?: "button" | "submit" | "reset";
color?: string;
};
class Button extends React.Component<Props> {
static defaultProps: Partial<Props> = {
type: "button",
color: "default"
};
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() {
const {
label,
title,
loading,
disabled,
type,
color,
className,
icon,
fullWidth,
reducedMobile,
children
} = this.props;
const loadingClass = loading ? "is-loading" : "";
const fullWidthClass = fullWidth ? "is-fullwidth" : "";
const reducedMobileClass = reducedMobile ? "is-reduced-mobile" : "";
if (icon) {
return (
<button
type={type}
title={title}
disabled={disabled}
onClick={this.onClick}
2019-10-21 10:57:56 +02:00
className={classNames("button", "is-" + color, loadingClass, fullWidthClass, reducedMobileClass, className)}
>
<span className="icon is-medium">
<Icon name={icon} color="inherit" />
</span>
<span>
{label} {children}
</span>
</button>
);
}
return (
<button
type={type}
title={title}
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)}
>
{label} {children}
</button>
);
}
}
2018-10-11 17:29:50 +02:00
export default withRouter(Button);