import * as React from "react"; import {observer} from "mobx-react/index"; import {observe} from "mobx/lib/mobx"; import {autobind} from "core-decorators"; import {DesignerHost} from '../../DesignerHost'; import CommandModel from '../ui-model/CommandModel'; import FocusStates from '../../../../reporting/common/FocusStates'; interface props { model : { title: string, name: string, width: number, options: Array, onChange: Function } } interface state { open: boolean } @observer class CommandSelect 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, width, options} = this.props.model; const {open} = this.state; const commandBarUseable = this._commandModel.useable; const status = this._commandModel.getItemStatus(name); const useable = commandBarUseable && status.useable; const currentValue = status.value; let currentTitle; const items = []; options.forEach((item, index) => { let className = 'option-item'; if(item.value === currentValue){ className = className + ' option-item-selected'; currentTitle = item.title; } items.push(
{item.title}
); }); const className = 'rd-commandbar-select' + (useable ? '' : ' rd-commandbar-select-disable'); const listStyle = {display: open ? 'block' : 'none'}; const classArrow = 'arrow ' + (open ? 'arrow-up' : 'arrow-down'); if (!open && this._dispos) { this._dispos(); this._dispos = null; } return (
{title}
{currentTitle}
{items}
) } @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: !this.state.open}); if (!open) { this._dispos = observe(FocusStates.focusElement, () => { const listElement = this.refs["wrapper"]; if (!FocusStates.isFocusIn(listElement)){ this.setState({open: false}); } }); } } onSelect(value, e) { const commandBarUseable = this._commandModel.useable; const useable = this._commandModel.getItemStatus(this.props.model.name).useable; const {onChange} = this.props.model; this.setState({open: false}); commandBarUseable && useable && onChange && onChange(value); } } export default CommandSelect;