From 0200a95f1094dd64f3d29b916913fcc68501d604 Mon Sep 17 00:00:00 2001 From: Tisha Soumya Date: Wed, 12 Jul 2023 15:19:38 +0530 Subject: [PATCH] Refactor Search Widget (#4864) Co-authored-by: Nilesh --- news/4864.feature | 1 + .../theme/SearchWidget/SearchWidget.jsx | 136 +++++------------- 2 files changed, 39 insertions(+), 98 deletions(-) create mode 100644 news/4864.feature diff --git a/news/4864.feature b/news/4864.feature new file mode 100644 index 0000000000..9601b56feb --- /dev/null +++ b/news/4864.feature @@ -0,0 +1 @@ +Refactor SearchWidget @Tishasoumya-02 \ No newline at end of file diff --git a/src/components/theme/SearchWidget/SearchWidget.jsx b/src/components/theme/SearchWidget/SearchWidget.jsx index 5dc5e40513..b8f2008813 100644 --- a/src/components/theme/SearchWidget/SearchWidget.jsx +++ b/src/components/theme/SearchWidget/SearchWidget.jsx @@ -1,14 +1,7 @@ -/** - * Search widget component. - * @module components/theme/SearchWidget/SearchWidget - */ - -import React, { Component } from 'react'; -import { withRouter } from 'react-router-dom'; +import { useState } from 'react'; +import { useHistory } from 'react-router-dom'; import { Form, Input } from 'semantic-ui-react'; -import { compose } from 'redux'; -import { PropTypes } from 'prop-types'; -import { defineMessages, injectIntl } from 'react-intl'; +import { defineMessages, useIntl } from 'react-intl'; import { Icon } from '@plone/volto/components'; import zoomSVG from '@plone/volto/icons/zoom.svg'; @@ -24,96 +17,43 @@ const messages = defineMessages({ }, }); -/** - * SearchWidget component class. - * @class SearchWidget - * @extends Component - */ -class SearchWidget extends Component { - /** - * Property types. - * @property {Object} propTypes Property types. - * @static - */ - static propTypes = { - pathname: PropTypes.string, +const SearchWidget = (props) => { + const intl = useIntl(); + const [text, setText] = useState(''); + const history = useHistory(); + const onChangeText = (event, { value }) => { + setText(value); }; - - /** - * Constructor - * @method constructor - * @param {Object} props Component properties - * @constructs WysiwygEditor - */ - constructor(props) { - super(props); - this.onChangeText = this.onChangeText.bind(this); - this.onSubmit = this.onSubmit.bind(this); - this.state = { - text: '', - }; - } - - /** - * On change text - * @method onChangeText - * @param {object} event Event object. - * @param {string} value Text value. - * @returns {undefined} - */ - onChangeText(event, { value }) { - this.setState({ - text: value, - }); - } - - /** - * Submit handler - * @method onSubmit - * @param {event} event Event object. - * @returns {undefined} - */ - onSubmit(event) { + const pathname = props.pathname; + const onSubmit = (event) => { const path = - this.props.pathname?.length > 0 - ? `&path=${encodeURIComponent(this.props.pathname)}` - : ''; - this.props.history.push( - `/search?SearchableText=${encodeURIComponent(this.state.text)}${path}`, - ); + pathname?.length > 0 ? `&path=${encodeURIComponent(pathname)}` : ''; + + history.push(`/search?SearchableText=${encodeURIComponent(text)}${path}`); // reset input value - this.setState({ - text: '', - }); + setText(''); event.preventDefault(); - } - - /** - * Render method. - * @method render - * @returns {string} Markup for the component. - */ - render() { - return ( -
- - - - -
- ); - } -} + }; -export default compose(withRouter, injectIntl)(SearchWidget); + return ( +
+ + + + +
+ ); +}; + +export default SearchWidget;