From c66dd49518fcfe06a79763a0529172c93f4fb67e Mon Sep 17 00:00:00 2001 From: Jesse Chan Date: Wed, 20 Jan 2021 21:46:22 +0800 Subject: [PATCH] SearchBox: migrate to Functional Component --- client/src/javascript/actions/UIActions.ts | 11 +- .../components/sidebar/SearchBox.tsx | 124 +++++++----------- 2 files changed, 52 insertions(+), 83 deletions(-) diff --git a/client/src/javascript/actions/UIActions.ts b/client/src/javascript/actions/UIActions.ts index 5169d749..31a1ee1f 100644 --- a/client/src/javascript/actions/UIActions.ts +++ b/client/src/javascript/actions/UIActions.ts @@ -1,4 +1,3 @@ -import debounce from 'lodash/debounce'; import {MouseEvent, TouchEvent} from 'react'; import type {TorrentStatus} from '@shared/constants/torrentStatusMap'; @@ -46,13 +45,9 @@ const UIActions = { TorrentFilterStore.setTrackerFilter(tracker); }, - setTorrentsSearchFilter: debounce( - (search: string) => { - TorrentFilterStore.setSearchFilter(search); - }, - 250, - {trailing: true}, - ), + setTorrentsSearchFilter: (search: string) => { + TorrentFilterStore.setSearchFilter(search); + }, } as const; export default UIActions; diff --git a/client/src/javascript/components/sidebar/SearchBox.tsx b/client/src/javascript/components/sidebar/SearchBox.tsx index 8af2a692..ace1215f 100644 --- a/client/src/javascript/components/sidebar/SearchBox.tsx +++ b/client/src/javascript/components/sidebar/SearchBox.tsx @@ -1,88 +1,62 @@ -import {Component, ChangeEvent} from 'react'; import classnames from 'classnames'; -import {injectIntl, WrappedComponentProps} from 'react-intl'; -import {reaction} from 'mobx'; +import {FC, useEffect, useRef} from 'react'; +import {observer} from 'mobx-react'; +import {useIntl} from 'react-intl'; import {Close, Search} from '@client/ui/icons'; import TorrentFilterStore from '@client/stores/TorrentFilterStore'; import UIActions from '@client/actions/UIActions'; -interface SearchBoxStates { - inputFieldKey: number; - isSearchActive: boolean; -} +const SearchBox: FC = observer(() => { + const intl = useIntl(); + const inputRef = useRef(null); -class SearchBox extends Component { - constructor(props: WrappedComponentProps) { - super(props); + const {searchFilter} = TorrentFilterStore.filters; - reaction( - () => TorrentFilterStore.filters.searchFilter, - (searchFilter) => { - if (searchFilter === '') { - this.resetSearch(); - } - }, - ); + useEffect(() => { + if (inputRef.current != null) { + if (searchFilter !== inputRef.current.value) { + inputRef.current.value = searchFilter; + } + } + }, [inputRef, searchFilter]); - this.state = { - inputFieldKey: 0, - isSearchActive: false, - }; - } + const isSearchActive = searchFilter !== ''; - handleSearchChange = (event: ChangeEvent) => { - const {value} = event.target; - this.setState({isSearchActive: value !== ''}); - UIActions.setTorrentsSearchFilter(value); - }; - - handleResetClick = () => { - this.resetSearch(); - UIActions.setTorrentsSearchFilter(''); - }; - - resetSearch = () => { - this.setState((state) => ({ - inputFieldKey: state.inputFieldKey + 1, - isSearchActive: false, - })); - }; - - render() { - const {intl} = this.props; - const {inputFieldKey, isSearchActive} = this.state; - let clearSearchButton = null; - const classes = classnames({ - sidebar__item: true, // eslint-disable-line - search: true, - 'is-in-use': isSearchActive, - }); - - if (isSearchActive) { - clearSearchButton = ( - - ); - } + )} + + { + UIActions.setTorrentsSearchFilter(event.target.value); + }} + /> + + ); +}); - return ( -
- {clearSearchButton} - - -
- ); - } -} - -export default injectIntl(SearchBox); +export default SearchBox;