From 2eaa9b503a3512677f59355eda37e21b8ba87eab Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Wed, 5 Aug 2020 12:35:29 +0300 Subject: [PATCH] SearchableAddressBook: change to be a function component. --- src/components/SearchableAddressBook.tsx | 84 +++++++++--------------- 1 file changed, 32 insertions(+), 52 deletions(-) diff --git a/src/components/SearchableAddressBook.tsx b/src/components/SearchableAddressBook.tsx index 656b36c..f77f7a2 100644 --- a/src/components/SearchableAddressBook.tsx +++ b/src/components/SearchableAddressBook.tsx @@ -13,57 +13,37 @@ import { ContactType } from "../pim-types"; import AddressBook from "../components/AddressBook"; -class SearchableAddressBook extends React.PureComponent { - public props: { - entries: ContactType[]; - 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) { - const name = event.target.name; - const value = event.target.value; - this.setState({ - [name]: value, - }); - } - - public render() { - const { - entries, - ...rest - } = this.props; - - const reg = new RegExp(this.state.searchQuery, "i"); - - return ( - - - {this.state.searchQuery && - this.setState({ searchQuery: "" })}> - - - } - - ent.fn?.match(reg)} {...rest} /> - - ); - } +interface PropsType { + entries: ContactType[]; + onItemClick: (contact: ContactType) => void; } -export default SearchableAddressBook; +export default function SearchableAddressBook(props: PropsType) { + const [searchQuery, setSearchQuery] = React.useState(""); + + const { + entries, + ...rest + } = props; + + const reg = new RegExp(searchQuery, "i"); + + return ( + + setSearchQuery(event.target.value)} + /> + {searchQuery && + setSearchQuery("")}> + + + } + + ent.fn?.match(reg)} {...rest} /> + + ); +}