1 | import React, { useCallback, useState } from 'react';
|
2 | import JSONArrow from './JSONArrow.js';
|
3 | import getCollectionEntries from './getCollectionEntries.js';
|
4 | import JSONNode from './JSONNode.js';
|
5 | import ItemRange from './ItemRange.js';
|
6 | function isRange(rangeOrEntry) {
|
7 | return rangeOrEntry.to !== undefined;
|
8 | }
|
9 | function renderChildNodes(props, from, to) {
|
10 | const { nodeType, data, collectionLimit, circularCache, keyPath, postprocessValue, sortObjectKeys, } = props;
|
11 | const childNodes = [];
|
12 | getCollectionEntries(nodeType, data, sortObjectKeys, collectionLimit, from, to).forEach((entry) => {
|
13 | if (isRange(entry)) {
|
14 | childNodes.push(React.createElement(ItemRange, { ...props, key: `ItemRange--${entry.from}-${entry.to}`, from: entry.from, to: entry.to, renderChildNodes: renderChildNodes }));
|
15 | }
|
16 | else {
|
17 | const { key, value } = entry;
|
18 | const isCircular = circularCache.indexOf(value) !== -1;
|
19 | childNodes.push(React.createElement(JSONNode, { ...props, postprocessValue, collectionLimit, key: `Node--${key}`, keyPath: [key, ...keyPath], value: postprocessValue(value), circularCache: [...circularCache, value], isCircular: isCircular, hideRoot: false }));
|
20 | }
|
21 | });
|
22 | return childNodes;
|
23 | }
|
24 | export default function JSONNestedNode(props) {
|
25 | const { circularCache = [], collectionLimit, createItemString, data, expandable, getItemString, hideRoot, isCircular, keyPath, labelRenderer, level = 0, nodeType, nodeTypeIndicator, shouldExpandNodeInitially, styling, } = props;
|
26 | const [expanded, setExpanded] = useState(
|
27 |
|
28 | isCircular ? false : shouldExpandNodeInitially(keyPath, data, level));
|
29 | const handleClick = useCallback(() => {
|
30 | if (expandable)
|
31 | setExpanded(!expanded);
|
32 | }, [expandable, expanded]);
|
33 | const renderedChildren = expanded || (hideRoot && level === 0)
|
34 | ? renderChildNodes({ ...props, circularCache, level: level + 1 })
|
35 | : null;
|
36 | const itemType = (React.createElement("span", { ...styling('nestedNodeItemType', expanded) }, nodeTypeIndicator));
|
37 | const renderedItemString = getItemString(nodeType, data, itemType, createItemString(data, collectionLimit), keyPath);
|
38 | const stylingArgs = [keyPath, nodeType, expanded, expandable];
|
39 | return hideRoot ? (React.createElement("li", { ...styling('rootNode', ...stylingArgs) },
|
40 | React.createElement("ul", { ...styling('rootNodeChildren', ...stylingArgs) }, renderedChildren))) : (React.createElement("li", { ...styling('nestedNode', ...stylingArgs) },
|
41 | expandable && (React.createElement(JSONArrow, { styling: styling, nodeType: nodeType, expanded: expanded, onClick: handleClick })),
|
42 | React.createElement("label", { ...styling(['label', 'nestedNodeLabel'], ...stylingArgs), onClick: handleClick }, labelRenderer(...stylingArgs)),
|
43 | React.createElement("span", { ...styling('nestedNodeItemString', ...stylingArgs), onClick: handleClick }, renderedItemString),
|
44 | React.createElement("ul", { ...styling('nestedNodeChildren', ...stylingArgs) }, renderedChildren)));
|
45 | }
|