mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Variables: Fixes error when manually non-matching entering custom value in variable input/picker (#32390)
This commit is contained in:
parent
27aa7bb900
commit
340334994f
@ -81,6 +81,28 @@ describe('optionsPickerReducer', () => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
describe('When toggleOption with undefined option is dispatched', () => {
|
||||||
|
it('should update selected values', () => {
|
||||||
|
const { initialState } = getVariableTestContext({
|
||||||
|
options: [],
|
||||||
|
selectedValues: [],
|
||||||
|
});
|
||||||
|
const payload = {
|
||||||
|
forceSelect: false,
|
||||||
|
clearOthers: true,
|
||||||
|
option: undefined,
|
||||||
|
};
|
||||||
|
reducerTester<OptionsPickerState>()
|
||||||
|
.givenReducer(optionsPickerReducer, cloneDeep(initialState))
|
||||||
|
.whenActionIsDispatched(toggleOption(payload))
|
||||||
|
.thenStateShouldEqual({
|
||||||
|
...initialState,
|
||||||
|
selectedValues: [],
|
||||||
|
options: [],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('toggleOption for multi value variable', () => {
|
describe('toggleOption for multi value variable', () => {
|
||||||
const multi = true;
|
const multi = true;
|
||||||
describe('and value All is selected in options', () => {
|
describe('and value All is selected in options', () => {
|
||||||
@ -112,6 +134,7 @@ describe('optionsPickerReducer', () => {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('and value A is selected in options', () => {
|
describe('and value A is selected in options', () => {
|
||||||
const options = opsA;
|
const options = opsA;
|
||||||
it.each`
|
it.each`
|
||||||
|
@ -7,7 +7,7 @@ import { applyStateChanges } from '../../../../core/utils/applyStateChanges';
|
|||||||
import { containsSearchFilter } from '../../utils';
|
import { containsSearchFilter } from '../../utils';
|
||||||
|
|
||||||
export interface ToggleOption {
|
export interface ToggleOption {
|
||||||
option: VariableOption;
|
option?: VariableOption;
|
||||||
forceSelect: boolean;
|
forceSelect: boolean;
|
||||||
clearOthers: boolean;
|
clearOthers: boolean;
|
||||||
}
|
}
|
||||||
@ -148,6 +148,8 @@ const optionsPickerSlice = createSlice({
|
|||||||
toggleOption: (state, action: PayloadAction<ToggleOption>): OptionsPickerState => {
|
toggleOption: (state, action: PayloadAction<ToggleOption>): OptionsPickerState => {
|
||||||
const { option, clearOthers, forceSelect } = action.payload;
|
const { option, clearOthers, forceSelect } = action.payload;
|
||||||
const { multi, selectedValues } = state;
|
const { multi, selectedValues } = state;
|
||||||
|
|
||||||
|
if (option) {
|
||||||
const selected = !selectedValues.find((o) => o.value === option.value);
|
const selected = !selectedValues.find((o) => o.value === option.value);
|
||||||
|
|
||||||
if (option.value === ALL_VARIABLE_VALUE || !multi || clearOthers) {
|
if (option.value === ALL_VARIABLE_VALUE || !multi || clearOthers) {
|
||||||
@ -158,13 +160,16 @@ const optionsPickerSlice = createSlice({
|
|||||||
}
|
}
|
||||||
return applyStateChanges(state, updateDefaultSelection, updateAllSelection, updateOptions);
|
return applyStateChanges(state, updateDefaultSelection, updateAllSelection, updateOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (forceSelect || selected) {
|
if (forceSelect || selected) {
|
||||||
state.selectedValues.push({ ...option, selected: true });
|
state.selectedValues.push({ ...option, selected: true });
|
||||||
return applyStateChanges(state, updateDefaultSelection, updateAllSelection, updateOptions);
|
return applyStateChanges(state, updateDefaultSelection, updateAllSelection, updateOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
state.selectedValues = selectedValues.filter((o) => o.value !== option.value);
|
state.selectedValues = selectedValues.filter((o) => o.value !== option.value);
|
||||||
|
} else {
|
||||||
|
state.selectedValues = [];
|
||||||
|
}
|
||||||
|
|
||||||
return applyStateChanges(state, updateDefaultSelection, updateAllSelection, updateOptions);
|
return applyStateChanges(state, updateDefaultSelection, updateAllSelection, updateOptions);
|
||||||
},
|
},
|
||||||
toggleTag: (state, action: PayloadAction<VariableTag>): OptionsPickerState => {
|
toggleTag: (state, action: PayloadAction<VariableTag>): OptionsPickerState => {
|
||||||
|
Loading…
Reference in New Issue
Block a user