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;
}
const [expanded, setExpanded] = useState<boolean>(false);
export default class ItemRange extends React.Component<Props, State> { const handleClick = useCallback(() => {
constructor(props: Props) { setExpanded(!expanded);
super(props); }, [expanded]);
this.state = { expanded: false };
} return expanded ? (
<div {...styling('itemRange', expanded)}>
render() { {renderChildNodes(props, from, to)}
const { styling, from, to, renderChildNodes, nodeType } = this.props; </div>
) : (
return this.state.expanded ? ( <div {...styling('itemRange', expanded)} onClick={handleClick}>
<div {...styling('itemRange', this.state.expanded)}> <JSONArrow
{renderChildNodes(this.props, from, to)} nodeType={nodeType}
</div> styling={styling}
) : ( expanded={false}
<div onClick={handleClick}
{...styling('itemRange', this.state.expanded)} arrowStyle="double"
onClick={this.handleClick} />
> {`${from} ... ${to}`}
<JSONArrow </div>
nodeType={nodeType} );
styling={styling}
expanded={false}
onClick={this.handleClick}
arrowStyle="double"
/>
{`${from} ... ${to}`}
</div>
);
}
handleClick = () => {
this.setState({ expanded: !this.state.expanded });
};
} }

View File

@ -16,15 +16,15 @@ 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) {
<JSONNestedNode return (
{...props} <JSONNestedNode
data={data} {...props}
nodeType="Array" data={data}
nodeTypeIndicator="[]" nodeType="Array"
createItemString={createItemString} nodeTypeIndicator="[]"
expandable={data.length > 0} createItemString={createItemString}
/> expandable={data.length > 0}
); />
);
export default JSONArrayNode; }

View File

@ -9,21 +9,21 @@ 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) {
<div {...styling('arrowContainer', arrowStyle)} onClick={onClick}> return (
<div {...styling(['arrow', 'arrowSign'], nodeType, expanded, arrowStyle)}> <div {...styling('arrowContainer', arrowStyle)} onClick={onClick}>
{'\u25B6'} <div {...styling(['arrow', 'arrowSign'], nodeType, expanded, arrowStyle)}>
{arrowStyle === 'double' && ( {'\u25B6'}
<div {...styling(['arrowSign', 'arrowSignInner'])}>{'\u25B6'}</div> {arrowStyle === 'double' && (
)} <div {...styling(['arrowSign', 'arrowSignInner'])}>{'\u25B6'}</div>
)}
</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,15 +15,15 @@ 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) {
<JSONNestedNode return (
{...props} <JSONNestedNode
data={data} {...props}
nodeType="Object" data={data}
nodeTypeIndicator={props.nodeType === 'Error' ? 'Error()' : '{}'} nodeType="Object"
createItemString={createItemString} nodeTypeIndicator={props.nodeType === 'Error' ? 'Error()' : '{}'}
expandable={Object.getOwnPropertyNames(data).length > 0} createItemString={createItemString}
/> 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,15 +32,15 @@ const JSONValueNode: React.FunctionComponent<Props> = ({
valueRenderer, valueRenderer,
value, value,
valueGetter = (value) => value, valueGetter = (value) => value,
}) => ( }: Props) {
<li {...styling('value', nodeType, keyPath)}> return (
<label {...styling(['label', 'valueLabel'], nodeType, keyPath)}> <li {...styling('value', nodeType, keyPath)}>
{labelRenderer(keyPath, nodeType, false, false)} <label {...styling(['label', 'valueLabel'], nodeType, keyPath)}>
</label> {labelRenderer(keyPath, nodeType, false, false)}
<span {...styling('valueText', nodeType, keyPath)}> </label>
{valueRenderer(valueGetter(value), value, ...keyPath)} <span {...styling('valueText', nodeType, keyPath)}>
</span> {valueRenderer(valueGetter(value), value, ...keyPath)}
</li> </span>
); </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;
} }