2020-06-17 09:52:03 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { Button as WPButton } from 'wordpress-components';
|
2021-04-26 09:49:57 +00:00
|
|
|
import type { ReactNode } from 'react';
|
2020-06-17 09:52:03 +00:00
|
|
|
import classNames from 'classnames';
|
2021-06-16 12:44:40 +00:00
|
|
|
import Spinner from '@woocommerce/base-components/spinner';
|
|
|
|
|
2020-06-17 09:52:03 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
|
|
|
|
2022-02-03 12:29:06 +00:00
|
|
|
export interface ButtonProps extends WPButton.ButtonProps {
|
|
|
|
/**
|
|
|
|
* Component wrapper classname
|
|
|
|
*
|
|
|
|
* @default 'wc-block-components-button'
|
|
|
|
*/
|
2021-04-26 09:49:57 +00:00
|
|
|
className?: string;
|
2022-02-03 12:29:06 +00:00
|
|
|
/**
|
|
|
|
* Show spinner
|
|
|
|
*
|
|
|
|
* @default false
|
|
|
|
*/
|
2021-04-26 09:49:57 +00:00
|
|
|
showSpinner?: boolean;
|
2022-02-03 12:29:06 +00:00
|
|
|
/**
|
|
|
|
* Button content
|
|
|
|
*/
|
2021-04-26 09:49:57 +00:00
|
|
|
children?: ReactNode;
|
2022-02-03 12:29:06 +00:00
|
|
|
/**
|
|
|
|
* Button state
|
|
|
|
*/
|
2021-12-13 16:44:28 +00:00
|
|
|
disabled?: boolean;
|
2022-02-03 12:29:06 +00:00
|
|
|
/**
|
|
|
|
* Event handler triggered when the button is clicked
|
|
|
|
*/
|
2021-12-13 16:44:28 +00:00
|
|
|
onClick?: ( e: React.MouseEvent< HTMLButtonElement, MouseEvent > ) => void;
|
2022-02-03 12:29:06 +00:00
|
|
|
/**
|
|
|
|
* Button type
|
|
|
|
*/
|
|
|
|
type?: 'button' | 'input' | 'submit';
|
2022-02-09 17:32:05 +00:00
|
|
|
/**
|
|
|
|
* Button variant
|
|
|
|
*/
|
|
|
|
variant?: 'text' | 'contained' | 'outlined';
|
2021-04-26 09:49:57 +00:00
|
|
|
}
|
2020-09-20 23:54:08 +00:00
|
|
|
|
2020-06-17 09:52:03 +00:00
|
|
|
/**
|
2020-09-20 23:54:08 +00:00
|
|
|
* Component that visually renders a button but semantically might be `<button>` or `<a>` depending
|
|
|
|
* on the props.
|
2020-06-17 09:52:03 +00:00
|
|
|
*/
|
2021-04-26 09:49:57 +00:00
|
|
|
const Button = ( {
|
|
|
|
className,
|
|
|
|
showSpinner = false,
|
|
|
|
children,
|
2022-02-09 17:32:05 +00:00
|
|
|
variant = 'contained',
|
2021-04-26 09:49:57 +00:00
|
|
|
...props
|
|
|
|
}: ButtonProps ): JSX.Element => {
|
2020-06-17 09:52:03 +00:00
|
|
|
const buttonClassName = classNames(
|
|
|
|
'wc-block-components-button',
|
|
|
|
className,
|
2022-02-09 17:32:05 +00:00
|
|
|
variant,
|
2020-06-17 09:52:03 +00:00
|
|
|
{
|
|
|
|
'wc-block-components-button--loading': showSpinner,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<WPButton className={ buttonClassName } { ...props }>
|
2021-06-16 12:44:40 +00:00
|
|
|
{ showSpinner && <Spinner /> }
|
2020-06-17 09:52:03 +00:00
|
|
|
<span className="wc-block-components-button__text">
|
|
|
|
{ children }
|
|
|
|
</span>
|
|
|
|
</WPButton>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Button;
|