2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
2021-11-16 11:35:58 +01:00
|
|
|
import React, { FC, MouseEvent, ReactNode, KeyboardEvent } from "react";
|
2019-10-20 16:59:02 +02:00
|
|
|
import classNames from "classnames";
|
2021-11-04 09:16:08 +01:00
|
|
|
import { Link } from "react-router-dom";
|
2019-10-20 16:59:02 +02:00
|
|
|
import Icon from "../Icon";
|
2020-08-11 10:34:29 +02:00
|
|
|
import { createAttributesForTesting } from "../devBuild";
|
2018-09-03 16:17:36 +02:00
|
|
|
|
|
|
|
|
export type ButtonProps = {
|
2019-10-19 16:38:07 +02:00
|
|
|
label?: string;
|
2021-04-21 14:36:52 +02:00
|
|
|
title?: string;
|
2019-10-19 16:38:07 +02:00
|
|
|
loading?: boolean;
|
|
|
|
|
disabled?: boolean;
|
2021-11-16 11:35:58 +01:00
|
|
|
action?: (event: MouseEvent | KeyboardEvent) => 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;
|
2020-08-11 10:34:29 +02:00
|
|
|
testId?: string;
|
2018-09-03 16:17:36 +02:00
|
|
|
};
|
|
|
|
|
|
2021-11-04 09:16:08 +01:00
|
|
|
type Props = ButtonProps & {
|
|
|
|
|
type?: "button" | "submit" | "reset";
|
|
|
|
|
color?: string;
|
|
|
|
|
};
|
2018-09-03 16:17:36 +02:00
|
|
|
|
2021-11-04 09:16:08 +01:00
|
|
|
const Button: FC<Props> = ({
|
|
|
|
|
link,
|
|
|
|
|
className,
|
|
|
|
|
icon,
|
|
|
|
|
fullWidth,
|
|
|
|
|
reducedMobile,
|
|
|
|
|
testId,
|
|
|
|
|
children,
|
|
|
|
|
label,
|
|
|
|
|
type = "button",
|
|
|
|
|
title,
|
|
|
|
|
loading,
|
|
|
|
|
disabled,
|
|
|
|
|
action,
|
2021-12-13 15:15:57 +01:00
|
|
|
color = "default"
|
2021-11-04 09:16:08 +01:00
|
|
|
}) => {
|
|
|
|
|
const renderIcon = () => {
|
|
|
|
|
return <>{icon ? <Icon name={icon} color="inherit" className="is-medium pr-1" /> : null}</>;
|
2018-10-11 17:29:50 +02:00
|
|
|
};
|
|
|
|
|
|
2021-12-13 15:15:57 +01:00
|
|
|
const classes = classNames(
|
|
|
|
|
"button",
|
|
|
|
|
"is-" + color,
|
|
|
|
|
{ "is-loading": loading },
|
|
|
|
|
{ "is-fullwidth": fullWidth },
|
|
|
|
|
{ "is-reduced-mobile": reducedMobile },
|
|
|
|
|
className
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const content = (
|
|
|
|
|
<>
|
|
|
|
|
{renderIcon()}{" "}
|
|
|
|
|
{(label || children) && (
|
|
|
|
|
<>
|
|
|
|
|
{label} {children}
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
|
2021-11-16 06:57:57 +01:00
|
|
|
if (link && !disabled) {
|
2021-12-13 15:15:57 +01:00
|
|
|
if (link.includes("://")) {
|
|
|
|
|
return (
|
|
|
|
|
<a className={classes} href={link} aria-label={label}>
|
|
|
|
|
{content}
|
|
|
|
|
</a>
|
|
|
|
|
);
|
|
|
|
|
}
|
2018-09-03 16:17:36 +02:00
|
|
|
return (
|
2021-12-13 15:15:57 +01:00
|
|
|
<Link className={classes} to={link} aria-label={label}>
|
|
|
|
|
{content}
|
2021-11-04 09:16:08 +01:00
|
|
|
</Link>
|
2018-09-03 16:17:36 +02:00
|
|
|
);
|
2019-09-23 14:12:02 +02:00
|
|
|
}
|
2018-09-03 16:17:36 +02:00
|
|
|
|
2021-11-04 09:16:08 +01:00
|
|
|
return (
|
|
|
|
|
<button
|
|
|
|
|
type={type}
|
|
|
|
|
title={title}
|
|
|
|
|
disabled={disabled}
|
2021-12-13 15:15:57 +01:00
|
|
|
onClick={event => action && action(event)}
|
|
|
|
|
className={classes}
|
2021-11-04 09:16:08 +01:00
|
|
|
{...createAttributesForTesting(testId)}
|
|
|
|
|
>
|
2021-12-13 15:15:57 +01:00
|
|
|
{content}
|
2021-11-04 09:16:08 +01:00
|
|
|
</button>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Button;
|