Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear value for multi-choice variable #66

Merged
merged 17 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Change Log

## 1.8.0 (IN PROGRESS)
## 1.8.0 (2023-10-16)

### Features / Enhancements

- Update variable option label for parent items in tree view (#62)
- Add handling pressing enter and escape keys for Text Variable (#69)
- Add table virtualization to improve performance (#64)

## 1.7.0 (2023-08-08)

Expand Down
4 changes: 2 additions & 2 deletions src/components/MinimizeView/MinimizeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ interface Props {
* Minimize View
*/
export const MinimizeView: React.FC<Props> = ({
options: { variable: variableName, padding = 0 } = {},
options: { variable: variableName, padding = 0, emptyValue = false } = {},
eventBus,
width,
}) => {
Expand Down Expand Up @@ -70,7 +70,7 @@ export const MinimizeView: React.FC<Props> = ({
<>
{(variable.type === VariableType.QUERY || variable.type === VariableType.CUSTOM) && (
<div style={{ maxWidth }}>
<OptionsVariable variable={variable} />
<OptionsVariable variable={variable} emptyValue={emptyValue} />
</div>
)}
{variable.type === VariableType.TEXTBOX && <TextVariable variable={variable} />}
Expand Down
25 changes: 24 additions & 1 deletion src/components/OptionsVariable/OptionsVariable.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { fireEvent, render, screen } from '@testing-library/react';
import { getJestSelectors } from '@volkovlabs/jest-selectors';
import { AllValue, AllValueParameter, TestIds } from '../../constants';
import { AllValue, AllValueParameter, NoValueParameter, TestIds } from '../../constants';
import { selectVariableValues } from '../../utils';
import { OptionsVariable } from './OptionsVariable';

Expand Down Expand Up @@ -235,6 +235,29 @@ describe('Options Variable', () => {
expect(selectVariableValues).toHaveBeenCalledWith([AllValueParameter], expect.any(Object));
});

it('Should clear value if no selected values and emptyValue enabled', () => {
render(
getComponent({
variable: {
...multiVariable,
options: [
allOption,
{
...option1,
selected: true,
},
option2,
],
} as any,
emptyValue: true,
})
);

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

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

it('Should deselect values', () => {
render(
getComponent({
Expand Down
21 changes: 17 additions & 4 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 { AllValueParameter, TestIds } from '../../constants';
import { AllValueParameter, NoValueParameter, TestIds } from '../../constants';
import { CustomVariableModel, QueryVariableModel } from '../../types';
import { selectVariableValues } from '../../utils';

Expand All @@ -13,13 +13,18 @@ interface Props {
* Variable
*/
variable: QueryVariableModel | CustomVariableModel;

/**
* Empty Value
*/
emptyValue: boolean;
}

/**
* Options Variable
* @param props
*/
export const OptionsVariable: React.FC<Props> = ({ variable }) => {
export const OptionsVariable: React.FC<Props> = ({ variable, emptyValue }) => {
/**
* Current values
*/
Expand All @@ -41,6 +46,14 @@ export const OptionsVariable: React.FC<Props> = ({ variable }) => {
* Deselect values
*/
if (values.length > updatedValues.length) {
/**
* Clear Value
*/
if (updatedValues.length === 0 && emptyValue) {
selectVariableValues([NoValueParameter], variable);
return;
}

/**
* Select all
*/
Expand Down Expand Up @@ -82,7 +95,7 @@ export const OptionsVariable: React.FC<Props> = ({ variable }) => {
*/
selectVariableValues(updatedValues, variable);
},
[values, variable]
[emptyValue, values, variable]
);

/**
Expand All @@ -96,7 +109,7 @@ export const OptionsVariable: React.FC<Props> = ({ variable }) => {
ariaLabel: TestIds.optionsVariable.option(option.value),
};
});
}, [variable]);
}, [variable.options]);

return (
<Select
Expand Down
8 changes: 8 additions & 0 deletions src/constants/panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,11 @@ export const GroupSelectionOptions = [
{ value: true, label: 'Enabled', description: 'Enable to allow group selection.' },
{ value: false, label: 'Disabled', description: 'Disable group selection.' },
];

/**
* Allow Empty Value Options
*/
export const AllowEmptyValueOptions = [
{ value: true, label: 'Enabled', description: 'Empty value enabled.' },
{ value: false, label: 'Disabled', description: 'Empty value disabled.' },
];
5 changes: 5 additions & 0 deletions src/constants/variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ export const AllValue = 'All';
* All Value Parameter
*/
export const AllValueParameter = '$__all';

/**
* No Value Parameter
*/
export const NoValueParameter = '$__empty';
32 changes: 22 additions & 10 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Field, FieldConfigProperty, FieldType, PanelPlugin } from '@grafana/dat
import { getTemplateSrv } from '@grafana/runtime';
import { GroupsEditor, VariablePanel } from './components';
import {
AllowEmptyValueOptions,
AutoScrollOptions,
DisplayModeOptions,
FavoritesOptions,
Expand Down Expand Up @@ -64,16 +65,27 @@ export const plugin = new PanelPlugin<PanelOptions>(VariablePanel)
/**
* Minimize Mode Options
*/
builder.addSliderInput({
path: 'padding',
name: 'Padding',
defaultValue: 10,
settings: {
min: 0,
max: 20,
},
showIf: (config) => showForMinimizeView(config) || showForButtonView(config),
});
builder
.addSliderInput({
path: 'padding',
name: 'Padding',
defaultValue: 10,
settings: {
min: 0,
max: 20,
},
showIf: (config) => showForMinimizeView(config) || showForButtonView(config),
})
.addRadio({
path: 'emptyValue',
name: 'Empty Value',
description: 'Allow Empty Value for multi-choice variable.',
defaultValue: false,
settings: {
options: AllowEmptyValueOptions,
},
showIf: (config) => showForMinimizeView(config) || showForButtonView(config),
});

builder
.addRadio({
Expand Down
5 changes: 5 additions & 0 deletions src/types/panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ export interface PanelOptions extends TableViewOptions {
*/
padding: number;

/**
* Empty Value
*/
emptyValue: boolean;

/**
* Group Selection
*/
Expand Down
26 changes: 25 additions & 1 deletion src/utils/variable.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { locationService } from '@grafana/runtime';
import { AllValue, AllValueParameter } from '../constants';
import { AllValue, AllValueParameter, NoValueParameter } from '../constants';
import { VariableType } from '../types';
import { selectVariableValues } from './variable';

Expand Down Expand Up @@ -51,6 +51,18 @@ describe('Variable Utils', () => {
true
);
});

it('Should apply no value', () => {
const variable = { name: 'variable', type: VariableType.CUSTOM, options: [] };
selectVariableValues([NoValueParameter], variable as any);

expect(locationService.partial).toHaveBeenCalledWith(
{
[`var-${variable.name}`]: '',
},
true
);
});
});

describe('Custom Multi', () => {
Expand All @@ -73,6 +85,18 @@ describe('Variable Utils', () => {
);
});

it('Should apply no value', () => {
const variable = { name: 'variable', type: VariableType.CUSTOM, options: [], multi: true };
selectVariableValues([NoValueParameter], variable as any);

expect(locationService.partial).toHaveBeenCalledWith(
{
[`var-${variable.name}`]: '',
},
true
);
});

it('Should add all values to already selected values', () => {
jest.mocked(locationService.getSearch).mockImplementation(
() =>
Expand Down
20 changes: 16 additions & 4 deletions src/utils/variable.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { TypedVariableModel } from '@grafana/data';
import { locationService } from '@grafana/runtime';
import { AllValue, AllValueParameter } from '../constants';
import { AllValue, AllValueParameter, NoValueParameter } from '../constants';
import {
CustomVariableModel,
QueryVariableModel,
RuntimeVariable,
RuntimeVariableWithOptions,
VariableType,
} from '../types';
import { TypedVariableModel } from '@grafana/data';

/**
* Set Variable Value
Expand Down Expand Up @@ -42,6 +42,11 @@ export const selectVariableValues = (values: string[], runtimeVariable?: Runtime
return;
}

if (values.some((value) => value === NoValueParameter)) {
setVariableValue(name, '');
return;
}

/**
* All Selected values for variable
*/
Expand Down Expand Up @@ -84,8 +89,15 @@ export const selectVariableValues = (values: string[], runtimeVariable?: Runtime
/**
* Single Value
*/
const value = values[0];
setVariableValue(name, value === AllValueParameter ? AllValue : value);
let value = values[0];

if (value === AllValueParameter) {
value = AllValue;
} else if (value === NoValueParameter) {
value = '';
}

setVariableValue(name, value);
return;
}
case VariableType.TEXTBOX: {
Expand Down