UNPKG

8.62 kBJavaScriptView Raw
1import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
2import _createClass from "@babel/runtime/helpers/createClass";
3import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
4import _inherits from "@babel/runtime/helpers/inherits";
5import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
6import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
7import _defineProperty from "@babel/runtime/helpers/defineProperty";
8import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
9import _extends from "@babel/runtime/helpers/extends";
10
11function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
12
13function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
14
15function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
16
17function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
18
19import React from 'react';
20import PropTypes from 'prop-types';
21import JSONArrow from './JSONArrow';
22import getCollectionEntries from './getCollectionEntries';
23import JSONNode from './JSONNode';
24import ItemRange from './ItemRange';
25
26function isRange(rangeOrEntry) {
27 return rangeOrEntry.to !== undefined;
28}
29
30function renderChildNodes(props, from, to) {
31 var nodeType = props.nodeType,
32 data = props.data,
33 collectionLimit = props.collectionLimit,
34 circularCache = props.circularCache,
35 keyPath = props.keyPath,
36 postprocessValue = props.postprocessValue,
37 sortObjectKeys = props.sortObjectKeys;
38 var childNodes = [];
39 getCollectionEntries(nodeType, data, sortObjectKeys, collectionLimit, from, to).forEach(function (entry) {
40 if (isRange(entry)) {
41 childNodes.push( /*#__PURE__*/React.createElement(ItemRange, _extends({}, props, {
42 key: "ItemRange--".concat(entry.from, "-").concat(entry.to),
43 from: entry.from,
44 to: entry.to,
45 renderChildNodes: renderChildNodes
46 })));
47 } else {
48 var key = entry.key,
49 value = entry.value;
50 var isCircular = circularCache.indexOf(value) !== -1;
51 childNodes.push( /*#__PURE__*/React.createElement(JSONNode, _extends({}, props, {
52 postprocessValue: postprocessValue,
53 collectionLimit: collectionLimit,
54 key: "Node--".concat(key),
55 keyPath: [key].concat(_toConsumableArray(keyPath)),
56 value: postprocessValue(value),
57 circularCache: [].concat(_toConsumableArray(circularCache), [value]),
58 isCircular: isCircular,
59 hideRoot: false
60 })));
61 }
62 });
63 return childNodes;
64}
65
66function getStateFromProps(props) {
67 // calculate individual node expansion if necessary
68 var expanded = !props.isCircular ? props.shouldExpandNode(props.keyPath, props.data, props.level) : false;
69 return {
70 expanded: expanded
71 };
72}
73
74var JSONNestedNode = /*#__PURE__*/function (_React$Component) {
75 _inherits(JSONNestedNode, _React$Component);
76
77 var _super = _createSuper(JSONNestedNode);
78
79 function JSONNestedNode(props) {
80 var _this;
81
82 _classCallCheck(this, JSONNestedNode);
83
84 _this = _super.call(this, props);
85
86 _defineProperty(_assertThisInitialized(_this), "handleClick", function () {
87 if (_this.props.expandable) {
88 _this.setState({
89 expanded: !_this.state.expanded
90 });
91 }
92 });
93
94 _this.state = getStateFromProps(props);
95 return _this;
96 }
97
98 _createClass(JSONNestedNode, [{
99 key: "UNSAFE_componentWillReceiveProps",
100 value: function UNSAFE_componentWillReceiveProps(nextProps) {
101 var nextState = getStateFromProps(nextProps);
102
103 if (getStateFromProps(this.props).expanded !== nextState.expanded) {
104 this.setState(nextState);
105 }
106 }
107 }, {
108 key: "shouldComponentUpdate",
109 value: function shouldComponentUpdate(nextProps, nextState) {
110 var _this2 = this;
111
112 return !!Object.keys(nextProps).find(function (key) {
113 return key !== 'circularCache' && (key === 'keyPath' ? nextProps[key].join('/') !== _this2.props[key].join('/') : nextProps[key] !== _this2.props[key]);
114 }) || nextState.expanded !== this.state.expanded;
115 }
116 }, {
117 key: "render",
118 value: function render() {
119 var _this$props = this.props,
120 getItemString = _this$props.getItemString,
121 nodeTypeIndicator = _this$props.nodeTypeIndicator,
122 nodeType = _this$props.nodeType,
123 data = _this$props.data,
124 hideRoot = _this$props.hideRoot,
125 createItemString = _this$props.createItemString,
126 styling = _this$props.styling,
127 collectionLimit = _this$props.collectionLimit,
128 keyPath = _this$props.keyPath,
129 labelRenderer = _this$props.labelRenderer,
130 expandable = _this$props.expandable;
131 var expanded = this.state.expanded;
132 var renderedChildren = expanded || hideRoot && this.props.level === 0 ? renderChildNodes(_objectSpread(_objectSpread({}, this.props), {}, {
133 level: this.props.level + 1
134 })) : null;
135 var itemType = /*#__PURE__*/React.createElement("span", styling('nestedNodeItemType', expanded), nodeTypeIndicator);
136 var renderedItemString = getItemString(nodeType, data, itemType, createItemString(data, collectionLimit), keyPath);
137 var stylingArgs = [keyPath, nodeType, expanded, expandable];
138 return hideRoot ? /*#__PURE__*/React.createElement("li", styling.apply(void 0, ['rootNode'].concat(stylingArgs)), /*#__PURE__*/React.createElement("ul", styling.apply(void 0, ['rootNodeChildren'].concat(stylingArgs)), renderedChildren)) : /*#__PURE__*/React.createElement("li", styling.apply(void 0, ['nestedNode'].concat(stylingArgs)), expandable && /*#__PURE__*/React.createElement(JSONArrow, {
139 styling: styling,
140 nodeType: nodeType,
141 expanded: expanded,
142 onClick: this.handleClick
143 }), /*#__PURE__*/React.createElement("label", _extends({}, styling.apply(void 0, [['label', 'nestedNodeLabel']].concat(stylingArgs)), {
144 onClick: this.handleClick
145 }), labelRenderer.apply(void 0, stylingArgs)), /*#__PURE__*/React.createElement("span", _extends({}, styling.apply(void 0, ['nestedNodeItemString'].concat(stylingArgs)), {
146 onClick: this.handleClick
147 }), renderedItemString), /*#__PURE__*/React.createElement("ul", styling.apply(void 0, ['nestedNodeChildren'].concat(stylingArgs)), renderedChildren));
148 }
149 }]);
150
151 return JSONNestedNode;
152}(React.Component);
153
154_defineProperty(JSONNestedNode, "propTypes", {
155 getItemString: PropTypes.func.isRequired,
156 nodeTypeIndicator: PropTypes.any,
157 nodeType: PropTypes.string.isRequired,
158 data: PropTypes.any,
159 hideRoot: PropTypes.bool.isRequired,
160 createItemString: PropTypes.func.isRequired,
161 styling: PropTypes.func.isRequired,
162 collectionLimit: PropTypes.number,
163 keyPath: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number])).isRequired,
164 labelRenderer: PropTypes.func.isRequired,
165 shouldExpandNode: PropTypes.func,
166 level: PropTypes.number.isRequired,
167 sortObjectKeys: PropTypes.oneOfType([PropTypes.func, PropTypes.bool]),
168 isCircular: PropTypes.bool,
169 expandable: PropTypes.bool
170});
171
172_defineProperty(JSONNestedNode, "defaultProps", {
173 data: [],
174 circularCache: [],
175 level: 0,
176 expandable: true
177});
178
179export { JSONNestedNode as default };
\No newline at end of file