2021-02-17 23:21:35 -06:00
|
|
|
import React, { ChangeEvent, FormEvent, FunctionComponent, useCallback } from 'react';
|
2020-11-25 03:21:48 -06:00
|
|
|
import { InlineFieldRow, VerticalGroup } from '@grafana/ui';
|
2020-04-27 02:09:05 -05:00
|
|
|
import { selectors } from '@grafana/e2e-selectors';
|
2020-03-10 02:53:41 -05:00
|
|
|
|
2020-06-04 06:44:48 -05:00
|
|
|
import { VariableWithMultiSupport } from '../types';
|
2020-03-10 02:53:41 -05:00
|
|
|
import { VariableEditorProps } from './types';
|
2020-04-27 02:09:05 -05:00
|
|
|
import { toVariableIdentifier, VariableIdentifier } from '../state/types';
|
2020-11-25 03:21:48 -06:00
|
|
|
import { VariableSectionHeader } from './VariableSectionHeader';
|
|
|
|
import { VariableSwitchField } from './VariableSwitchField';
|
|
|
|
import { VariableTextField } from './VariableTextField';
|
2020-03-10 02:53:41 -05:00
|
|
|
|
|
|
|
export interface SelectionOptionsEditorProps<Model extends VariableWithMultiSupport = VariableWithMultiSupport>
|
2020-04-01 11:13:38 -05:00
|
|
|
extends VariableEditorProps<Model> {
|
|
|
|
onMultiChanged: (identifier: VariableIdentifier, value: boolean) => void;
|
|
|
|
}
|
2020-03-10 02:53:41 -05:00
|
|
|
|
2021-03-25 06:42:14 -05:00
|
|
|
export const SelectionOptionsEditor: FunctionComponent<SelectionOptionsEditorProps> = ({
|
|
|
|
onMultiChanged: onMultiChangedProps,
|
|
|
|
onPropChange,
|
|
|
|
variable,
|
|
|
|
}) => {
|
2020-03-10 02:53:41 -05:00
|
|
|
const onMultiChanged = useCallback(
|
2021-02-17 23:21:35 -06:00
|
|
|
(event: ChangeEvent<HTMLInputElement>) => {
|
2021-03-25 06:42:14 -05:00
|
|
|
onMultiChangedProps(toVariableIdentifier(variable), event.target.checked);
|
2020-03-10 02:53:41 -05:00
|
|
|
},
|
2021-03-25 06:42:14 -05:00
|
|
|
[onMultiChangedProps, variable]
|
2020-03-10 02:53:41 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
const onIncludeAllChanged = useCallback(
|
2021-02-17 23:21:35 -06:00
|
|
|
(event: ChangeEvent<HTMLInputElement>) => {
|
2021-03-25 06:42:14 -05:00
|
|
|
onPropChange({ propName: 'includeAll', propValue: event.target.checked });
|
2020-03-10 02:53:41 -05:00
|
|
|
},
|
2021-03-25 06:42:14 -05:00
|
|
|
[onPropChange]
|
2020-03-10 02:53:41 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
const onAllValueChanged = useCallback(
|
2021-02-17 23:21:35 -06:00
|
|
|
(event: FormEvent<HTMLInputElement>) => {
|
2021-03-25 06:42:14 -05:00
|
|
|
onPropChange({ propName: 'allValue', propValue: event.currentTarget.value });
|
2020-03-10 02:53:41 -05:00
|
|
|
},
|
2021-03-25 06:42:14 -05:00
|
|
|
[onPropChange]
|
2020-03-10 02:53:41 -05:00
|
|
|
);
|
2021-03-25 06:42:14 -05:00
|
|
|
|
2020-03-10 02:53:41 -05:00
|
|
|
return (
|
2020-11-25 03:21:48 -06:00
|
|
|
<VerticalGroup spacing="none">
|
2021-04-01 11:17:39 -05:00
|
|
|
<VariableSectionHeader name="Selection options" />
|
2020-11-25 03:21:48 -06:00
|
|
|
<InlineFieldRow>
|
|
|
|
<VariableSwitchField
|
2021-03-25 06:42:14 -05:00
|
|
|
value={variable.multi}
|
2020-11-25 03:21:48 -06:00
|
|
|
name="Multi-value"
|
|
|
|
tooltip="Enables multiple values to be selected at the same time"
|
|
|
|
onChange={onMultiChanged}
|
|
|
|
ariaLabel={selectors.pages.Dashboard.Settings.Variables.Edit.General.selectionOptionsMultiSwitch}
|
|
|
|
/>
|
|
|
|
</InlineFieldRow>
|
|
|
|
<InlineFieldRow>
|
|
|
|
<VariableSwitchField
|
2021-03-25 06:42:14 -05:00
|
|
|
value={variable.includeAll}
|
2020-11-25 03:21:48 -06:00
|
|
|
name="Include All option"
|
|
|
|
tooltip="Enables an option to include all variables"
|
|
|
|
onChange={onIncludeAllChanged}
|
|
|
|
ariaLabel={selectors.pages.Dashboard.Settings.Variables.Edit.General.selectionOptionsIncludeAllSwitch}
|
|
|
|
/>
|
|
|
|
</InlineFieldRow>
|
2021-03-25 06:42:14 -05:00
|
|
|
{variable.includeAll && (
|
2020-11-25 03:21:48 -06:00
|
|
|
<InlineFieldRow>
|
|
|
|
<VariableTextField
|
2021-03-25 06:42:14 -05:00
|
|
|
value={variable.allValue ?? ''}
|
2020-03-10 02:53:41 -05:00
|
|
|
onChange={onAllValueChanged}
|
2020-11-25 03:21:48 -06:00
|
|
|
name="Custom all value"
|
2020-03-10 02:53:41 -05:00
|
|
|
placeholder="blank = auto"
|
2020-11-25 03:21:48 -06:00
|
|
|
ariaLabel={selectors.pages.Dashboard.Settings.Variables.Edit.General.selectionOptionsCustomAllInput}
|
|
|
|
labelWidth={20}
|
2020-03-10 02:53:41 -05:00
|
|
|
/>
|
2020-11-25 03:21:48 -06:00
|
|
|
</InlineFieldRow>
|
2020-03-10 02:53:41 -05:00
|
|
|
)}
|
2020-11-25 03:21:48 -06:00
|
|
|
</VerticalGroup>
|
2020-03-10 02:53:41 -05:00
|
|
|
);
|
|
|
|
};
|
|
|
|
SelectionOptionsEditor.displayName = 'SelectionOptionsEditor';
|