import * as React from 'react'; import classNames from 'classnames'; export interface Action { icon?: string; caption?: string; action?: () => void; } export interface DropdownActionsProps { icon?: string; actions: Action[]; isRight?: boolean; } export interface DropdownActionsState { dropdownOpen: boolean; } export class DropdownActions extends React.Component { private menu: HTMLDivElement; private button: HTMLElement; constructor(props:DropdownActionsProps) { super(props); this.state = { dropdownOpen: false }; } componentWillMount() { document.addEventListener('click', this.handleDocumentClick); document.addEventListener('touchstart', this.handleDocumentClick); } componentWillUnmount() { document.removeEventListener('click', this.handleDocumentClick); document.removeEventListener('touchstart', this.handleDocumentClick); } private handleDocumentClick = (evt:any) => { if (this.state.dropdownOpen === false) return; if (this.button && this.button.contains(evt.target)) return; if (!this.menu) return; //if (!this.menu.contains(evt.target)) this.toggle(); } private toggle = () => { this.setState({ dropdownOpen: !this.state.dropdownOpen }); } render() { let {icon, actions, isRight} = this.props; if (isRight === undefined) isRight = true; let hasIcon = actions.some(v => v.icon!==undefined); let {dropdownOpen} = this.state; //isOpen={this.state.dropdownOpen} toggle={this.toggle} return
this.menu=v} className={classNames({"dropdown-menu":true, "dropdown-menu-right":isRight, "show":dropdownOpen})}> { actions.map((v,index) => { let {icon, caption, action} = v; if (icon === undefined && caption === undefined) return
; let i:any; if (hasIcon === true) { if (icon !== undefined) icon = 'fa-' + icon; i = <>  ; } if (action === undefined) return
{i} {caption}
; return )=>{ evt.preventDefault();action()}}>{i} {caption} }) }
} } /* export class DropdownActions extends React.Component { constructor(props) { super(props); this.state = { dropdownOpen: false }; } private toggle = () => { this.setState({ dropdownOpen: !this.state.dropdownOpen }); } render() { let {icon, actions, isRight} = this.props; if (isRight === undefined) isRight = true; let hasIcon = actions.some(v => v.icon!==undefined); return {actions.map((v,index) => { let {icon, caption, action} = v; if (icon === undefined && caption === undefined) return
; let i; if (hasIcon === true) { if (icon !== undefined) icon = 'fa-' + icon; i = <>  ; } if (action === undefined) return
{i} {caption}
; return {i} {caption} })} } } */