SearchableAddressBook: change to be a function component.

master
Tom Hacohen 4 years ago
parent c1b57eecfd
commit 2eaa9b503a

@ -13,49 +13,32 @@ import { ContactType } from "../pim-types";
import AddressBook from "../components/AddressBook"; import AddressBook from "../components/AddressBook";
class SearchableAddressBook extends React.PureComponent { interface PropsType {
public props: {
entries: ContactType[]; entries: ContactType[];
onItemClick: (contact: ContactType) => void; onItemClick: (contact: ContactType) => void;
};
public state: {
searchQuery: string;
};
constructor(props: any) {
super(props);
this.state = { searchQuery: "" };
this.handleInputChange = this.handleInputChange.bind(this);
} }
public handleInputChange(event: React.ChangeEvent<any>) { export default function SearchableAddressBook(props: PropsType) {
const name = event.target.name; const [searchQuery, setSearchQuery] = React.useState("");
const value = event.target.value;
this.setState({
[name]: value,
});
}
public render() {
const { const {
entries, entries,
...rest ...rest
} = this.props; } = props;
const reg = new RegExp(this.state.searchQuery, "i"); const reg = new RegExp(searchQuery, "i");
return ( return (
<React.Fragment> <React.Fragment>
<TextField <TextField
name="searchQuery" name="searchQuery"
value={this.state.searchQuery} value={searchQuery}
style={{ fontSize: "120%", marginLeft: 20 }} style={{ fontSize: "120%", marginLeft: 20 }}
placeholder="Find Contacts" placeholder="Find Contacts"
onChange={this.handleInputChange} onChange={(event) => setSearchQuery(event.target.value)}
/> />
{this.state.searchQuery && {searchQuery &&
<IconButton onClick={() => this.setState({ searchQuery: "" })}> <IconButton onClick={() => setSearchQuery("")}>
<IconClear /> <IconClear />
</IconButton> </IconButton>
} }
@ -64,6 +47,3 @@ class SearchableAddressBook extends React.PureComponent {
</React.Fragment> </React.Fragment>
); );
} }
}
export default SearchableAddressBook;

Loading…
Cancel
Save