import React from 'react'; import {OverlayTrigger, Tooltip} from 'react-bootstrap'; import classNames from 'classnames'; import {defer, get, groupBy} from 'lodash'; import {Menu, Label, Divider, Dropdown as DropMenu} from '../Dropdown'; import {gettext} from '../utils'; interface IDropdownAction { id: string; label: string; action(): void; disabled?: boolean; icon?: string; divider?: false; className?: string; group?: string; } interface IDropdownDivider { divider: true; } export type IDropdownItem = IDropdownDivider | IDropdownAction; interface IProps { icon?: string; buttonLabel?: string; buttonLabelClassName?: string; label?: string; items: Array; alignRight?: boolean; group?: boolean; disableSelection?: boolean; defaultAction?(): void; dropUp?: boolean; navbtn?: boolean; // defaults to true className?: string; tooltip?: string; scrollable?: boolean; searchable?: boolean; } interface IState { open: boolean; filterValue: string; } export class Dropdown extends React.Component { private searchInput: React.RefObject; inToggle: boolean; constructor(props) { super(props); this.state = { open: false, filterValue: '', }; this.inToggle = false; this.searchInput = React.createRef(); this.toggle = this.toggle.bind(this); this.close = this.close.bind(this); this.updateSearchText = this.updateSearchText.bind(this); } toggle() { // change state only when click event handling is over this.inToggle = true; defer(() => { const newState: IState = { open: !this.state.open, filterValue: this.state.open ? '' : this.state.filterValue, }; this.setState(newState, () => { if (this.state.open) { document.addEventListener('mousedown', this.close); if (this.searchInput.current != undefined) { this.searchInput.current.focus(); } } else { document.removeEventListener('mousedown', this.close); } }); this.inToggle = false; }); } close(event) { if (event.target.classList.contains('dropdown-filter')) { return; } if (!this.inToggle && this.state.open && !(get(event.target, 'nodeName') === 'UL')) { this.setState({ open: false, filterValue: '', }); } } componentWillUnmount() { if (this.state.open) { document.removeEventListener('mousedown', this.close); } } updateSearchText(event: React.ChangeEvent) { this.setState({filterValue: event.target.value}); } renderButtonDropMenu() { const isCreate = this.props.icon === 'icon-plus-large'; const buttonClassName = classNames( 'dropdown-toggle', 'dropdown__toggle', this.props.buttonLabelClassName, { navbtn: this.props.navbtn ?? true, 'sd-create-btn': isCreate, 'navbtn--text-only': this.props.buttonLabel, } ); return ( ); } renderDropdownItem(item: IDropdownItem, index: number) { return item.divider === true ? ( ) : (
  • ); } render() { const filterValueNormalized = this.state.filterValue.trim().toLowerCase(); const filteredItems = filterValueNormalized.length < 1 ? this.props.items : this.props.items.filter( (item) => ( item.divider !== true && item.label.toLowerCase().includes(filterValueNormalized) ) ); const filterGroups = !this.props.group ? {} : groupBy(filteredItems, 'group'); return ( {this.props.tooltip ? ( {this.props.tooltip} )} > {this.renderButtonDropMenu()} ) : this.renderButtonDropMenu() } {this.props.label == undefined ? null : ( )} {(this.props.searchable !== true || this.props.items.length < 3) ? null : (
    ) } {this.props.group ? ( Object.keys(filterGroups) .map((group, index) => ( {!group?.length ? null : ( )} {filterGroups[group].map(this.renderDropdownItem)} )) ) : ( filteredItems.map(this.renderDropdownItem) )}
    ); } }