This commit is contained in:
Nathan Bierema 2023-01-03 09:17:23 -05:00
parent 5175bc0a00
commit 4c60c7d75a
11 changed files with 87 additions and 102 deletions

View File

@ -1,4 +1,4 @@
import React from 'react';
import React, { useCallback, useState } from 'react';
import JSONArrow from './JSONArrow';
import type { CircularCache, CommonInternalProps } from './types';
@ -9,43 +9,31 @@ interface Props extends CommonInternalProps {
to: number;
renderChildNodes: (props: Props, from: number, to: number) => React.ReactNode;
circularCache: CircularCache;
level: number;
}
interface State {
expanded: boolean;
}
export default function ItemRange(props: Props) {
const { styling, from, to, renderChildNodes, nodeType } = props;
export default class ItemRange extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = { expanded: false };
}
const [expanded, setExpanded] = useState<boolean>(false);
const handleClick = useCallback(() => {
setExpanded(!expanded);
}, [expanded]);
render() {
const { styling, from, to, renderChildNodes, nodeType } = this.props;
return this.state.expanded ? (
<div {...styling('itemRange', this.state.expanded)}>
{renderChildNodes(this.props, from, to)}
return expanded ? (
<div {...styling('itemRange', expanded)}>
{renderChildNodes(props, from, to)}
</div>
) : (
<div
{...styling('itemRange', this.state.expanded)}
onClick={this.handleClick}
>
<div {...styling('itemRange', expanded)} onClick={handleClick}>
<JSONArrow
nodeType={nodeType}
styling={styling}
expanded={false}
onClick={this.handleClick}
onClick={handleClick}
arrowStyle="double"
/>
{`${from} ... ${to}`}
</div>
);
}
handleClick = () => {
this.setState({ expanded: !this.state.expanded });
};
}

View File

@ -16,7 +16,8 @@ interface Props extends CommonInternalProps {
}
// Configures <JSONNestedNode> to render an Array
const JSONArrayNode: React.FunctionComponent<Props> = ({ data, ...props }) => (
export default function JSONArrayNode({ data, ...props }: Props) {
return (
<JSONNestedNode
{...props}
data={data}
@ -25,6 +26,5 @@ const JSONArrayNode: React.FunctionComponent<Props> = ({ data, ...props }) => (
createItemString={createItemString}
expandable={data.length > 0}
/>
);
export default JSONArrayNode;
);
}

View File

@ -9,13 +9,14 @@ interface Props {
onClick: React.MouseEventHandler<HTMLDivElement>;
}
const JSONArrow: React.FunctionComponent<Props> = ({
export default function JSONArrow({
styling,
arrowStyle = 'single',
expanded,
nodeType,
onClick,
}) => (
}: Props) {
return (
<div {...styling('arrowContainer', arrowStyle)} onClick={onClick}>
<div {...styling(['arrow', 'arrowSign'], nodeType, expanded, arrowStyle)}>
{'\u25B6'}
@ -24,6 +25,5 @@ const JSONArrow: React.FunctionComponent<Props> = ({
)}
</div>
</div>
);
export default JSONArrow;
);
}

View File

@ -28,7 +28,7 @@ interface Props extends CommonInternalProps {
}
// Configures <JSONNestedNode> to render an iterable
const JSONIterableNode: React.FunctionComponent<Props> = ({ ...props }) => {
export default function JSONIterableNode(props: Props) {
return (
<JSONNestedNode
{...props}
@ -38,6 +38,4 @@ const JSONIterableNode: React.FunctionComponent<Props> = ({ ...props }) => {
expandable
/>
);
};
export default JSONIterableNode;
}

View File

@ -13,6 +13,7 @@ export interface RenderChildNodesProps extends CommonInternalProps {
data: any;
nodeType: string;
circularCache: CircularCache;
level: number;
}
interface Range {

View File

@ -8,10 +8,9 @@ import type { CommonInternalProps } from './types';
interface Props extends CommonInternalProps {
value: any;
isCustomNode: (value: any) => boolean;
}
const JSONNode: React.FunctionComponent<Props> = ({
export default function JSONNode({
getItemString,
keyPath,
labelRenderer,
@ -20,7 +19,7 @@ const JSONNode: React.FunctionComponent<Props> = ({
valueRenderer,
isCustomNode,
...rest
}) => {
}: Props) {
const nodeType = isCustomNode(value) ? 'Custom' : objType(value);
const simpleNodeProps = {
@ -100,6 +99,4 @@ const JSONNode: React.FunctionComponent<Props> = ({
/>
);
}
};
export default JSONNode;
}

View File

@ -15,7 +15,8 @@ interface Props extends CommonInternalProps {
}
// Configures <JSONNestedNode> to render an Object
const JSONObjectNode: React.FunctionComponent<Props> = ({ data, ...props }) => (
export default function JSONObjectNode({ data, ...props }: Props) {
return (
<JSONNestedNode
{...props}
data={data}
@ -24,6 +25,5 @@ const JSONObjectNode: React.FunctionComponent<Props> = ({ data, ...props }) => (
createItemString={createItemString}
expandable={Object.getOwnPropertyNames(data).length > 0}
/>
);
export default JSONObjectNode;
);
}

View File

@ -24,7 +24,7 @@ interface Props {
valueGetter?: (value: any) => any;
}
const JSONValueNode: React.FunctionComponent<Props> = ({
export default function JSONValueNode({
nodeType,
styling,
labelRenderer,
@ -32,7 +32,8 @@ const JSONValueNode: React.FunctionComponent<Props> = ({
valueRenderer,
value,
valueGetter = (value) => value,
}) => (
}: Props) {
return (
<li {...styling('value', nodeType, keyPath)}>
<label {...styling(['label', 'valueLabel'], nodeType, keyPath)}>
{labelRenderer(keyPath, nodeType, false, false)}
@ -41,6 +42,5 @@ const JSONValueNode: React.FunctionComponent<Props> = ({
{valueRenderer(valueGetter(value), value, ...keyPath)}
</span>
</li>
);
export default JSONValueNode;
);
}

View File

@ -1,11 +1,12 @@
import type { CurriedFunction1 } from 'lodash';
import {
import { createStyling } from 'react-base16-styling';
import type {
Base16Theme,
createStyling,
StylingConfig,
StylingFunction,
Theme,
} from 'react-base16-styling';
import solarized from './themes/solarized';
import { StylingFunction, Theme } from 'react-base16-styling/src';
const colorMap = (theme: Base16Theme) => ({
BACKGROUND_COLOR: theme.base00,

View File

@ -74,4 +74,4 @@ export function JSONTree({
);
}
export { StylingValue };
export type { StylingValue };

View File

@ -58,6 +58,6 @@ export interface CommonExternalProps {
export interface CommonInternalProps extends CommonExternalProps {
styling: StylingFunction;
circularCache?: CircularCache;
isCircular?: boolean;
level?: number;
isCircular?: boolean;
}