TextBoxVariableEditor.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import React, { FormEvent, ReactElement, useCallback } from 'react';
  2. import { selectors } from '@grafana/e2e-selectors';
  3. import { VerticalGroup } from '@grafana/ui';
  4. import { VariableSectionHeader } from '../editor/VariableSectionHeader';
  5. import { VariableTextField } from '../editor/VariableTextField';
  6. import { VariableEditorProps } from '../editor/types';
  7. import { TextBoxVariableModel } from '../types';
  8. export interface Props extends VariableEditorProps<TextBoxVariableModel> {}
  9. export function TextBoxVariableEditor({ onPropChange, variable: { query } }: Props): ReactElement {
  10. const updateVariable = useCallback(
  11. (event: FormEvent<HTMLInputElement>, updateOptions: boolean) => {
  12. event.preventDefault();
  13. onPropChange({ propName: 'originalQuery', propValue: event.currentTarget.value, updateOptions: false });
  14. onPropChange({ propName: 'query', propValue: event.currentTarget.value, updateOptions });
  15. },
  16. [onPropChange]
  17. );
  18. const onChange = useCallback((e: FormEvent<HTMLInputElement>) => updateVariable(e, false), [updateVariable]);
  19. const onBlur = useCallback((e: FormEvent<HTMLInputElement>) => updateVariable(e, true), [updateVariable]);
  20. return (
  21. <VerticalGroup spacing="xs">
  22. <VariableSectionHeader name="Text options" />
  23. <VariableTextField
  24. value={query}
  25. name="Default value"
  26. placeholder="default value, if any"
  27. onChange={onChange}
  28. onBlur={onBlur}
  29. labelWidth={20}
  30. grow
  31. testId={selectors.pages.Dashboard.Settings.Variables.Edit.TextBoxVariable.textBoxOptionsQueryInputV2}
  32. />
  33. </VerticalGroup>
  34. );
  35. }