Skip to content

updated Searchbar class component to functional component #2439

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
92 changes: 40 additions & 52 deletions client/modules/IDE/components/Searchbar/Searchbar.jsx
Original file line number Diff line number Diff line change
@@ -1,70 +1,58 @@
import React, { useState, useEffect } 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);
}
function Searchbar(props) {
const [searchValue, setSearchValue] = useState(props.searchTerm);

componentWillUnmount() {
this.props.resetSearchTerm();
}
const searchChange = () => props.setSearchTerm(searchValue.trim());

handleResetSearch = () => {
this.setState({ searchValue: '' }, () => {
this.props.resetSearchTerm();
});
};
const throttledSearchChange = throttle(searchChange, 500);

useEffect(
() => () => {
props.resetSearchTerm();
},
[]
);

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 (
<div
className={`searchbar ${
searchValue === '' ? 'searchbar--is-empty' : ''
}`}
>
<div className="searchbar__button">
<SearchIcon
className="searchbar__icon"
focusable="false"
aria-hidden="true"
/>
</div>
<input
className="searchbar__input"
type="text"
value={searchValue}
placeholder={this.props.searchLabel}
onChange={this.handleSearchChange}
return (
<div
className={`searchbar ${searchValue === '' ? 'searchbar--is-empty' : ''}`}
>
<div className="searchbar__button">
<SearchIcon
className="searchbar__icon"
focusable="false"
aria-hidden="true"
/>
<button
className="searchbar__clear-button"
onClick={this.handleResetSearch}
>
{this.props.t('Searchbar.ClearTerm')}
</button>
</div>
);
}
<input
className="searchbar__input"
type="text"
value={searchValue}
placeholder={props.searchLabel}
onChange={handleSearchChange}
/>
<button className="searchbar__clear-button" onClick={handleResetSearch}>
{props.t('Searchbar.ClearTerm')}
</button>
</div>
);
}

Searchbar.propTypes = {
Expand Down
2 changes: 1 addition & 1 deletion docker-compose-development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
app:
build:
context: ./
dockerfile: Dockerfile
dockerfile: Dockerfile
target: development
environment:
- MONGO_URL=mongodb://mongo:27017/p5js-web-editor
Expand Down