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 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} ;
0 commit comments