Last active
January 30, 2019 17:16
-
-
Save ip512/621639ec64f35e903c0af61c26abd06c to your computer and use it in GitHub Desktop.
Sort react component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
{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> | |
{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