fleet/frontend/components/buttons/Button/Button.tsx
Martavis Parker bcfac603f0
Added components to Storybook library (#2768)
* added storybook

* added avatar component

* added button story

* added dropdown button story

* removed unused ellipsis component

* cleaned up modal path

* reorganized enroll secrets table file

* added flash story; removed unused persistent flash

* added fleet ace story

* added checkbox story

* added dropdown story

* added input story

* fixed storybook build

* fixed avatar

* added input with icon story

* added radio button story

* added select targets dropdown story

* added slider story

* added tooltip story

* added info banner story

* removed unused loaders; added spinner story

* added modal story

* removed unused NumberPill

* added pagination story

* lint fixes

* added documentation to run

* modified documentation

* fixed corelayout test

* fixed format for date-fns

* fixed date format that breaks tests

* wait for page
2021-11-06 23:41:09 -07:00

125 lines
2.3 KiB
TypeScript

import React from "react";
import classnames from "classnames";
const baseClass = "button";
export type ButtonVariant =
| "brand"
| "success"
| "alert"
| "blue-green"
| "grey"
| "warning"
| "link"
| "label"
| "text-link"
| "text-icon"
| "inverse"
| "inverse-alert"
| "block"
| "unstyled"
| "unstyled-modal-query"
| "contextual-nav-item"
| "small-text-icon";
export interface IButtonProps {
autofocus?: boolean;
children: React.ReactChild;
className?: string;
disabled?: boolean;
size?: string;
tabIndex?: number;
type?: "button" | "submit" | "reset";
title?: string;
variant?: ButtonVariant;
onClick?:
| ((value?: any) => void)
| ((evt: React.MouseEvent<HTMLButtonElement>) => void);
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface IButtonState {}
interface Inputs {
button?: HTMLButtonElement;
}
class Button extends React.Component<IButtonProps, IButtonState> {
static defaultProps = {
size: "",
type: "button",
variant: "default",
};
componentDidMount(): void {
const { autofocus } = this.props;
const {
inputs: { button },
} = this;
if (autofocus && button) {
button.focus();
}
}
setRef = (button: HTMLButtonElement): boolean => {
this.inputs.button = button;
return false;
};
inputs: Inputs = {};
handleClick = (evt: React.MouseEvent<HTMLButtonElement>): boolean => {
const { disabled, onClick } = this.props;
if (disabled) {
return false;
}
if (onClick) {
onClick(evt);
}
return false;
};
render(): JSX.Element {
const { handleClick, setRef } = this;
const {
children,
className,
disabled,
size,
tabIndex,
type,
title,
variant,
} = this.props;
const fullClassName = classnames(
baseClass,
`${baseClass}--${variant}`,
className,
{
[`${baseClass}--disabled`]: disabled,
[`${baseClass}--${size}`]: size !== undefined,
}
);
return (
<button
className={fullClassName}
disabled={disabled}
onClick={handleClick}
tabIndex={tabIndex}
type={type}
title={title}
ref={setRef}
>
{children}
</button>
);
}
}
export default Button;