Skip to content

Commit af33c3b

Browse files
chore: use CompatibleCombobox
1 parent 43d40d1 commit af33c3b

2 files changed

Lines changed: 21 additions & 8 deletions

File tree

src/components/CompatibleCombobox.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ export const CompatibleCombobox: typeof Combobox = (props) => {
2727
});
2828
};
2929

30+
const handleCreateOption = (customValue: string) => {
31+
props.onChange({
32+
value: customValue as any,
33+
label: customValue,
34+
});
35+
};
36+
3037
const asyncOption = useCallback((value: SelectValue<any>) => {
3138
if (typeof props.options === 'function') {
3239
return props.options(value);
@@ -61,6 +68,7 @@ export const CompatibleCombobox: typeof Combobox = (props) => {
6168
loadOptions={selectOptions}
6269
defaultOptions
6370
allowCustomValue={props.createCustomValue}
71+
onCreateOption={props.createCustomValue ? handleCreateOption : undefined}
6472
onChange={handleSelectChange}
6573
isClearable={props.isClearable}
6674
isLoading={props.loading}
@@ -76,6 +84,7 @@ export const CompatibleCombobox: typeof Combobox = (props) => {
7684
value={normalizedValue}
7785
options={selectOptions}
7886
allowCustomValue={props.createCustomValue}
87+
onCreateOption={props.createCustomValue ? handleCreateOption : undefined}
7988
onChange={handleSelectChange}
8089
isClearable={props.isClearable}
8190
isLoading={props.loading}

src/components/VariableQueryEditor/VariableQueryEditor.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { debounce } from 'lodash';
22
import React, { FormEvent, useEffect, useState } from 'react';
33

44
import { DEFAULT_FIELD_DISPLAY_VALUES_LIMIT, QueryEditorProps, SelectableValue } from '@grafana/data';
5-
import { Combobox, InlineField, InlineFieldRow, Input, Select } from '@grafana/ui';
5+
import { InlineField, InlineFieldRow, Input, Select } from '@grafana/ui';
6+
7+
import { CompatibleCombobox } from '../CompatibleCombobox';
68

79
import { VictoriaLogsDatasource } from '../../datasource';
810
import { FilterFieldType, Options, Query, VariableQuery } from '../../types';
@@ -21,7 +23,7 @@ export const VariableQueryEditor = ({ onChange, query, datasource, range }: Prop
2123
const [queryFilter, setQueryFilter] = useState<string>('');
2224
const [field, setField] = useState<string>('');
2325
const [limit, setLimit] = useState<number>(DEFAULT_FIELD_DISPLAY_VALUES_LIMIT);
24-
const [fieldNames, setFieldNames] = useState<SelectableValue<string>[]>([]);
26+
const [fieldNames, setFieldNames] = useState<Array<{ value: string; label: string; description?: string }>>([]);
2527
const [isLoading, setIsLoading] = useState<boolean>(false);
2628
const [error, setError] = useState<string>('');
2729

@@ -33,6 +35,12 @@ export const VariableQueryEditor = ({ onChange, query, datasource, range }: Prop
3335
onChange({ refId, type: newType.value, field, query: queryFilter });
3436
};
3537

38+
const handleFieldChange = (option: { value: string }) => {
39+
const newField = String(option.value);
40+
setField(newField);
41+
onChange({ refId, type: type!, field: newField, query: queryFilter, limit });
42+
};
43+
3644
const handleBlur = () => {
3745
if (!type) {
3846
return;
@@ -127,13 +135,9 @@ export const VariableQueryEditor = ({ onChange, query, datasource, range }: Prop
127135
error={error}
128136
invalid={!!error}
129137
>
130-
<Combobox
138+
<CompatibleCombobox
131139
placeholder='Select field'
132-
onChange={(option) => {
133-
const newField = String(option.value);
134-
setField(newField);
135-
onChange({ refId, type: type!, field: newField, query: queryFilter, limit });
136-
}}
140+
onChange={handleFieldChange}
137141
value={field || null}
138142
options={fieldNames}
139143
width={20}

0 commit comments

Comments
 (0)