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';
|
|
|
|
import cx from 'classnames';
|
|
|
|
import { ButtonSize, ButtonStyles, ButtonVariant } from './types';
|
2019-03-27 18:50:36 +08:00
|
|
|
|
2019-10-29 16:39:44 +08:00
|
|
|
type CommonProps = {
|
|
|
|
size?: ButtonSize;
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
type ButtonProps = CommonProps & ButtonHTMLAttributes<HTMLButtonElement>;
|
2019-03-27 18:50:36 +08:00
|
|
|
export const Button: React.FunctionComponent<ButtonProps> = props => {
|
|
|
|
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';
|
|
|
|
const styles =
|
|
|
|
stylesProp || getButtonStyles({ theme, size: size || 'md', variant: variant || 'primary', withIcon: !!icon });
|
|
|
|
|
|
|
|
return (
|
|
|
|
<button className={cx(styles.button, className)} {...buttonProps}>
|
|
|
|
<ButtonContent iconClassName={styles.icon} className={styles.iconWrap} icon={icon}>
|
|
|
|
{children}
|
|
|
|
</ButtonContent>
|
|
|
|
</button>
|
|
|
|
);
|
2019-03-27 18:50:36 +08:00
|
|
|
};
|
|
|
|
Button.displayName = 'Button';
|
|
|
|
|
2019-10-29 16:39:44 +08:00
|
|
|
type LinkButtonProps = CommonProps &
|
|
|
|
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;
|
|
|
|
};
|
2019-03-27 18:50:36 +08:00
|
|
|
export const LinkButton: React.FunctionComponent<LinkButtonProps> = props => {
|
|
|
|
const theme = useContext(ThemeContext);
|
2019-10-29 16:39:44 +08:00
|
|
|
const { size, variant, icon, children, className, styles: stylesProp, ...anchorProps } = props;
|
|
|
|
const styles =
|
|
|
|
stylesProp || getButtonStyles({ theme, size: size || 'md', variant: variant || 'primary', withIcon: !!icon });
|
|
|
|
|
|
|
|
return (
|
|
|
|
<a className={cx(styles.button, className)} {...anchorProps}>
|
|
|
|
<ButtonContent iconClassName={styles.icon} className={styles.iconWrap} icon={icon}>
|
|
|
|
{children}
|
|
|
|
</ButtonContent>
|
|
|
|
</a>
|
|
|
|
);
|
2019-03-27 18:50:36 +08:00
|
|
|
};
|
|
|
|
LinkButton.displayName = 'LinkButton';
|