import React from 'react'; import {OverlayTrigger, Tooltip} from 'react-bootstrap'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import {defer} from 'lodash'; import {firstCharUpperCase} from '../utils'; import {Menu, Label, Divider, Dropdown as DropMenu} from '../Dropdown/'; /** * @ngdoc react * @name Dropdown * @description Dropdown of a Sub Nav bar */ export class Dropdown extends React.Component { static propTypes: any; static defaultProps: any; inToggle: any; constructor(props) { super(props); this.state = {open: false}; this.toggle = this.toggle.bind(this); this.close = this.close.bind(this); } toggle() { // change state only when click event handling is over this.inToggle = true; defer(() => { this.setState({open: !this.state.open}, () => { if (this.state.open) { document.addEventListener('click', this.close); } else { document.removeEventListener('click', this.close); } }); this.inToggle = false; }); } close() { if (!this.inToggle && this.state.open) { this.setState({open: false}); } } componentWillUnmount() { if (this.state.open) { document.removeEventListener('click', this.close); } } render() { const isCreate = this.props.icon === 'icon-plus-large'; const buttonClassName = classNames( 'dropdown-toggle', 'dropdown__toggle', this.props.buttonLabelClassName, { navbtn: this.props.navbtn, 'sd-create-btn': isCreate, 'navbtn--text-only': this.props.buttonLabel, }, ); const buttonDropMenu = ( ); return ( {this.props.tooltip ? ( {this.props.tooltip} )} > {buttonDropMenu} ) : buttonDropMenu } {this.props.label && ( )} {this.props.label && ( )} {this.props.items.map((item, index) => { if (item.divider) { return ; } else { return (
  • ); } })}
    ); } } Dropdown.propTypes = { icon: PropTypes.string, buttonLabel: PropTypes.string, buttonLabelClassName: PropTypes.string, label: PropTypes.string, items: PropTypes.arrayOf(PropTypes.shape({ label: PropTypes.string, divider: PropTypes.bool, icon: PropTypes.string, action: PropTypes.func, className: PropTypes.string, disabled: PropTypes.bool, })), alignRight: PropTypes.bool, disableSelection: PropTypes.bool, defaultAction: PropTypes.func, dropUp: PropTypes.bool, navbtn: PropTypes.bool, className: PropTypes.string, tooltip: PropTypes.string, scrollable: PropTypes.bool, }; Dropdown.defaultProps = { alignRight: false, navbtn: true, scrollable: false, };