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

React component conversion(class to functional) - 2 #3209

Merged
merged 2 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 15 additions & 2 deletions client/modules/IDE/components/QuickAddList/QuickAddList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,21 @@ const Item = ({ isAdded, onSelect, name, url }) => {
const buttonLabel = isAdded
? t('QuickAddList.ButtonRemoveARIA')
: t('QuickAddList.ButtonAddToCollectionARIA');

const handleKeyDown = (event) => {
Copy link
Collaborator

@khanniie khanniie Aug 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think buttons should do this natively? might not be necessary~

Copy link
Collaborator

@khanniie khanniie Aug 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh nvm i see it's a div not a button -- i think in that case then we should just let the button within it be focusable / interactive and let the div be a container? meaning we remove the role & onClick/onKeydown handlers and tabindex. let me know if there's something im missing though!

if (event.key === 'Enter' || event.key === ' ') {
onSelect(event);
}
};

return (
<li className="quick-add__item" onClick={onSelect}> { /* eslint-disable-line */ }
<div
role="button"
className="quick-add__item"
onClick={onSelect}
onKeyDown={handleKeyDown}
tabIndex={0}
>
<button
className="quick-add__item-toggle"
onClick={onSelect}
Expand All @@ -28,7 +41,7 @@ const Item = ({ isAdded, onSelect, name, url }) => {
>
{t('QuickAddList.View')}
</Link>
</li>
</div>
);
};

Expand Down
36 changes: 22 additions & 14 deletions client/modules/IDE/components/Searchbar/Collection.jsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import i18next from 'i18next';
import * as SortingActions from '../../actions/sorting';

import Searchbar from './Searchbar';

const scope = 'collection';

function mapStateToProps(state) {
return {
searchLabel: i18next.t('Searchbar.SearchCollection'),
searchTerm: state.search[`${scope}SearchTerm`]
const SearchbarContainer = () => {
const dispatch = useDispatch();
const searchLabel = i18next.t('Searchbar.SearchCollection');
const searchTerm = useSelector((state) => state.search[`${scope}SearchTerm`]);

const setSearchTerm = (term) => {
dispatch(SortingActions.setSearchTerm(scope, term));
};
}

function mapDispatchToProps(dispatch) {
const actions = {
setSearchTerm: (term) => SortingActions.setSearchTerm(scope, term),
resetSearchTerm: () => SortingActions.resetSearchTerm(scope)
const resetSearchTerm = () => {
dispatch(SortingActions.resetSearchTerm(scope));
};
return bindActionCreators(Object.assign({}, actions), dispatch);
}

export default connect(mapStateToProps, mapDispatchToProps)(Searchbar);
return (
<Searchbar
searchLabel={searchLabel}
searchTerm={searchTerm}
setSearchTerm={setSearchTerm}
resetSearchTerm={resetSearchTerm}
/>
);
};

export default SearchbarContainer;
13 changes: 6 additions & 7 deletions client/modules/IDE/components/Searchbar/Searchbar.jsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import React, { useState, useCallback, useEffect } from 'react';
import PropTypes from 'prop-types';
import { throttle } from 'lodash';
import { withTranslation } from 'react-i18next';
import { useTranslation } from 'react-i18next';
import i18next from 'i18next';
import SearchIcon from '../../../../images/magnifyingglass.svg';

const Searchbar = ({
searchTerm,
setSearchTerm,
resetSearchTerm,
searchLabel,
t
searchLabel
}) => {
const { t } = useTranslation();
const [searchValue, setSearchValue] = useState(searchTerm);

const throttledSearchChange = useCallback(
throttle((value) => {
setSearchTerm(value.trim());
}, 500),
[]
[setSearchTerm]
);

const handleResetSearch = () => {
Expand Down Expand Up @@ -65,12 +65,11 @@ Searchbar.propTypes = {
searchTerm: PropTypes.string.isRequired,
setSearchTerm: PropTypes.func.isRequired,
resetSearchTerm: PropTypes.func.isRequired,
searchLabel: PropTypes.string,
t: PropTypes.func.isRequired
searchLabel: PropTypes.string
};

Searchbar.defaultProps = {
searchLabel: i18next.t('Searchbar.SearchSketch')
};

export default withTranslation()(Searchbar);
export default Searchbar;
37 changes: 22 additions & 15 deletions client/modules/IDE/components/Searchbar/Sketch.jsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import i18next from 'i18next';
import * as SortingActions from '../../actions/sorting';

import Searchbar from './Searchbar';

const scope = 'sketch';

function mapStateToProps(state) {
return {
searchLabel: i18next.t('Searchbar.SearchSketch'),
searchTerm: state.search[`${scope}SearchTerm`]
const SearchbarContainer = () => {
const dispatch = useDispatch();
const searchLabel = i18next.t('Searchbar.SearchSketch');
const searchTerm = useSelector((state) => state.search[`${scope}SearchTerm`]);

const setSearchTerm = (term) => {
dispatch(SortingActions.setSearchTerm(scope, term));
};
}

function mapDispatchToProps(dispatch) {
const actions = {
setSearchTerm: (term) => SortingActions.setSearchTerm(scope, term),
resetSearchTerm: () => SortingActions.resetSearchTerm(scope)
const resetSearchTerm = () => {
dispatch(SortingActions.resetSearchTerm(scope));
};
return bindActionCreators(Object.assign({}, actions), dispatch);
}

export default connect(mapStateToProps, mapDispatchToProps)(Searchbar);
return (
<Searchbar
searchLabel={searchLabel}
searchTerm={searchTerm}
setSearchTerm={setSearchTerm}
resetSearchTerm={resetSearchTerm}
/>
);
};

export default SearchbarContainer;
Loading