import * as React from "react"; import {observer} from "mobx-react/index"; import {observe} from "mobx/lib/mobx"; import {autobind} from "core-decorators"; import FocusStates from '../../../../reporting/common/FocusStates'; import {DesignerHost} from '../../DesignerHost'; import CommandModel from '../ui-model/CommandModel'; interface props { model : { title: string, name: string, items: Array, defaultValue: any, onClick: Function } } interface state { open: boolean } @observer class CommandDropDownMenu extends React.Component { private _commandModel:CommandModel; private _dispos: any; constructor(props, context) { super(props, context); this._commandModel = DesignerHost.instance().uiModel.commandModel; this.state = { open : false }; } componentWillUnmount() { if (this._dispos) { this._dispos(); this._dispos = null; } } render():JSX.Element { const {title, name, items} = this.props.model; const {open} = this.state; const commandBarUseable = this._commandModel.useable; const useable = commandBarUseable && this._commandModel.getItemStatus(name).useable; if (!open && this._dispos) { this._dispos(); this._dispos = null; } const menuItems = items.map((item, index) => { const className = 'menu-item'; return (
{item.title}
); }); const className = 'rd-commandbar-drop-down-menu' + (useable ? '' : ' rd-commandbar-drop-down-menu-disable'); const listStyle = {display: open ? 'block' : 'none'}; const classArrow = 'arrow ' + (open ? 'arrow-up' : 'arrow-down'); return (
{title}
{menuItems}
) } @autobind onOpen() { const commandBarUseable = this._commandModel.useable; const useable = this._commandModel.getItemStatus(this.props.model.name).useable; if (!commandBarUseable || !useable){ return; } const open = this.state.open; this.setState({open: !open}); if (!open) { this._dispos = observe(FocusStates.focusElement, () => { const listElement = this.refs["list"]; const arrowElement = this.refs["arrow"]; if (!FocusStates.isFocusIn(listElement) && !FocusStates.isFocus(arrowElement)){ this.setState({open: false}); } }); } } @autobind onClick() { const {defaultValue} = this.props.model; if (defaultValue !== undefined) { this.clickHandler(defaultValue); } } @autobind onItemClick(value) { this.clickHandler(value); this.setState({open: false}); } clickHandler(value) { const commandBarUseable = this._commandModel.useable; const useable = this._commandModel.getItemStatus(this.props.model.name).useable; const {onClick} = this.props.model; commandBarUseable && useable && onClick && onClick(value); } } export default CommandDropDownMenu;