import * as React from "react"; import { ClassHelpers } from "../../utilities/classHelpers"; import { Icon } from "./../display/icon"; export interface IButtonProps extends React.ButtonHTMLAttributes { /** (string) An icon to show on the left of the buttons content */ leftIcon?: string; /** (string) An icon to show on the right of the buttons content */ rightIcon?: string; /** (boolean) Wether or not the button should have rounded edges */ rounded?: boolean; /** (boolean) If true, disables actions and puts button into a 'pending' state */ pending?: boolean; } export interface IButton { focus: () => void, blur: () => void } const ButtonRef: React.RefForwardingComponent = (props, ref) => { const { onClick, leftIcon, rightIcon, className, rounded, pending, disabled, type, children, ...attrs } = props const buttonRef = React.useRef() React.useImperativeHandle(ref, () => ({ focus: () => { if (buttonRef.current) { buttonRef.current.focus() } }, blur: () => { if (buttonRef.current) { buttonRef.current.blur() } }, }), [buttonRef.current]) const handleClick = React.useCallback(e => { if (onClick && !pending) { onClick(e); } }, [onClick, pending]) const classes = ClassHelpers.classNames( "btn", className, { "rounded": rounded, "icon-button-left": leftIcon !== undefined, "icon-button-right": rightIcon !== undefined, "pending": pending, }, ); return ( ); } export const Button = React.forwardRef(ButtonRef)