| 1 |
import { useState } from 'react'; |
| 2 |
import LongArrowAltUpIcon from '@patternfly/react-icons/dist/esm/icons/long-arrow-alt-up-icon'; |
| 3 |
import LongArrowAltDownIcon from '@patternfly/react-icons/dist/esm/icons/long-arrow-alt-down-icon'; |
| 4 |
import ArrowsAltVIcon from '@patternfly/react-icons/dist/esm/icons/arrows-alt-v-icon'; |
| 5 |
import { css } from '@patternfly/react-styles'; |
| 6 |
import styles from '@patternfly/react-styles/css/components/Table/table'; |
| 7 |
import { TableText } from './TableText'; |
| 8 |
import { ActionList, ActionListItem } from '@patternfly/react-core/dist/esm/components/ActionList'; |
| 9 |
import { Button } from '@patternfly/react-core/dist/esm/components/Button'; |
| 10 |
import { TooltipProps } from '@patternfly/react-core/dist/esm/components/Tooltip'; |
| 11 |
import { FavoriteButtonProps } from './base/types'; |
| 12 |
|
| 13 |
export enum SortByDirection { |
| 14 |
asc = 'asc', |
| 15 |
desc = 'desc' |
| 16 |
} |
| 17 |
|
| 18 |
export interface SortColumnProps extends React.ButtonHTMLAttributes<HTMLButtonElement> { |
| 19 |
children?: React.ReactNode; |
| 20 |
className?: string; |
| 21 |
isSortedBy?: boolean; |
| 22 |
onSort?: Function; |
| 23 |
sortDirection?: string; |
| 24 |
tooltip?: React.ReactNode; |
| 25 |
tooltipProps?: Omit<TooltipProps, 'content'>; |
| 26 |
tooltipHasDefaultBehavior?: boolean; |
| 27 |
favoriteButtonProps?: FavoriteButtonProps; |
| 28 |
} |
| 29 |
|
| 30 |
export const SortColumn: React.FunctionComponent<SortColumnProps> = ({ |
| 31 |
children = null, |
| 32 |
className = '', |
| 33 |
isSortedBy = false, |
| 34 |
onSort = null, |
| 35 |
sortDirection = '', |
| 36 |
type = 'button', |
| 37 |
tooltip, |
| 38 |
tooltipProps, |
| 39 |
tooltipHasDefaultBehavior, |
| 40 |
favoriteButtonProps, |
| 41 |
...props |
| 42 |
}: SortColumnProps) => { |
| 43 |
let SortedByIcon; |
| 44 |
const [focused, setFocused] = useState(false); |
| 45 |
if (isSortedBy) { |
| 46 |
SortedByIcon = sortDirection === SortByDirection.asc ? LongArrowAltUpIcon : LongArrowAltDownIcon; |
| 47 |
} else { |
| 48 |
SortedByIcon = ArrowsAltVIcon; |
| 49 |
} |
| 50 |
|
| 51 |
if (favoriteButtonProps) { |
| 52 |
const { favorited, ...rest } = favoriteButtonProps; |
| 53 |
return ( |
| 54 |
<ActionList isIconList> |
| 55 |
<ActionListItem> |
| 56 |
<Button variant="plain" isFavorite isFavorited={favorited} {...rest} /> |
| 57 |
</ActionListItem> |
| 58 |
<ActionListItem> |
| 59 |
<Button |
| 60 |
variant="plain" |
| 61 |
icon={ |
| 62 |
<span className={css(styles.tableSortIndicator)}> |
| 63 |
<SortedByIcon /> |
| 64 |
</span> |
| 65 |
} |
| 66 |
onClick={(event) => onSort && onSort(event)} |
| 67 |
{...props} |
| 68 |
/> |
| 69 |
</ActionListItem> |
| 70 |
</ActionList> |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
return ( |
| 75 |
<button |
| 76 |
{...props} |
| 77 |
type={type} |
| 78 |
className={css(className, styles.tableButton)} |
| 79 |
onClick={(event) => onSort && onSort(event)} |
| 80 |
onFocus={() => setFocused(true)} |
| 81 |
onBlur={() => setFocused(false)} |
| 82 |
> |
| 83 |
<div className={css(className, styles.tableButtonContent)}> |
| 84 |
<TableText |
| 85 |
tooltip={tooltip} |
| 86 |
tooltipProps={tooltipProps} |
| 87 |
tooltipHasDefaultBehavior={tooltipHasDefaultBehavior} |
| 88 |
focused={focused} |
| 89 |
> |
| 90 |
{children} |
| 91 |
</TableText> |
| 92 |
<span className={css(styles.tableSortIndicator)}> |
| 93 |
<SortedByIcon /> |
| 94 |
</span> |
| 95 |
</div> |
| 96 |
</button> |
| 97 |
); |
| 98 |
}; |
| 99 |
SortColumn.displayName = 'SortColumn'; |