import React from 'react'; import PropTypes from 'prop-types'; import { ComponentProps } from '../utils/types'; /** @public */ type JSONElement = string | { [x: string]: JSONElement; } | number | boolean | null | JSONElement[]; /** @public */ type JSONTreeClickValueHandler = (event: React.MouseEvent, data: { key: string; keyPath: (string | number)[]; value: string; }) => void; /** @public */ type JSONTreeClickKeyHandler = (event: React.MouseEvent, data: { key: string; keyPath: (string | number)[]; }) => void; interface JSONTreePropsBase { /** * A React ref which is set to the DOM element when the component mounts and null when it unmounts. */ elementRef?: React.Ref; /** * If set to `true`, `defaultExpanded` will expand all nodes on initial render. * The default is `false`, which expands only the first level of properties. */ defaultExpanded?: boolean; /** * If set to `true`, using `shift + click` or `shift + enter` will expand all descendant nodes * of the tree at once. A tooltip is added to the expand all / collapse all button to indicate that this feature * is enabled. Defaults to `true`. */ expandChildrenOnShiftKey?: boolean; /** * Number of space characters per level of indentation. */ indent?: number; /** * The JSON string to visualize. Alternatively, this prop also accepts objects and other * possible return types of `JSON.parse`. `JSONTree` doesn't perform any runtime type * validation. If the passed value contains circular dependencies or types not * representable in JSON like functions and symbols, the component behavior is unspecified. */ json: string | JSONElement; /** * Optional event handler to call if keys are clicked on. * The function signature is `onClickKey({path, keyPath, value})`, where `path` is the property path *, `.a.b`; and keyPath is an array of that property path, `['a', 'b']. */ onClickKey?: JSONTreeClickKeyHandler; /** * Optional event handler to call if values are clicked on. * The function signature is `onClickValue({path, keyPath, value})`, where `path` is the property path *, `.a.b`; keyPath is an array of that property path, `['a', 'b']; and `value` is the value that was clicked. */ onClickValue?: JSONTreeClickValueHandler; /** * Handles overflow by wrapping values or by enabling scrolling. */ overflow?: 'wrap' | 'scroll'; /** * @private * Controls how string values in leaf nodes are rendered. * - `literal`: renders strings in quotes and respects control characters (new lines, tabs, etc.). * - `stringify`: renders strings using `JSON.stringify`, which causes control characters to be escaped. */ stringRenderer?: 'literal' | 'stringify'; } type JSONTreeProps = ComponentProps; /** * Used to visualize a JSON string. */ declare function JSONTree({ elementRef, defaultExpanded, indent, json, onClickKey, onClickValue, overflow, expandChildrenOnShiftKey, stringRenderer, ...otherProps }: JSONTreeProps): React.JSX.Element; declare namespace JSONTree { var propTypes: { elementRef: PropTypes.Requireable; defaultExpanded: PropTypes.Requireable; indent: PropTypes.Requireable; json: PropTypes.Validator>>; onClickKey: PropTypes.Requireable<(...args: any[]) => any>; onClickValue: PropTypes.Requireable<(...args: any[]) => any>; overflow: PropTypes.Requireable; expandChildrenOnShiftKey: PropTypes.Requireable; }; } export default JSONTree; export { JSONElement, JSONTreeClickValueHandler, JSONTreeClickKeyHandler };