/* eslint-disable react/prop-types */
import { isFinite, isString, isArray, isObject, keys, map } from "lodash";
import React, { useState } from "react";
import cx from "classnames";
import PropTypes from "prop-types";
import "./json-view-interactive.less";
function JsonBlock({ value, children, openingBrace, closingBrace, withKeys }) {
const [isExpanded, setIsExpanded] = useState(false);
const objectKeys = keys(value);
const count = objectKeys.length;
return (
{count > 0 && (
setIsExpanded(!isExpanded)}>
)}
{openingBrace}
{!isExpanded && count > 0 && (
setIsExpanded(true)}>
…
)}
{isExpanded && (
{map(objectKeys, (key, index) => {
const isFirst = index === 0;
const isLast = index === count - 1;
const comma = isLast ? null : ,;
return (
{withKeys && (
:
)}
{comma}
);
})}
)}
{closingBrace}
{children}
{!isExpanded && {" // " + count + " " + (count === 1 ? "item" : "items")}}
);
}
function JsonValue({ value, children }) {
if (value === null || value === false || value === true || isFinite(value)) {
return (
{"" + value}
{children}
);
}
if (isString(value)) {
return (
"
{value}
"
{children}
);
}
if (isArray(value)) {
return (
{children}
);
}
if (isObject(value)) {
return (
{children}
);
}
return null;
}
export default function JsonViewInteractive({ value }) {
return (
);
}
JsonViewInteractive.propTypes = {
value: PropTypes.any, // eslint-disable-line react/forbid-prop-types
};
JsonViewInteractive.defaultProps = {
// `null` will be rendered as "null" because it is a valid JSON value, so use `undefined` for no value
value: undefined,
};