Skip to content

Commit

Permalink
Fix all option selection in minimize view
Browse files Browse the repository at this point in the history
  • Loading branch information
asimonok committed Sep 26, 2023
1 parent a18e5b4 commit 0f5db40
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/components/OptionsVariable/OptionsVariable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ describe('Options Variable', () => {
})
);

fireEvent.change(selectors.root(), { target: { values: [AllValue, option1.value] } });
fireEvent.change(selectors.root(), { target: { values: [AllValueParameter, option1.value] } });

expect(selectVariableValues).toHaveBeenCalledWith([option1.value], expect.any(Object));
});
Expand All @@ -208,9 +208,9 @@ describe('Options Variable', () => {
})
);

fireEvent.change(selectors.root(), { target: { values: [AllValue, option2.value] } });
fireEvent.change(selectors.root(), { target: { values: [AllValueParameter, option2.value] } });

expect(selectVariableValues).toHaveBeenCalledWith([AllValue], expect.any(Object));
expect(selectVariableValues).toHaveBeenCalledWith([AllValueParameter], expect.any(Object));
});

it('Should select all value if no selected values', () => {
Expand All @@ -232,7 +232,7 @@ describe('Options Variable', () => {

fireEvent.change(selectors.root(), { target: { values: [] } });

expect(selectVariableValues).toHaveBeenCalledWith([AllValue], expect.any(Object));
expect(selectVariableValues).toHaveBeenCalledWith([AllValueParameter], expect.any(Object));
});

it('Should deselect values', () => {
Expand Down
25 changes: 13 additions & 12 deletions src/components/OptionsVariable/OptionsVariable.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useCallback, useMemo } from 'react';
import { SelectableValue } from '@grafana/data';
import { Select } from '@grafana/ui';
import { AllValue, AllValueParameter, TestIds } from '../../constants';
import { AllValueParameter, TestIds } from '../../constants';
import { CustomVariableModel, QueryVariableModel } from '../../types';
import { selectVariableValues } from '../../utils';

Expand All @@ -24,9 +24,7 @@ export const OptionsVariable: React.FC<Props> = ({ variable }) => {
* Current values
*/
const values = useMemo(() => {
return variable.options
.filter((option) => option.selected)
.map((option) => (option.value === AllValueParameter ? AllValue : option.value));
return variable.options.filter((option) => option.selected).map((option) => option.value);
}, [variable]);

/**
Expand All @@ -47,7 +45,7 @@ export const OptionsVariable: React.FC<Props> = ({ variable }) => {
* Select all
*/
if (updatedValues.length === 0 && variable?.multi && variable.includeAll) {
selectVariableValues([AllValue], variable);
selectVariableValues([AllValueParameter], variable);
return;
}

Expand All @@ -59,9 +57,9 @@ export const OptionsVariable: React.FC<Props> = ({ variable }) => {
/**
* Selected value while All is selected
*/
if (updatedValues.length > 1 && values.includes(AllValue) && updatedValues.includes(AllValue)) {
if (updatedValues.length > 1 && values.includes(AllValueParameter) && updatedValues.includes(AllValueParameter)) {
selectVariableValues(
updatedValues.filter((value) => value !== AllValue),
updatedValues.filter((value) => value !== AllValueParameter),
variable
);
return;
Expand All @@ -70,8 +68,12 @@ export const OptionsVariable: React.FC<Props> = ({ variable }) => {
/**
* Select All Value
*/
if (updatedValues.length > 1 && !values.includes(AllValue) && updatedValues.includes(AllValue)) {
selectVariableValues([AllValue], variable);
if (
updatedValues.length > 1 &&
!values.includes(AllValueParameter) &&
updatedValues.includes(AllValueParameter)
) {
selectVariableValues([AllValueParameter], variable);
return;
}

Expand All @@ -88,11 +90,10 @@ export const OptionsVariable: React.FC<Props> = ({ variable }) => {
*/
const options = useMemo(() => {
return variable.options.map((option) => {
const value = option.value === AllValueParameter ? AllValue : option.value;
return {
label: option.text,
value,
ariaLabel: TestIds.optionsVariable.option(value),
value: option.value,
ariaLabel: TestIds.optionsVariable.option(option.value),
};
});
}, [variable]);
Expand Down

0 comments on commit 0f5db40

Please sign in to comment.