import classNames from 'classnames'; import React from 'react'; import { noop } from '../../utils'; export interface ISortToggleProps { sortKey: string; label: string; onChange?: (newVal: string) => void; currentSort: string; } export class SortToggle extends React.Component { public static defaultProps: Partial = { onChange: noop, }; private isSortKey(): boolean { const { currentSort, sortKey } = this.props; const field = currentSort; return field === sortKey || field === '-' + sortKey; } private isReverse(): boolean { return this.props.currentSort && this.props.currentSort.startsWith('-'); } private setSortKey = (event: React.MouseEvent): void => { const { sortKey, onChange } = this.props; event.preventDefault(); const predicate = this.isSortKey() && this.isReverse() ? '' : '-'; onChange(predicate + sortKey); }; public render() { const isSortKey = this.isSortKey(); const className = classNames({ 'inactive-sort-key': !isSortKey, 'sort-toggle': true, clickable: true, }); return ( {this.props.label} {(!this.isReverse() || !isSortKey) && } {this.isReverse() && isSortKey && } ); } }