Skip to content

Commit 1557684

Browse files
fix: Variable query editor display string
1 parent bbf42d2 commit 1557684

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

src/components/VariableQueryEditor.tsx

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
import React, { useState } from 'react';
2-
import { HaystackVariableQuery } from '../types';
1+
import React from 'react';
2+
import { HaystackDataSourceOptions, HaystackQuery, HaystackVariableQuery } from '../types';
33
import { HaystackQueryTypeSelector } from './HaystackQueryTypeSelector';
44
import { HaystackQueryInput } from './HaystackQueryInput';
5-
import { InlineField, Input } from '@grafana/ui';
5+
import { QueryEditorProps } from '@grafana/data';
6+
import { InlineField, Input, Stack } from '@grafana/ui';
7+
import { DataSource } from 'datasource';
68

7-
interface VariableQueryProps {
8-
query: HaystackVariableQuery;
9-
onChange: (query: HaystackVariableQuery, definition: string) => void;
10-
}
9+
type Props = QueryEditorProps<DataSource, HaystackQuery, HaystackDataSourceOptions, HaystackVariableQuery>;
1110

12-
export const VariableQueryEditor: React.FC<VariableQueryProps> = ({ onChange, query: variableQuery }) => {
11+
export const VariableQueryEditor = ({ onChange, query }: Props) => {
1312
let variableInputWidth = 30;
14-
const [query, setState] = useState(variableQuery);
1513

16-
const saveQuery = () => {
14+
// Computes the query string and calls the onChange function. Should be used instead of onChange for all mutating functions.
15+
const onChangeAndSave = (query: HaystackVariableQuery) => {
1716
let type = query.type;
1817
let queryCmd = "";
1918
if (query.type === "eval") {
@@ -34,35 +33,38 @@ export const VariableQueryEditor: React.FC<VariableQueryProps> = ({ onChange, qu
3433
displayColumn = `'${query.displayColumn}'`;
3534
}
3635
let displayString = `${type}: '${queryCmd}', Column: ${column}, Display: ${displayColumn}`
37-
onChange(query, displayString);
36+
onChange({ ...query, query: displayString });
3837
};
3938

4039
const onTypeChange = (newType: string) => {
41-
setState({ ...query, type: newType});
40+
onChangeAndSave({ ...query, type: newType});
4241
};
4342

4443
const onQueryChange = (newQuery: string) => {
4544
if (query.type === "eval") {
46-
setState({ ...query, eval: newQuery });
45+
onChangeAndSave({ ...query, eval: newQuery });
4746
} else if (query.type === "hisRead") {
48-
setState({ ...query, hisRead: newQuery });
47+
onChangeAndSave({ ...query, hisRead: newQuery });
4948
} else if (query.type === "hisReadFilter") {
50-
setState({ ...query, hisReadFilter: newQuery });
49+
onChangeAndSave({ ...query, hisReadFilter: newQuery });
5150
} else if (query.type === "read") {
52-
setState({ ...query, read: newQuery });
51+
onChangeAndSave({ ...query, read: newQuery });
5352
}
5453
};
5554

5655
const onColumnChange = (event: React.FormEvent<HTMLInputElement>) => {
57-
setState({...query, column: event.currentTarget.value,});
56+
onChangeAndSave({...query, column: event.currentTarget.value,});
5857
};
5958

6059
const onDisplayColumnChange = (event: React.FormEvent<HTMLInputElement>) => {
61-
setState({...query, displayColumn: event.currentTarget.value,});
60+
onChangeAndSave({...query, displayColumn: event.currentTarget.value,});
6261
};
6362

6463
return (
65-
<div onBlur={saveQuery}>
64+
<Stack
65+
direction="column"
66+
alignItems="flex-start"
67+
>
6668
<HaystackQueryTypeSelector
6769
datasource={null}
6870
type={query.type}
@@ -89,6 +91,6 @@ export const VariableQueryEditor: React.FC<VariableQueryProps> = ({ onChange, qu
8991
placeholder="Defaults to 'Column'"
9092
/>
9193
</InlineField>
92-
</div>
94+
</Stack>
9395
);
9496
};

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface QueryType extends SelectableValue<string> {
2929
}
3030

3131
export interface HaystackVariableQuery extends HaystackQuery {
32+
query: string; // Used to set 'definition' display string in the UI
3233
column: string;
3334
displayColumn: string;
3435
refId: string;

0 commit comments

Comments
 (0)