react-toolbox/components/button/Button.js

137 lines
3.6 KiB
JavaScript
Raw Normal View History

2016-05-28 16:37:10 +03:00
import React, { Component, PropTypes } from 'react';
2016-05-16 15:40:03 +03:00
import classnames from 'classnames';
import { themr } from 'react-css-themr';
2017-01-26 20:05:32 +03:00
import { BUTTON } from '../identifiers';
import InjectFontIcon from '../font_icon/FontIcon';
import rippleFactory from '../ripple/Ripple';
2016-05-28 16:37:10 +03:00
const factory = (ripple, FontIcon) => {
class Button extends Component {
static propTypes = {
accent: PropTypes.bool,
children: PropTypes.node,
className: PropTypes.string,
disabled: PropTypes.bool,
flat: PropTypes.bool,
floating: PropTypes.bool,
href: PropTypes.string,
icon: PropTypes.oneOfType([
PropTypes.string,
2017-01-26 20:05:32 +03:00
PropTypes.element,
2016-05-28 16:37:10 +03:00
]),
inverse: PropTypes.bool,
label: PropTypes.string,
mini: PropTypes.bool,
neutral: PropTypes.bool,
onMouseLeave: PropTypes.func,
onMouseUp: PropTypes.func,
primary: PropTypes.bool,
raised: PropTypes.bool,
theme: PropTypes.shape({
accent: PropTypes.string,
button: PropTypes.string,
flat: PropTypes.string,
floating: PropTypes.string,
icon: PropTypes.string,
inverse: PropTypes.string,
mini: PropTypes.string,
neutral: PropTypes.string,
primary: PropTypes.string,
raised: PropTypes.string,
rippleWrapper: PropTypes.string,
2017-01-26 20:05:32 +03:00
toggle: PropTypes.string,
2016-05-28 16:37:10 +03:00
}),
2017-01-26 20:05:32 +03:00
type: PropTypes.string,
2016-05-28 16:37:10 +03:00
};
2016-05-28 16:37:10 +03:00
static defaultProps = {
accent: false,
className: '',
flat: false,
floating: false,
mini: false,
neutral: true,
primary: false,
2016-11-22 18:44:47 +03:00
raised: false,
2017-01-26 20:05:32 +03:00
type: 'button',
2016-05-28 16:37:10 +03:00
};
2017-01-26 20:05:32 +03:00
getLevel = () => {
if (this.props.primary) return 'primary';
if (this.props.accent) return 'accent';
return 'neutral';
}
getShape = () => {
if (this.props.raised) return 'raised';
if (this.props.floating) return 'floating';
return 'flat';
}
2016-05-28 16:37:10 +03:00
handleMouseUp = (event) => {
2017-01-26 20:05:32 +03:00
this.buttonNode.blur();
2016-05-28 16:37:10 +03:00
if (this.props.onMouseUp) this.props.onMouseUp(event);
};
2015-12-11 21:20:58 +03:00
2016-05-28 16:37:10 +03:00
handleMouseLeave = (event) => {
2017-01-26 20:05:32 +03:00
this.buttonNode.blur();
2016-05-28 16:37:10 +03:00
if (this.props.onMouseLeave) this.props.onMouseLeave(event);
};
2015-11-29 14:39:55 +03:00
2017-01-26 20:05:32 +03:00
render() {
const {
accent, // eslint-disable-line
children,
className,
flat, // eslint-disable-line
floating, // eslint-disable-line
href,
icon,
inverse,
label,
mini,
neutral,
primary, // eslint-disable-line
raised, // eslint-disable-line
theme,
type,
...others
} = this.props;
2016-05-28 16:37:10 +03:00
const element = href ? 'a' : 'button';
2017-01-26 20:05:32 +03:00
const level = this.getLevel();
const shape = this.getShape();
2016-05-28 16:37:10 +03:00
const classes = classnames(theme.button, [theme[shape]], {
[theme[level]]: neutral,
[theme.mini]: mini,
2017-01-26 20:05:32 +03:00
[theme.inverse]: inverse,
2016-05-28 16:37:10 +03:00
}, className);
2015-10-04 16:12:53 +03:00
2016-05-28 16:37:10 +03:00
const props = {
...others,
href,
2017-01-26 20:05:32 +03:00
ref: (node) => { this.buttonNode = node; },
2016-05-28 16:37:10 +03:00
className: classes,
disabled: this.props.disabled,
onMouseUp: this.handleMouseUp,
onMouseLeave: this.handleMouseLeave,
2016-11-22 18:44:47 +03:00
type: !href ? type : null,
2017-01-26 20:05:32 +03:00
'data-react-toolbox': 'button',
2016-05-28 16:37:10 +03:00
};
2016-05-28 16:37:10 +03:00
return React.createElement(element, props,
2017-01-26 20:05:32 +03:00
icon ? <FontIcon className={theme.icon} value={icon} /> : null,
2016-05-28 16:37:10 +03:00
label,
2017-01-26 20:05:32 +03:00
children,
2016-05-28 16:37:10 +03:00
);
}
}
2015-10-22 02:31:17 +03:00
2016-05-28 16:37:10 +03:00
return ripple(Button);
};
2016-05-25 01:25:43 +03:00
2016-05-28 16:37:10 +03:00
const Button = factory(rippleFactory({ centered: false }), InjectFontIcon);
export default themr(BUTTON)(Button);
export { factory as buttonFactory };
2016-05-25 01:25:43 +03:00
export { Button };