2017-06-04 10:40:14 +08:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2016-05-20 21:37:19 +08:00
|
|
|
import cx from 'classnames';
|
2017-12-05 03:43:08 +08:00
|
|
|
import Tooltip from '/imports/ui/components/tooltip/component';
|
2017-08-21 08:16:39 +08:00
|
|
|
import styles from './styles';
|
2016-05-20 21:37:19 +08:00
|
|
|
import Icon from '../icon/component';
|
2017-08-21 08:16:39 +08:00
|
|
|
import BaseButton from './base/component';
|
2016-05-20 21:37:19 +08:00
|
|
|
|
|
|
|
const SIZES = [
|
2016-10-12 03:04:50 +08:00
|
|
|
'jumbo', 'lg', 'md', 'sm',
|
2016-05-20 21:37:19 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
const COLORS = [
|
|
|
|
'default', 'primary', 'danger', 'success',
|
|
|
|
];
|
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
...BaseButton.propTypes,
|
|
|
|
/**
|
|
|
|
* Defines the button size style
|
|
|
|
* @type {("lg"|"md"|"sm")}
|
|
|
|
* @defaultValue 'md'
|
|
|
|
*/
|
|
|
|
size: PropTypes.oneOf(SIZES),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines the button color style
|
|
|
|
* @type {("default"|"primary"|"danger"|"success")}
|
|
|
|
* @defaultValue 'md'
|
|
|
|
*/
|
|
|
|
color: PropTypes.oneOf(COLORS),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines if the button should be styled as a ghost (outline)
|
|
|
|
* @defaultValue false
|
|
|
|
*/
|
|
|
|
ghost: PropTypes.bool,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines if the button should be styled as circle
|
|
|
|
* @defaultValue false
|
|
|
|
*/
|
|
|
|
circle: PropTypes.bool,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines if the button should have `display: block`
|
|
|
|
* @defaultValue false
|
|
|
|
*/
|
|
|
|
block: PropTypes.bool,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines the button icon
|
|
|
|
* @defaultValue undefined
|
|
|
|
*/
|
|
|
|
icon: PropTypes.string,
|
|
|
|
|
2016-05-20 22:49:28 +08:00
|
|
|
/**
|
|
|
|
* Defines the button icon is on the right side
|
|
|
|
* @defaultValue false
|
|
|
|
*/
|
|
|
|
iconRight: PropTypes.bool,
|
|
|
|
|
2016-05-20 21:37:19 +08:00
|
|
|
/**
|
2016-06-18 00:42:17 +08:00
|
|
|
* Defines the button label should be visible
|
|
|
|
* @defaultValue false
|
2016-05-20 21:37:19 +08:00
|
|
|
*/
|
2016-06-18 00:42:17 +08:00
|
|
|
hideLabel: PropTypes.bool,
|
2017-04-19 08:46:01 +08:00
|
|
|
|
|
|
|
/**
|
2017-06-03 07:41:28 +08:00
|
|
|
* Optional SVG / html object can be passed to the button as an icon
|
2017-04-19 08:46:01 +08:00
|
|
|
* Has to be styled before being sent to the Button
|
|
|
|
* (e.g width, height, position and percentage-based object's coordinates)
|
|
|
|
* @defaultvalue undefined
|
|
|
|
*/
|
2017-06-03 07:41:28 +08:00
|
|
|
customIcon: PropTypes.node,
|
2016-05-20 21:37:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
...BaseButton.defaultProps,
|
|
|
|
size: 'md',
|
|
|
|
color: 'default',
|
|
|
|
ghost: false,
|
|
|
|
circle: false,
|
|
|
|
block: false,
|
2016-05-20 22:49:28 +08:00
|
|
|
iconRight: false,
|
2016-06-18 00:42:17 +08:00
|
|
|
hideLabel: false,
|
2016-05-20 21:37:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default class Button extends BaseButton {
|
|
|
|
_getClassNames() {
|
|
|
|
const {
|
|
|
|
size,
|
|
|
|
color,
|
2016-08-06 02:39:24 +08:00
|
|
|
disabled,
|
2016-05-20 21:37:19 +08:00
|
|
|
ghost,
|
|
|
|
circle,
|
|
|
|
block,
|
2016-05-20 22:49:28 +08:00
|
|
|
iconRight,
|
2016-05-20 21:37:19 +08:00
|
|
|
} = this.props;
|
|
|
|
|
2017-08-21 08:16:39 +08:00
|
|
|
const propClassNames = {};
|
2017-02-02 00:16:02 +08:00
|
|
|
|
2016-05-20 21:37:19 +08:00
|
|
|
propClassNames[styles.button] = true;
|
|
|
|
propClassNames[styles[size]] = true;
|
|
|
|
propClassNames[styles[color]] = true;
|
|
|
|
propClassNames[styles.ghost] = ghost;
|
2017-02-02 03:40:00 +08:00
|
|
|
propClassNames[styles.circle] = circle;
|
2016-05-20 21:37:19 +08:00
|
|
|
propClassNames[styles.block] = block;
|
2016-05-20 22:49:28 +08:00
|
|
|
propClassNames[styles.iconRight] = iconRight;
|
2016-08-06 02:39:24 +08:00
|
|
|
propClassNames[styles.disabled] = disabled;
|
2016-05-20 21:37:19 +08:00
|
|
|
|
|
|
|
return propClassNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-12-05 03:43:08 +08:00
|
|
|
const {
|
|
|
|
circle,
|
|
|
|
hideLabel,
|
|
|
|
label,
|
2017-12-08 00:11:34 +08:00
|
|
|
'aria-label' : ariaLabel
|
2017-12-05 03:43:08 +08:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const renderFuncName = circle ? 'renderCircle' : 'renderDefault';
|
|
|
|
|
|
|
|
if (hideLabel) {
|
2017-12-08 00:11:34 +08:00
|
|
|
const tooltipLabel = label ? label : ariaLabel;
|
|
|
|
|
2017-12-05 03:43:08 +08:00
|
|
|
return (
|
|
|
|
<Tooltip
|
2017-12-08 00:11:34 +08:00
|
|
|
title={tooltipLabel}
|
2017-12-05 03:43:08 +08:00
|
|
|
>
|
|
|
|
{this[renderFuncName]()}
|
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
}
|
2016-05-20 21:37:19 +08:00
|
|
|
|
|
|
|
return this[renderFuncName]();
|
|
|
|
}
|
|
|
|
|
|
|
|
renderDefault() {
|
|
|
|
const {
|
|
|
|
className,
|
2016-05-20 22:49:28 +08:00
|
|
|
iconRight,
|
2017-08-21 08:16:39 +08:00
|
|
|
...otherProps
|
2016-05-20 21:37:19 +08:00
|
|
|
} = this.props;
|
|
|
|
|
2016-07-12 04:10:55 +08:00
|
|
|
const remainingProps = Object.assign({}, otherProps);
|
|
|
|
delete remainingProps.icon;
|
2017-06-03 07:41:28 +08:00
|
|
|
delete remainingProps.customIcon;
|
2016-07-12 04:10:55 +08:00
|
|
|
delete remainingProps.size;
|
|
|
|
delete remainingProps.color;
|
|
|
|
delete remainingProps.ghost;
|
|
|
|
delete remainingProps.circle;
|
|
|
|
delete remainingProps.block;
|
|
|
|
delete remainingProps.hideLabel;
|
|
|
|
|
2016-05-21 00:17:39 +08:00
|
|
|
/* TODO: We can change this and make the button with flexbox to avoid html
|
|
|
|
changes */
|
2016-05-20 22:49:28 +08:00
|
|
|
const renderLeftFuncName = !iconRight ? 'renderIcon' : 'renderLabel';
|
|
|
|
const renderRightFuncName = !iconRight ? 'renderLabel' : 'renderIcon';
|
|
|
|
|
2016-05-20 21:37:19 +08:00
|
|
|
return (
|
2016-06-18 00:42:17 +08:00
|
|
|
<BaseButton
|
2016-05-20 21:37:19 +08:00
|
|
|
className={cx(this._getClassNames(), className)}
|
2016-07-12 04:10:55 +08:00
|
|
|
{...remainingProps}
|
|
|
|
>
|
2016-05-20 22:49:28 +08:00
|
|
|
{this[renderLeftFuncName]()}
|
|
|
|
{this[renderRightFuncName]()}
|
2016-06-18 00:42:17 +08:00
|
|
|
</BaseButton>
|
2016-05-20 21:37:19 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderCircle() {
|
|
|
|
const {
|
|
|
|
className,
|
|
|
|
size,
|
2016-05-27 08:13:53 +08:00
|
|
|
iconRight,
|
2017-08-21 08:16:39 +08:00
|
|
|
...otherProps
|
2016-05-20 21:37:19 +08:00
|
|
|
} = this.props;
|
|
|
|
|
2016-07-12 04:10:55 +08:00
|
|
|
const remainingProps = Object.assign({}, otherProps);
|
|
|
|
delete remainingProps.icon;
|
|
|
|
delete remainingProps.color;
|
|
|
|
delete remainingProps.ghost;
|
|
|
|
delete remainingProps.circle;
|
|
|
|
delete remainingProps.block;
|
|
|
|
delete remainingProps.hideLabel;
|
|
|
|
|
2016-05-20 21:37:19 +08:00
|
|
|
return (
|
2016-06-18 00:42:17 +08:00
|
|
|
<BaseButton
|
|
|
|
className={cx(styles[size], styles.buttonWrapper, className)}
|
2016-07-12 04:10:55 +08:00
|
|
|
{...remainingProps}
|
|
|
|
>
|
2016-06-18 00:42:17 +08:00
|
|
|
{!iconRight ? null : this.renderLabel()}
|
|
|
|
<span className={cx(this._getClassNames())}>
|
2016-05-20 21:37:19 +08:00
|
|
|
{this.renderIcon()}
|
2016-06-18 00:42:17 +08:00
|
|
|
</span>
|
|
|
|
{iconRight ? null : this.renderLabel()}
|
|
|
|
</BaseButton>
|
2016-05-20 21:37:19 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderIcon() {
|
|
|
|
const iconName = this.props.icon;
|
2017-06-03 07:41:28 +08:00
|
|
|
const customIcon = this.props.customIcon;
|
2016-05-20 21:37:19 +08:00
|
|
|
|
|
|
|
if (iconName) {
|
|
|
|
return (<Icon className={styles.icon} iconName={iconName} />);
|
2017-08-21 08:16:39 +08:00
|
|
|
} else if (customIcon) {
|
2017-06-03 07:41:28 +08:00
|
|
|
return customIcon;
|
2016-05-20 21:37:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-06-18 00:42:17 +08:00
|
|
|
renderLabel() {
|
|
|
|
const { label, hideLabel } = this.props;
|
2016-05-20 21:37:19 +08:00
|
|
|
|
2017-08-21 08:16:39 +08:00
|
|
|
const classNames = {};
|
2016-06-18 00:42:17 +08:00
|
|
|
|
|
|
|
classNames[styles.label] = true;
|
|
|
|
classNames[styles.hideLabel] = hideLabel;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<span className={cx(classNames)}>
|
|
|
|
{label}
|
|
|
|
{this.props.children}
|
|
|
|
</span>
|
|
|
|
);
|
2016-05-20 21:37:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Button.propTypes = propTypes;
|
|
|
|
Button.defaultProps = defaultProps;
|