From 11a2c45efc21bd286908caeaa109a08555c81d5a Mon Sep 17 00:00:00 2001 From: Souvik Raj Singh Date: Mon, 18 Sep 2023 12:44:01 +0530 Subject: [PATCH] Moved Search bar to be a functional component --- .../IDE/components/Searchbar/Searchbar.jsx | 101 +++++++++--------- 1 file changed, 48 insertions(+), 53 deletions(-) diff --git a/client/modules/IDE/components/Searchbar/Searchbar.jsx b/client/modules/IDE/components/Searchbar/Searchbar.jsx index a7c79d1f46..e466b3b665 100644 --- a/client/modules/IDE/components/Searchbar/Searchbar.jsx +++ b/client/modules/IDE/components/Searchbar/Searchbar.jsx @@ -1,71 +1,66 @@ +import React, { useState, useEffect, useCallback } from 'react'; import PropTypes from 'prop-types'; -import React from 'react'; import { throttle } from 'lodash'; import { withTranslation } from 'react-i18next'; import i18next from 'i18next'; import SearchIcon from '../../../../images/magnifyingglass.svg'; -class Searchbar extends React.Component { - constructor(props) { - super(props); - this.state = { - searchValue: this.props.searchTerm - }; - this.throttledSearchChange = throttle(this.searchChange, 500); - } +const Searchbar = (props) => { + const [searchValue, setSearchValue] = useState(props.searchTerm); - componentWillUnmount() { - this.props.resetSearchTerm(); - } + const throttledSearchChange = useCallback( + throttle((value) => { + props.setSearchTerm(value.trim()); + }, 500), + [] + ); - handleResetSearch = () => { - this.setState({ searchValue: '' }, () => { - this.props.resetSearchTerm(); - }); - }; + useEffect(() => { + return () => { + props.resetSearchTerm(); + }; + }, [props]); - searchChange = () => { - this.props.setSearchTerm(this.state.searchValue.trim()); + const handleResetSearch = () => { + setSearchValue(''); + props.resetSearchTerm(); }; - handleSearchChange = (e) => { - this.setState({ searchValue: e.target.value }, () => { - this.throttledSearchChange(this.state.searchValue.trim()); - }); + const handleSearchChange = (e) => { + const newValue = e.target.value; + setSearchValue(newValue); + throttledSearchChange(newValue.trim()); }; - render() { - const { searchValue } = this.state; - return ( -
-
-
- +
+
- ); - } -} + + +
+ ); +}; Searchbar.propTypes = { searchTerm: PropTypes.string.isRequired,