@sb1/ffe-tables-react 8.0.31 → 8.0.33

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.
@@ -1,19 +1,29 @@
1
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
- function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
5
- function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
6
- function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
7
- function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
8
- function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
9
- function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
10
- function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
11
- function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
12
- function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
13
- function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
14
- function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
15
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
16
- function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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 = /*#__PURE__*/function (_Component) {
26
- function SortableTable(props) {
27
- var _this;
28
- _classCallCheck(this, SortableTable);
29
- _this = _callSuper(this, SortableTable, [props]);
30
- _defineProperty(_this, "sort", function (columns, data, sortBy, descending) {
31
- var _this$props$onSort = _this.props.onSort,
32
- onSort = _this$props$onSort === void 0 ? function () {} : _this$props$onSort;
33
- var tableData = sortData(columns, data, sortBy, descending);
34
- onSort({
35
- sortBy: sortBy,
36
- descending: descending,
37
- tableData: tableData
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
- if (column.notSortable) {
101
- return column.header;
102
- }
103
- return /*#__PURE__*/React.createElement("span", {
104
- tabIndex: "0",
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
- key: "render",
123
- value: function render() {
124
- var _this3 = this;
125
- var sortableColumns = this.props.columns.map(function (column) {
126
- return _objectSpread(_objectSpread({}, column), {}, {
127
- ariaSort: _this3.getAriaSort(column),
128
- header: _this3.decorateSortableTableHeader(column)
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
- var caption = this.props.caption;
132
- var _this$props = this.props,
133
- expandedContentMapper = _this$props.expandedContentMapper,
134
- condensed = _this$props.condensed,
135
- columnLayoutMobile = _this$props.columnLayoutMobile,
136
- smallHeader = _this$props.smallHeader,
137
- alignLeft = _this$props.alignLeft,
138
- breakpoint = _this$props.breakpoint,
139
- limit = _this$props.limit,
140
- offset = _this$props.offset,
141
- srOnlyCaption = _this$props.srOnlyCaption,
142
- className = _this$props.className,
143
- rowRender = _this$props.rowRender,
144
- headerRender = _this$props.headerRender,
145
- footerRender = _this$props.footerRender,
146
- columns = _this$props.columns,
147
- data = _this$props.data;
148
- var _this$state = this.state,
149
- sortBy = _this$state.sortBy,
150
- descending = _this$state.descending;
151
- if (srOnlyCaption && sortBy) {
152
- caption = caption.concat(' ', sortBy, ' ', descending ? 'descending' : 'ascending');
153
- }
154
- var tableData = this.sortTableData(columns, data, sortBy, descending);
155
- return /*#__PURE__*/React.createElement(Table, {
156
- caption: caption,
157
- srOnlyCaption: srOnlyCaption,
158
- expandedContentMapper: expandedContentMapper,
159
- columnLayoutMobile: columnLayoutMobile,
160
- columns: sortableColumns,
161
- data: tableData,
162
- condensed: condensed,
163
- smallHeader: smallHeader,
164
- alignLeft: alignLeft,
165
- breakpoint: breakpoint,
166
- limit: limit,
167
- offset: offset,
168
- sort: {
169
- sortBy: sortBy,
170
- descending: descending
171
- },
172
- className: className,
173
- rowRender: rowRender,
174
- headerRender: headerRender,
175
- footerRender: footerRender
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
- caption: PropTypes.node,
182
- srOnlyCaption: PropTypes.bool,
183
- expandedContentMapper: PropTypes.func,
184
- offset: PropTypes.number,
185
- limit: PropTypes.number,
186
- columnLayoutMobile: PropTypes.bool,
187
- condensed: PropTypes.bool,
188
- smallHeader: PropTypes.bool,
189
- alignLeft: PropTypes.bool,
190
- breakpoint: PropTypes.oneOf(['sm', 'none']),
191
- columns: PropTypes.arrayOf(PropTypes.object).isRequired,
192
- data: PropTypes.arrayOf(PropTypes.object).isRequired,
193
- sortBy: PropTypes.string,
194
- descending: PropTypes.bool,
195
- onSort: PropTypes.func,
196
- rowRender: PropTypes.func,
197
- headerRender: PropTypes.func,
198
- footerRender: PropTypes.func,
199
- className: PropTypes.string
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
- function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
2
- function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
3
- function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
4
- function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
5
- function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
6
- function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
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
- var sortedData = _toConsumableArray(data);
9
- var compare = function compare(data1, data2) {
10
- if (!isNaN(data1[sortBy])) {
11
- return data1[sortBy] - data2[sortBy];
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
- if (!data1[sortBy]) {
14
- return data2[sortBy] ? 1 : 0;
26
+ sortedData.sort(compare);
27
+ if (descending) {
28
+ sortedData.reverse();
15
29
  }
16
- return data1[sortBy].localeCompare(data2[sortBy]);
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
+ });