import * as React from 'react'; import * as classNames from 'classnames'; import { useDispatch } from 'react-redux'; import { CampusRegion } from '../models/facility.model'; import { setSearchTermAction, setSelectedCampusRegionAction } from '../store/ui/ui.actions'; import { trackPiwikEvent } from '../piwik/piwik'; import { IconButton, Input, Paper, MenuItem, Select, FormControl } from '@material-ui/core'; import SearchIcon from '@material-ui/icons/Search'; import CloseIcon from '@material-ui/icons/Close'; import ArrowBackIcon from '@material-ui/icons/ArrowBack'; interface SearchBarProps { onSearchExpand: () => void; onSearchCollapse: () => void; } export default ({onSearchExpand, onSearchCollapse}: SearchBarProps) => { const [isFocused, setIsFocused] = React.useState(false); const [isMobileOpen, setIsMobileOpen] = React.useState(false); const [value, setValue] = React.useState(''); const [campus, setCampus] = React.useState(CampusRegion.Fairfax); const [inputElement, setInputElement] = React.useState(null); const dispatch = useDispatch(); const setSearchTerm = (term: string) => dispatch(setSearchTermAction(term)); const setSelectedCampusRegion = (region: CampusRegion) => dispatch(setSelectedCampusRegionAction(region)); /** * Handles a change in the search term. */ const handleChange = (e: React.ChangeEvent) => { setValue(e.target.value); setSearchTerm(e.target.value); }; const handleRegionChange = (e: React.ChangeEvent) => { trackPiwikEvent('change-campus', e.target.value); const campusRegion: CampusRegion = e.target.value as CampusRegion; setCampus(campusRegion); setSelectedCampusRegion(campusRegion); }; const handleFocus = () => { trackPiwikEvent('search-action', 'focused'); setIsFocused(true); }; const handleBlur = () => setIsFocused(false); const handleMobileExpand = () => { setIsMobileOpen(true); onSearchExpand(); /* This timeout is necessary because inputElement is not displayed when execution reaches this point. By using setTimeout with no delay, inputElement.focus() is added to the end of the execution stack, therefore being executed after the input is visible., */ setTimeout(() => inputElement?.focus()); }; const handleMobileCollapse = () => { setIsMobileOpen(false); onSearchCollapse(); }; const clear = () => { setValue(''); setSearchTerm(''); }; return ( setInputElement(el)} value={value} /> ); };