2020-03-27 04:48:46 +08:00
|
|
|
import React, { useMemo } from 'react';
|
2020-04-10 03:22:43 +08:00
|
|
|
import { PanelOptionsEditorItem, PanelPlugin } from '@grafana/data';
|
2020-03-29 06:11:50 +08:00
|
|
|
import { set as lodashSet, get as lodashGet } from 'lodash';
|
2020-04-15 00:52:56 +08:00
|
|
|
import { Label, Field } from '@grafana/ui';
|
2020-04-10 03:22:43 +08:00
|
|
|
import groupBy from 'lodash/groupBy';
|
|
|
|
|
import { OptionsGroup } from './OptionsGroup';
|
2020-03-27 04:48:46 +08:00
|
|
|
|
|
|
|
|
interface PanelOptionsEditorProps<TOptions> {
|
|
|
|
|
plugin: PanelPlugin;
|
|
|
|
|
options: TOptions;
|
|
|
|
|
onChange: (options: TOptions) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const PanelOptionsEditor: React.FC<PanelOptionsEditorProps<any>> = ({ plugin, options, onChange }) => {
|
2020-04-10 03:22:43 +08:00
|
|
|
const optionEditors = useMemo<Record<string, PanelOptionsEditorItem[]>>(() => {
|
|
|
|
|
return groupBy(plugin.optionEditors.list(), i => {
|
|
|
|
|
return i.category ? i.category[0] : 'Display';
|
|
|
|
|
});
|
|
|
|
|
}, [plugin]);
|
2020-03-27 04:48:46 +08:00
|
|
|
|
|
|
|
|
const onOptionChange = (key: string, value: any) => {
|
2020-03-29 06:11:50 +08:00
|
|
|
const newOptions = lodashSet({ ...options }, key, value);
|
|
|
|
|
onChange(newOptions);
|
2020-03-27 04:48:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2020-04-10 19:28:26 +08:00
|
|
|
{Object.keys(optionEditors).map((c, i) => {
|
2020-04-10 03:22:43 +08:00
|
|
|
const optionsToShow = optionEditors[c]
|
2020-04-10 19:28:26 +08:00
|
|
|
.map((e, j) => {
|
2020-04-10 03:22:43 +08:00
|
|
|
if (e.showIf && !e.showIf(options)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-04-09 01:21:26 +08:00
|
|
|
|
2020-04-10 03:22:43 +08:00
|
|
|
const label = (
|
2020-04-15 00:52:56 +08:00
|
|
|
<Label description={e.description} category={e.category?.slice(1)}>
|
2020-04-10 03:22:43 +08:00
|
|
|
{e.name}
|
2020-04-15 00:52:56 +08:00
|
|
|
</Label>
|
2020-04-10 03:22:43 +08:00
|
|
|
);
|
|
|
|
|
return (
|
2020-04-15 00:52:56 +08:00
|
|
|
<Field label={label} key={`${e.id}/${j}`}>
|
2020-04-10 03:22:43 +08:00
|
|
|
<e.editor
|
|
|
|
|
value={lodashGet(options, e.path)}
|
|
|
|
|
onChange={value => onOptionChange(e.path, value)}
|
|
|
|
|
item={e}
|
|
|
|
|
/>
|
2020-04-15 00:52:56 +08:00
|
|
|
</Field>
|
2020-04-10 03:22:43 +08:00
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
.filter(e => e !== null);
|
|
|
|
|
|
|
|
|
|
return optionsToShow.length > 0 ? (
|
2020-04-10 19:28:26 +08:00
|
|
|
<OptionsGroup title={c} defaultToClosed key={`${c}/${i}`}>
|
2020-04-10 03:22:43 +08:00
|
|
|
<div>{optionsToShow}</div>
|
|
|
|
|
</OptionsGroup>
|
|
|
|
|
) : null;
|
2020-03-27 04:48:46 +08:00
|
|
|
})}
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|