1
1
/** @jsx h */
2
2
3
- import { h , Component } from 'preact' ;
4
- import PropTypes from 'prop-types' ;
3
+ import { h , createRef , Component } from 'preact' ;
5
4
import { noop } from '../../lib/utils' ;
6
5
import Template from '../Template/Template' ;
7
-
8
- const SearchBoxCSSClasses = PropTypes . shape ( {
9
- root : PropTypes . string . isRequired ,
10
- form : PropTypes . string . isRequired ,
11
- input : PropTypes . string . isRequired ,
12
- submit : PropTypes . string . isRequired ,
13
- submitIcon : PropTypes . string . isRequired ,
14
- reset : PropTypes . string . isRequired ,
15
- resetIcon : PropTypes . string . isRequired ,
16
- loadingIndicator : PropTypes . string . isRequired ,
17
- loadingIcon : PropTypes . string . isRequired ,
18
- } ) ;
19
-
20
- class SearchBox extends Component {
21
- static propTypes = {
22
- placeholder : PropTypes . string . isRequired ,
23
- cssClasses : SearchBoxCSSClasses . isRequired ,
24
- templates : PropTypes . object . isRequired ,
25
- query : PropTypes . string ,
26
- showSubmit : PropTypes . bool ,
27
- showReset : PropTypes . bool ,
28
- showLoadingIndicator : PropTypes . bool ,
29
- refine : PropTypes . func ,
30
- autofocus : PropTypes . bool ,
31
- searchAsYouType : PropTypes . bool ,
32
- isSearchStalled : PropTypes . bool ,
33
- disabled : PropTypes . bool ,
34
- onChange : PropTypes . func ,
35
- onSubmit : PropTypes . func ,
36
- onReset : PropTypes . func ,
37
- } ;
38
-
39
- static defaultProps = {
40
- query : '' ,
41
- showSubmit : true ,
42
- showReset : true ,
43
- showLoadingIndicator : true ,
44
- autofocus : false ,
45
- searchAsYouType : true ,
46
- isSearchStalled : false ,
47
- disabled : false ,
48
- onChange : noop ,
49
- onSubmit : noop ,
50
- onReset : noop ,
51
- refine : noop ,
52
- } ;
53
-
54
- state = {
6
+ import {
7
+ SearchBoxRendererCSSClasses ,
8
+ SearchBoxTemplates ,
9
+ } from '../../widgets/search-box/search-box' ;
10
+
11
+ type SearchBoxProps = {
12
+ placeholder ?: string ;
13
+ cssClasses : SearchBoxRendererCSSClasses ;
14
+ templates ?: SearchBoxTemplates ;
15
+ query ?: string ;
16
+ showSubmit ?: boolean ;
17
+ showReset ?: boolean ;
18
+ showLoadingIndicator ?: boolean ;
19
+ refine ?: ( value : string ) => void ;
20
+ autofocus ?: boolean ;
21
+ searchAsYouType ?: boolean ;
22
+ isSearchStalled ?: boolean ;
23
+ disabled ?: boolean ;
24
+ onChange ?: ( event : Event ) => void ;
25
+ onSubmit ?: ( event : Event ) => void ;
26
+ onReset ?: ( event : Event ) => void ;
27
+ } ;
28
+
29
+ const defaultProps = {
30
+ query : '' ,
31
+ showSubmit : true ,
32
+ showReset : true ,
33
+ showLoadingIndicator : true ,
34
+ autofocus : false ,
35
+ searchAsYouType : true ,
36
+ isSearchStalled : false ,
37
+ disabled : false ,
38
+ onChange : noop ,
39
+ onSubmit : noop ,
40
+ onReset : noop ,
41
+ refine : noop ,
42
+ } ;
43
+
44
+ type SearchBoxPropsWithDefaultProps = SearchBoxProps &
45
+ Readonly < typeof defaultProps > ;
46
+
47
+ type SearchBoxState = {
48
+ query : string ;
49
+ focused : boolean ;
50
+ } ;
51
+
52
+ class SearchBox extends Component <
53
+ SearchBoxPropsWithDefaultProps ,
54
+ SearchBoxState
55
+ > {
56
+ public static defaultProps = defaultProps ;
57
+
58
+ public state = {
55
59
query : this . props . query ,
56
60
focused : false ,
57
61
} ;
58
62
63
+ private input = createRef < HTMLInputElement > ( ) ;
64
+
59
65
/**
60
66
* This public method is used in the RefinementList SFFV search box
61
67
* to reset the input state when an item is selected.
62
68
*
63
69
* @see RefinementList#componentWillReceiveProps
64
70
* @return {undefined }
65
71
*/
66
- resetInput ( ) {
72
+ public resetInput ( ) {
67
73
this . setState ( { query : '' } ) ;
68
74
}
69
75
70
- onInput = event => {
76
+ private onInput = ( event : Event ) => {
71
77
const { searchAsYouType, refine, onChange } = this . props ;
72
- const query = event . target . value ;
78
+ const query = ( event . target as HTMLInputElement ) . value ;
73
79
74
80
if ( searchAsYouType ) {
75
81
refine ( query ) ;
@@ -79,7 +85,7 @@ class SearchBox extends Component {
79
85
onChange ( event ) ;
80
86
} ;
81
87
82
- componentWillReceiveProps ( nextProps ) {
88
+ public componentWillReceiveProps ( nextProps : SearchBoxPropsWithDefaultProps ) {
83
89
/**
84
90
* when the user is typing, we don't want to replace the query typed
85
91
* by the user (state.query) with the query exposed by the connector (props.query)
@@ -90,12 +96,14 @@ class SearchBox extends Component {
90
96
}
91
97
}
92
98
93
- onSubmit = event => {
99
+ private onSubmit = ( event : Event ) => {
94
100
const { searchAsYouType, refine, onSubmit } = this . props ;
95
101
96
102
event . preventDefault ( ) ;
97
103
event . stopPropagation ( ) ;
98
- this . input . blur ( ) ;
104
+ if ( this . input . current ) {
105
+ this . input . current . blur ( ) ;
106
+ }
99
107
100
108
if ( ! searchAsYouType ) {
101
109
refine ( this . state . query ) ;
@@ -106,27 +114,29 @@ class SearchBox extends Component {
106
114
return false ;
107
115
} ;
108
116
109
- onReset = event => {
117
+ private onReset = ( event : Event ) => {
110
118
const { refine, onReset } = this . props ;
111
119
const query = '' ;
112
120
113
- this . input . focus ( ) ;
121
+ if ( this . input . current ) {
122
+ this . input . current . focus ( ) ;
123
+ }
114
124
115
125
refine ( query ) ;
116
126
this . setState ( { query } ) ;
117
127
118
128
onReset ( event ) ;
119
129
} ;
120
130
121
- onBlur = ( ) => {
131
+ private onBlur = ( ) => {
122
132
this . setState ( { focused : false } ) ;
123
133
} ;
124
134
125
- onFocus = ( ) => {
135
+ private onFocus = ( ) => {
126
136
this . setState ( { focused : true } ) ;
127
137
} ;
128
138
129
- render ( ) {
139
+ public render ( ) {
130
140
const {
131
141
cssClasses,
132
142
placeholder,
@@ -146,10 +156,11 @@ class SearchBox extends Component {
146
156
className = { cssClasses . form }
147
157
noValidate
148
158
onSubmit = { this . onSubmit }
159
+ // @ts -expect-error `onReset` attibute is missing in preact 10.0.0 JSX types
149
160
onReset = { this . onReset }
150
161
>
151
162
< input
152
- ref = { inputRef => ( this . input = inputRef ) }
163
+ ref = { this . input }
153
164
value = { this . state . query }
154
165
disabled = { this . props . disabled }
155
166
className = { cssClasses . input }
@@ -158,6 +169,7 @@ class SearchBox extends Component {
158
169
autoFocus = { autofocus }
159
170
autoComplete = "off"
160
171
autoCorrect = "off"
172
+ // @ts -expect-error `autoCapitalize` attibute is missing in preact 10.0.0 JSX types
161
173
autoCapitalize = "off"
162
174
spellCheck = "false"
163
175
maxLength = { 512 }
0 commit comments