@sb1/ffe-tables-react 8.0.30 → 8.0.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/DefaultTable/DefaultTable.js +144 -197
- package/es/DefaultTable/DefaultTable.spec.js +441 -0
- package/es/SortableTable/SortableTable.js +127 -187
- package/es/SortableTable/SortableTable.spec.js +68 -0
- package/es/SortableTable/sort-data.js +28 -29
- package/es/SortableTable/sort-data.spec.js +53 -0
- package/es/Table.js +101 -83
- package/es/TableParts/TableCell.js +19 -31
- package/es/TableParts/TableFooter.js +61 -72
- package/es/TableParts/TableHeaders.js +52 -59
- package/es/TableParts/TableRow.js +54 -74
- package/es/TableParts/TableRowExpandable.js +90 -128
- package/es/TableParts/TableRowExpandable.spec.js +99 -0
- package/es/index.js +1 -1
- package/lib/DefaultTable/DefaultTable.js +176 -210
- package/lib/DefaultTable/DefaultTable.spec.js +446 -0
- package/lib/SortableTable/SortableTable.js +160 -201
- package/lib/SortableTable/SortableTable.spec.js +73 -0
- package/lib/SortableTable/sort-data.js +30 -35
- package/lib/SortableTable/sort-data.spec.js +58 -0
- package/lib/Table.js +108 -92
- package/lib/TableParts/TableCell.js +26 -40
- package/lib/TableParts/TableFooter.js +69 -82
- package/lib/TableParts/TableHeaders.js +58 -67
- package/lib/TableParts/TableRow.js +62 -84
- package/lib/TableParts/TableRowExpandable.js +123 -142
- package/lib/TableParts/TableRowExpandable.spec.js +104 -0
- package/lib/index.js +6 -8
- package/package.json +7 -7
- package/es/index.d.js +0 -1
- package/lib/index.d.js +0 -6
|
@@ -1,19 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
function
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
function
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
function
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
1
|
+
var __extends = (this && this.__extends) || (function () {
|
|
2
|
+
var extendStatics = function (d, b) {
|
|
3
|
+
extendStatics = Object.setPrototypeOf ||
|
|
4
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
5
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
6
|
+
return extendStatics(d, b);
|
|
7
|
+
};
|
|
8
|
+
return function (d, b) {
|
|
9
|
+
if (typeof b !== "function" && b !== null)
|
|
10
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
11
|
+
extendStatics(d, b);
|
|
12
|
+
function __() { this.constructor = d; }
|
|
13
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
14
|
+
};
|
|
15
|
+
})();
|
|
16
|
+
var __assign = (this && this.__assign) || function () {
|
|
17
|
+
__assign = Object.assign || function(t) {
|
|
18
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
19
|
+
s = arguments[i];
|
|
20
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
21
|
+
t[p] = s[p];
|
|
22
|
+
}
|
|
23
|
+
return t;
|
|
24
|
+
};
|
|
25
|
+
return __assign.apply(this, arguments);
|
|
26
|
+
};
|
|
17
27
|
import { Icon } from '@sb1/ffe-icons-react';
|
|
18
28
|
import classNames from 'classnames';
|
|
19
29
|
import equal from 'deep-equal';
|
|
@@ -22,180 +32,110 @@ import React, { Component } from 'react';
|
|
|
22
32
|
import Table from '../Table';
|
|
23
33
|
import sortData from './sort-data';
|
|
24
34
|
import memoize from 'memoize-one';
|
|
25
|
-
var SortableTable =
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
});
|
|
39
|
-
return tableData;
|
|
40
|
-
});
|
|
41
|
-
_defineProperty(_this, "sortTableData", memoize(function (columns, data, sortBy, descending) {
|
|
42
|
-
return sortBy ? _this.sort(columns, data, sortBy, descending) : data;
|
|
43
|
-
}));
|
|
44
|
-
var _sortBy = props.sortBy,
|
|
45
|
-
_props$descending = props.descending,
|
|
46
|
-
_descending = _props$descending === void 0 ? false : _props$descending;
|
|
47
|
-
_this.state = {
|
|
48
|
-
sortBy: _sortBy,
|
|
49
|
-
descending: _descending
|
|
50
|
-
};
|
|
51
|
-
return _this;
|
|
52
|
-
}
|
|
53
|
-
_inherits(SortableTable, _Component);
|
|
54
|
-
return _createClass(SortableTable, [{
|
|
55
|
-
key: "sortStateHasChanged",
|
|
56
|
-
value: function sortStateHasChanged(nextState) {
|
|
57
|
-
return nextState.sortBy !== this.state.sortBy || nextState.descending !== this.state.descending;
|
|
58
|
-
}
|
|
59
|
-
}, {
|
|
60
|
-
key: "shouldComponentUpdate",
|
|
61
|
-
value: function shouldComponentUpdate(nextProps, nextState) {
|
|
62
|
-
return !equal(nextProps, this.props) || this.sortStateHasChanged(nextState);
|
|
63
|
-
}
|
|
64
|
-
}, {
|
|
65
|
-
key: "tableHeaderClicked",
|
|
66
|
-
value: function tableHeaderClicked(columnKey) {
|
|
67
|
-
this.setState(function (prevState) {
|
|
68
|
-
var descending = columnKey === prevState.sortBy ? !prevState.descending : false;
|
|
69
|
-
return {
|
|
70
|
-
sortBy: columnKey,
|
|
71
|
-
descending: descending
|
|
35
|
+
var SortableTable = /** @class */ (function (_super) {
|
|
36
|
+
__extends(SortableTable, _super);
|
|
37
|
+
function SortableTable(props) {
|
|
38
|
+
var _this = _super.call(this, props) || this;
|
|
39
|
+
_this.sort = function (columns, data, sortBy, descending) {
|
|
40
|
+
var _a = _this.props.onSort, onSort = _a === void 0 ? function () { } : _a;
|
|
41
|
+
var tableData = sortData(columns, data, sortBy, descending);
|
|
42
|
+
onSort({
|
|
43
|
+
sortBy: sortBy,
|
|
44
|
+
descending: descending,
|
|
45
|
+
tableData: tableData,
|
|
46
|
+
});
|
|
47
|
+
return tableData;
|
|
72
48
|
};
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}, {
|
|
76
|
-
key: "handleKeyPress",
|
|
77
|
-
value: function handleKeyPress(columnKey, event) {
|
|
78
|
-
if (event.key === 'Enter' || event.key === ' ') {
|
|
79
|
-
this.tableHeaderClicked(columnKey);
|
|
80
|
-
event.preventDefault();
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}, {
|
|
84
|
-
key: "getAriaSort",
|
|
85
|
-
value: function getAriaSort(column) {
|
|
86
|
-
if (this.state.sortBy !== column.key) {
|
|
87
|
-
return 'none';
|
|
88
|
-
}
|
|
89
|
-
return this.state.descending ? 'descending' : 'ascending';
|
|
90
|
-
}
|
|
91
|
-
}, {
|
|
92
|
-
key: "decorateSortableTableHeader",
|
|
93
|
-
value: function decorateSortableTableHeader(column) {
|
|
94
|
-
var _this2 = this;
|
|
95
|
-
if (column.header === '') {
|
|
96
|
-
return /*#__PURE__*/React.createElement("span", {
|
|
97
|
-
className: "ffe-sortable-table__header"
|
|
49
|
+
_this.sortTableData = memoize(function (columns, data, sortBy, descending) {
|
|
50
|
+
return sortBy ? _this.sort(columns, data, sortBy, descending) : data;
|
|
98
51
|
});
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
onKeyDown: function onKeyDown(event) {
|
|
106
|
-
return _this2.handleKeyPress(column.key, event);
|
|
107
|
-
},
|
|
108
|
-
className: classNames('ffe-sortable-table__header', {
|
|
109
|
-
'ffe-sortable-table__header--active': this.state.sortBy === column.key
|
|
110
|
-
}),
|
|
111
|
-
role: "button",
|
|
112
|
-
onClick: this.tableHeaderClicked.bind(this, column.key)
|
|
113
|
-
}, column.header, /*#__PURE__*/React.createElement(Icon, {
|
|
114
|
-
className: classNames('icon', 'ffe-sortable-table__sort-arrow', {
|
|
115
|
-
'ffe-sortable-table__sort-arrow--descending': this.state.sortBy === column.key && this.state.descending
|
|
116
|
-
}),
|
|
117
|
-
fileUrl: "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgLTk2MCA5NjAgOTYwIiB3aWR0aD0iMjQiPjxwYXRoIGQ9Ik00NjAuODA4LTM5My4wNCAzMzQuNzYzLTUxOS4wODRxLTIuNjA4LTIuNjA4LTQuMTA4LTUuODI0LTEuNS0zLjIxNy0xLjUtNi44OTMgMC03LjM1MiA0Ljk3Mi0xMi43NzUgNC45NzEtNS40MjMgMTMuMTA1LTUuNDIzaDI2NS41MzZxOC4xMzQgMCAxMy4xMDUgNS40NzUgNC45NzIgNS40NzYgNC45NzIgMTIuNzc2IDAgMS44MjYtNS42MTYgMTIuNjcyTDQ5OS4xOTItMzkzLjAzOXEtNC4zNDYgNC4zNDYtOC45ODUgNi4zNDYtNC42NCAyLTEwLjIwNyAyLTUuNTY3IDAtMTAuMjA3LTItNC42MzktMi04Ljk4NS02LjM0N1oiLz48L3N2Zz4=",
|
|
118
|
-
size: "md"
|
|
119
|
-
}));
|
|
52
|
+
var sortBy = props.sortBy, _a = props.descending, descending = _a === void 0 ? false : _a;
|
|
53
|
+
_this.state = {
|
|
54
|
+
sortBy: sortBy,
|
|
55
|
+
descending: descending,
|
|
56
|
+
};
|
|
57
|
+
return _this;
|
|
120
58
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
return
|
|
127
|
-
|
|
128
|
-
|
|
59
|
+
SortableTable.prototype.sortStateHasChanged = function (nextState) {
|
|
60
|
+
return (nextState.sortBy !== this.state.sortBy ||
|
|
61
|
+
nextState.descending !== this.state.descending);
|
|
62
|
+
};
|
|
63
|
+
SortableTable.prototype.shouldComponentUpdate = function (nextProps, nextState) {
|
|
64
|
+
return (!equal(nextProps, this.props) || this.sortStateHasChanged(nextState));
|
|
65
|
+
};
|
|
66
|
+
SortableTable.prototype.tableHeaderClicked = function (columnKey) {
|
|
67
|
+
this.setState(function (prevState) {
|
|
68
|
+
var descending = columnKey === prevState.sortBy ? !prevState.descending : false;
|
|
69
|
+
return {
|
|
70
|
+
sortBy: columnKey,
|
|
71
|
+
descending: descending,
|
|
72
|
+
};
|
|
129
73
|
});
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
limit
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
className: className,
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
}]);
|
|
179
|
-
}(Component);
|
|
74
|
+
};
|
|
75
|
+
SortableTable.prototype.handleKeyPress = function (columnKey, event) {
|
|
76
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
77
|
+
this.tableHeaderClicked(columnKey);
|
|
78
|
+
event.preventDefault();
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
SortableTable.prototype.getAriaSort = function (column) {
|
|
82
|
+
if (this.state.sortBy !== column.key) {
|
|
83
|
+
return 'none';
|
|
84
|
+
}
|
|
85
|
+
return this.state.descending ? 'descending' : 'ascending';
|
|
86
|
+
};
|
|
87
|
+
SortableTable.prototype.decorateSortableTableHeader = function (column) {
|
|
88
|
+
var _this = this;
|
|
89
|
+
if (column.header === '') {
|
|
90
|
+
return React.createElement("span", { className: "ffe-sortable-table__header" });
|
|
91
|
+
}
|
|
92
|
+
if (column.notSortable) {
|
|
93
|
+
return column.header;
|
|
94
|
+
}
|
|
95
|
+
return (React.createElement("span", { tabIndex: "0", onKeyDown: function (event) { return _this.handleKeyPress(column.key, event); }, className: classNames('ffe-sortable-table__header', {
|
|
96
|
+
'ffe-sortable-table__header--active': this.state.sortBy === column.key,
|
|
97
|
+
}), role: "button", onClick: this.tableHeaderClicked.bind(this, column.key) },
|
|
98
|
+
column.header,
|
|
99
|
+
React.createElement(Icon, { className: classNames('icon', 'ffe-sortable-table__sort-arrow', {
|
|
100
|
+
'ffe-sortable-table__sort-arrow--descending': this.state.sortBy === column.key &&
|
|
101
|
+
this.state.descending,
|
|
102
|
+
}), fileUrl: "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgLTk2MCA5NjAgOTYwIiB3aWR0aD0iMjQiPjxwYXRoIGQ9Ik00NjAuODA4LTM5My4wNCAzMzQuNzYzLTUxOS4wODRxLTIuNjA4LTIuNjA4LTQuMTA4LTUuODI0LTEuNS0zLjIxNy0xLjUtNi44OTMgMC03LjM1MiA0Ljk3Mi0xMi43NzUgNC45NzEtNS40MjMgMTMuMTA1LTUuNDIzaDI2NS41MzZxOC4xMzQgMCAxMy4xMDUgNS40NzUgNC45NzIgNS40NzYgNC45NzIgMTIuNzc2IDAgMS44MjYtNS42MTYgMTIuNjcyTDQ5OS4xOTItMzkzLjAzOXEtNC4zNDYgNC4zNDYtOC45ODUgNi4zNDYtNC42NCAyLTEwLjIwNyAyLTUuNTY3IDAtMTAuMjA3LTItNC42MzktMi04Ljk4NS02LjM0N1oiLz48L3N2Zz4=", size: "md" })));
|
|
103
|
+
};
|
|
104
|
+
SortableTable.prototype.render = function () {
|
|
105
|
+
var _this = this;
|
|
106
|
+
var sortableColumns = this.props.columns.map(function (column) {
|
|
107
|
+
return __assign(__assign({}, column), { ariaSort: _this.getAriaSort(column), header: _this.decorateSortableTableHeader(column) });
|
|
108
|
+
});
|
|
109
|
+
var caption = this.props.caption;
|
|
110
|
+
var _a = this.props, expandedContentMapper = _a.expandedContentMapper, condensed = _a.condensed, columnLayoutMobile = _a.columnLayoutMobile, smallHeader = _a.smallHeader, alignLeft = _a.alignLeft, breakpoint = _a.breakpoint, limit = _a.limit, offset = _a.offset, srOnlyCaption = _a.srOnlyCaption, className = _a.className, rowRender = _a.rowRender, headerRender = _a.headerRender, footerRender = _a.footerRender, columns = _a.columns, data = _a.data;
|
|
111
|
+
var _b = this.state, sortBy = _b.sortBy, descending = _b.descending;
|
|
112
|
+
if (srOnlyCaption && sortBy) {
|
|
113
|
+
caption = caption.concat(' ', sortBy, ' ', descending ? 'descending' : 'ascending');
|
|
114
|
+
}
|
|
115
|
+
var tableData = this.sortTableData(columns, data, sortBy, descending);
|
|
116
|
+
return (React.createElement(Table, { caption: caption, srOnlyCaption: srOnlyCaption, expandedContentMapper: expandedContentMapper, columnLayoutMobile: columnLayoutMobile, columns: sortableColumns, data: tableData, condensed: condensed, smallHeader: smallHeader, alignLeft: alignLeft, breakpoint: breakpoint, limit: limit, offset: offset, sort: { sortBy: sortBy, descending: descending }, className: className, rowRender: rowRender, headerRender: headerRender, footerRender: footerRender }));
|
|
117
|
+
};
|
|
118
|
+
return SortableTable;
|
|
119
|
+
}(Component));
|
|
180
120
|
SortableTable.propTypes = {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
121
|
+
caption: PropTypes.node,
|
|
122
|
+
srOnlyCaption: PropTypes.bool,
|
|
123
|
+
expandedContentMapper: PropTypes.func,
|
|
124
|
+
offset: PropTypes.number,
|
|
125
|
+
limit: PropTypes.number,
|
|
126
|
+
columnLayoutMobile: PropTypes.bool,
|
|
127
|
+
condensed: PropTypes.bool,
|
|
128
|
+
smallHeader: PropTypes.bool,
|
|
129
|
+
alignLeft: PropTypes.bool,
|
|
130
|
+
breakpoint: PropTypes.oneOf(['sm', 'none']),
|
|
131
|
+
columns: PropTypes.arrayOf(PropTypes.object).isRequired,
|
|
132
|
+
data: PropTypes.arrayOf(PropTypes.object).isRequired,
|
|
133
|
+
sortBy: PropTypes.string,
|
|
134
|
+
descending: PropTypes.bool,
|
|
135
|
+
onSort: PropTypes.func,
|
|
136
|
+
rowRender: PropTypes.func,
|
|
137
|
+
headerRender: PropTypes.func,
|
|
138
|
+
footerRender: PropTypes.func,
|
|
139
|
+
className: PropTypes.string,
|
|
200
140
|
};
|
|
201
|
-
export default SortableTable;
|
|
141
|
+
export default SortableTable;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { assert, match, spy } from 'sinon';
|
|
3
|
+
import SortableTable from './SortableTable';
|
|
4
|
+
describe('<SortableTable>', function () {
|
|
5
|
+
var columns = [
|
|
6
|
+
{ header: 'Name', key: 'name' },
|
|
7
|
+
{ header: 'Age', key: 'age' },
|
|
8
|
+
{ header: 'Employee-id', key: 'id' },
|
|
9
|
+
{ header: '', key: 'button' },
|
|
10
|
+
];
|
|
11
|
+
var data = [
|
|
12
|
+
{
|
|
13
|
+
name: 'Zombie Mountain',
|
|
14
|
+
age: 36,
|
|
15
|
+
id: 2,
|
|
16
|
+
button: React.createElement("button", null, "poke"),
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
name: 'Daenerys Targaryen',
|
|
20
|
+
age: 16,
|
|
21
|
+
id: 4,
|
|
22
|
+
button: React.createElement("button", null, "poke"),
|
|
23
|
+
},
|
|
24
|
+
{ name: 'Ned Stark', age: 48, id: 3, button: React.createElement("button", null, "poke") },
|
|
25
|
+
{ name: 'Jon Snow', age: 20, id: 1, button: React.createElement("button", null, "poke") },
|
|
26
|
+
];
|
|
27
|
+
var wrapper = render(React.createElement(SortableTable, { columns: columns, data: data }));
|
|
28
|
+
it('should show right amount of th tags', function () {
|
|
29
|
+
expect(wrapper.find('th')).toHaveLength(columns.length);
|
|
30
|
+
});
|
|
31
|
+
it('should render a row for each data array entry', function () {
|
|
32
|
+
expect(wrapper.find('tbody tr')).toHaveLength(data.length);
|
|
33
|
+
});
|
|
34
|
+
it('should set data-th property on each cell rendered', function () {
|
|
35
|
+
expect(wrapper.find('[data-th]')).toHaveLength(data.length * columns.length);
|
|
36
|
+
});
|
|
37
|
+
it('should add `aria-sort` property', function () {
|
|
38
|
+
expect(wrapper.find('[aria-sort]')).toHaveLength(columns.length);
|
|
39
|
+
});
|
|
40
|
+
it('should not show sorting arrow if header is blank string', function () {
|
|
41
|
+
expect(wrapper.find('.ffe-sortable-table__sort-arrow').length).toBe(columns.length - 1);
|
|
42
|
+
});
|
|
43
|
+
it('should show buttons in table when passing in buttons in data', function () {
|
|
44
|
+
expect(wrapper.find('button').length).toBe(data.length);
|
|
45
|
+
});
|
|
46
|
+
it('should call onSort after sorting table', function () {
|
|
47
|
+
var onSort = spy();
|
|
48
|
+
var table = shallow(React.createElement(SortableTable, { columns: columns, data: data, onSort: onSort }));
|
|
49
|
+
table.instance().tableHeaderClicked('name');
|
|
50
|
+
assert.calledWith(onSort, match(function (val) {
|
|
51
|
+
return ('sortBy' in val && 'descending' in val && 'tableData' in val);
|
|
52
|
+
}));
|
|
53
|
+
});
|
|
54
|
+
it('should call onSort after initial sort', function () {
|
|
55
|
+
var onSort = spy();
|
|
56
|
+
shallow(React.createElement(SortableTable, { columns: columns, data: data, onSort: onSort, sortBy: 'name' }));
|
|
57
|
+
assert.calledWith(onSort, match(function (val) {
|
|
58
|
+
return ('sortBy' in val && 'descending' in val && 'tableData' in val);
|
|
59
|
+
}));
|
|
60
|
+
});
|
|
61
|
+
describe('condensed', function () {
|
|
62
|
+
it('should by default not be condensed', function () {
|
|
63
|
+
return expect(wrapper.find('.ffe-table--condensed')).toHaveLength(0);
|
|
64
|
+
});
|
|
65
|
+
var condensedWrapper = render(React.createElement(SortableTable, { condensed: true, columns: columns, data: data }));
|
|
66
|
+
it('can be condensed', function () { return expect(condensedWrapper).toHaveLength(1); });
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -1,32 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
2
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
3
|
+
if (ar || !(i in from)) {
|
|
4
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
5
|
+
ar[i] = from[i];
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
9
|
+
};
|
|
7
10
|
export default (function (columns, data, sortBy, descending) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
var sortedData = __spreadArray([], data, true);
|
|
12
|
+
var compare = function (data1, data2) {
|
|
13
|
+
if (!isNaN(data1[sortBy])) {
|
|
14
|
+
return data1[sortBy] - data2[sortBy];
|
|
15
|
+
}
|
|
16
|
+
if (!data1[sortBy]) {
|
|
17
|
+
return data2[sortBy] ? 1 : 0;
|
|
18
|
+
}
|
|
19
|
+
return data1[sortBy].localeCompare(data2[sortBy]);
|
|
20
|
+
};
|
|
21
|
+
var column = columns.find(function (col) { return col.key === sortBy; });
|
|
22
|
+
var customCompare = column && typeof column.compare === 'function' ? column.compare : null;
|
|
23
|
+
if (customCompare) {
|
|
24
|
+
compare = function (data1, data2) { return customCompare(data1[sortBy], data2[sortBy]); };
|
|
12
25
|
}
|
|
13
|
-
|
|
14
|
-
|
|
26
|
+
sortedData.sort(compare);
|
|
27
|
+
if (descending) {
|
|
28
|
+
sortedData.reverse();
|
|
15
29
|
}
|
|
16
|
-
return
|
|
17
|
-
|
|
18
|
-
var column = columns.find(function (col) {
|
|
19
|
-
return col.key === sortBy;
|
|
20
|
-
});
|
|
21
|
-
var customCompare = column && typeof column.compare === 'function' ? column.compare : null;
|
|
22
|
-
if (customCompare) {
|
|
23
|
-
compare = function compare(data1, data2) {
|
|
24
|
-
return customCompare(data1[sortBy], data2[sortBy]);
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
sortedData.sort(compare);
|
|
28
|
-
if (descending) {
|
|
29
|
-
sortedData.reverse();
|
|
30
|
-
}
|
|
31
|
-
return sortedData;
|
|
32
|
-
});
|
|
30
|
+
return sortedData;
|
|
31
|
+
});
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import sortData from './sort-data';
|
|
2
|
+
var columns = [
|
|
3
|
+
{ key: 'name', header: 'Name' },
|
|
4
|
+
{ key: 'age', header: 'Age' },
|
|
5
|
+
{ key: 'id', header: 'Employee-id' },
|
|
6
|
+
{
|
|
7
|
+
key: 'rand',
|
|
8
|
+
header: 'A random number with thousand separator needs a custom compare',
|
|
9
|
+
compare: function (a, b) { return a.replace(/\s/g, '') - b.replace(/\s/g, ''); },
|
|
10
|
+
},
|
|
11
|
+
];
|
|
12
|
+
var data = [
|
|
13
|
+
{ name: 'Zombie Mountain', age: 36, id: 2, rand: '69 873' },
|
|
14
|
+
{ name: 'Daenerys Targaryen', age: 16, id: 4, rand: '9 156' },
|
|
15
|
+
{ name: 'Ned Stark', age: 48, id: 3, rand: '3 863 914' },
|
|
16
|
+
{ name: 'Jon Snow', age: 20, id: 1, rand: '145 678' },
|
|
17
|
+
];
|
|
18
|
+
describe('sortData', function () {
|
|
19
|
+
it('should sort data in descending order', function () {
|
|
20
|
+
var ascendingTableData = sortData(columns, data, 'age', true);
|
|
21
|
+
for (var i = 1; i > ascendingTableData.length; i++) {
|
|
22
|
+
expect(ascendingTableData[i].id).toBeLessThan(ascendingTableData[i - 1].id);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
it('should sort data in ascending order', function () {
|
|
26
|
+
var ascendingTableData = sortData(columns, data, 'age', false);
|
|
27
|
+
for (var i = 1; i > ascendingTableData.length; i++) {
|
|
28
|
+
expect(ascendingTableData[i].id).toBeGreaterThan(ascendingTableData[i - 1].id);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
it('should sort data based on `sortBy` parameter', function () {
|
|
32
|
+
var tableData = sortData(columns, data, 'age', true);
|
|
33
|
+
expect(tableData[0].age).toBe(48);
|
|
34
|
+
expect(tableData[1].age).toBe(36);
|
|
35
|
+
expect(tableData[2].age).toBe(20);
|
|
36
|
+
expect(tableData[3].age).toBe(16);
|
|
37
|
+
});
|
|
38
|
+
it('should sort data even though an attribute is not set', function () {
|
|
39
|
+
data[3] = { name: 'Jon Snow', id: 1, rand: '145 678' }; // no age key
|
|
40
|
+
var tableData = sortData(columns, data, 'age', false);
|
|
41
|
+
expect(tableData[0].age).toBe(16);
|
|
42
|
+
expect(tableData[1].age).toBe(36);
|
|
43
|
+
expect(tableData[2].age).toBe(48);
|
|
44
|
+
expect(tableData[3].age).toBe(undefined);
|
|
45
|
+
});
|
|
46
|
+
it('should sort data with a custom compare function when provided', function () {
|
|
47
|
+
var tableData = sortData(columns, data, 'rand', false);
|
|
48
|
+
expect(tableData[0].rand).toBe('9 156');
|
|
49
|
+
expect(tableData[1].rand).toBe('69 873');
|
|
50
|
+
expect(tableData[2].rand).toBe('145 678');
|
|
51
|
+
expect(tableData[3].rand).toBe('3 863 914');
|
|
52
|
+
});
|
|
53
|
+
});
|