CustomVariableEditor.tsx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import React, { FormEvent, PureComponent } from 'react';
  2. import { MapDispatchToProps, MapStateToProps } from 'react-redux';
  3. import { selectors } from '@grafana/e2e-selectors';
  4. import { VerticalGroup } from '@grafana/ui';
  5. import { connectWithStore } from 'app/core/utils/connectWithReduxStore';
  6. import { StoreState } from 'app/types';
  7. import { SelectionOptionsEditor } from '../editor/SelectionOptionsEditor';
  8. import { VariableSectionHeader } from '../editor/VariableSectionHeader';
  9. import { VariableTextAreaField } from '../editor/VariableTextAreaField';
  10. import { OnPropChangeArguments, VariableEditorProps } from '../editor/types';
  11. import { changeVariableMultiValue } from '../state/actions';
  12. import { CustomVariableModel, VariableWithMultiSupport } from '../types';
  13. interface OwnProps extends VariableEditorProps<CustomVariableModel> {}
  14. interface ConnectedProps {}
  15. interface DispatchProps {
  16. changeVariableMultiValue: typeof changeVariableMultiValue;
  17. }
  18. export type Props = OwnProps & ConnectedProps & DispatchProps;
  19. class CustomVariableEditorUnconnected extends PureComponent<Props> {
  20. onChange = (event: FormEvent<HTMLTextAreaElement>) => {
  21. this.props.onPropChange({
  22. propName: 'query',
  23. propValue: event.currentTarget.value,
  24. });
  25. };
  26. onSelectionOptionsChange = async ({ propName, propValue }: OnPropChangeArguments<VariableWithMultiSupport>) => {
  27. this.props.onPropChange({ propName, propValue, updateOptions: true });
  28. };
  29. onBlur = (event: FormEvent<HTMLTextAreaElement>) => {
  30. this.props.onPropChange({
  31. propName: 'query',
  32. propValue: event.currentTarget.value,
  33. updateOptions: true,
  34. });
  35. };
  36. render() {
  37. return (
  38. <VerticalGroup spacing="xs">
  39. <VariableSectionHeader name="Custom options" />
  40. <VerticalGroup spacing="md">
  41. <VerticalGroup spacing="none">
  42. <VariableTextAreaField
  43. name="Values separated by comma"
  44. value={this.props.variable.query}
  45. placeholder="1, 10, mykey : myvalue, myvalue, escaped\,value"
  46. onChange={this.onChange}
  47. onBlur={this.onBlur}
  48. required
  49. width={50}
  50. labelWidth={27}
  51. testId={selectors.pages.Dashboard.Settings.Variables.Edit.CustomVariable.customValueInput}
  52. />
  53. </VerticalGroup>
  54. <SelectionOptionsEditor
  55. variable={this.props.variable}
  56. onPropChange={this.onSelectionOptionsChange}
  57. onMultiChanged={this.props.changeVariableMultiValue}
  58. />{' '}
  59. </VerticalGroup>
  60. </VerticalGroup>
  61. );
  62. }
  63. }
  64. const mapStateToProps: MapStateToProps<ConnectedProps, OwnProps, StoreState> = (state, ownProps) => ({});
  65. const mapDispatchToProps: MapDispatchToProps<DispatchProps, OwnProps> = {
  66. changeVariableMultiValue,
  67. };
  68. export const CustomVariableEditor = connectWithStore(
  69. CustomVariableEditorUnconnected,
  70. mapStateToProps,
  71. mapDispatchToProps
  72. );