11import { SelectItem } from '@konturio/ui-kit' ;
22import { useAction , useAtom } from '@reatom/npm-react' ;
33import cn from 'clsx' ;
4- import { forwardRef } from 'react' ;
4+ import { forwardRef , useEffect } from 'react' ;
55import { searchLocationsAtom } from '~features/search/searchLocationAtoms' ;
66import {
77 itemSelectAction ,
@@ -16,6 +16,9 @@ import {
1616 MCDASuggestionAtom ,
1717} from '~features/search/searchMcdaAtoms' ;
1818import { SearchInput } from '~components/Search/SearchInput/SearchInput' ;
19+ import { searchHighlightedGeometryAtom } from '../../atoms/highlightedGeometry' ;
20+ import type { Feature } from 'geojson' ;
21+ import { EMPTY_HIGHLIGHT } from '../../constants' ;
1922import { useSearchMenu } from '~utils/hooks/useSearchMenu' ;
2023import style from './SearchBar.module.css' ;
2124import type { AggregatedSearchItem } from '~features/search/searchAtoms' ;
@@ -33,19 +36,27 @@ export const SearchBar = forwardRef<HTMLInputElement, SearchBarProps>(
3336 ? i18n . t ( 'search.input_placeholder_mcda' )
3437 : i18n . t ( 'search.input_placeholder' ) ;
3538
36- const search = useAction ( searchAction ) ;
37- const itemSelectActionFn = useAction ( itemSelectAction ) ;
38- const reset = useAction ( resetSearchAction ) ;
39+ const search = useAction ( searchAction ) ;
40+ const itemSelectActionFn = useAction ( itemSelectAction ) ;
41+ const reset = useAction ( resetSearchAction ) ;
42+ const setHighlightedGeometry = useAction ( searchHighlightedGeometryAtom ) ;
3943
4044 const itemSelect = ( item : AggregatedSearchItem ) => {
4145 itemSelectActionFn ( item ) ;
46+ setHighlightedGeometry ( EMPTY_HIGHLIGHT ) ;
4247 onItemSelect ?.( ) ;
4348 } ;
4449
4550 const [ { error, loading, data } ] = useAtom ( searchLocationsAtom ) ;
4651 const emptyLocations = data ? data . length === 0 : false ;
4752 const [ mcdaSearchStatus ] = useAtom ( MCDASuggestionAtom ) ;
48- const [ aggregatedResults ] = useAtom ( aggregatedSearchAtom ) ;
53+ const [ aggregatedResults ] = useAtom ( aggregatedSearchAtom ) ;
54+
55+ useEffect ( ( ) => {
56+ return ( ) => {
57+ setHighlightedGeometry ( EMPTY_HIGHLIGHT ) ;
58+ } ;
59+ } , [ setHighlightedGeometry ] ) ;
4960
5061 const renderError = ( ) => (
5162 < SelectItem
@@ -91,6 +102,11 @@ export const SearchBar = forwardRef<HTMLInputElement, SearchBarProps>(
91102 className = { style . listItem }
92103 itemProps = { {
93104 onClick : ( ) => handleItemSelect ( item ) ,
105+ onMouseEnter : ( ) =>
106+ item . geometry &&
107+ setHighlightedGeometry ( item as Feature ) ,
108+ onMouseLeave : ( ) =>
109+ setHighlightedGeometry ( EMPTY_HIGHLIGHT ) ,
94110 role : 'option' ,
95111 } }
96112 />
@@ -108,6 +124,10 @@ export const SearchBar = forwardRef<HTMLInputElement, SearchBarProps>(
108124 highlighted = { highlightedIndex === index }
109125 itemProps = { {
110126 onClick : ( ) => handleItemSelect ( item ) ,
127+ onMouseEnter : ( ) =>
128+ setHighlightedGeometry ( EMPTY_HIGHLIGHT ) ,
129+ onMouseLeave : ( ) =>
130+ setHighlightedGeometry ( EMPTY_HIGHLIGHT ) ,
111131 role : 'option' ,
112132 } }
113133 />
@@ -147,6 +167,10 @@ export const SearchBar = forwardRef<HTMLInputElement, SearchBarProps>(
147167 emptyLocations ,
148168 } ) ;
149169
170+ useEffect ( ( ) => {
171+ if ( ! isMenuOpen ) setHighlightedGeometry ( EMPTY_HIGHLIGHT ) ;
172+ } , [ isMenuOpen , setHighlightedGeometry ] ) ;
173+
150174 return (
151175 < >
152176 < div className = { cn ( style . searchBar , searchBarClass ) } ref = { searchBarRef } >
0 commit comments