import React, { FormEvent, PureComponent } from 'react'; import { MapDispatchToProps, MapStateToProps } from 'react-redux'; import { InlineFieldRow, VerticalGroup } from '@grafana/ui'; import { DataSourceVariableModel, VariableWithMultiSupport } from '../types'; import { OnPropChangeArguments, VariableEditorProps } from '../editor/types'; import { SelectionOptionsEditor } from '../editor/SelectionOptionsEditor'; import { VariableEditorState } from '../editor/reducer'; import { DataSourceVariableEditorState } from './reducer'; import { initDataSourceVariableEditor } from './actions'; import { StoreState } from '../../../types'; import { connectWithStore } from '../../../core/utils/connectWithReduxStore'; import { changeVariableMultiValue } from '../state/actions'; import { VariableSectionHeader } from '../editor/VariableSectionHeader'; import { VariableSelectField } from '../editor/VariableSelectField'; import { SelectableValue } from '@grafana/data'; import { VariableTextField } from '../editor/VariableTextField'; export interface OwnProps extends VariableEditorProps {} interface ConnectedProps { editor: VariableEditorState; } interface DispatchProps { initDataSourceVariableEditor: typeof initDataSourceVariableEditor; changeVariableMultiValue: typeof changeVariableMultiValue; } type Props = OwnProps & ConnectedProps & DispatchProps; export class DataSourceVariableEditorUnConnected extends PureComponent { componentDidMount() { this.props.initDataSourceVariableEditor(); } onRegExChange = (event: FormEvent) => { this.props.onPropChange({ propName: 'regex', propValue: event.currentTarget.value, }); }; onRegExBlur = (event: FormEvent) => { this.props.onPropChange({ propName: 'regex', propValue: event.currentTarget.value, updateOptions: true, }); }; onSelectionOptionsChange = async ({ propValue, propName }: OnPropChangeArguments) => { this.props.onPropChange({ propName, propValue, updateOptions: true }); }; getSelectedDataSourceTypeValue = (): string => { if (!this.props.editor.extended?.dataSourceTypes?.length) { return ''; } const foundItem = this.props.editor.extended?.dataSourceTypes.find((ds) => ds.value === this.props.variable.query); const value = foundItem ? foundItem.value : this.props.editor.extended?.dataSourceTypes[0].value; return value ?? ''; }; onDataSourceTypeChanged = (option: SelectableValue) => { this.props.onPropChange({ propName: 'query', propValue: option.value, updateOptions: true }); }; render() { const typeOptions = this.props.editor.extended?.dataSourceTypes?.length ? this.props.editor.extended?.dataSourceTypes?.map((ds) => ({ value: ds.value ?? '', label: ds.text })) : []; const typeValue = typeOptions.find((o) => o.value === this.props.variable.query) ?? typeOptions[0]; return ( Regex filter for which data source instances to choose from in the variable value list. Leave empty for all.

Example: /^prod/ } />
); } } const mapStateToProps: MapStateToProps = (state, ownProps) => ({ editor: state.templating.editor as VariableEditorState, }); const mapDispatchToProps: MapDispatchToProps = { initDataSourceVariableEditor, changeVariableMultiValue, }; export const DataSourceVariableEditor = connectWithStore( DataSourceVariableEditorUnConnected, mapStateToProps, mapDispatchToProps );