fleet/frontend/components/buttons/Rocker/Rocker.jsx

65 lines
1.8 KiB
React
Raw Normal View History

2016-11-18 15:41:52 +00:00
import React, { Component, PropTypes } from 'react';
import { noop } from 'lodash';
import classnames from 'classnames';
import Icon from 'components/icons/Icon';
2016-11-28 19:20:15 +00:00
2016-11-18 15:41:52 +00:00
class Rocker extends Component {
static propTypes = {
className: PropTypes.string,
2016-12-16 15:54:49 +00:00
onChange: PropTypes.func,
2016-11-18 15:41:52 +00:00
options: PropTypes.shape({
2016-12-16 15:54:49 +00:00
rightText: PropTypes.string,
rightIcon: PropTypes.string,
leftText: PropTypes.string,
leftIcon: PropTypes.string,
2016-11-18 15:41:52 +00:00
}),
value: PropTypes.string,
};
static defaultProps = {
2016-12-16 15:54:49 +00:00
onChange: noop,
};
handleChange = (evt) => {
const { onChange, options: { rightText, leftText }, value } = this.props;
evt.preventDefault();
const newOption = value === leftText ? rightText : leftText;
onChange(newOption);
2016-11-18 15:41:52 +00:00
};
render () {
2016-12-16 15:54:49 +00:00
const { handleChange } = this;
const { className, options, value } = this.props;
const { rightText, rightIcon, leftText, leftIcon } = options;
2016-11-18 15:41:52 +00:00
const baseClass = 'kolide-rocker';
const rockerClasses = classnames(baseClass, className);
2016-12-16 15:54:49 +00:00
const buttonClasses = classnames(`${baseClass}__button`, 'button', 'button--unstyled', {
[`${baseClass}__button--checked`]: value === leftText,
});
2016-11-18 15:41:52 +00:00
return (
<div className={rockerClasses}>
2016-12-16 15:54:49 +00:00
<button className={buttonClasses} onClick={handleChange}>
<span className={`${baseClass}__switch ${baseClass}__switch--left`}>
2016-11-18 15:41:52 +00:00
<span className={`${baseClass}__text`}>
2016-12-16 15:54:49 +00:00
<Icon name={leftIcon} /> {leftText}
2016-11-18 15:41:52 +00:00
</span>
</span>
2016-12-16 15:54:49 +00:00
<span className={`${baseClass}__switch ${baseClass}__switch--right`}>
2016-11-18 15:41:52 +00:00
<span className={`${baseClass}__text`}>
2016-12-16 15:54:49 +00:00
<Icon name={rightIcon} /> {rightText}
2016-11-18 15:41:52 +00:00
</span>
</span>
2016-12-16 15:54:49 +00:00
</button>
2016-11-18 15:41:52 +00:00
</div>
);
}
}
export default Rocker;