Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Search Widget #4864

Merged
merged 19 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/4864.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Refactor SearchWidget @Tishasoumya-02
135 changes: 40 additions & 95 deletions src/components/theme/SearchWidget/SearchWidget.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
/**
* 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';
Expand All @@ -24,96 +18,47 @@ 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,
Tishasoumya-02 marked this conversation as resolved.
Show resolved Hide resolved
const SearchWidget = ({ pathname }) => {
Tishasoumya-02 marked this conversation as resolved.
Show resolved Hide resolved
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 onSubmit = (event) => {
const path =
this.props.pathname?.length > 0
Tishasoumya-02 marked this conversation as resolved.
Show resolved Hide resolved
? `&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();
}
};

return (
<Form action="/search" onSubmit={onSubmit}>
<Form.Field className="searchbox">
<Input
aria-label={intl.formatMessage(messages.search)}
onChange={onChangeText}
name="SearchableText"
value={text}
transparent
autoComplete="off"
placeholder={intl.formatMessage(messages.searchSite)}
title={intl.formatMessage(messages.search)}
/>
<button aria-label={intl.formatMessage(messages.search)}>
<Icon name={zoomSVG} size="18px" />
</button>
</Form.Field>
</Form>
);
};

/**
* Render method.
* @method render
* @returns {string} Markup for the component.
*/
render() {
return (
<Form action="/search" onSubmit={this.onSubmit}>
<Form.Field className="searchbox">
<Input
aria-label={this.props.intl.formatMessage(messages.search)}
onChange={this.onChangeText}
name="SearchableText"
value={this.state.text}
transparent
autoComplete="off"
placeholder={this.props.intl.formatMessage(messages.searchSite)}
title={this.props.intl.formatMessage(messages.search)}
/>
<button aria-label={this.props.intl.formatMessage(messages.search)}>
<Icon name={zoomSVG} size="18px" />
</button>
</Form.Field>
</Form>
);
}
}
SearchWidget.propTypes = {
pathname: PropTypes.string.isRequired,
Tishasoumya-02 marked this conversation as resolved.
Show resolved Hide resolved
};

export default compose(withRouter, injectIntl)(SearchWidget);
export default SearchWidget;