import { FormEvent } from 'react'; import { useAsync } from 'react-use'; import { DataSourceInstanceSettings, SelectableValue, TimeRange } from '@grafana/data'; import { selectors } from '@grafana/e2e-selectors'; import { Trans, useTranslate } from '@grafana/i18n'; import { getDataSourceSrv } from '@grafana/runtime'; import { QueryVariable } from '@grafana/scenes'; import { DataSourceRef, VariableRefresh, VariableSort } from '@grafana/schema'; import { Field, TextLink } from '@grafana/ui'; import { QueryEditor } from 'app/features/dashboard-scene/settings/variables/components/QueryEditor'; import { SelectionOptionsForm } from 'app/features/dashboard-scene/settings/variables/components/SelectionOptionsForm'; import { DataSourcePicker } from 'app/features/datasources/components/picker/DataSourcePicker'; import { getVariableQueryEditor } from 'app/features/variables/editor/getVariableQueryEditor'; import { QueryVariableRefreshSelect } from 'app/features/variables/query/QueryVariableRefreshSelect'; import { QueryVariableSortSelect } from 'app/features/variables/query/QueryVariableSortSelect'; import { VariableLegend } from './VariableLegend'; import { VariableTextAreaField } from './VariableTextAreaField'; type VariableQueryType = QueryVariable['state']['query']; interface QueryVariableEditorFormProps { datasource?: DataSourceRef; onDataSourceChange: (dsSettings: DataSourceInstanceSettings) => void; query: VariableQueryType; onQueryChange: (query: VariableQueryType) => void; onLegacyQueryChange: (query: VariableQueryType, definition: string) => void; timeRange: TimeRange; regex: string | null; onRegExChange: (event: FormEvent) => void; sort: VariableSort; onSortChange: (option: SelectableValue) => void; refresh: VariableRefresh; onRefreshChange: (option: VariableRefresh) => void; isMulti: boolean; onMultiChange: (event: FormEvent) => void; allowCustomValue?: boolean; onAllowCustomValueChange?: (event: FormEvent) => void; includeAll: boolean; onIncludeAllChange: (event: FormEvent) => void; allValue: string; onAllValueChange: (event: FormEvent) => void; } export function QueryVariableEditorForm({ datasource: datasourceRef, onDataSourceChange, query, onQueryChange, onLegacyQueryChange, timeRange, regex, onRegExChange, sort, onSortChange, refresh, onRefreshChange, isMulti, onMultiChange, allowCustomValue, onAllowCustomValueChange, includeAll, onIncludeAllChange, allValue, onAllValueChange, }: QueryVariableEditorFormProps) { const { value: dsConfig } = useAsync(async () => { const datasource = await getDataSourceSrv().get(datasourceRef ?? ''); const VariableQueryEditor = await getVariableQueryEditor(datasource); const defaultQuery = datasource?.variables?.getDefaultQuery?.(); if (!query && defaultQuery) { const query = typeof defaultQuery === 'string' ? defaultQuery : { ...defaultQuery, refId: defaultQuery.refId ?? 'A' }; onQueryChange(query); } return { datasource, VariableQueryEditor }; }, [datasourceRef]); const { t } = useTranslate(); const { datasource, VariableQueryEditor } = dsConfig ?? {}; return ( <> Query options {datasource && VariableQueryEditor && ( )} Optional, if you want to extract part of a series name or metric node segment.
Named capture groups can be used to separate the display text and value ( see examples ). } // eslint-disable-next-line @grafana/i18n/no-untranslated-strings placeholder="/.*-(?.*)-(?.*)-.*/" onBlur={onRegExChange} testId={selectors.pages.Dashboard.Settings.Variables.Edit.QueryVariable.queryOptionsRegExInputV2} width={52} /> Selection options ); }