1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-02 22:52:21 +01:00

50 lines
1.3 KiB
TypeScript
Raw Normal View History

import { ReactNode, BaseSyntheticEvent } from 'react';
import * as React from 'react';
interface FabButtonProps {
onClick?: (event: BaseSyntheticEvent) => void,
icon?: ReactNode,
className?: string,
2021-02-08 15:28:47 +01:00
disabled?: boolean,
2021-02-09 12:09:26 +01:00
type?: 'submit' | 'reset' | 'button',
form?: string,
tooltip?: string,
}
/**
* This component is a template for a clickable button that wraps the application style
*/
export const FabButton: React.FC<FabButtonProps> = ({ onClick, icon, className, disabled, type, form, tooltip, children }) => {
/**
* Check if the current component was provided an icon to display
*/
const hasIcon = (): boolean => {
return !!icon;
2021-07-01 12:34:10 +02:00
};
/**
* Check if the current button has children properties (like some text)
*/
const hasChildren = (): boolean => {
return !!children;
2021-07-01 12:34:10 +02:00
};
/**
* Handle the action of the button
*/
const handleClick = (e: BaseSyntheticEvent): void => {
if (typeof onClick === 'function') {
onClick(e);
}
2021-07-01 12:34:10 +02:00
};
return (
<button type={type} form={form} onClick={handleClick} disabled={disabled} className={`fab-button ${className || ''}`} title={tooltip}>
{hasIcon() && <span className={hasChildren() ? 'fab-button--icon' : 'fab-button--icon-only'}>{icon}</span>}
{children}
</button>
);
2021-07-01 12:34:10 +02:00
};
2021-02-09 12:09:26 +01:00
FabButton.defaultProps = { type: 'button' };