// @flow import React from 'react'; import EditableValue from './EditableValue'; import { getMetaValueLabel } from '../utils'; import { meta } from '../../../hydration'; import styles from './KeyValue.css'; type OverrideValueFn = (path: Array, value: any) => void; type KeyValueProps = {| depth: number, name: string, overrideValueFn?: ?OverrideValueFn, path?: Array, value: any, |}; export default function KeyValue({ depth, name, overrideValueFn, path = [], 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'; } else if (value === null) { displayValue = 'null'; } else if (value === undefined) { displayValue = 'undefined'; } const nameClassName = typeof overrideValueFn === 'function' ? styles.EditableName : styles.Name; children = (
{name} {typeof overrideValueFn === 'function' ? ( ) : ( {displayValue} )}
); } else if (value.hasOwnProperty(meta.type)) { // TODO Is this type even necessary? Can we just drop it? children = (
{name} {getMetaValueLabel(value)}
); } else { if (Array.isArray(value)) { children = value.map((innerValue, index) => ( )); children.unshift(
{name} Array
); } else { // $FlowFixMe "Missing type annotation for U" whatever that means children = Object.entries(value).map(([name, value]) => ( )); children.unshift(
{name} Object
); } } return children; }