Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { JSONSchemaLevelContext, JSONSchemaPathContext } from "../../context"
import { isEmptyObject, isEmptyArray } from "../../fn"

const JSONViewer = ({ name, value, className }) => {
const JSONViewer = ({ name, value, className, hideArrayIndices = false }) => {
const fn = useFn()
const { path } = usePath(name)
const { isExpanded, setExpanded, setCollapsed } = useIsExpanded(name)
Expand Down Expand Up @@ -55,13 +55,25 @@ const JSONViewer = ({ name, value, className }) => {
/**
* Rendering.
*/
const isArrayIndex = typeof name === "string" && name.startsWith("#")
const shouldHideName = hideArrayIndices && isArrayIndex

if (isPrimitive) {
return (
<div className={classNames("json-schema-2020-12-json-viewer", className)}>
<span className="json-schema-2020-12-json-viewer__name json-schema-2020-12-json-viewer__name--secondary">
{name}
</span>
<span className="json-schema-2020-12-json-viewer__value json-schema-2020-12-json-viewer__value--secondary">
{!shouldHideName && (
<span className="json-schema-2020-12-json-viewer__name json-schema-2020-12-json-viewer__name--secondary">
{name}
</span>
)}
<span
className={classNames(
"json-schema-2020-12-json-viewer__value json-schema-2020-12-json-viewer__value--secondary",
{
"json-schema-2020-12-json-viewer__value--no-name": shouldHideName,
}
)}
>
{fn.stringify(value)}
</span>
</div>
Expand All @@ -71,9 +83,11 @@ const JSONViewer = ({ name, value, className }) => {
if (isEmpty) {
return (
<div className={classNames("json-schema-2020-12-json-viewer", className)}>
<span className="json-schema-2020-12-json-viewer__name json-schema-2020-12-json-viewer__name--secondary">
{name}
</span>
{!shouldHideName && (
<span className="json-schema-2020-12-json-viewer__name json-schema-2020-12-json-viewer__name--secondary">
{name}
</span>
)}
<strong className="json-schema-2020-12__attribute json-schema-2020-12__attribute--primary">
{Array.isArray(value) ? "empty array" : "empty object"}
</strong>
Expand All @@ -89,9 +103,11 @@ const JSONViewer = ({ name, value, className }) => {
data-json-schema-level={level}
>
<Accordion expanded={isExpanded} onChange={handleExpansion}>
<span className="json-schema-2020-12-json-viewer__name json-schema-2020-12-json-viewer__name--secondary">
{name}
</span>
{!shouldHideName && (
<span className="json-schema-2020-12-json-viewer__name json-schema-2020-12-json-viewer__name--secondary">
{name}
</span>
)}
</Accordion>
<ExpandDeepButton
expanded={isExpanded}
Expand All @@ -118,6 +134,7 @@ const JSONViewer = ({ name, value, className }) => {
name={`#${index}`}
value={item}
className={className}
hideArrayIndices={hideArrayIndices}
/>
</li>
))
Expand All @@ -131,6 +148,7 @@ const JSONViewer = ({ name, value, className }) => {
name={propertyName}
value={propertyValue}
className={className}
hideArrayIndices={false}
/>
</li>
)
Expand All @@ -148,6 +166,7 @@ JSONViewer.propTypes = {
name: PropTypes.string.isRequired,
value: PropTypes.any.isRequired,
className: PropTypes.string,
hideArrayIndices: PropTypes.bool,
}

export default JSONViewer
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@
+ .json-schema-2020-12-json-viewer__value--secondary::before {
content: "=";
}

.json-schema-2020-12-json-viewer__value--no-name::before {
content: none !important;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const Enum = ({ schema }) => {
name="Enum"
value={schema.enum}
className="json-schema-2020-12-keyword json-schema-2020-12-keyword--enum"
hideArrayIndices={true}
/>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const Examples = ({ schema }) => {
name="Examples"
value={schema.examples}
className="json-schema-2020-12-keyword json-schema-2020-12-keyword--examples"
hideArrayIndices={true}
/>
)
}
Expand Down