@tabula/ui-json-view
Version:
Allows to view JSON in interactive way
113 lines (112 loc) • 3.41 kB
TypeScript
import { ComponentType } from 'react';
export type JsonPrimitiveValue = boolean | null | number | string;
export type JsonArray = JsonValue[];
export type JsonObject = {
[key: string]: JsonValue;
};
export type JsonValue = JsonArray | JsonObject | JsonPrimitiveValue;
export type Property = number | string;
export type PrimitiveType = 'bool' | 'null' | 'int' | 'float' | 'string';
export type ComplexType = 'array' | 'object';
export type ValueType = PrimitiveType | ComplexType;
export declare enum LineKind {
Value = 0,
Open = 1,
Close = 2,
Placeholder = 3
}
type BaseLine<Kind extends LineKind> = {
path: string;
kind: Kind;
level: number;
isFirst?: boolean;
isLast?: boolean;
};
export type ValueLine = BaseLine<LineKind.Value> & {
jsonPath: string;
property?: Property;
type: PrimitiveType;
value: string;
};
export type OpenLine = BaseLine<LineKind.Open> & {
jsonPath: string;
property?: Property;
openSymbol: string;
closeSymbol: string;
size: number;
type: ComplexType;
isCollapsed?: boolean;
};
export type CloseLine = BaseLine<LineKind.Close> & {
closeSymbol: string;
};
export type PlaceholderLine = BaseLine<LineKind.Placeholder> & {
placeholder: string;
};
export type Line = ValueLine | OpenLine | CloseLine | PlaceholderLine;
export type CollapsedKeys = {
isEmpty: boolean;
has: (key: string) => boolean;
};
export type OnToggleFn = (key: string) => void;
export type QueryFn = (jsonPath: string) => Readonly<JsonValue>;
export type ActionFn = (jsonPath: string, query: QueryFn) => void;
export type VisibleFn = (jsonPath: string, type: ValueType) => boolean;
export type Action = ActionFn | {
action: ActionFn;
isVisible?: VisibleFn;
trackId?: string;
};
export type OnActionFn = (jsonPath: string, action: ActionFn) => void;
export type Actions = Record<string, Action>;
export type JsonViewOptions = {
/**
* User defined actions.
*/
actions: Actions;
/**
* Shows button, that copy json path to value
*/
isCopyPathAllowed?: boolean;
/**
* Shows button, that copy value to clipboard
*/
isCopyValueAllowed?: boolean;
/**
* Enables expand/collapse controls for arrays and objects, and enables action buttons for each line instead of
* closing and placeholder lines.
*/
isInteractive: boolean;
/**
* Trigger which will be called when a user toggle option in UI.
*
* Enables option control in UI if provided.
*/
onToggleDataTypes?: (showDataTypes: boolean) => void;
/**
* Trigger which will be called when a user toggle option in UI.
*
* Enables option control in UI if provided.
*/
onToggleObjectSize?: (showObjectSize: boolean) => void;
/**
* Allows to trim long string values after the given length.
*
* A user can click by a string value to toggle between full and trimmed versions of the value.
*/
shortStringAfterLength?: number;
/**
* Enables showing a primitive type names near the values instead of `null`.
*/
showDataTypes: boolean;
/**
* Enables showing a size of arrays and objects near of open line.
*/
showObjectSize: boolean;
};
export type ViewProps = {
className: string;
lines: Line[];
};
export type ViewComponentType = ComponentType<ViewProps>;
export {};