react-toolbox/components/button/Button.jsx

86 lines
2.6 KiB
React
Raw Normal View History

import React from 'react';
import FontIcon from '../font_icon';
import Ripple from '../ripple';
2015-11-04 18:28:11 +03:00
import Tooltip from '../tooltip';
2015-10-23 02:26:12 +03:00
import style from './style';
2015-10-06 23:08:37 +03:00
import events from '../utils/events';
2015-10-22 02:31:17 +03:00
class Button extends React.Component {
static propTypes = {
2015-10-21 01:48:24 +03:00
accent: React.PropTypes.bool,
children: React.PropTypes.node,
className: React.PropTypes.string,
disabled: React.PropTypes.bool,
flat: React.PropTypes.bool,
floating: React.PropTypes.bool,
href: React.PropTypes.string,
icon: React.PropTypes.string,
label: React.PropTypes.string,
loading: React.PropTypes.bool,
2015-10-21 01:48:24 +03:00
mini: React.PropTypes.bool,
primary: React.PropTypes.bool,
raised: React.PropTypes.bool,
ripple: React.PropTypes.bool,
toggle: React.PropTypes.bool,
2015-11-04 18:28:11 +03:00
tooltip: React.PropTypes.string,
2015-11-21 07:27:33 +03:00
tooltipDelay: React.PropTypes.number,
type: React.PropTypes.string
};
static defaultProps = {
2015-10-21 05:58:11 +03:00
accent: false,
className: '',
flat: false,
floating: false,
2015-10-21 05:58:11 +03:00
loading: false,
mini: false,
primary: false,
raised: false,
ripple: true,
toggle: false
};
2015-10-22 02:31:17 +03:00
handleMouseDown = (event) => {
2015-10-06 23:08:37 +03:00
events.pauseEvent(event);
if (this.refs.ripple) this.refs.ripple.start(event);
if (this.props.onMouseDown) this.props.onMouseDown(event);
2015-10-22 11:02:36 +03:00
};
2015-11-19 22:13:47 +03:00
handleTouchStart = (event) => {
events.pauseEvent(event);
if (this.refs.ripple) this.refs.ripple.start(event.touches[0], true);
if (this.props.onTouchStart) this.props.onTouchStart(event);
};
render () {
const {accent, flat, floating, href, icon, label,
loading, mini, primary, raised, ripple, toggle,
tooltip, tooltipDelay, ...others} = this.props; //eslint-disable-line no-redeclare
const element = href ? 'a' : 'button';
const level = primary ? 'primary' : accent ? 'accent' : 'neutral';
const shape = flat ? 'flat' : raised ? 'raised' : floating ? 'floating' : toggle ? 'toggle' : 'flat';
let className = `${style[shape]} ${style[level]}`;
2015-10-04 16:12:53 +03:00
if (this.props.className) className += ` ${this.props.className}`;
if (mini) className += ` ${style.mini}`;
2015-10-04 16:12:53 +03:00
const props = {
...others,
href,
className,
disabled: this.props.disabled || this.props.loading,
2015-11-19 22:13:47 +03:00
onMouseDown: this.handleMouseDown,
onTouchStart: this.handleTouchStart
};
return React.createElement(element, props,
ripple ? <Ripple ref='ripple' loading={loading}/> : null,
2015-11-21 07:27:33 +03:00
tooltip ? <Tooltip className={style.tooltip} delay={tooltipDelay} label={tooltip}/> : null,
icon ? <FontIcon className={style.icon} value={icon}/> : null,
label ? label : this.props.children
);
}
2015-10-21 05:58:11 +03:00
}
2015-10-22 02:31:17 +03:00
export default Button;