SortControl.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import React, { FC } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { SORT_AXIS, SORT_ORDER } from '../../interfaces/search';
  4. const { DESC, ASC } = SORT_ORDER;
  5. type Props = {
  6. sort: SORT_AXIS,
  7. order: SORT_ORDER,
  8. onChangeSortInvoked?: (nextSort: SORT_AXIS, nextOrder: SORT_ORDER) => void,
  9. }
  10. const SortControl: FC <Props> = (props: Props) => {
  11. const { t } = useTranslation('');
  12. const onClickChangeSort = (nextSortAxis: SORT_AXIS, nextSortOrder: SORT_ORDER) => {
  13. if (props.onChangeSortInvoked != null) {
  14. props.onChangeSortInvoked(nextSortAxis, nextSortOrder);
  15. }
  16. };
  17. const renderOrderIcon = (order: SORT_ORDER) => {
  18. const iconClassName = ASC === order ? 'fa fa-sort-amount-asc' : 'fa fa-sort-amount-desc';
  19. return <i className={iconClassName} aria-hidden="true" />;
  20. };
  21. const renderSortItem = (sort, order) => {
  22. return (
  23. <div className="d-flex align-items-center justify-content-between w-100">
  24. <span className="mr-3">{t(`search_result.sort_axis.${sort}`)}</span>
  25. {renderOrderIcon(order)}
  26. </div>
  27. );
  28. };
  29. return (
  30. <>
  31. <div className="input-group">
  32. <div className="input-group-prepend">
  33. <div className="input-group-text border" id="btnGroupAddon">
  34. {renderOrderIcon(props.order)}
  35. </div>
  36. </div>
  37. <div className="btn-group" role="group">
  38. <button
  39. type="button"
  40. className="btn border dropdown-toggle"
  41. data-toggle="dropdown"
  42. >
  43. <span className="mr-4 text-secondary">{t(`search_result.sort_axis.${props.sort}`)}</span>
  44. </button>
  45. <div className="dropdown-menu dropdown-menu-right">
  46. {Object.values(SORT_AXIS).map((sortAxis) => {
  47. const nextOrder = (props.sort !== sortAxis || props.order === ASC) ? DESC : ASC;
  48. return (
  49. <button
  50. key={sortAxis}
  51. className="dropdown-item d-flex justify-content-between"
  52. type="button"
  53. onClick={() => { onClickChangeSort(sortAxis, nextOrder) }}
  54. >
  55. {renderSortItem(sortAxis, nextOrder)}
  56. </button>
  57. );
  58. })}
  59. </div>
  60. </div>
  61. </div>
  62. </>
  63. );
  64. };
  65. export default SortControl;