import PropTypes from 'prop-types'; import React from 'react'; const getClassName = (className, icon) => { const hasIcon = icon !== ''; let iconName = ''; if (hasIcon) { if (typeof icon === 'string') { iconName = ` icon-${icon}`; } else { iconName = icon.map(val => ` icon-${val}`).join(''); } } return `${className} ${hasIcon ? 'icon' : ''}${iconName}`; }; const handleClick = (href, onClick, preventDefault, e) => { if (preventDefault && href === '#') { e.preventDefault(); e.stopPropagation(); } if (onClick) { onClick(e); } }; /** * A reusable button. Uses a tag underneath. */ const Button = ({ className, icon, children, accessibleLabel, isLoading, href, target, preventDefault, onClick, }) => { const hasText = children !== null; const iconName = isLoading ? 'spinner' : icon; const accessibleElt = accessibleLabel ? ( {accessibleLabel} ) : null; return ( {hasText ? children : accessibleElt} ); }; Button.propTypes = { href: PropTypes.string, className: PropTypes.string, icon: PropTypes.oneOfType([ PropTypes.string, PropTypes.arrayOf(PropTypes.string), ]), target: PropTypes.string, children: PropTypes.node, accessibleLabel: PropTypes.string, onClick: PropTypes.func, isLoading: PropTypes.bool, preventDefault: PropTypes.bool, }; Button.defaultProps = { href: '#', className: '', icon: '', target: null, children: null, accessibleLabel: null, onClick: null, isLoading: false, preventDefault: true, }; export default Button;