@bigfishtv/cockpit
Version:
114 lines (99 loc) • 3.92 kB
JavaScript
var _class, _temp;
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import classnames from 'classnames';
import Button from '../Button';
import Icon from '../../Icon';
/**
* Is the core Dropdown component
*/
var DropdownButton = (_temp = _class = function (_Component) {
_inherits(DropdownButton, _Component);
function DropdownButton() {
_classCallCheck(this, DropdownButton);
var _this = _possibleConstructorReturn(this, _Component.call(this));
_this.state = { expanded: false };
_this.toggleOpen = _this.toggleOpen.bind(_this);
return _this;
}
DropdownButton.prototype.componentDidUpdate = function componentDidUpdate(prevProps, prevState) {
var wasExpanded = prevState.wasExpanded;
var expanded = this.state.expanded;
if (expanded != wasExpanded) {
if (expanded) {
document.addEventListener('mouseup', this.toggleOpen);
} else {
document.removeEventListener('mouseup', this.toggleOpen);
}
}
};
DropdownButton.prototype.toggleOpen = function toggleOpen() {
this.setState({ expanded: !this.state.expanded });
};
DropdownButton.prototype.render = function render() {
var _props = this.props,
icon = _props.icon,
text = _props.text,
direction = _props.direction,
pullRight = _props.pullRight,
style = _props.style,
size = _props.size,
caret = _props.caret,
disabled = _props.disabled,
children = _props.children;
return React.createElement(
'div',
{ className: 'button-group drop' + direction },
React.createElement(
Button,
{ style: style, size: size, disabled: disabled, onClick: this.toggleOpen },
icon && React.createElement(
'span',
null,
React.createElement(Icon, { name: icon, size: 18 }),
' '
),
React.createElement(
'span',
null,
text,
' '
),
caret && React.createElement('span', { className: 'caret caret-right' })
),
React.createElement(
'ul',
{ className: classnames('dropdown-menu', { open: this.state.expanded, 'dropdown-menu-right': pullRight }) },
children
)
);
};
return DropdownButton;
}(Component), _class.propTypes = {
/** Size string as per turret guidelines i.e. xsmall, small, medium, large, xlarge */
size: PropTypes.string,
/** Button's text, by default is just a carat button with no button text */
text: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
/** Outputs custom button class e.g 'primary' = 'button-primary' */
style: PropTypes.string,
/** Direction of dropdown menu -- up or down */
direction: PropTypes.oneOf(['up', 'down']),
/** whether or not to align dropdown menu to left of button */
pullRight: PropTypes.bool,
/** Whether or not to display carat */
caret: PropTypes.bool,
/** Icon name to display */
icon: PropTypes.string
}, _class.defaultProps = {
text: null,
size: 'medium',
style: 'default',
direction: 'down',
pullRight: false,
caret: true,
icon: null
}, _temp);
export { DropdownButton as default };