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

61 lines
2.3 KiB
TypeScript
Raw Normal View History

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';
import { determineOrderDir, OrderDir } from './utils';
import './SortingDropdown.scss';
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
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-29 00:54:08 +03:00
onChange(newOrderDir ? fieldKey : undefined, newOrderDir);
};
return (
<UncontrolledDropdown>
<DropdownToggle
caret
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
>
{!isButton && <>Order by</>}
{isButton && !orderField && <>Order by...</>}
{isButton && orderField && `Order by: "${items[orderField]}" - "${orderDir ?? 'DESC'}"`}
2018-10-29 00:54:08 +03:00
</DropdownToggle>
<DropdownMenu
right={right}
className={classNames('w-100', { 'sorting-dropdown__menu--link': !isButton })}
2018-10-29 00:54:08 +03:00
>
{toPairs(items).map(([ fieldKey, fieldValue ]) => (
<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>
</DropdownItem>
2018-10-29 00:54:08 +03:00
</DropdownMenu>
</UncontrolledDropdown>
);
}