diff --git a/packages/geoview-core/src/core/components/geolocator/geolocator-style.ts b/packages/geoview-core/src/core/components/geolocator/geolocator-style.ts index 4db7fa1a0fa..5b2dbb6e924 100644 --- a/packages/geoview-core/src/core/components/geolocator/geolocator-style.ts +++ b/packages/geoview-core/src/core/components/geolocator/geolocator-style.ts @@ -1,4 +1,5 @@ -import { Input, styled } from '@mui/material'; +import { Input, styled, InputProps } from '@mui/material'; +import { Theme } from '@mui/material/styles'; export const sxClasses = { root: { @@ -90,4 +91,4 @@ export const StyledInputField = styled(Input)(({ theme }) => ({ transition: theme.transitions.create('width'), width: '100%', }, -})) as unknown as typeof Input; +})) as React.ComponentType; diff --git a/packages/geoview-core/src/core/components/geolocator/geolocator.tsx b/packages/geoview-core/src/core/components/geolocator/geolocator.tsx index 834f6be7fda..eece8cd0f77 100644 --- a/packages/geoview-core/src/core/components/geolocator/geolocator.tsx +++ b/packages/geoview-core/src/core/components/geolocator/geolocator.tsx @@ -1,10 +1,10 @@ -import { ChangeEvent, useCallback, useRef, useState } from 'react'; +import { ChangeEvent, useCallback, useEffect, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import debounce from 'lodash/debounce'; import { CloseIcon, SearchIcon, AppBar, Box, Divider, IconButton, ProgressBar, Toolbar } from '@/ui'; import { StyledInputField, sxClasses } from './geolocator-style'; import { OL_ZOOM_DURATION } from '@/core/utils/constant'; -import { useUIAppbarGeolocatorActive } from '@/core/stores/store-interface-and-intial-values/ui-state'; +import { useUIAppbarGeolocatorActive, useUIStoreActions } from '@/core/stores/store-interface-and-intial-values/ui-state'; import { useAppGeolocatorServiceURL, useAppDisplayLanguage } from '@/core/stores/store-interface-and-intial-values/app-state'; import { GeolocatorResult } from './geolocator-result'; import { logger } from '@/core/utils/logger'; @@ -38,8 +38,10 @@ export function Geolocator() { // set the active (visible) or not active (hidden) from geolocator button click const active = useUIAppbarGeolocatorActive(); + const { setGeolocatorActive } = useUIStoreActions(); const urlRef = useRef(`${geolocatorServiceURL}&lang=${displayLanguage}`); + const searchInputRef = useRef(null); /** * Send fetch call to the service for given search term. @@ -68,6 +70,7 @@ export function Geolocator() { */ const resetSearch = useCallback(() => { setIsSearchInputVisible(false); + setGeolocatorActive(false); setSearchValue(''); setData(undefined); }, []); @@ -105,6 +108,29 @@ export function Geolocator() { } }; + const handleKeyDown = (event: KeyboardEvent) => { + if (event.key === 'Escape') { + setGeolocatorActive(false); + } + }; + + useEffect(() => { + if (searchInputRef.current && active) { + searchInputRef.current?.click(); + } + setSearchValue(active ? searchValue : ''); + + if (active) { + document.addEventListener('keydown', handleKeyDown); + } + + return () => { + document.removeEventListener('keydown', handleKeyDown); + }; + + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [active]); + return ( @@ -123,21 +149,18 @@ export function Geolocator() { getGeolocations(searchValue); }} > - {isSearchInputVisible && ( - - )} + { - if (!isSearchInputVisible) { - setIsSearchInputVisible(true); - } else if (searchValue.length) { + if (searchValue.length) { doRequest.cancel(); getGeolocations(searchValue); } @@ -148,7 +171,7 @@ export function Geolocator() { {isSearchInputVisible && ( <> - +