2018-10-28 23:26:47 +03:00
|
|
|
import { UncontrolledDropdown, DropdownToggle, DropdownMenu, DropdownItem } from 'reactstrap';
|
|
|
|
import { toPairs } from 'ramda';
|
2019-01-06 00:25:54 +03:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import { faSortAmountUp as sortAscIcon, faSortAmountDown as sortDescIcon } from '@fortawesome/free-solid-svg-icons';
|
2018-10-29 00:54:08 +03:00
|
|
|
import classNames from 'classnames';
|
2020-08-31 19:38:27 +03:00
|
|
|
import { determineOrderDir, OrderDir } from './utils';
|
2018-10-28 23:26:47 +03:00
|
|
|
import './SortingDropdown.scss';
|
|
|
|
|
2020-08-31 19:38:27 +03:00
|
|
|
export interface SortingDropdownProps<T extends string = string> {
|
|
|
|
items: Record<T, string>;
|
|
|
|
orderField?: T;
|
|
|
|
orderDir?: OrderDir;
|
|
|
|
onChange: (orderField?: T, orderDir?: OrderDir) => void;
|
|
|
|
isButton?: boolean;
|
|
|
|
right?: boolean;
|
|
|
|
}
|
2018-10-29 00:54:08 +03:00
|
|
|
|
2020-08-31 19:38:27 +03:00
|
|
|
export default function SortingDropdown<T extends string = string>(
|
|
|
|
{ items, orderField, orderDir, onChange, isButton = true, right = false }: SortingDropdownProps<T>,
|
|
|
|
) {
|
|
|
|
const handleItemClick = (fieldKey: T) => () => {
|
2018-10-29 00:54:08 +03:00
|
|
|
const newOrderDir = determineOrderDir(fieldKey, orderField, orderDir);
|
2018-10-28 23:26:47 +03:00
|
|
|
|
2018-10-29 00:54:08 +03:00
|
|
|
onChange(newOrderDir ? fieldKey : undefined, newOrderDir);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<UncontrolledDropdown>
|
|
|
|
<DropdownToggle
|
|
|
|
caret
|
2020-12-25 12:54:49 +03:00
|
|
|
color={isButton ? 'primary' : 'link'}
|
|
|
|
className={classNames({ 'dropdown-btn__toggle btn-block': isButton, 'btn-sm p-0': !isButton })}
|
2018-10-29 00:54:08 +03:00
|
|
|
>
|
2020-12-25 12:54:49 +03:00
|
|
|
{!isButton && <>Order by</>}
|
|
|
|
{isButton && !orderField && <>Order by...</>}
|
2020-12-25 13:05:50 +03:00
|
|
|
{isButton && orderField && `Order by: "${items[orderField]}" - "${orderDir ?? 'DESC'}"`}
|
2018-10-29 00:54:08 +03:00
|
|
|
</DropdownToggle>
|
|
|
|
<DropdownMenu
|
|
|
|
right={right}
|
2020-12-15 00:58:15 +03:00
|
|
|
className={classNames('w-100', { 'sorting-dropdown__menu--link': !isButton })}
|
2018-10-29 00:54:08 +03:00
|
|
|
>
|
|
|
|
{toPairs(items).map(([ fieldKey, fieldValue ]) => (
|
2020-08-31 19:38:27 +03:00
|
|
|
<DropdownItem key={fieldKey} active={orderField === fieldKey} onClick={handleItemClick(fieldKey as T)}>
|
2018-10-29 00:54:08 +03:00
|
|
|
{fieldValue}
|
|
|
|
{orderField === fieldKey && (
|
|
|
|
<FontAwesomeIcon
|
2018-10-30 09:35:35 +03:00
|
|
|
icon={orderDir === 'ASC' ? sortAscIcon : sortDescIcon}
|
2018-10-29 00:54:08 +03:00
|
|
|
className="sorting-dropdown__sort-icon"
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</DropdownItem>
|
|
|
|
))}
|
|
|
|
<DropdownItem divider />
|
2018-10-29 01:04:52 +03:00
|
|
|
<DropdownItem disabled={!orderField} onClick={() => onChange()}>
|
2018-10-29 00:54:08 +03:00
|
|
|
<i>Clear selection</i>
|
2018-10-28 23:26:47 +03:00
|
|
|
</DropdownItem>
|
2018-10-29 00:54:08 +03:00
|
|
|
</DropdownMenu>
|
|
|
|
</UncontrolledDropdown>
|
|
|
|
);
|
2020-08-31 19:38:27 +03:00
|
|
|
}
|