1
1
import React , { useEffect , useState } from 'react' ;
2
- import { v4 as uuidv4 } from 'uuid' ;
3
2
import { UISearchBox } from '@sap-ux/ui-components' ;
3
+ import type { ISearchBox } from '@fluentui/react' ;
4
4
5
5
import { fetchTreesData } from '../../../../features/Trees/Trees.utils' ;
6
6
import { useAppSelector } from '../../../../state/hooks' ;
@@ -26,28 +26,42 @@ export const SearchField: React.FC = (): JSX.Element => {
26
26
const activeSearch : string = useAppSelector ( getSearchQuery ) ;
27
27
28
28
const [ searchTerm , setSearchTerm ] = useState < string > ( activeSearch ) ;
29
- const [ uuidKey , setUuidKey ] = useState < any > ( uuidv4 ) ;
29
+
30
+ let searchInputRef : React . RefObject < ISearchBox > ;
31
+
32
+ const getSearchInputRef = ( ) : React . RefObject < ISearchBox > => {
33
+ searchInputRef = React . createRef ( ) ;
34
+ return searchInputRef ;
35
+ } ;
30
36
31
37
const onClear = ( ) : void => {
32
38
if ( activeSearch !== '' ) {
33
- actions . setQueryValue ( '' ) ;
39
+ setSearchTerm ( '' ) ;
40
+
41
+ if ( searchInputRef ?. current ) {
42
+ searchInputRef ?. current . focus ( ) ;
43
+ }
34
44
}
35
45
} ;
36
46
37
47
const onSearch = ( searchItem : string ) : void => {
38
48
if ( ! / \S / . test ( searchItem ) ) {
39
49
searchItem = '' ;
50
+ } else {
51
+ searchItem = searchItem . trim ( ) ;
40
52
}
53
+
41
54
if ( activeSearch !== searchItem ) {
42
55
actions . setQueryValue ( searchItem ) ;
56
+ } else {
57
+ setSearchTerm ( searchItem ) ;
43
58
}
44
59
} ;
45
60
46
61
useEffect ( ( ) => {
47
62
if ( activeScreen === 'HOME' && activeSearch === '' ) {
48
63
setSearchTerm ( '' ) ;
49
- setUuidKey ( uuidv4 ) ;
50
- } else if ( activeSearch !== searchTerm ) {
64
+ } else {
51
65
setSearchTerm ( activeSearch ) ;
52
66
fetchTreesData (
53
67
activeSearch ,
@@ -64,13 +78,15 @@ export const SearchField: React.FC = (): JSX.Element => {
64
78
} , [ activeSearch ] ) ;
65
79
66
80
return (
67
- < div className = "guided-answer__header__searchField" id = "search-field-container" key = { uuidKey } >
81
+ < div className = "guided-answer__header__searchField" id = "search-field-container" >
68
82
< UISearchBox
83
+ componentRef = { getSearchInputRef ( ) }
69
84
className = "tree-search-field"
70
- defaultValue = { searchTerm }
85
+ value = { searchTerm }
71
86
readOnly = { networkStatus === 'LOADING' }
72
87
placeholder = "Search Guided Answers"
73
88
id = "search-field"
89
+ onChange = { ( e , t ) => setSearchTerm ( t ?? '' ) }
74
90
onClear = { onClear }
75
91
onSearch = { onSearch } > </ UISearchBox >
76
92
{ activeScreen === 'SEARCH' && < Filters /> }
0 commit comments