Commit 22a0f3cf by Johannes Schill Committed by Marcus Efraimsson

fix: Text box variables with empty values should not be considered fa… (#13791)

* fix: text box template variable doesn't work properly without a default value
parent 8a3b1cf4
...@@ -429,6 +429,11 @@ describe('templateSrv', () => { ...@@ -429,6 +429,11 @@ describe('templateSrv', () => {
name: 'period', name: 'period',
current: { value: '$__auto_interval_interval', text: 'auto' }, current: { value: '$__auto_interval_interval', text: 'auto' },
}, },
{
type: 'textbox',
name: 'empty_on_init',
current: { value: '', text: '' },
},
]); ]);
_templateSrv.setGrafanaVariable('$__auto_interval_interval', '13m'); _templateSrv.setGrafanaVariable('$__auto_interval_interval', '13m');
_templateSrv.updateTemplateData(); _templateSrv.updateTemplateData();
...@@ -438,6 +443,11 @@ describe('templateSrv', () => { ...@@ -438,6 +443,11 @@ describe('templateSrv', () => {
const target = _templateSrv.replaceWithText('Server: $server, period: $period'); const target = _templateSrv.replaceWithText('Server: $server, period: $period');
expect(target).toBe('Server: All, period: 13m'); expect(target).toBe('Server: All, period: 13m');
}); });
it('should replace empty string-values with an empty string', () => {
const target = _templateSrv.replaceWithText('Hello $empty_on_init');
expect(target).toBe('Hello ');
});
}); });
describe('built in interval variables', () => { describe('built in interval variables', () => {
......
...@@ -30,17 +30,14 @@ export class TemplateSrv { ...@@ -30,17 +30,14 @@ export class TemplateSrv {
} }
updateTemplateData() { updateTemplateData() {
this.index = {}; const existsOrEmpty = value => value || value === '';
for (let i = 0; i < this.variables.length; i++) { this.index = this.variables.reduce((acc, currentValue) => {
const variable = this.variables[i]; if (currentValue.current && !currentValue.current.isNone && existsOrEmpty(currentValue.current.value)) {
acc[currentValue.name] = currentValue;
if (!variable.current || (!variable.current.isNone && !variable.current.value)) {
continue;
}
this.index[variable.name] = variable;
} }
return acc;
}, {});
} }
variableInitialized(variable) { variableInitialized(variable) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment