-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
TableSortLabel.js
143 lines (134 loc) · 3.49 KB
/
TableSortLabel.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// @inheritedComponent ButtonBase
import React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import ArrowDownwardIcon from '../internal/svg-icons/ArrowDownward';
import makeStyles from '../styles/makeStyles';
import useThemeProps from '../styles/useThemeProps';
import ButtonBase from '../ButtonBase';
import { capitalize } from '../utils/helpers';
export const styles = theme => ({
/* Styles applied to the root element. */
root: {
cursor: 'pointer',
display: 'inline-flex',
justifyContent: 'flex-start',
flexDirection: 'inherit',
alignItems: 'center',
'&:focus': {
color: theme.palette.text.primary,
},
'&:hover': {
color: theme.palette.text.primary,
'& $icon': {
opacity: 1,
color: theme.palette.text.secondary,
},
},
'&$active': {
color: theme.palette.text.primary,
// && instead of & is a workaround for https://github.com/cssinjs/jss/issues/1045
'&& $icon': {
opacity: 1,
color: theme.palette.text.primary,
},
},
},
/* Styles applied to the root element if `active={true}`. */
active: {},
/* Styles applied to the icon component. */
icon: {
height: 18,
marginRight: 4,
marginLeft: 4,
opacity: 0,
transition: theme.transitions.create(['opacity', 'transform'], {
duration: theme.transitions.duration.shorter,
}),
userSelect: 'none',
width: 18,
},
/* Styles applied to the icon component if `direction="desc"`. */
iconDirectionDesc: {
transform: 'rotate(0deg)',
},
/* Styles applied to the icon component if `direction="asc"`. */
iconDirectionAsc: {
transform: 'rotate(180deg)',
},
});
const useStyles = makeStyles(styles, { name: 'MuiTableSortLabel' });
/**
* A button based label for placing inside `TableCell` for column sorting.
*/
const TableSortLabel = React.forwardRef(function TableSortLabel(props, ref) {
const {
active,
children,
classes: _,
className,
direction,
hideSortIcon,
IconComponent,
...other
} = useThemeProps(props, { name: 'MuiTableSortLabel' });
const classes = useStyles(props);
return (
<ButtonBase
className={clsx(classes.root, { [classes.active]: active }, className)}
component="span"
disableRipple
ref={ref}
{...other}
>
{children}
{hideSortIcon && !active ? null : (
<IconComponent
className={clsx(classes.icon, classes[`iconDirection${capitalize(direction)}`])}
/>
)}
</ButtonBase>
);
});
TableSortLabel.propTypes = {
/**
* If `true`, the label will have the active styling (should be true for the sorted column).
*/
active: PropTypes.bool,
/**
* Label contents, the arrow will be appended automatically.
*/
children: PropTypes.node,
/**
* Override or extend the styles applied to the component.
* See [CSS API](#css) below for more details.
*/
classes: PropTypes.object,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The current sort direction.
*/
direction: PropTypes.oneOf(['asc', 'desc']),
/**
* Hide sort icon when active is false.
*/
hideSortIcon: PropTypes.bool,
/**
* Sort icon to use.
*/
IconComponent: PropTypes.elementType,
};
TableSortLabel.defaultProps = {
active: false,
direction: 'desc',
hideSortIcon: false,
IconComponent: ArrowDownwardIcon,
};
TableSortLabel.useStyles = useStyles;
TableSortLabel.options = {
name: 'MuiTableSortLabel',
};
export default TableSortLabel;