From c03e6752234b9aba65a67cc821d7535a6c403138 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B5=A9=E8=BF=9C?= Date: Tue, 16 Jun 2020 10:10:04 +0800 Subject: [PATCH] fix uncontrolled article search --- src/components/utils/article-search.tsx | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/components/utils/article-search.tsx b/src/components/utils/article-search.tsx index 26a6d3a..fac0e98 100644 --- a/src/components/utils/article-search.tsx +++ b/src/components/utils/article-search.tsx @@ -12,7 +12,11 @@ type SearchProps = { dispatch: AppDispatch } -class ArticleSearch extends React.Component { +type SearchState = { + query: string +} + +class ArticleSearch extends React.Component { debouncedSearch: (query: string) => void inputRef: React.RefObject @@ -27,10 +31,12 @@ class ArticleSearch extends React.Component { } }, 750) this.inputRef = React.createRef() + this.state = { query: props.initQuery } } onSearchChange = (_, newValue: string) => { this.debouncedSearch(newValue) + this.setState({ query: newValue }) } componentDidUpdate(prevProps: SearchProps) { @@ -45,7 +51,7 @@ class ArticleSearch extends React.Component { componentRef={this.inputRef} className="article-search" placeholder={intl.get("search")} - defaultValue={this.props.initQuery} + value={this.state.query} onChange={this.onSearchChange} /> ) }