// @flow
import React from 'react';
import { getMetaValueLabel } from './utils';
import { meta } from '../../hydration';
import styles from './InspectedElementTree.css';
type Props = {|
data: Object | null,
label: string,
showWhenEmpty?: boolean,
|};
export default function InspectedElementTree({
data,
label,
showWhenEmpty = false,
}: Props) {
const isEmpty = data === null || Object.keys(data).length === 0;
if (isEmpty && !showWhenEmpty) {
return null;
} else {
// TODO Add click and key handlers for toggling element open/close state.
// TODO Support editable props
return (
{label}
{isEmpty &&
None
}
{!isEmpty &&
Object.keys((data: any)).map(name => (
))}
);
}
}
type KeyValueProps = {|
depth: number,
name: string,
value: any,
|};
function KeyValue({ depth, name, value }: KeyValueProps) {
const dataType = typeof value;
const isSimpleType =
dataType === 'number' ||
dataType === 'string' ||
dataType === 'boolean' ||
value == null;
const paddingLeft = `${depth * 0.75}rem`;
let children = null;
if (isSimpleType) {
let displayValue = value;
if (dataType === 'string') {
displayValue = `"${value}"`;
} else if (dataType === 'boolean') {
displayValue = value ? 'true' : 'false';
}
children = (
{name}:{' '}
{displayValue}
);
} else if (value.hasOwnProperty(meta.type)) {
children = (
{name}:{' '}
{getMetaValueLabel(value)}
);
} else {
if (Array.isArray(value)) {
children = value.map((innerValue, index) => (
));
children.unshift(
{name}: Array
);
} else {
children = Object.entries(value).map(([name, value]) => (
));
children.unshift(
{name}: Object
);
}
}
return children;
}