2022-04-22 08:33:13 -05:00
|
|
|
import { css, cx } from '@emotion/css';
|
2023-06-15 06:59:52 -05:00
|
|
|
import React, { useId } from 'react';
|
2022-04-22 08:33:13 -05:00
|
|
|
import Highlighter from 'react-highlight-words';
|
|
|
|
|
2021-03-25 02:33:13 -05:00
|
|
|
import {
|
|
|
|
DynamicConfigValue,
|
|
|
|
FieldConfigOptionsRegistry,
|
|
|
|
FieldConfigProperty,
|
|
|
|
FieldOverrideContext,
|
2022-11-03 08:54:18 -05:00
|
|
|
GrafanaTheme2,
|
2021-03-25 02:33:13 -05:00
|
|
|
} from '@grafana/data';
|
2022-11-03 08:54:18 -05:00
|
|
|
import { Counter, Field, HorizontalGroup, IconButton, Label, useStyles2 } from '@grafana/ui';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
2021-03-25 02:33:13 -05:00
|
|
|
import { OptionsPaneCategory } from './OptionsPaneCategory';
|
2020-04-19 06:18:36 -05:00
|
|
|
|
2020-02-14 06:46:08 -06:00
|
|
|
interface DynamicConfigValueEditorProps {
|
|
|
|
property: DynamicConfigValue;
|
2020-04-06 09:24:41 -05:00
|
|
|
registry: FieldConfigOptionsRegistry;
|
2020-02-14 06:46:08 -06:00
|
|
|
onChange: (value: DynamicConfigValue) => void;
|
|
|
|
context: FieldOverrideContext;
|
|
|
|
onRemove: () => void;
|
2021-01-06 14:40:32 -06:00
|
|
|
isSystemOverride?: boolean;
|
2021-11-16 05:41:53 -06:00
|
|
|
searchQuery: string;
|
2020-02-14 06:46:08 -06:00
|
|
|
}
|
|
|
|
|
2023-03-15 09:56:09 -05:00
|
|
|
export const DynamicConfigValueEditor = ({
|
2020-02-14 06:46:08 -06:00
|
|
|
property,
|
|
|
|
context,
|
2020-04-06 09:24:41 -05:00
|
|
|
registry,
|
2020-02-14 06:46:08 -06:00
|
|
|
onChange,
|
|
|
|
onRemove,
|
2021-01-06 14:40:32 -06:00
|
|
|
isSystemOverride,
|
2021-11-16 05:41:53 -06:00
|
|
|
searchQuery,
|
2023-03-15 09:56:09 -05:00
|
|
|
}: DynamicConfigValueEditorProps) => {
|
2022-11-03 08:54:18 -05:00
|
|
|
const styles = useStyles2(getStyles);
|
2020-04-06 09:24:41 -05:00
|
|
|
const item = registry?.getIfExists(property.id);
|
2020-03-16 08:26:03 -05:00
|
|
|
|
2023-06-15 06:59:52 -05:00
|
|
|
const componentId = useId();
|
|
|
|
|
2020-03-16 08:26:03 -05:00
|
|
|
if (!item) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-03-25 02:33:13 -05:00
|
|
|
|
|
|
|
const isCollapsible =
|
|
|
|
Array.isArray(property.value) ||
|
|
|
|
property.id === FieldConfigProperty.Thresholds ||
|
|
|
|
property.id === FieldConfigProperty.Links ||
|
|
|
|
property.id === FieldConfigProperty.Mappings;
|
|
|
|
|
|
|
|
const labelCategory = item.category?.filter((c) => c !== item.name);
|
2020-04-17 05:52:01 -05:00
|
|
|
let editor;
|
2020-04-18 07:05:38 -05:00
|
|
|
|
2022-02-02 06:02:32 -06:00
|
|
|
/* eslint-disable react/display-name */
|
|
|
|
const renderLabel =
|
|
|
|
(includeDescription = true, includeCounter = false) =>
|
|
|
|
(isExpanded = false) =>
|
|
|
|
(
|
|
|
|
<HorizontalGroup justify="space-between">
|
2023-06-15 06:59:52 -05:00
|
|
|
<Label
|
|
|
|
category={labelCategory}
|
|
|
|
description={includeDescription ? item.description : undefined}
|
|
|
|
htmlFor={componentId}
|
|
|
|
>
|
2022-02-02 06:02:32 -06:00
|
|
|
<Highlighter
|
|
|
|
textToHighlight={item.name}
|
|
|
|
searchWords={[searchQuery]}
|
|
|
|
highlightClassName={'search-fragment-highlight'}
|
|
|
|
/>
|
|
|
|
{!isExpanded && includeCounter && item.getItemsCount && (
|
|
|
|
<Counter value={item.getItemsCount(property.value)} />
|
|
|
|
)}
|
|
|
|
</Label>
|
|
|
|
{!isSystemOverride && (
|
|
|
|
<div>
|
2023-06-08 03:23:28 -05:00
|
|
|
<IconButton name="times" onClick={onRemove} tooltip="Remove label" />
|
2022-02-02 06:02:32 -06:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</HorizontalGroup>
|
|
|
|
);
|
|
|
|
/* eslint-enable react/display-name */
|
2020-03-16 08:26:03 -05:00
|
|
|
|
2020-04-17 05:52:01 -05:00
|
|
|
if (isCollapsible) {
|
|
|
|
editor = (
|
2021-03-25 02:33:13 -05:00
|
|
|
<OptionsPaneCategory
|
2020-04-22 12:56:37 -05:00
|
|
|
id={item.name}
|
2020-04-18 07:05:38 -05:00
|
|
|
renderTitle={renderLabel(false, true)}
|
2020-04-17 05:52:01 -05:00
|
|
|
className={css`
|
|
|
|
padding-left: 0;
|
|
|
|
padding-right: 0;
|
|
|
|
`}
|
2021-03-25 02:33:13 -05:00
|
|
|
isNested
|
|
|
|
isOpenDefault={property.value !== undefined}
|
2020-04-17 05:52:01 -05:00
|
|
|
>
|
|
|
|
<item.override
|
|
|
|
value={property.value}
|
2021-01-20 00:59:48 -06:00
|
|
|
onChange={(value) => {
|
2020-04-17 05:52:01 -05:00
|
|
|
onChange(value);
|
|
|
|
}}
|
|
|
|
item={item}
|
|
|
|
context={context}
|
|
|
|
/>
|
2021-03-25 02:33:13 -05:00
|
|
|
</OptionsPaneCategory>
|
2020-04-17 05:52:01 -05:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
editor = (
|
|
|
|
<div>
|
2020-04-20 01:47:25 -05:00
|
|
|
<Field label={renderLabel()()} description={item.description}>
|
2020-03-19 06:28:05 -05:00
|
|
|
<item.override
|
|
|
|
value={property.value}
|
2021-01-20 00:59:48 -06:00
|
|
|
onChange={(value) => {
|
2020-03-19 06:28:05 -05:00
|
|
|
onChange(value);
|
|
|
|
}}
|
|
|
|
item={item}
|
|
|
|
context={context}
|
2023-06-15 06:59:52 -05:00
|
|
|
id={componentId}
|
2020-03-19 06:28:05 -05:00
|
|
|
/>
|
2020-04-17 05:52:01 -05:00
|
|
|
</Field>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={cx(
|
|
|
|
isCollapsible && styles.collapsibleOverrideEditor,
|
|
|
|
!isCollapsible && 'dynamicConfigValueEditor--nonCollapsible'
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{editor}
|
2020-02-14 06:46:08 -06:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-11-03 08:54:18 -05:00
|
|
|
const getStyles = (theme: GrafanaTheme2) => {
|
2020-02-14 06:46:08 -06:00
|
|
|
return {
|
2020-04-17 05:52:01 -05:00
|
|
|
collapsibleOverrideEditor: css`
|
|
|
|
label: collapsibleOverrideEditor;
|
|
|
|
& + .dynamicConfigValueEditor--nonCollapsible {
|
2022-11-03 08:54:18 -05:00
|
|
|
margin-top: ${theme.spacing(1)};
|
2020-02-14 06:46:08 -06:00
|
|
|
}
|
|
|
|
`,
|
|
|
|
};
|
2022-11-03 08:54:18 -05:00
|
|
|
};
|