[v11.0.x] IntervalVariableEditor: Do not add current value as interval prop (#86591)

IntervalVariableEditor: Do not add current value as interval prop (#86446)

(cherry picked from commit 65afe90124)

Co-authored-by: Ivan Ortega Alba <ivanortegaalba@gmail.com>
This commit is contained in:
grafana-delivery-bot[bot]
2024-04-19 13:36:07 +02:00
committed by GitHub
co-authored by Ivan Ortega Alba
parent dce8a05fef
commit 9c604036e7
2 changed files with 13 additions and 3 deletions
@@ -46,6 +46,7 @@ describe('IntervalVariableEditor', () => {
name: 'test',
type: 'interval',
intervals: ['1m', '10m', '1h', '6h', '1d', '7d'],
value: '10m',
});
const onRunQuery = jest.fn();
@@ -61,6 +62,8 @@ describe('IntervalVariableEditor', () => {
expect(intervalsInput).toBeInTheDocument();
expect(intervalsInput).toHaveValue('7d,30d, 1y, 5y, 10y');
// If the value is not in the list, it should be set to the first value
expect(variable.state.value).toBe('7d');
expect(onRunQuery).toHaveBeenCalledTimes(1);
});
@@ -15,14 +15,21 @@ interface IntervalVariableEditorProps {
}
export function IntervalVariableEditor({ variable, onRunQuery }: IntervalVariableEditorProps) {
const { intervals, autoStepCount, autoEnabled, autoMinInterval } = variable.useState();
const { intervals, autoStepCount, autoEnabled, autoMinInterval, value } = variable.useState();
//transform intervals array into string
const intervalsCombined = getIntervalsQueryFromNewIntervalModel(intervals);
const onIntervalsChange = (event: FormEvent<HTMLInputElement>) => {
const intervalsArray = getIntervalsFromQueryString(event.currentTarget.value);
variable.setState({ intervals: intervalsArray });
const newIntervals = getIntervalsFromQueryString(event.currentTarget.value);
// if the current value is not in the new intervals, set the value to the first interval
const newValue = newIntervals.includes(value) ? value : newIntervals[0];
variable.setState({
intervals: newIntervals,
value: newValue,
});
onRunQuery();
};