1
- import PropTypes from 'prop-types' ;
2
1
import React from 'react' ;
3
2
import classNames from 'classnames' ;
4
3
5
- class SearchBar extends React . PureComponent {
6
- static propTypes = {
7
- query : PropTypes . string ,
8
- defaultQuery : PropTypes . string ,
9
- onSearch : PropTypes . func ,
10
- placeholder : PropTypes . string ,
11
- } ;
4
+ type Props = {
5
+ query : string ;
6
+ defaultQuery : string ;
7
+ onSearch : ( query : string ) => void ;
8
+ placeholder ?: string ;
9
+ className ?: string ;
10
+ } ;
12
11
13
- static defaultProps = {
14
- defaultQuery : '' ,
12
+ type State = {
13
+ query : string ;
14
+ dropdownVisible : boolean ;
15
+ } ;
16
+
17
+ class SearchBar extends React . PureComponent < Props , State > {
18
+ static defaultProps : Partial < Props > = {
15
19
query : '' ,
20
+ defaultQuery : '' ,
16
21
onSearch : function ( ) { } ,
17
22
} ;
18
23
19
- constructor ( ...args ) {
20
- super ( ...args ) ;
21
- this . state = {
22
- query : this . props . query || this . props . defaultQuery ,
23
- } ;
24
- this . searchInputRef = React . createRef ( ) ;
25
- }
24
+ state = {
25
+ query : this . props . query || this . props . defaultQuery ,
26
+ dropdownVisible : false ,
27
+ } ;
26
28
27
- componentWillReceiveProps ( nextProps ) {
29
+ componentWillReceiveProps ( nextProps : Props ) {
28
30
if ( nextProps . query !== this . props . query ) {
29
31
this . setState ( {
30
32
query : nextProps . query ,
31
33
} ) ;
32
34
}
33
35
}
34
36
37
+ searchInputRef = React . createRef < HTMLInputElement > ( ) ;
38
+
35
39
blur = ( ) => {
36
- this . searchInputRef . current . blur ( ) ;
40
+ if ( this . searchInputRef . current ) {
41
+ this . searchInputRef . current . blur ( ) ;
42
+ }
37
43
} ;
38
44
39
- onSubmit = evt => {
45
+ onSubmit = ( evt : React . FormEvent < HTMLFormElement > ) => {
40
46
evt . preventDefault ( ) ;
41
47
this . blur ( ) ;
42
48
this . props . onSearch ( this . state . query ) ;
@@ -58,12 +64,13 @@ class SearchBar extends React.PureComponent {
58
64
this . setState ( { dropdownVisible : false } ) ;
59
65
} ;
60
66
61
- onQueryChange = evt => {
67
+ onQueryChange = ( evt : React . ChangeEvent < HTMLInputElement > ) => {
62
68
this . setState ( { query : evt . target . value } ) ;
63
69
} ;
64
70
65
71
render ( ) {
66
72
const { className} = this . props ;
73
+
67
74
return (
68
75
< div className = { classNames ( 'search' , className ) } >
69
76
< form className = "form-horizontal" onSubmit = { this . onSubmit } >
0 commit comments