1 | import React, {PureComponent} from 'react';
|
2 | import PropTypes from 'prop-types';
|
3 | import classNames from 'classnames';
|
4 | import sortableIcon from '@jetbrains/icons/unsorted-10px.svg';
|
5 | import sortedIcon from '@jetbrains/icons/chevron-10px.svg';
|
6 |
|
7 | import Icon from '../icon';
|
8 |
|
9 | import style from './table.css';
|
10 |
|
11 | export default class HeaderCell extends PureComponent {
|
12 | static propTypes = {
|
13 | children: PropTypes.any,
|
14 | className: PropTypes.string,
|
15 | column: PropTypes.object.isRequired,
|
16 | onSort: PropTypes.func,
|
17 | sortKey: PropTypes.string,
|
18 | sortOrder: PropTypes.bool
|
19 | };
|
20 |
|
21 | static defaultProps = {
|
22 | onSort: () => {}
|
23 | };
|
24 |
|
25 | onClick = () => {
|
26 | if (this.sortable) {
|
27 | const {column, onSort, sortOrder} = this.props;
|
28 | onSort({column, order: !(this.sorted && sortOrder)});
|
29 | }
|
30 | };
|
31 |
|
32 | onChildrenClick(e) {
|
33 | e.stopPropagation();
|
34 | }
|
35 |
|
36 | render() {
|
37 | const {className, column, onSort, sortKey, sortOrder, ...restProps} = this.props;
|
38 |
|
39 | this.sortable = column.sortable === true;
|
40 | this.sorted = sortKey === column.id;
|
41 |
|
42 | const glyph = this.sorted ? sortedIcon : sortableIcon;
|
43 |
|
44 | const classes = classNames(className, column.headerClassName, {
|
45 | [style.headerCell]: true,
|
46 | [style.headerCellSortable]: this.sortable,
|
47 | [style.headerCellSorted]: this.sorted,
|
48 | [style.sortedUp]: sortOrder && this.sorted,
|
49 | [style.cellRight]: column.rightAlign
|
50 | });
|
51 |
|
52 | return (
|
53 | <th
|
54 | {...restProps}
|
55 | className={classes}
|
56 | onClick={this.onClick}
|
57 | data-test="ring-table-header-cell"
|
58 | >
|
59 | <span onClick={this.onChildrenClick}>{this.props.children}</span>
|
60 |
|
61 | {column.getHeaderValue ? column.getHeaderValue() : column.title}
|
62 |
|
63 | {this.sortable && (
|
64 | <span className={style.sorter}>
|
65 | <Icon glyph={glyph} className={style.icon}/>
|
66 | </span>
|
67 | )}
|
68 | </th>
|
69 | );
|
70 | }
|
71 | }
|