DataSourceVariableEditor.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import React, { FormEvent, PureComponent } from 'react';
  2. import { connect, ConnectedProps } from 'react-redux';
  3. import { SelectableValue } from '@grafana/data';
  4. import { selectors } from '@grafana/e2e-selectors';
  5. import { InlineFieldRow, VerticalGroup } from '@grafana/ui';
  6. import { StoreState } from '../../../types';
  7. import { SelectionOptionsEditor } from '../editor/SelectionOptionsEditor';
  8. import { VariableSectionHeader } from '../editor/VariableSectionHeader';
  9. import { VariableSelectField } from '../editor/VariableSelectField';
  10. import { VariableTextField } from '../editor/VariableTextField';
  11. import { initialVariableEditorState } from '../editor/reducer';
  12. import { getDatasourceVariableEditorState } from '../editor/selectors';
  13. import { OnPropChangeArguments, VariableEditorProps } from '../editor/types';
  14. import { changeVariableMultiValue } from '../state/actions';
  15. import { getVariablesState } from '../state/selectors';
  16. import { DataSourceVariableModel, VariableWithMultiSupport } from '../types';
  17. import { initDataSourceVariableEditor } from './actions';
  18. const mapStateToProps = (state: StoreState, ownProps: OwnProps) => {
  19. const {
  20. variable: { rootStateKey },
  21. } = ownProps;
  22. if (!rootStateKey) {
  23. console.error('DataSourceVariableEditor: variable has no rootStateKey');
  24. return {
  25. extended: getDatasourceVariableEditorState(initialVariableEditorState),
  26. };
  27. }
  28. const { editor } = getVariablesState(rootStateKey, state);
  29. return {
  30. extended: getDatasourceVariableEditorState(editor),
  31. };
  32. };
  33. const mapDispatchToProps = {
  34. initDataSourceVariableEditor,
  35. changeVariableMultiValue,
  36. };
  37. const connector = connect(mapStateToProps, mapDispatchToProps);
  38. export interface OwnProps extends VariableEditorProps<DataSourceVariableModel> {}
  39. type Props = OwnProps & ConnectedProps<typeof connector>;
  40. export class DataSourceVariableEditorUnConnected extends PureComponent<Props> {
  41. componentDidMount() {
  42. const { rootStateKey } = this.props.variable;
  43. if (!rootStateKey) {
  44. console.error('DataSourceVariableEditor: variable has no rootStateKey');
  45. return;
  46. }
  47. this.props.initDataSourceVariableEditor(rootStateKey);
  48. }
  49. onRegExChange = (event: FormEvent<HTMLInputElement>) => {
  50. this.props.onPropChange({
  51. propName: 'regex',
  52. propValue: event.currentTarget.value,
  53. });
  54. };
  55. onRegExBlur = (event: FormEvent<HTMLInputElement>) => {
  56. this.props.onPropChange({
  57. propName: 'regex',
  58. propValue: event.currentTarget.value,
  59. updateOptions: true,
  60. });
  61. };
  62. onSelectionOptionsChange = async ({ propValue, propName }: OnPropChangeArguments<VariableWithMultiSupport>) => {
  63. this.props.onPropChange({ propName, propValue, updateOptions: true });
  64. };
  65. getSelectedDataSourceTypeValue = (): string => {
  66. const { extended } = this.props;
  67. if (!extended?.dataSourceTypes.length) {
  68. return '';
  69. }
  70. const foundItem = extended.dataSourceTypes.find((ds) => ds.value === this.props.variable.query);
  71. const value = foundItem ? foundItem.value : extended.dataSourceTypes[0].value;
  72. return value ?? '';
  73. };
  74. onDataSourceTypeChanged = (option: SelectableValue<string>) => {
  75. this.props.onPropChange({ propName: 'query', propValue: option.value, updateOptions: true });
  76. };
  77. render() {
  78. const { variable, extended, changeVariableMultiValue } = this.props;
  79. const typeOptions = extended?.dataSourceTypes?.length
  80. ? extended.dataSourceTypes?.map((ds) => ({ value: ds.value ?? '', label: ds.text }))
  81. : [];
  82. const typeValue = typeOptions.find((o) => o.value === variable.query) ?? typeOptions[0];
  83. return (
  84. <VerticalGroup spacing="xs">
  85. <VariableSectionHeader name="Data source options" />
  86. <VerticalGroup spacing="md">
  87. <VerticalGroup spacing="xs">
  88. <InlineFieldRow>
  89. <VariableSelectField
  90. name="Type"
  91. value={typeValue}
  92. options={typeOptions}
  93. onChange={this.onDataSourceTypeChanged}
  94. labelWidth={10}
  95. testId={selectors.pages.Dashboard.Settings.Variables.Edit.DatasourceVariable.datasourceSelect}
  96. />
  97. </InlineFieldRow>
  98. <InlineFieldRow>
  99. <VariableTextField
  100. value={this.props.variable.regex}
  101. name="Instance name filter"
  102. placeholder="/.*-(.*)-.*/"
  103. onChange={this.onRegExChange}
  104. onBlur={this.onRegExBlur}
  105. labelWidth={20}
  106. tooltip={
  107. <div>
  108. Regex filter for which data source instances to choose from in the variable value list. Leave empty
  109. for all.
  110. <br />
  111. <br />
  112. Example: <code>/^prod/</code>
  113. </div>
  114. }
  115. />
  116. </InlineFieldRow>
  117. </VerticalGroup>
  118. <SelectionOptionsEditor
  119. variable={variable}
  120. onPropChange={this.onSelectionOptionsChange}
  121. onMultiChanged={changeVariableMultiValue}
  122. />
  123. </VerticalGroup>
  124. </VerticalGroup>
  125. );
  126. }
  127. }
  128. export const DataSourceVariableEditor = connector(DataSourceVariableEditorUnConnected);