2019-10-29 16:39:44 +08:00
|
|
|
import React, { AnchorHTMLAttributes, ButtonHTMLAttributes, useContext } from 'react';
|
2019-03-27 18:50:36 +08:00
|
|
|
import { ThemeContext } from '../../themes';
|
2019-10-29 16:39:44 +08:00
|
|
|
import { getButtonStyles } from './styles';
|
|
|
|
import { ButtonContent } from './ButtonContent';
|
2020-03-25 20:10:37 +08:00
|
|
|
import { ComponentSize } from '../../types/size';
|
|
|
|
import { ButtonStyles, ButtonVariant } from './types';
|
2020-03-03 15:22:26 +08:00
|
|
|
import { cx } from 'emotion';
|
2019-03-27 18:50:36 +08:00
|
|
|
|
2019-10-29 16:39:44 +08:00
|
|
|
type CommonProps = {
|
2020-03-25 20:10:37 +08:00
|
|
|
size?: ComponentSize;
|
2019-10-29 16:39:44 +08:00
|
|
|
variant?: ButtonVariant;
|
|
|
|
/**
|
|
|
|
* icon prop is a temporary solution. It accepts legacy icon class names for the icon to be rendered.
|
|
|
|
* TODO: migrate to a component when we are going to migrate icons to @grafana/ui
|
|
|
|
*/
|
|
|
|
icon?: string;
|
|
|
|
className?: string;
|
|
|
|
styles?: ButtonStyles;
|
|
|
|
};
|
|
|
|
|
2020-03-03 20:04:28 +08:00
|
|
|
export type ButtonProps = CommonProps & ButtonHTMLAttributes<HTMLButtonElement>;
|
2020-01-07 16:20:06 +08:00
|
|
|
export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>((props, ref) => {
|
2019-03-27 18:50:36 +08:00
|
|
|
const theme = useContext(ThemeContext);
|
2019-10-29 16:39:44 +08:00
|
|
|
const { size, variant, icon, children, className, styles: stylesProp, ...buttonProps } = props;
|
|
|
|
|
|
|
|
// Default this to 'button', otherwise html defaults to 'submit' which then submits any form it is in.
|
|
|
|
buttonProps.type = buttonProps.type || 'button';
|
2020-01-07 16:20:06 +08:00
|
|
|
|
2019-11-01 17:25:44 +08:00
|
|
|
const styles: ButtonStyles =
|
|
|
|
stylesProp ||
|
|
|
|
getButtonStyles({
|
|
|
|
theme,
|
|
|
|
size: size || 'md',
|
|
|
|
variant: variant || 'primary',
|
2020-01-13 02:55:10 +08:00
|
|
|
textAndIcon: !!(children && icon),
|
2019-11-01 17:25:44 +08:00
|
|
|
});
|
2019-10-29 16:39:44 +08:00
|
|
|
|
|
|
|
return (
|
2020-03-03 15:22:26 +08:00
|
|
|
<button className={cx(styles.button, className)} {...buttonProps} ref={ref}>
|
2020-01-07 16:20:06 +08:00
|
|
|
<ButtonContent icon={icon}>{children}</ButtonContent>
|
2019-10-29 16:39:44 +08:00
|
|
|
</button>
|
|
|
|
);
|
2020-01-07 16:20:06 +08:00
|
|
|
});
|
|
|
|
|
2019-03-27 18:50:36 +08:00
|
|
|
Button.displayName = 'Button';
|
|
|
|
|
2020-03-03 20:04:28 +08:00
|
|
|
export type LinkButtonProps = CommonProps &
|
2019-10-29 16:39:44 +08:00
|
|
|
AnchorHTMLAttributes<HTMLAnchorElement> & {
|
|
|
|
// We allow disabled here even though it is not standard for a link. We use it as a selector to style it as
|
|
|
|
// disabled.
|
|
|
|
disabled?: boolean;
|
|
|
|
};
|
2020-01-07 16:20:06 +08:00
|
|
|
|
|
|
|
export const LinkButton = React.forwardRef<HTMLAnchorElement, LinkButtonProps>((props, ref) => {
|
2019-03-27 18:50:36 +08:00
|
|
|
const theme = useContext(ThemeContext);
|
2019-10-29 16:39:44 +08:00
|
|
|
const { size, variant, icon, children, className, styles: stylesProp, ...anchorProps } = props;
|
2019-11-01 17:25:44 +08:00
|
|
|
const styles: ButtonStyles =
|
|
|
|
stylesProp ||
|
|
|
|
getButtonStyles({
|
|
|
|
theme,
|
|
|
|
size: size || 'md',
|
|
|
|
variant: variant || 'primary',
|
2020-01-13 02:55:10 +08:00
|
|
|
textAndIcon: !!(children && icon),
|
2019-11-01 17:25:44 +08:00
|
|
|
});
|
2019-10-29 16:39:44 +08:00
|
|
|
|
|
|
|
return (
|
2020-03-03 15:22:26 +08:00
|
|
|
<a className={cx(styles.button, className)} {...anchorProps} ref={ref}>
|
2020-01-07 16:20:06 +08:00
|
|
|
<ButtonContent icon={icon}>{children}</ButtonContent>
|
2019-10-29 16:39:44 +08:00
|
|
|
</a>
|
|
|
|
);
|
2020-01-07 16:20:06 +08:00
|
|
|
});
|
2019-03-27 18:50:36 +08:00
|
|
|
LinkButton.displayName = 'LinkButton';
|