2022-02-25 11:18:08 +01:00
|
|
|
import { SelectableValue, toOption } from '@grafana/data';
|
2022-02-28 10:52:58 +01:00
|
|
|
import { Select } from '@grafana/ui';
|
2022-01-31 07:57:14 +01:00
|
|
|
import React, { ComponentType } from 'react';
|
|
|
|
|
import { QueryBuilderOperationParamDef, QueryBuilderOperationParamEditorProps } from '../shared/types';
|
2022-02-28 10:52:58 +01:00
|
|
|
import { AutoSizeInput } from './AutoSizeInput';
|
2022-02-16 08:05:16 +01:00
|
|
|
import { getOperationParamId } from './operationUtils';
|
2022-01-31 07:57:14 +01:00
|
|
|
|
|
|
|
|
export function getOperationParamEditor(
|
|
|
|
|
paramDef: QueryBuilderOperationParamDef
|
|
|
|
|
): ComponentType<QueryBuilderOperationParamEditorProps> {
|
|
|
|
|
if (paramDef.editor) {
|
|
|
|
|
return paramDef.editor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (paramDef.options) {
|
|
|
|
|
return SelectInputParamEditor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return SimpleInputParamEditor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function SimpleInputParamEditor(props: QueryBuilderOperationParamEditorProps) {
|
|
|
|
|
return (
|
2022-02-28 10:52:58 +01:00
|
|
|
<AutoSizeInput
|
2022-02-16 08:05:16 +01:00
|
|
|
id={getOperationParamId(props.operationIndex, props.index)}
|
2022-02-28 10:52:58 +01:00
|
|
|
defaultValue={props.value}
|
|
|
|
|
onCommitChange={(evt) => {
|
2022-01-31 07:57:14 +01:00
|
|
|
props.onChange(props.index, evt.currentTarget.value);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 08:05:16 +01:00
|
|
|
function SelectInputParamEditor({
|
|
|
|
|
paramDef,
|
|
|
|
|
value,
|
|
|
|
|
index,
|
|
|
|
|
operationIndex,
|
|
|
|
|
onChange,
|
|
|
|
|
}: QueryBuilderOperationParamEditorProps) {
|
2022-02-25 11:18:08 +01:00
|
|
|
let selectOptions = paramDef.options as Array<SelectableValue<any>>;
|
|
|
|
|
|
|
|
|
|
if (!selectOptions[0]?.label) {
|
|
|
|
|
selectOptions = paramDef.options!.map((option) => ({
|
|
|
|
|
label: option.toString(),
|
|
|
|
|
value: option as string,
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let valueOption = selectOptions.find((x) => x.value === value) ?? toOption(value as string);
|
2022-01-31 07:57:14 +01:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Select
|
2022-02-16 08:05:16 +01:00
|
|
|
id={getOperationParamId(operationIndex, index)}
|
2022-01-31 07:57:14 +01:00
|
|
|
menuShouldPortal
|
2022-02-25 11:18:08 +01:00
|
|
|
value={valueOption}
|
2022-01-31 07:57:14 +01:00
|
|
|
options={selectOptions}
|
|
|
|
|
onChange={(value) => onChange(index, value.value!)}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|