Skip to content

Commit 6bef54b

Browse files
authored
ref(ts): Convert <SearchBar> to typescript (#16357)
* ref(ts): Convert <SearchBar> to typescript * removed proptypes, placeholder optional
1 parent c59b4d4 commit 6bef54b

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

src/sentry/static/sentry/app/components/searchBar.jsx renamed to src/sentry/static/sentry/app/components/searchBar.tsx

+28-21
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,48 @@
1-
import PropTypes from 'prop-types';
21
import React from 'react';
32
import classNames from 'classnames';
43

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+
};
1211

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> = {
1519
query: '',
20+
defaultQuery: '',
1621
onSearch: function() {},
1722
};
1823

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+
};
2628

27-
componentWillReceiveProps(nextProps) {
29+
componentWillReceiveProps(nextProps: Props) {
2830
if (nextProps.query !== this.props.query) {
2931
this.setState({
3032
query: nextProps.query,
3133
});
3234
}
3335
}
3436

37+
searchInputRef = React.createRef<HTMLInputElement>();
38+
3539
blur = () => {
36-
this.searchInputRef.current.blur();
40+
if (this.searchInputRef.current) {
41+
this.searchInputRef.current.blur();
42+
}
3743
};
3844

39-
onSubmit = evt => {
45+
onSubmit = (evt: React.FormEvent<HTMLFormElement>) => {
4046
evt.preventDefault();
4147
this.blur();
4248
this.props.onSearch(this.state.query);
@@ -58,12 +64,13 @@ class SearchBar extends React.PureComponent {
5864
this.setState({dropdownVisible: false});
5965
};
6066

61-
onQueryChange = evt => {
67+
onQueryChange = (evt: React.ChangeEvent<HTMLInputElement>) => {
6268
this.setState({query: evt.target.value});
6369
};
6470

6571
render() {
6672
const {className} = this.props;
73+
6774
return (
6875
<div className={classNames('search', className)}>
6976
<form className="form-horizontal" onSubmit={this.onSubmit}>

0 commit comments

Comments
 (0)