2019-10-19 16:38:07 +02:00
|
|
|
import * as React from 'react';
|
|
|
|
|
import classNames from 'classnames';
|
|
|
|
|
import { withRouter } 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;
|
|
|
|
|
action?: (event: Event) => void;
|
|
|
|
|
link?: string;
|
|
|
|
|
className?: string;
|
|
|
|
|
icon?: string;
|
|
|
|
|
fullWidth?: boolean;
|
|
|
|
|
reducedMobile?: boolean;
|
|
|
|
|
children?: React.Node;
|
2018-09-03 16:17:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type Props = ButtonProps & {
|
2019-10-19 16:38:07 +02:00
|
|
|
type: string;
|
|
|
|
|
color: string;
|
2018-10-17 14:11:28 +02:00
|
|
|
|
2018-10-11 17:29:50 +02:00
|
|
|
// context prop
|
2019-10-19 16:38:07 +02:00
|
|
|
history: any;
|
2018-09-03 16:17:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Button extends React.Component<Props> {
|
|
|
|
|
static defaultProps = {
|
2019-10-19 16:38:07 +02:00
|
|
|
type: 'button',
|
|
|
|
|
color: 'default',
|
2018-09-03 16:17:36 +02:00
|
|
|
};
|
|
|
|
|
|
2018-10-11 17:29:50 +02:00
|
|
|
onClick = (event: Event) => {
|
|
|
|
|
const { action, link, history } = this.props;
|
|
|
|
|
if (action) {
|
|
|
|
|
action(event);
|
|
|
|
|
} else if (link) {
|
|
|
|
|
history.push(link);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
2018-09-03 16:17:36 +02:00
|
|
|
const {
|
|
|
|
|
label,
|
|
|
|
|
loading,
|
|
|
|
|
disabled,
|
|
|
|
|
type,
|
|
|
|
|
color,
|
2019-09-23 14:12:02 +02:00
|
|
|
className,
|
|
|
|
|
icon,
|
2018-09-03 16:17:36 +02:00
|
|
|
fullWidth,
|
2019-09-23 14:12:02 +02:00
|
|
|
reducedMobile,
|
2019-10-19 16:38:07 +02:00
|
|
|
children,
|
2018-09-03 16:17:36 +02:00
|
|
|
} = this.props;
|
2019-10-19 16:38:07 +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}
|
|
|
|
|
disabled={disabled}
|
|
|
|
|
onClick={this.onClick}
|
|
|
|
|
className={classNames(
|
2019-10-19 16:38:07 +02:00
|
|
|
'button',
|
|
|
|
|
'is-' + color,
|
2019-09-23 14:12:02 +02:00
|
|
|
loadingClass,
|
|
|
|
|
fullWidthClass,
|
|
|
|
|
reducedMobileClass,
|
2019-10-19 16:38:07 +02:00
|
|
|
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}
|
|
|
|
|
disabled={disabled}
|
2018-10-11 17:29:50 +02:00
|
|
|
onClick={this.onClick}
|
2018-09-03 16:17:36 +02:00
|
|
|
className={classNames(
|
2019-10-19 16:38:07 +02:00
|
|
|
'button',
|
|
|
|
|
'is-' + color,
|
2018-09-03 16:17:36 +02:00
|
|
|
loadingClass,
|
|
|
|
|
fullWidthClass,
|
2019-10-19 16:38:07 +02:00
|
|
|
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);
|