2021-02-17 23:21:35 -06:00
|
|
|
import React, { FormEvent, PureComponent } from 'react';
|
2020-11-25 03:21:48 -06:00
|
|
|
import { MapDispatchToProps, MapStateToProps } from 'react-redux';
|
|
|
|
import { InlineFieldRow, VerticalGroup } from '@grafana/ui';
|
2020-03-16 00:32:04 -05:00
|
|
|
|
2020-06-04 06:44:48 -05:00
|
|
|
import { DataSourceVariableModel, VariableWithMultiSupport } from '../types';
|
2020-03-16 00:32:04 -05:00
|
|
|
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';
|
2020-04-01 11:13:38 -05:00
|
|
|
import { changeVariableMultiValue } from '../state/actions';
|
2020-11-25 03:21:48 -06:00
|
|
|
import { VariableSectionHeader } from '../editor/VariableSectionHeader';
|
|
|
|
import { VariableSelectField } from '../editor/VariableSelectField';
|
|
|
|
import { SelectableValue } from '@grafana/data';
|
|
|
|
import { VariableTextField } from '../editor/VariableTextField';
|
2020-03-16 00:32:04 -05:00
|
|
|
|
|
|
|
export interface OwnProps extends VariableEditorProps<DataSourceVariableModel> {}
|
|
|
|
|
|
|
|
interface ConnectedProps {
|
|
|
|
editor: VariableEditorState<DataSourceVariableEditorState>;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface DispatchProps {
|
|
|
|
initDataSourceVariableEditor: typeof initDataSourceVariableEditor;
|
2020-04-01 11:13:38 -05:00
|
|
|
changeVariableMultiValue: typeof changeVariableMultiValue;
|
2020-03-16 00:32:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type Props = OwnProps & ConnectedProps & DispatchProps;
|
|
|
|
|
|
|
|
export class DataSourceVariableEditorUnConnected extends PureComponent<Props> {
|
2020-12-09 04:18:58 -06:00
|
|
|
componentDidMount() {
|
|
|
|
this.props.initDataSourceVariableEditor();
|
2020-03-16 00:32:04 -05:00
|
|
|
}
|
|
|
|
|
2021-02-17 23:21:35 -06:00
|
|
|
onRegExChange = (event: FormEvent<HTMLInputElement>) => {
|
2020-03-16 00:32:04 -05:00
|
|
|
this.props.onPropChange({
|
|
|
|
propName: 'regex',
|
2021-02-17 23:21:35 -06:00
|
|
|
propValue: event.currentTarget.value,
|
2020-03-16 00:32:04 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-02-17 23:21:35 -06:00
|
|
|
onRegExBlur = (event: FormEvent<HTMLInputElement>) => {
|
2020-03-16 00:32:04 -05:00
|
|
|
this.props.onPropChange({
|
|
|
|
propName: 'regex',
|
2021-02-17 23:21:35 -06:00
|
|
|
propValue: event.currentTarget.value,
|
2020-03-16 00:32:04 -05:00
|
|
|
updateOptions: true,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
onSelectionOptionsChange = async ({ propValue, propName }: OnPropChangeArguments<VariableWithMultiSupport>) => {
|
|
|
|
this.props.onPropChange({ propName, propValue, updateOptions: true });
|
|
|
|
};
|
|
|
|
|
|
|
|
getSelectedDataSourceTypeValue = (): string => {
|
|
|
|
if (!this.props.editor.extended?.dataSourceTypes?.length) {
|
|
|
|
return '';
|
|
|
|
}
|
2021-01-20 00:59:48 -06:00
|
|
|
const foundItem = this.props.editor.extended?.dataSourceTypes.find((ds) => ds.value === this.props.variable.query);
|
2020-03-16 00:32:04 -05:00
|
|
|
const value = foundItem ? foundItem.value : this.props.editor.extended?.dataSourceTypes[0].value;
|
|
|
|
return value ?? '';
|
|
|
|
};
|
|
|
|
|
2020-11-25 03:21:48 -06:00
|
|
|
onDataSourceTypeChanged = (option: SelectableValue<string>) => {
|
|
|
|
this.props.onPropChange({ propName: 'query', propValue: option.value, updateOptions: true });
|
2020-03-16 00:32:04 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2020-11-25 03:21:48 -06:00
|
|
|
const typeOptions = this.props.editor.extended?.dataSourceTypes?.length
|
2021-01-20 00:59:48 -06:00
|
|
|
? this.props.editor.extended?.dataSourceTypes?.map((ds) => ({ value: ds.value ?? '', label: ds.text }))
|
2020-11-25 03:21:48 -06:00
|
|
|
: [];
|
2021-01-20 00:59:48 -06:00
|
|
|
const typeValue = typeOptions.find((o) => o.value === this.props.variable.query) ?? typeOptions[0];
|
2020-11-25 03:21:48 -06:00
|
|
|
|
2020-03-16 00:32:04 -05:00
|
|
|
return (
|
2020-11-25 03:21:48 -06:00
|
|
|
<VerticalGroup spacing="xs">
|
|
|
|
<VariableSectionHeader name="Data source options" />
|
|
|
|
<VerticalGroup spacing="md">
|
|
|
|
<VerticalGroup spacing="xs">
|
|
|
|
<InlineFieldRow>
|
|
|
|
<VariableSelectField
|
|
|
|
name="Type"
|
|
|
|
value={typeValue}
|
|
|
|
options={typeOptions}
|
2020-03-16 00:32:04 -05:00
|
|
|
onChange={this.onDataSourceTypeChanged}
|
2020-11-25 03:21:48 -06:00
|
|
|
labelWidth={10}
|
|
|
|
/>
|
|
|
|
</InlineFieldRow>
|
|
|
|
<InlineFieldRow>
|
|
|
|
<VariableTextField
|
|
|
|
value={this.props.variable.regex}
|
|
|
|
name="Instance name filter"
|
|
|
|
placeholder="/.*-(.*)-.*/"
|
|
|
|
onChange={this.onRegExChange}
|
|
|
|
onBlur={this.onRegExBlur}
|
|
|
|
labelWidth={20}
|
|
|
|
tooltip={
|
|
|
|
<div>
|
2021-04-01 11:17:39 -05:00
|
|
|
Regex filter for which data source instances to choose from in the variable value list. Leave empty
|
|
|
|
for all.
|
2020-11-25 03:21:48 -06:00
|
|
|
<br />
|
|
|
|
<br />
|
|
|
|
Example: <code>/^prod/</code>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</InlineFieldRow>
|
|
|
|
</VerticalGroup>
|
|
|
|
|
|
|
|
<SelectionOptionsEditor
|
|
|
|
variable={this.props.variable}
|
|
|
|
onPropChange={this.onSelectionOptionsChange}
|
|
|
|
onMultiChanged={this.props.changeVariableMultiValue}
|
|
|
|
/>
|
|
|
|
</VerticalGroup>
|
|
|
|
</VerticalGroup>
|
2020-03-16 00:32:04 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps: MapStateToProps<ConnectedProps, OwnProps, StoreState> = (state, ownProps) => ({
|
|
|
|
editor: state.templating.editor as VariableEditorState<DataSourceVariableEditorState>,
|
|
|
|
});
|
|
|
|
|
|
|
|
const mapDispatchToProps: MapDispatchToProps<DispatchProps, OwnProps> = {
|
|
|
|
initDataSourceVariableEditor,
|
2020-04-01 11:13:38 -05:00
|
|
|
changeVariableMultiValue,
|
2020-03-16 00:32:04 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
export const DataSourceVariableEditor = connectWithStore(
|
|
|
|
DataSourceVariableEditorUnConnected,
|
|
|
|
mapStateToProps,
|
|
|
|
mapDispatchToProps
|
|
|
|
);
|