2020-07-03 08:26:04 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import styled from "styled-components";
|
|
|
|
|
import { InputGroup } from "@blueprintjs/core";
|
2020-08-20 09:31:49 +00:00
|
|
|
import { debounce } from "lodash";
|
2020-07-03 08:26:04 +00:00
|
|
|
|
|
|
|
|
interface SearchProps {
|
|
|
|
|
onSearch: (value: any) => void;
|
|
|
|
|
placeholder: string;
|
|
|
|
|
value: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const SearchInputWrapper = styled(InputGroup)`
|
|
|
|
|
&&& input {
|
|
|
|
|
box-shadow: none;
|
2020-08-17 07:41:50 +00:00
|
|
|
font-size: 12px;
|
2020-07-03 08:26:04 +00:00
|
|
|
}
|
|
|
|
|
&&& svg {
|
|
|
|
|
opacity: 0.6;
|
|
|
|
|
}
|
2020-08-13 16:41:08 +00:00
|
|
|
margin: 5px 20px;
|
2020-07-03 08:26:04 +00:00
|
|
|
width: 250px;
|
2020-08-10 11:16:13 +00:00
|
|
|
min-width: 150px;
|
2020-07-03 08:26:04 +00:00
|
|
|
`;
|
|
|
|
|
|
2020-08-07 18:09:54 +00:00
|
|
|
class SearchComponent extends React.Component<
|
|
|
|
|
SearchProps,
|
|
|
|
|
{ localValue: string }
|
|
|
|
|
> {
|
2020-08-20 09:31:49 +00:00
|
|
|
onDebouncedSearch = debounce(this.props.onSearch, 400);
|
2020-08-07 18:09:54 +00:00
|
|
|
constructor(props: SearchProps) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
|
|
|
|
localValue: props.value,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
componentDidUpdate(prevProps: Readonly<SearchProps>) {
|
|
|
|
|
// Reset local state if the value has updated via default value
|
|
|
|
|
if (prevProps.value !== this.props.value) {
|
|
|
|
|
this.setState({ localValue: this.props.value });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleSearch = (
|
2020-07-03 08:26:04 +00:00
|
|
|
event:
|
|
|
|
|
| React.ChangeEvent<HTMLInputElement>
|
|
|
|
|
| React.ChangeEvent<HTMLTextAreaElement>,
|
|
|
|
|
) => {
|
|
|
|
|
const search = event.target.value;
|
2020-08-07 18:09:54 +00:00
|
|
|
this.setState({ localValue: search });
|
2020-08-20 09:31:49 +00:00
|
|
|
this.onDebouncedSearch(search);
|
2020-07-03 08:26:04 +00:00
|
|
|
};
|
2020-08-07 18:09:54 +00:00
|
|
|
render() {
|
|
|
|
|
return (
|
|
|
|
|
<SearchInputWrapper
|
|
|
|
|
leftIcon="search"
|
|
|
|
|
type="search"
|
|
|
|
|
onChange={this.handleSearch}
|
|
|
|
|
placeholder={this.props.placeholder}
|
|
|
|
|
value={this.state.localValue}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-03 08:26:04 +00:00
|
|
|
|
|
|
|
|
export default SearchComponent;
|