1 | import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
|
2 | import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
|
3 | import _inherits from 'babel-runtime/helpers/inherits';
|
4 |
|
5 | var _class, _temp2;
|
6 |
|
7 | import React from 'react';
|
8 | import PropTypes from 'prop-types';
|
9 | import classnames from 'classnames';
|
10 | import Icon from '../../icon';
|
11 | import { KEYCODE } from '../../util';
|
12 |
|
13 |
|
14 | var Sort = (_temp2 = _class = function (_React$Component) {
|
15 | _inherits(Sort, _React$Component);
|
16 |
|
17 | function Sort() {
|
18 | var _temp, _this, _ret;
|
19 |
|
20 | _classCallCheck(this, Sort);
|
21 |
|
22 | for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
|
23 | args[_key] = arguments[_key];
|
24 | }
|
25 |
|
26 | return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.handleClick = function () {
|
27 | var _this$props = _this.props,
|
28 | sort = _this$props.sort,
|
29 | dataIndex = _this$props.dataIndex,
|
30 | sortDirections = _this$props.sortDirections;
|
31 |
|
32 |
|
33 | var nextSortType = '';
|
34 |
|
35 | sortDirections.forEach(function (dir, i) {
|
36 | if (sort[dataIndex] === dir) {
|
37 | nextSortType = sortDirections.length - 1 > i ? sortDirections[i + 1] : sortDirections[0];
|
38 | }
|
39 | });
|
40 |
|
41 | if (!sort[dataIndex]) {
|
42 | nextSortType = sortDirections[0];
|
43 | }
|
44 |
|
45 | _this.onSort(dataIndex, nextSortType);
|
46 | }, _this.keydownHandler = function (e) {
|
47 | e.preventDefault();
|
48 | e.stopPropagation();
|
49 |
|
50 | if (e.keyCode === KEYCODE.ENTER) {
|
51 | _this.handleClick();
|
52 | }
|
53 | }, _this.onSort = function (dataIndex, order) {
|
54 | var sort = {};
|
55 | sort[dataIndex] = order;
|
56 |
|
57 | _this.props.onSort(dataIndex, order, sort);
|
58 | }, _temp), _possibleConstructorReturn(_this, _ret);
|
59 | }
|
60 |
|
61 |
|
62 | Sort.prototype.renderSort = function renderSort() {
|
63 | var _classnames;
|
64 |
|
65 | var _props = this.props,
|
66 | prefix = _props.prefix,
|
67 | sort = _props.sort,
|
68 | sortIcons = _props.sortIcons,
|
69 | className = _props.className,
|
70 | dataIndex = _props.dataIndex,
|
71 | locale = _props.locale,
|
72 | sortDirections = _props.sortDirections,
|
73 | rtl = _props.rtl,
|
74 | sortStatus = sort[dataIndex],
|
75 | map = {
|
76 | desc: 'descending',
|
77 | asc: 'ascending'
|
78 | };
|
79 |
|
80 |
|
81 | var icons = sortDirections.map(function (sortOrder) {
|
82 | return sortOrder === 'default' ? null : React.createElement(
|
83 | 'a',
|
84 | { key: sortOrder, className: sortStatus === sortOrder ? 'current' : '' },
|
85 | sortIcons ? sortIcons[sortOrder] : React.createElement(Icon, { rtl: rtl, type: map[sortOrder], size: 'xs' })
|
86 | );
|
87 | });
|
88 |
|
89 | var cls = classnames((_classnames = {}, _classnames[prefix + 'table-sort'] = true, _classnames[className] = className, _classnames));
|
90 |
|
91 | return React.createElement(
|
92 | 'span',
|
93 | {
|
94 | role: 'button',
|
95 | tabIndex: '0',
|
96 | 'aria-label': locale[sortStatus],
|
97 | className: cls,
|
98 | onClick: this.handleClick.bind(this),
|
99 | onKeyDown: this.keydownHandler
|
100 | },
|
101 | icons
|
102 | );
|
103 | };
|
104 |
|
105 | Sort.prototype.render = function render() {
|
106 | return this.renderSort();
|
107 | };
|
108 |
|
109 | return Sort;
|
110 | }(React.Component), _class.propTypes = {
|
111 | prefix: PropTypes.string,
|
112 | rtl: PropTypes.bool,
|
113 | className: PropTypes.string,
|
114 | sort: PropTypes.object,
|
115 | sortIcons: PropTypes.object,
|
116 | onSort: PropTypes.func,
|
117 | sortDirections: PropTypes.arrayOf(PropTypes.oneOf(['desc', 'asc', 'default'])),
|
118 | dataIndex: PropTypes.string,
|
119 | locale: PropTypes.object
|
120 | }, _class.defaultProps = {
|
121 | sort: {},
|
122 | sortDirections: ['desc', 'asc']
|
123 | }, _temp2);
|
124 | Sort.displayName = 'Sort';
|
125 | export { Sort as default }; |
\ | No newline at end of file |