import { JsonNode } from 'headless-json-editor'; import { ReactNode } from 'react'; import { Editor } from './Editor'; type AnyOption = Record; export type WidgetProps = { node: T; editor: Editor; options?: Partial; }; /** * interface of generic json editor component. * it takes the current editor and the current node along with a localized option interface */ export type WidgetComponent = (props: WidgetProps) => ReactNode | null; /** * props of your decorated editor */ export type DecoratedWidgetProps = { node: NodeType; editor: Editor; options: NodeType['options']; setValue: (value: ValueType) => void; }; /** * interface to your decorated editor */ export type DecoratedWidget = (props: DecoratedWidgetProps) => ReactNode | null; /** * add setValue helper to editor component and reduce update cycles */ export declare function widget(WidgetComponent: DecoratedWidget): import("react").MemoExoticComponent<(props: WidgetProps) => ReactNode>; /** * Editor plugin type for widgets * * @example * import { Editor, type WidgetPlugin, type ArrayNode } from "@sagold/rect-json-editor"; * * const ArrayWidgetPlugin: WidgetPlugin = { * id: 'array-widget', * use: (node) => node.type === 'array', * Widget: ArrayWidget * }; * * // add widget to editor instance * const editor = new Editor({ widgets: [ArrayWidgetPlugin, ...defaultWidgets] }); */ export type WidgetPlugin = { /** * Unique id of this widget */ readonly id: string; /** * `use` will select this widget to render the passed JsonNode when true is returned * * @param node JsonNode which searches for a matching widget to render * @param options node options and options passed to getWidget function * @returns `true` if this widget should be selected to render the node */ use: (node: JsonNode, options?: AnyOption) => boolean; /** * The widget component to render * * @example * import { type WidgetPlugin, type DecoratedWidgetProps, widget, type StringNode } from "@sagold/rect-json-editor"; * * function MyStringWidget({ editor, node, options, setValue }: DecoratedWidgetProps) {} * * const MyWidgetPlugin: WidgetPlugin = { * id: 'my-string-widget', * use: (node) => node.type === 'string', * Widget: widget(MyStringWidget) * }; * * @example // using widget decorator only * import { type WidgetPlugin, widget, type StringNode } from "@sagold/rect-json-editor"; * * const MyStringWidget = widget(({ editor, node, options, setValue }) {}); * * const MyWidgetPlugin: WidgetPlugin = { * id: 'my-string-widget', * use: (node) => node.type === 'string', * Widget: MyStringWidget * }; * */ Widget: WidgetComponent; }; export {};