1- import React , { useState } from 'react' ;
2- import { HaystackVariableQuery } from '../types' ;
1+ import React from 'react' ;
2+ import { HaystackDataSourceOptions , HaystackQuery , HaystackVariableQuery } from '../types' ;
33import { HaystackQueryTypeSelector } from './HaystackQueryTypeSelector' ;
44import { 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 VARIABLE_REF_ID = "variable" ;
13-
14- export const VariableQueryEditor : React . FC < VariableQueryProps > = ( { onChange, query : variableQuery } ) => {
11+ export const VariableQueryEditor = ( { onChange, query } : Props ) => {
1512 let variableInputWidth = 30 ;
16- const [ query , setState ] = useState ( variableQuery ) ;
17-
18- const saveQuery = ( ) => {
19- // refId must match but doesn't get set originally so set should set it on every change
20- setState ( { ...query , refId : VARIABLE_REF_ID } ) ;
2113
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 ) => {
2216 let type = query . type ;
2317 let queryCmd = "" ;
2418 if ( query . type === "eval" ) {
25- queryCmd = query . eval
19+ queryCmd = query . eval ?? "" ;
2620 } else if ( query . type === "hisRead" ) {
27- queryCmd = query . hisRead
21+ queryCmd = query . hisRead ?? "" ;
2822 } else if ( query . type === "hisReadFilter" ) {
29- queryCmd = query . hisReadFilter
23+ queryCmd = query . hisReadFilter ?? "" ;
3024 } else if ( query . type === "read" ) {
31- queryCmd = query . read
25+ queryCmd = query . read ?? "" ;
3226 }
3327 let column = "none" ;
3428 if ( query . column !== undefined && query . column !== '' ) {
@@ -39,39 +33,42 @@ export const VariableQueryEditor: React.FC<VariableQueryProps> = ({ onChange, qu
3933 displayColumn = `'${ query . displayColumn } '` ;
4034 }
4135 let displayString = `${ type } : '${ queryCmd } ', Column: ${ column } , Display: ${ displayColumn } `
42- onChange ( query , displayString ) ;
36+ onChange ( { ... query , query : displayString } ) ;
4337 } ;
4438
4539 const onTypeChange = ( newType : string ) => {
46- setState ( { ...query , type : newType } ) ;
40+ onChangeAndSave ( { ...query , type : newType } ) ;
4741 } ;
4842
4943 const onQueryChange = ( newQuery : string ) => {
5044 if ( query . type === "eval" ) {
51- setState ( { ...query , eval : newQuery } ) ;
45+ onChangeAndSave ( { ...query , eval : newQuery } ) ;
5246 } else if ( query . type === "hisRead" ) {
53- setState ( { ...query , hisRead : newQuery } ) ;
47+ onChangeAndSave ( { ...query , hisRead : newQuery } ) ;
5448 } else if ( query . type === "hisReadFilter" ) {
55- setState ( { ...query , hisReadFilter : newQuery } ) ;
49+ onChangeAndSave ( { ...query , hisReadFilter : newQuery } ) ;
5650 } else if ( query . type === "read" ) {
57- setState ( { ...query , read : newQuery } ) ;
51+ onChangeAndSave ( { ...query , read : newQuery } ) ;
5852 }
5953 } ;
6054
6155 const onColumnChange = ( event : React . FormEvent < HTMLInputElement > ) => {
62- setState ( { ...query , column : event . currentTarget . value , } ) ;
56+ onChangeAndSave ( { ...query , column : event . currentTarget . value , } ) ;
6357 } ;
6458
6559 const onDisplayColumnChange = ( event : React . FormEvent < HTMLInputElement > ) => {
66- setState ( { ...query , displayColumn : event . currentTarget . value , } ) ;
60+ onChangeAndSave ( { ...query , displayColumn : event . currentTarget . value , } ) ;
6761 } ;
6862
6963 return (
70- < div onBlur = { saveQuery } >
64+ < Stack
65+ direction = "column"
66+ alignItems = "flex-start"
67+ >
7168 < HaystackQueryTypeSelector
7269 datasource = { null }
7370 type = { query . type }
74- refId = { query . refId ?? VARIABLE_REF_ID }
71+ refId = { query . refId }
7572 onChange = { onTypeChange }
7673 />
7774 < HaystackQueryInput
@@ -94,6 +91,6 @@ export const VariableQueryEditor: React.FC<VariableQueryProps> = ({ onChange, qu
9491 placeholder = "Defaults to 'Column'"
9592 />
9693 </ InlineField >
97- </ div >
94+ </ Stack >
9895 ) ;
9996} ;
0 commit comments