import * as React from "react" import { connect } from "react-redux" import * as _ from "lodash" import * as ActionCreators from "./../ActionCreators" import * as State from "./../State" import { Icon } from "./../Icon" import { HighlightTextByIndex } from "./HighlightText" import { Visible } from "./Visible" /** * Popup menu */ require("./Menu.less") // tslint:disable-line no-var-requires export interface IMenuProps { visible: boolean selectedIndex: number filterText: string onChangeFilterText: (text: string) => void onSelect: (openInSplit: boolean, selectedIndex?: number) => void items: State.IMenuOptionWithHighlights[] } export class Menu extends React.Component { private _inputElement: HTMLInputElement = null as any // FIXME: null public render(): null | JSX.Element { if (!this.props.visible) { return null } // TODO: sync max display items (10) with value in Reducer.popupMenuReducer() (Reducer.ts) const initialItems = _.take(this.props.items, 10) // const pinnedItems = initialItems.filter(f => f.pinned) // const unpinnedItems = initialItems.filter(f => !f.pinned) const items = initialItems.map((menuItem, index) => this.props.onSelect(false, index)} />) return
{ this._inputElement = inputElement if (this._inputElement) { this._inputElement.focus() } }} onChange={(evt) => this._onChange(evt)} />
{items}
} private _onChange(evt: React.FormEvent) { const target: any = evt.target this.props.onChangeFilterText(target.value) } } const mapStateToProps = (state: State.IState) => { if (!state.popupMenu) { return { visible: false, selectedIndex: 0, filterText: "", items: [], } } else { const popupMenu = state.popupMenu return { visible: true, selectedIndex: popupMenu.selectedIndex, filterText: popupMenu.filter, items: popupMenu.filteredOptions, } } } const mapDispatchToProps = (dispatch: any) => { const dispatchFilterText = (text: string) => { dispatch(ActionCreators.filterMenu(text)) } const selectItem = (openInSplit: boolean, selectedIndex: number) => { dispatch(ActionCreators.selectMenuItem(openInSplit, selectedIndex)) } return { onChangeFilterText: dispatchFilterText, onSelect: selectItem, } } export const MenuContainer = connect(mapStateToProps, mapDispatchToProps)(Menu) export interface IMenuItemProps { icon?: string isSelected: boolean filterText: string label: string labelHighlights: number[][] detail: string detailHighlights: number[][] pinned: boolean onClick: Function } export class MenuItem extends React.Component { public render(): JSX.Element { let className = "item" if (this.props.isSelected) { className += " selected" } const icon = this.props.icon ? : null return
this.props.onClick()}> {icon}
} }