1
- import React , { ChangeEvent , useRef , useState } from 'react' ;
1
+ import React , { useRef , useState } from 'react' ;
2
2
3
+ import type { SearchQuery } from '@codemirror/search' ;
3
4
import { ChevronDown , ChevronUp , Xmark } from '@gravity-ui/icons' ;
4
5
import {
5
6
Button ,
6
7
Card ,
7
8
Checkbox ,
8
9
Icon ,
9
- PopoverAnchorRef ,
10
+ type PopoverAnchorRef ,
10
11
Popup ,
11
12
TextInput ,
12
- TextInputProps ,
13
+ type TextInputProps ,
13
14
sp ,
14
15
} from '@gravity-ui/uikit' ;
15
16
@@ -19,12 +20,11 @@ import {enterKeyHandler} from '../../../../utils/handlers';
19
20
20
21
import './SearchPopup.scss' ;
21
22
22
- interface SearchConfig {
23
- isCaseSensitive : boolean ;
24
- isWholeWord : boolean ;
25
- }
23
+ type SearchInitial = Pick < SearchQuery , 'search' | 'caseSensitive' | 'wholeWord' > ;
24
+ type SearchConfig = Pick < SearchInitial , 'caseSensitive' | 'wholeWord' > ;
26
25
27
26
interface SearchCardProps {
27
+ initial : SearchInitial ;
28
28
onSearchKeyDown ?: ( query : string ) => void ;
29
29
onChange ?: ( query : string ) => void ;
30
30
onClose ?: ( query : string ) => void ;
@@ -36,28 +36,26 @@ interface SearchCardProps {
36
36
const b = cn ( 'search-card' ) ;
37
37
38
38
const noop = ( ) => { } ;
39
+ const inverse = ( val : boolean ) => ! val ;
39
40
40
41
export const SearchCard : React . FC < SearchCardProps > = ( {
42
+ initial,
41
43
onChange = noop ,
42
44
onClose = noop ,
43
45
onSearchPrev = noop ,
44
46
onSearchNext = noop ,
45
47
onConfigChange = noop ,
46
48
} ) => {
47
- const [ query , setQuery ] = useState < string > ( '' ) ;
48
- const [ isCaseSensitive , setIsCaseSensitive ] = useState < boolean > ( false ) ;
49
- const [ isWholeWord , setIsWholeWord ] = useState < boolean > ( false ) ;
49
+ const [ query , setQuery ] = useState < string > ( initial . search ) ;
50
+ const [ isCaseSensitive , setIsCaseSensitive ] = useState < boolean > ( initial . caseSensitive ) ;
51
+ const [ isWholeWord , setIsWholeWord ] = useState < boolean > ( initial . wholeWord ) ;
50
52
const textInputRef = useRef < HTMLInputElement > ( null ) ;
51
53
52
54
const setInputFocus = ( ) => {
53
55
textInputRef . current ?. focus ( ) ;
54
56
} ;
55
57
56
- const handleInputChange = ( event : ChangeEvent < HTMLInputElement > ) => {
57
- const {
58
- target : { value} ,
59
- } = event ;
60
-
58
+ const handleInputChange = ( value : string ) => {
61
59
setQuery ( value ) ;
62
60
onChange ( value ) ;
63
61
} ;
@@ -80,19 +78,19 @@ export const SearchCard: React.FC<SearchCardProps> = ({
80
78
81
79
const handleIsCaseSensitive = ( ) => {
82
80
onConfigChange ( {
83
- isCaseSensitive : ! isCaseSensitive ,
84
- isWholeWord,
81
+ caseSensitive : ! isCaseSensitive ,
82
+ wholeWord : isWholeWord ,
85
83
} ) ;
86
- setIsCaseSensitive ( ( isCaseSensitive ) => ! isCaseSensitive ) ;
84
+ setIsCaseSensitive ( inverse ) ;
87
85
setInputFocus ( ) ;
88
86
} ;
89
87
90
88
const handleIsWholeWord = ( ) => {
91
89
onConfigChange ( {
92
- isCaseSensitive,
93
- isWholeWord : ! isWholeWord ,
90
+ caseSensitive : isCaseSensitive ,
91
+ wholeWord : ! isWholeWord ,
94
92
} ) ;
95
- setIsWholeWord ( ( isWholeWord ) => ! isWholeWord ) ;
93
+ setIsWholeWord ( inverse ) ;
96
94
setInputFocus ( ) ;
97
95
} ;
98
96
@@ -112,7 +110,7 @@ export const SearchCard: React.FC<SearchCardProps> = ({
112
110
size = "s"
113
111
autoFocus
114
112
onKeyPress = { handleSearchKeyPress }
115
- onChange = { handleInputChange }
113
+ onUpdate = { handleInputChange }
116
114
value = { query }
117
115
endContent = {
118
116
< >
@@ -140,14 +138,10 @@ export const SearchCard: React.FC<SearchCardProps> = ({
140
138
) ;
141
139
} ;
142
140
143
- export interface SearchPopupProps {
141
+ export interface SearchPopupProps extends SearchCardProps {
142
+ open : boolean ;
144
143
anchor : HTMLElement ;
145
- onChange : ( query : string ) => void ;
146
144
onClose : ( ) => void ;
147
- onSearchNext : ( ) => void ;
148
- onSearchPrev : ( ) => void ;
149
- onConfigChange : ( config : SearchConfig ) => void ;
150
- open : boolean ;
151
145
}
152
146
153
147
export const SearchPopup : React . FC < SearchPopupProps > = ( { open, anchor, onClose, ...props } ) => {
0 commit comments