grafana/public/app/features/variables/editor/SelectionOptionsEditor.tsx
Hugo Häggmark dbec2b02fd
Variables: move state tree into a keyed state (#44642)
* Variables: move state tree into a keyed state

* Update public/app/features/variables/state/transactionReducer.ts

Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com>

* Chore: fix prettier error

* Chore: renamed slices and lastUid

* Chore: rename toUidAction

* Chore: rename dashboardVariableReducer

* Chore: rename state prop back to templating

* Chore renames variable.dashboardUid

* Chore: rename toDashboardVariableIdentifier

* Chore: rename getDashboardVariable

* Chore: rename getDashboardVariablesState

* Chore: rename getDashboardVariables

* Chore: some more renames

* Chore: small clean up

* Chore: small rename

* Chore: removes unused function

* Chore: rename VariableModel.stateKey

* Chore: rename KeyedVariableIdentifier.stateKey

* user essentials mob! 🔱

* user essentials mob! 🔱

* user essentials mob! 🔱

* user essentials mob! 🔱

Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com>
Co-authored-by: kay delaney <kay@grafana.com>
Co-authored-by: Alexandra Vargas <alexa1866@gmail.com>
Co-authored-by: Ashley Harrison <ashley.harrison@grafana.com>
2022-02-18 06:06:04 +01:00

81 lines
2.9 KiB
TypeScript

import React, { ChangeEvent, FormEvent, FunctionComponent, useCallback } from 'react';
import { InlineFieldRow, VerticalGroup } from '@grafana/ui';
import { selectors } from '@grafana/e2e-selectors';
import { VariableWithMultiSupport } from '../types';
import { VariableEditorProps } from './types';
import { KeyedVariableIdentifier } from '../state/types';
import { VariableSectionHeader } from './VariableSectionHeader';
import { VariableSwitchField } from './VariableSwitchField';
import { VariableTextField } from './VariableTextField';
import { toKeyedVariableIdentifier } from '../utils';
export interface SelectionOptionsEditorProps<Model extends VariableWithMultiSupport = VariableWithMultiSupport>
extends VariableEditorProps<Model> {
onMultiChanged: (identifier: KeyedVariableIdentifier, value: boolean) => void;
}
export const SelectionOptionsEditor: FunctionComponent<SelectionOptionsEditorProps> = ({
onMultiChanged: onMultiChangedProps,
onPropChange,
variable,
}) => {
const onMultiChanged = useCallback(
(event: ChangeEvent<HTMLInputElement>) => {
onMultiChangedProps(toKeyedVariableIdentifier(variable), event.target.checked);
},
[onMultiChangedProps, variable]
);
const onIncludeAllChanged = useCallback(
(event: ChangeEvent<HTMLInputElement>) => {
onPropChange({ propName: 'includeAll', propValue: event.target.checked });
},
[onPropChange]
);
const onAllValueChanged = useCallback(
(event: FormEvent<HTMLInputElement>) => {
onPropChange({ propName: 'allValue', propValue: event.currentTarget.value });
},
[onPropChange]
);
return (
<VerticalGroup spacing="none">
<VariableSectionHeader name="Selection options" />
<InlineFieldRow>
<VariableSwitchField
value={variable.multi}
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
value={variable.includeAll}
name="Include All option"
tooltip="Enables an option to include all variables"
onChange={onIncludeAllChanged}
ariaLabel={selectors.pages.Dashboard.Settings.Variables.Edit.General.selectionOptionsIncludeAllSwitch}
/>
</InlineFieldRow>
{variable.includeAll && (
<InlineFieldRow>
<VariableTextField
value={variable.allValue ?? ''}
onChange={onAllValueChanged}
name="Custom all value"
placeholder="blank = auto"
testId={selectors.pages.Dashboard.Settings.Variables.Edit.General.selectionOptionsCustomAllInputV2}
labelWidth={20}
/>
</InlineFieldRow>
)}
</VerticalGroup>
);
};
SelectionOptionsEditor.displayName = 'SelectionOptionsEditor';