SelectionOptionsEditor.tsx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import React, { ChangeEvent, FormEvent, FunctionComponent, useCallback } from 'react';
  2. import { selectors } from '@grafana/e2e-selectors';
  3. import { InlineFieldRow, VerticalGroup } from '@grafana/ui';
  4. import { KeyedVariableIdentifier } from '../state/types';
  5. import { VariableWithMultiSupport } from '../types';
  6. import { toKeyedVariableIdentifier } from '../utils';
  7. import { VariableSectionHeader } from './VariableSectionHeader';
  8. import { VariableSwitchField } from './VariableSwitchField';
  9. import { VariableTextField } from './VariableTextField';
  10. import { VariableEditorProps } from './types';
  11. export interface SelectionOptionsEditorProps<Model extends VariableWithMultiSupport = VariableWithMultiSupport>
  12. extends VariableEditorProps<Model> {
  13. onMultiChanged: (identifier: KeyedVariableIdentifier, value: boolean) => void;
  14. }
  15. export const SelectionOptionsEditor: FunctionComponent<SelectionOptionsEditorProps> = ({
  16. onMultiChanged: onMultiChangedProps,
  17. onPropChange,
  18. variable,
  19. }) => {
  20. const onMultiChanged = useCallback(
  21. (event: ChangeEvent<HTMLInputElement>) => {
  22. onMultiChangedProps(toKeyedVariableIdentifier(variable), event.target.checked);
  23. },
  24. [onMultiChangedProps, variable]
  25. );
  26. const onIncludeAllChanged = useCallback(
  27. (event: ChangeEvent<HTMLInputElement>) => {
  28. onPropChange({ propName: 'includeAll', propValue: event.target.checked });
  29. },
  30. [onPropChange]
  31. );
  32. const onAllValueChanged = useCallback(
  33. (event: FormEvent<HTMLInputElement>) => {
  34. onPropChange({ propName: 'allValue', propValue: event.currentTarget.value });
  35. },
  36. [onPropChange]
  37. );
  38. return (
  39. <VerticalGroup spacing="none">
  40. <VariableSectionHeader name="Selection options" />
  41. <InlineFieldRow>
  42. <VariableSwitchField
  43. value={variable.multi}
  44. name="Multi-value"
  45. tooltip="Enables multiple values to be selected at the same time"
  46. onChange={onMultiChanged}
  47. ariaLabel={selectors.pages.Dashboard.Settings.Variables.Edit.General.selectionOptionsMultiSwitch}
  48. />
  49. </InlineFieldRow>
  50. <InlineFieldRow>
  51. <VariableSwitchField
  52. value={variable.includeAll}
  53. name="Include All option"
  54. tooltip="Enables an option to include all variables"
  55. onChange={onIncludeAllChanged}
  56. ariaLabel={selectors.pages.Dashboard.Settings.Variables.Edit.General.selectionOptionsIncludeAllSwitch}
  57. />
  58. </InlineFieldRow>
  59. {variable.includeAll && (
  60. <InlineFieldRow>
  61. <VariableTextField
  62. value={variable.allValue ?? ''}
  63. onChange={onAllValueChanged}
  64. name="Custom all value"
  65. placeholder="blank = auto"
  66. testId={selectors.pages.Dashboard.Settings.Variables.Edit.General.selectionOptionsCustomAllInputV2}
  67. labelWidth={20}
  68. />
  69. </InlineFieldRow>
  70. )}
  71. </VerticalGroup>
  72. );
  73. };
  74. SelectionOptionsEditor.displayName = 'SelectionOptionsEditor';