import * as React from 'react'; import classNames from 'classnames'; import {get} from 'lodash'; import {superdeskApi} from '../../superdeskApi'; import {ILocation} from '../../interfaces'; import {KEYCODES} from '../../constants'; import {uiUtils, onEventCapture} from '../../utils'; import {Button} from '../UI'; import {Popup, Content} from '../UI/Popup'; import {LocationLookupResultItem} from './LocationLookupResultItem'; import './style.scss'; interface IProps { suggests?: Array>; localSuggests?: Array; showExternalSearch?: boolean; showAddLocation?: boolean; target: string; searching?: boolean; onChange(location: Partial): void; onCancel(): void; handleSearchClick(): void; onLocalSearchOnly(): void; onAddNewLocation(): void; onPopupOpen?(): void; onPopupClose?(): void; } interface IState { activeOptionIndex: number; tab: 'localResults' | 'searchResults'; } export class AddGeoLookupResultsPopUp extends React.Component { dom: { itemList: React.RefObject; }; constructor(props) { super(props); this.state = { activeOptionIndex: -1, tab: 'localResults', }; this.handleKeyBoardEvent = this.handleKeyBoardEvent.bind(this); this.onTabChange = this.onTabChange.bind(this); this.dom = {itemList: React.createRef()}; } handleKeyBoardEvent(event) { if (event) { switch (event.keyCode) { case KEYCODES.ENTER: onEventCapture(event); this.handleEnterKey(); break; case KEYCODES.DOWN: onEventCapture(event); this.handleDownArrowKey(); break; case KEYCODES.UP: onEventCapture(event); this.handleUpArrowKey(); break; } } } handleEnterKey() { if (this.state.activeOptionIndex > -1) { if (this.state.activeOptionIndex < get(this.props.localSuggests, 'length', -1)) { this.props.onChange(this.props.localSuggests[this.state.activeOptionIndex]); return; } if (this.state.activeOptionIndex === get(this.props.localSuggests, 'length', 0)) { this.onSearch(); return; } if (this.state.activeOptionIndex >= get(this.props.localSuggests, 'length', 0) + 1) { this.props.onChange(this.props.suggests[ this.state.activeOptionIndex - (get(this.props.localSuggests, 'length', 0) + 1)]); } } } handleDownArrowKey() { if (this.state.activeOptionIndex < (1 + // External search button get(this.props.localSuggests, 'length', 0) + get(this.props.suggests, 'length', 0)) - 1 ) { this.setState({activeOptionIndex: this.state.activeOptionIndex + 1}); if (this.dom.itemList.current != null) { uiUtils.scrollListItemIfNeeded(this.state.activeOptionIndex, this.dom.itemList.current); } } } handleUpArrowKey() { if (this.state.activeOptionIndex > 0) { this.setState({activeOptionIndex: this.state.activeOptionIndex - 1}); if (this.dom.itemList.current != null) { uiUtils.scrollListItemIfNeeded(this.state.activeOptionIndex, this.dom.itemList.current); } } } onSearch() { this.props.handleSearchClick(); } onTabChange(tabName) { if (tabName !== this.state.tab) { this.setState({tab: tabName}); if (this.state.tab === 'localResults') { this.onSearch(); } else { this.props.onLocalSearchOnly(); } } } render() { const {gettext} = superdeskApi.localization; const localSuggests = get(this.props.localSuggests, 'length') > 0 ? this.props.localSuggests : []; const suggests = get(this.props.suggests, 'length') > 0 ? this.props.suggests : []; return (
  • {this.props.showExternalSearch && (
  • )}
{this.state.tab === 'localResults' && (
    {localSuggests.map((suggest, index) => ( ))} {get(localSuggests, 'length') === 0 && (
  • {gettext('No results found')}
  • )}
)} {this.state.tab === 'searchResults' && (
{this.props.searching ? (
) : (
    {suggests.map((suggest, index) => ( ))} {get(suggests, 'length') === 0 && (
  • {gettext('No results found')}
  • )}
)}
)}
{this.props.showAddLocation && ( )} ); } }