UNPKG

2.96 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
8
9var _react = require('react');
10
11var _react2 = _interopRequireDefault(_react);
12
13var _getLevel = require('./get-level');
14
15var _getLevel2 = _interopRequireDefault(_getLevel);
16
17var _hasChildren = require('./has-children');
18
19var _hasChildren2 = _interopRequireDefault(_hasChildren);
20
21function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
23function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
24
25var toggleChildren = function toggleChildren() {
26 var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
27 getRows = _ref.getRows,
28 getShowingChildren = _ref.getShowingChildren,
29 toggleShowingChildren = _ref.toggleShowingChildren,
30 props = _ref.props,
31 idField = _ref.idField,
32 parentField = _ref.parentField;
33
34 if (!getRows) {
35 throw new Error('tree.toggleChildren - Missing getRows!');
36 }
37
38 if (!getShowingChildren) {
39 throw new Error('tree.toggleChildren - Missing getShowingChildren!');
40 }
41
42 if (!toggleShowingChildren) {
43 throw new Error('tree.toggleChildren - Missing toggleShowingChildren!');
44 }
45
46 var toggle = function toggle(e, index) {
47 e.stopPropagation();
48 e.preventDefault();
49
50 toggleShowingChildren(index);
51 };
52
53 return function (value, extra) {
54 var _ref2 = props || {},
55 className = _ref2.className,
56 restProps = _objectWithoutProperties(_ref2, ['className']); // eslint-disable-line react/prop-types
57
58
59 var rowData = extra.rowData;
60
61 var rows = getRows();
62 var showingChildren = getShowingChildren(extra);
63 var index = rowData._index;
64 var containsChildren = (0, _hasChildren2.default)({ index: index, idField: idField })(rows) ? 'has-children' : '';
65 var level = (0, _getLevel2.default)({ index: index, parentField: parentField })(rows);
66 var hasParent = level > 0 ? 'has-parent' : '';
67
68 return _react2.default.createElement(
69 'div',
70 _extends({
71 style: { paddingLeft: level + 'em' },
72 onClick: function onClick(e) {
73 return toggle(e, index);
74 },
75 className: containsChildren + ' ' + hasParent + ' ' + (className || '')
76 }, restProps),
77 containsChildren && _react2.default.createElement('span', {
78 className: showingChildren ? 'show-less' : 'show-more',
79 onClick: function onClick(e) {
80 return toggle(e, index);
81 }
82 }),
83 value
84 );
85 };
86};
87
88exports.default = toggleChildren;
\No newline at end of file