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

View File

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

View File

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

View File

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

View File

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

View File

@ -8,10 +8,9 @@ import type { CommonInternalProps } from './types';
interface Props extends CommonInternalProps { interface Props extends CommonInternalProps {
value: any; value: any;
isCustomNode: (value: any) => boolean;
} }
const JSONNode: React.FunctionComponent<Props> = ({ export default function JSONNode({
getItemString, getItemString,
keyPath, keyPath,
labelRenderer, labelRenderer,
@ -20,7 +19,7 @@ const JSONNode: React.FunctionComponent<Props> = ({
valueRenderer, valueRenderer,
isCustomNode, isCustomNode,
...rest ...rest
}) => { }: Props) {
const nodeType = isCustomNode(value) ? 'Custom' : objType(value); const nodeType = isCustomNode(value) ? 'Custom' : objType(value);
const simpleNodeProps = { 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 // Configures <JSONNestedNode> to render an Object
const JSONObjectNode: React.FunctionComponent<Props> = ({ data, ...props }) => ( export default function JSONObjectNode({ data, ...props }: Props) {
return (
<JSONNestedNode <JSONNestedNode
{...props} {...props}
data={data} data={data}
@ -24,6 +25,5 @@ const JSONObjectNode: React.FunctionComponent<Props> = ({ data, ...props }) => (
createItemString={createItemString} createItemString={createItemString}
expandable={Object.getOwnPropertyNames(data).length > 0} expandable={Object.getOwnPropertyNames(data).length > 0}
/> />
); );
}
export default JSONObjectNode;

View File

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

View File

@ -1,11 +1,12 @@
import type { CurriedFunction1 } from 'lodash'; import type { CurriedFunction1 } from 'lodash';
import { import { createStyling } from 'react-base16-styling';
import type {
Base16Theme, Base16Theme,
createStyling,
StylingConfig, StylingConfig,
StylingFunction,
Theme,
} from 'react-base16-styling'; } from 'react-base16-styling';
import solarized from './themes/solarized'; import solarized from './themes/solarized';
import { StylingFunction, Theme } from 'react-base16-styling/src';
const colorMap = (theme: Base16Theme) => ({ const colorMap = (theme: Base16Theme) => ({
BACKGROUND_COLOR: theme.base00, 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 { export interface CommonInternalProps extends CommonExternalProps {
styling: StylingFunction; styling: StylingFunction;
circularCache?: CircularCache; circularCache?: CircularCache;
isCircular?: boolean;
level?: number; level?: number;
isCircular?: boolean;
} }