UNPKG

3.51 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import clsx from 'clsx';
6import ArrowDownwardIcon from '../internal/svg-icons/ArrowDownward';
7import withStyles from '../styles/withStyles';
8import ButtonBase from '../ButtonBase';
9import capitalize from '../utils/capitalize';
10export const styles = theme => ({
11 /* Styles applied to the root element. */
12 root: {
13 cursor: 'pointer',
14 display: 'inline-flex',
15 justifyContent: 'flex-start',
16 flexDirection: 'inherit',
17 alignItems: 'center',
18 '&:focus': {
19 color: theme.palette.text.secondary
20 },
21 '&:hover': {
22 color: theme.palette.text.secondary,
23 '& $icon': {
24 opacity: 0.5
25 }
26 },
27 '&$active': {
28 color: theme.palette.text.primary,
29 // && instead of & is a workaround for https://github.com/cssinjs/jss/issues/1045
30 '&& $icon': {
31 opacity: 1,
32 color: theme.palette.text.secondary
33 }
34 }
35 },
36
37 /* Pseudo-class applied to the root element if `active={true}`. */
38 active: {},
39
40 /* Styles applied to the icon component. */
41 icon: {
42 fontSize: 18,
43 marginRight: 4,
44 marginLeft: 4,
45 opacity: 0,
46 transition: theme.transitions.create(['opacity', 'transform'], {
47 duration: theme.transitions.duration.shorter
48 }),
49 userSelect: 'none'
50 },
51
52 /* Styles applied to the icon component if `direction="desc"`. */
53 iconDirectionDesc: {
54 transform: 'rotate(0deg)'
55 },
56
57 /* Styles applied to the icon component if `direction="asc"`. */
58 iconDirectionAsc: {
59 transform: 'rotate(180deg)'
60 }
61});
62/**
63 * A button based label for placing inside `TableCell` for column sorting.
64 */
65
66const TableSortLabel = /*#__PURE__*/React.forwardRef(function TableSortLabel(props, ref) {
67 const {
68 active = false,
69 children,
70 classes,
71 className,
72 direction = 'asc',
73 hideSortIcon = false,
74 IconComponent = ArrowDownwardIcon
75 } = props,
76 other = _objectWithoutPropertiesLoose(props, ["active", "children", "classes", "className", "direction", "hideSortIcon", "IconComponent"]);
77
78 return /*#__PURE__*/React.createElement(ButtonBase, _extends({
79 className: clsx(classes.root, className, active && classes.active),
80 component: "span",
81 disableRipple: true,
82 ref: ref
83 }, other), children, hideSortIcon && !active ? null : /*#__PURE__*/React.createElement(IconComponent, {
84 className: clsx(classes.icon, classes[`iconDirection${capitalize(direction)}`])
85 }));
86});
87process.env.NODE_ENV !== "production" ? TableSortLabel.propTypes = {
88 /**
89 * If `true`, the label will have the active styling (should be true for the sorted column).
90 */
91 active: PropTypes.bool,
92
93 /**
94 * Label contents, the arrow will be appended automatically.
95 */
96 children: PropTypes.node,
97
98 /**
99 * Override or extend the styles applied to the component.
100 * See [CSS API](#css) below for more details.
101 */
102 classes: PropTypes.object.isRequired,
103
104 /**
105 * @ignore
106 */
107 className: PropTypes.string,
108
109 /**
110 * The current sort direction.
111 */
112 direction: PropTypes.oneOf(['asc', 'desc']),
113
114 /**
115 * Hide sort icon when active is false.
116 */
117 hideSortIcon: PropTypes.bool,
118
119 /**
120 * Sort icon to use.
121 */
122 IconComponent: PropTypes.elementType
123} : void 0;
124export default withStyles(styles, {
125 name: 'MuiTableSortLabel'
126})(TableSortLabel);
\No newline at end of file