import React, { useMemo } from 'react'; import { DataFrame, InterpolateFunction, PanelOptionsEditorItem, PanelPlugin, StandardEditorContext, VariableSuggestionsScope, } from '@grafana/data'; import { get as lodashGet, set as lodashSet } from 'lodash'; import { Field, Label } from '@grafana/ui'; import groupBy from 'lodash/groupBy'; import { OptionsGroup } from './OptionsGroup'; import { getPanelOptionsVariableSuggestions } from 'app/features/panel/panellinks/link_srv'; interface PanelOptionsEditorProps { plugin: PanelPlugin; data?: DataFrame[]; replaceVariables: InterpolateFunction; options: TOptions; onChange: (options: TOptions) => void; } export const PanelOptionsEditor: React.FC> = ({ plugin, options, onChange, data, replaceVariables, }) => { const optionEditors = useMemo>(() => { return groupBy(plugin.optionEditors.list(), i => { return i.category ? i.category[0] : 'Display'; }); }, [plugin]); const onOptionChange = (key: string, value: any) => { const newOptions = lodashSet({ ...options }, key, value); onChange(newOptions); }; const context: StandardEditorContext = { data: data || [], replaceVariables, options, getSuggestions: (scope?: VariableSuggestionsScope) => { return getPanelOptionsVariableSuggestions(plugin, data); }, }; return ( <> {Object.keys(optionEditors).map((c, i) => { const optionsToShow = optionEditors[c] .map((e, j) => { if (e.showIf && !e.showIf(options)) { return null; } const label = ( ); return ( onOptionChange(e.path, value)} item={e} context={context} /> ); }) .filter(e => e !== null); return optionsToShow.length > 0 ? (
{optionsToShow}
) : null; })} ); };