react-toolbox/components/button/Button.js

107 lines
3.1 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';
2016-05-28 16:37:10 +03:00
import { BUTTON } from '../identifiers.js';
import InjectFontIcon from '../font_icon/FontIcon.js';
import rippleFactory from '../ripple/Ripple.js';
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,
PropTypes.element
]),
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,
toggle: PropTypes.string
}),
type: PropTypes.string
};
2016-05-28 16:37:10 +03:00
static defaultProps = {
accent: false,
className: '',
flat: false,
floating: false,
mini: false,
neutral: true,
primary: false,
raised: false
};
2016-05-28 16:37:10 +03:00
handleMouseUp = (event) => {
this.refs.button.blur();
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) => {
this.refs.button.blur();
if (this.props.onMouseLeave) this.props.onMouseLeave(event);
};
2015-11-29 14:39:55 +03:00
2016-05-28 16:37:10 +03:00
render () {
const { accent, children, className, flat, floating, href, icon,
inverse, label, mini, neutral, primary, theme, raised, ...others} = this.props;
const element = href ? 'a' : 'button';
const level = primary ? 'primary' : accent ? 'accent' : 'neutral';
const shape = flat ? 'flat' : raised ? 'raised' : floating ? 'floating' : 'flat';
2016-05-28 16:37:10 +03:00
const classes = classnames(theme.button, [theme[shape]], {
[theme[level]]: neutral,
[theme.mini]: mini,
[theme.inverse]: inverse
}, className);
2015-10-04 16:12:53 +03:00
2016-05-28 16:37:10 +03:00
const props = {
...others,
href,
ref: 'button',
className: classes,
disabled: this.props.disabled,
onMouseUp: this.handleMouseUp,
onMouseLeave: this.handleMouseLeave,
'data-react-toolbox': 'button'
};
2016-05-28 16:37:10 +03:00
return React.createElement(element, props,
icon ? <FontIcon className={theme.icon} value={icon}/> : null,
label,
children
);
}
}
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 };