shlink-web-client/src/utils/SortingDropdown.js

69 lines
2.3 KiB
JavaScript
Raw Normal View History

import React from 'react';
import { UncontrolledDropdown, DropdownToggle, DropdownMenu, DropdownItem } from 'reactstrap';
import { toPairs } from 'ramda';
import PropTypes from 'prop-types';
import FontAwesomeIcon from '@fortawesome/react-fontawesome';
import caretUpIcon from '@fortawesome/fontawesome-free-solid/faCaretUp';
import caretDownIcon from '@fortawesome/fontawesome-free-solid/faCaretDown';
2018-10-29 00:54:08 +03:00
import classNames from 'classnames';
import { determineOrderDir } from '../utils/utils';
import './SortingDropdown.scss';
const propTypes = {
2018-10-29 00:54:08 +03:00
items: PropTypes.object.isRequired,
orderField: PropTypes.string,
orderDir: PropTypes.oneOf([ 'ASC', 'DESC' ]),
2018-10-29 00:54:08 +03:00
onChange: PropTypes.func.isRequired,
isButton: PropTypes.bool,
right: PropTypes.bool,
};
2018-10-29 00:54:08 +03:00
const defaultProps = {
isButton: true,
right: false,
};
const SortingDropdown = ({ items, orderField, orderDir, onChange, isButton, right }) => {
const handleItemClick = (fieldKey) => () => {
const newOrderDir = determineOrderDir(fieldKey, orderField, orderDir);
2018-10-29 00:54:08 +03:00
onChange(newOrderDir ? fieldKey : undefined, newOrderDir);
};
return (
<UncontrolledDropdown>
<DropdownToggle
caret
color={isButton ? 'secondary' : 'link'}
className={classNames({ 'btn-block': isButton, 'btn-sm sorting-dropdown__paddingless': !isButton })}
>
Order by
</DropdownToggle>
<DropdownMenu
right={right}
className={classNames('sorting-dropdown__menu', { 'sorting-dropdown__menu--link': !isButton })}
>
{toPairs(items).map(([ fieldKey, fieldValue ]) => (
<DropdownItem key={fieldKey} active={orderField === fieldKey} onClick={handleItemClick(fieldKey)}>
{fieldValue}
{orderField === fieldKey && (
<FontAwesomeIcon
icon={orderDir === 'ASC' ? caretUpIcon : caretDownIcon}
className="sorting-dropdown__sort-icon"
/>
)}
</DropdownItem>
))}
<DropdownItem divider />
<DropdownItem onClick={() => onChange()}>
<i>Clear selection</i>
</DropdownItem>
2018-10-29 00:54:08 +03:00
</DropdownMenu>
</UncontrolledDropdown>
);
};
SortingDropdown.propTypes = propTypes;
2018-10-29 00:54:08 +03:00
SortingDropdown.defaultProps = defaultProps;
export default SortingDropdown;