Skip to content

Instantly share code, notes, and snippets.

@ip512
Last active January 30, 2019 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ip512/621639ec64f35e903c0af61c26abd06c to your computer and use it in GitHub Desktop.
Save ip512/621639ec64f35e903c0af61c26abd06c to your computer and use it in GitHub Desktop.
Sort react component
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Translator from 'bazinga-translator';
const sortTypes = ['publicationDate', 'price', 'km'];
const sortDirections = ['ascending', 'descending'];
export default class Sort extends Component {
getLabel(sortType) {
const labels = {
publicationDate: Translator.trans('publication_date'),
price: Translator.trans('vehicle.latest_price', {}, 'entities'),
km: Translator.trans('vehicle.latest_km', {}, 'entities'),
};
return labels[sortType];
}
render() {
const { sortType, sortDirection, onChangeSortType, onChangeSortDirection } = this.props;
return (
<div className="ks-sortable btn-group">
<button type="button" className="btn btn-outline-primary ks-light dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span className="ks-btn-text-lighter">{Translator.trans('sort_by')}</span>&nbsp;
{this.getLabel(sortType).toLowerCase()}
</button>
<div className="dropdown-menu dropdown-menu-right ks-dropdown-menu-sortable">
{sortTypes.map((item) => (
<button
type="button"
key={item}
className={`dropdown-item ${sortType === item && 'dropdown-item-checked'}`}
onClick={() => onChangeSortType(item)}
>
<span className="ks-sort-by-text">{Translator.trans('sort_by')}</span>&nbsp;
{this.getLabel(item).toLowerCase()}
</button>
))}
<div type="button" className="dropdown-divider"></div>
{sortDirections.map((item) => (
<button
type="button"
key={item}
className={`dropdown-item ${(sortDirection === item) && 'dropdown-item-checked'}`}
onClick={() => onChangeSortDirection(item)}
>
<span>{Translator.trans(item)}</span>
</button>
))}
</div>
</div>
)
}
}
Sort.propTypes = {
sortType: PropTypes.oneOf(sortTypes).isRequired,
sortDirection: PropTypes.oneOf(sortDirections).isRequired,
onChangeSortType: PropTypes.func,
onChangeSortDirection: PropTypes.func,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment