import React, { Component } from 'react'; import { Link } from 'gatsby'; import { css } from '@emotion/core'; import { SerializedStyles } from '@emotion/serialize'; import * as Styles from './button.styles'; import * as Constants from './button.constants'; import { ButtonPriorityOption, ButtonSizeOption } from './button.types'; import LoadingSpinner from 'components/loading-spinner'; import { colors } from 'utils/theme'; export interface ButtonProps { children: React.ReactNode; isInternal?: boolean; isDisabled?: boolean; isLoading: boolean; size: ButtonSizeOption; priority?: ButtonPriorityOption; fullwidth: boolean; url?: string; customCSS?: SerializedStyles; // @TODO: refactor so we don't need this onClick?: (event: React.MouseEvent) => void; } class Button extends Component { static defaultProps = { isInternal: false, isDisabled: false, fullwidth: false, size: 'normal', priority: 'primary', url: null, isLoading: false }; onClick = (event: React.MouseEvent): void => { if (this.props.isDisabled) { // Should only reach here if is gatsby link // As the `to` prop is required // So cannot programatically remove event.preventDefault(); } else if (this.props.onClick) { this.props.onClick(event); } }; renderButtonContent = () => { return (
{this.props.children}
{ this.props.isLoading && }
) } getButtonPriorityCss = (): SerializedStyles => this.props.priority === 'primary' ? Styles.ButtonPrimary : Styles.ButtonSecondary; render(): JSX.Element { const {isDisabled, customCSS, size, fullwidth} = this.props; const buttonSize = Constants.SIZES[size]; const buttonWidth = {width: fullwidth ? '100%' : 'auto'}; if (this.props.isInternal && this.props.url) { return ( {this.renderButtonContent()} ); } else if (this.props.onClick) { return ( ); } return ( {this.renderButtonContent()} ); } } export default Button;