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