react-toolbox/components/button/IconButton.js

117 lines
3.1 KiB
JavaScript
Raw Normal View History

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';
2015-11-24 02:29:02 +03:00
2016-05-28 16:37:10 +03:00
const factory = (ripple, FontIcon) => {
class IconButton extends Component {
static propTypes = {
accent: PropTypes.bool,
children: PropTypes.node,
className: PropTypes.string,
disabled: 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,
neutral: PropTypes.bool,
onMouseLeave: PropTypes.func,
onMouseUp: PropTypes.func,
primary: PropTypes.bool,
2017-01-26 20:05:32 +03:00
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
};
2015-11-24 02:29:02 +03:00
2016-05-28 16:37:10 +03:00
static defaultProps = {
accent: false,
className: '',
neutral: true,
2016-11-22 18:44:47 +03:00
primary: false,
2017-01-26 20:05:32 +03:00
type: 'button',
2016-05-28 16:37:10 +03:00
};
2015-11-24 02:29:02 +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';
}
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);
};
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,
href,
icon,
inverse,
neutral,
primary, // 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();
2016-05-28 16:37:10 +03:00
const classes = classnames([theme.toggle], {
[theme[level]]: neutral,
2017-01-26 20:05:32 +03:00
[theme.inverse]: inverse,
2016-05-28 16:37:10 +03:00
}, className);
2015-11-24 02:29:02 +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
};
2015-11-24 02:29:02 +03:00
2017-01-26 20:05:32 +03:00
const iconElement = typeof icon === 'string'
? <FontIcon className={theme.icon} value={icon} />
: icon;
2016-05-28 16:37:10 +03:00
return React.createElement(element, props,
2017-01-26 20:05:32 +03:00
icon && iconElement,
children,
2016-05-28 16:37:10 +03:00
);
}
2015-11-24 02:29:02 +03:00
}
2016-05-28 16:37:10 +03:00
return ripple(IconButton);
};
2017-01-26 20:05:32 +03:00
const IconButton = factory(rippleFactory({ centered: true }), InjectFontIcon);
2016-05-28 16:37:10 +03:00
export default themr(BUTTON)(IconButton);
export { factory as iconButtonFactory };
export { IconButton };