1- import React , { useState } from 'react' ;
1+ import React , { PureComponent } from 'react' ;
22import { HaystackVariableQuery } from '../types' ;
33import { HaystackQueryTypeSelector } from './HaystackQueryTypeSelector' ;
44import { HaystackQueryInput } from './HaystackQueryInput' ;
@@ -9,91 +9,104 @@ interface VariableQueryProps {
99 onChange : ( query : HaystackVariableQuery , definition : string ) => void ;
1010}
1111
12+ interface VariableState {
13+ query : HaystackVariableQuery ;
14+ refId ?: string ;
15+ }
16+
1217const refId = "variable" ;
1318
14- export const VariableQueryEditor : React . FC < VariableQueryProps > = ( { onChange, query : variableQuery } ) => {
15- let variableInputWidth = 30 ;
16- const [ query , setState ] = useState ( variableQuery ) ;
19+ export class VariableQueryEditor extends PureComponent < VariableQueryProps , VariableState > {
20+ constructor ( props : VariableQueryProps ) {
21+ super ( props ) ;
22+ this . state = {
23+ query : props . query
24+ } ;
25+ }
1726
18- const saveQuery = ( ) => {
27+ variableInputWidth = 30 ;
28+
29+ saveQuery ( ) {
1930 // refId must match but doesn't get set originally so set should set it on every change
20- setState ( { ...query , refId : refId } ) ;
31+ this . setState ( { ...this . state , refId : refId } ) ;
2132
22- let type = query . type ;
33+ let type = this . state . query . type ;
2334 let queryCmd = "" ;
24- if ( query . type === "eval" ) {
25- queryCmd = query . eval
26- } else if ( query . type === "hisRead" ) {
27- queryCmd = query . hisRead
28- } else if ( query . type === "hisReadFilter" ) {
29- queryCmd = query . hisReadFilter
30- } else if ( query . type === "read" ) {
31- queryCmd = query . read
35+ if ( this . state . query . type === "eval" ) {
36+ queryCmd = this . state . query . eval
37+ } else if ( this . state . query . type === "hisRead" ) {
38+ queryCmd = this . state . query . hisRead
39+ } else if ( this . state . query . type === "hisReadFilter" ) {
40+ queryCmd = this . state . query . hisReadFilter
41+ } else if ( this . state . query . type === "read" ) {
42+ queryCmd = this . state . query . read
3243 }
3344 let column = "none" ;
34- if ( query . column !== undefined && query . column !== '' ) {
35- column = `'${ query . column } '` ;
45+ if ( this . state . query . column !== undefined && this . state . query . column !== '' ) {
46+ column = `'${ this . state . query . column } '` ;
3647 }
3748 let displayColumn = "none" ;
38- if ( query . displayColumn !== undefined && query . displayColumn !== '' ) {
39- displayColumn = `'${ query . displayColumn } '` ;
49+ if ( this . state . query . displayColumn !== undefined && this . state . query . displayColumn !== '' ) {
50+ displayColumn = `'${ this . state . query . displayColumn } '` ;
4051 }
4152 let displayString = `${ type } : '${ queryCmd } ', Column: ${ column } , Display: ${ displayColumn } `
42- onChange ( query , displayString ) ;
53+ this . props . onChange ( this . state . query , displayString ) ;
4354 } ;
4455
45- const onTypeChange = ( newType : string ) => {
46- setState ( { ...query , type : newType } ) ;
56+ onTypeChange ( newType : string ) {
57+ this . setState ( { ...this . state , query : { ... this . state . query , type : newType } } ) ;
4758 } ;
4859
49- const onQueryChange = ( newQuery : string ) => {
50- if ( query . type === "eval" ) {
51- setState ( { ...query , eval : newQuery } ) ;
52- } else if ( query . type === "hisRead" ) {
53- setState ( { ...query , hisRead : newQuery } ) ;
54- } else if ( query . type === "hisReadFilter" ) {
55- setState ( { ...query , hisReadFilter : newQuery } ) ;
56- } else if ( query . type === "read" ) {
57- setState ( { ...query , read : newQuery } ) ;
60+ onQueryChange ( newQuery : string ) {
61+ if ( this . props . query . type === "eval" ) {
62+ this . setState ( { ...this . state , query : { ... this . props . query , eval : newQuery } } ) ;
63+ } else if ( this . props . query . type === "hisRead" ) {
64+ this . setState ( { ...this . state , query : { ... this . props . query , hisRead : newQuery } } ) ;
65+ } else if ( this . props . query . type === "hisReadFilter" ) {
66+ this . setState ( { ...this . state , query : { ... this . props . query , hisReadFilter : newQuery } } ) ;
67+ } else if ( this . props . query . type === "read" ) {
68+ this . setState ( { ...this . state , query : { ... this . props . query , read : newQuery } } ) ;
5869 }
5970 } ;
6071
61- const onColumnChange = ( event : React . FormEvent < HTMLInputElement > ) => {
62- setState ( { ...query , column : event . currentTarget . value , } ) ;
72+ onColumnChange ( event : React . FormEvent < HTMLInputElement > ) {
73+ this . setState ( { ...this . state , query : { ... this . props . query , column : event . currentTarget . value , } } ) ;
6374 } ;
6475
65- const onDisplayColumnChange = ( event : React . FormEvent < HTMLInputElement > ) => {
66- setState ( { ...query , displayColumn : event . currentTarget . value , } ) ;
76+ onDisplayColumnChange ( event : React . FormEvent < HTMLInputElement > ) {
77+ this . setState ( { ...this . state , query : { ... this . props . query , displayColumn : event . currentTarget . value , } } ) ;
6778 } ;
6879
69- return (
70- < div onBlur = { saveQuery } >
71- < HaystackQueryTypeSelector
72- datasource = { null }
73- type = { query . type }
74- refId = { query . refId ?? refId }
75- onChange = { onTypeChange }
76- />
77- < HaystackQueryInput
78- query = { query }
79- onChange = { onQueryChange }
80- />
81- < InlineField label = "Column" >
82- < Input
83- width = { variableInputWidth }
84- onChange = { onColumnChange }
85- value = { query . column }
86- placeholder = "Defaults to 'id' or first column"
80+ render ( ) {
81+ return (
82+ < div onBlur = { ( ) => this . saveQuery ( ) } >
83+ < HaystackQueryTypeSelector
84+ datasource = { null }
85+ type = { this . state . query . type }
86+ refId = { this . state . query . refId ?? refId }
87+ onChange = { ( e ) => this . onTypeChange ( e ) }
8788 />
88- </ InlineField >
89- < InlineField label = "Display Column" >
90- < Input
91- width = { variableInputWidth }
92- onChange = { onDisplayColumnChange }
93- value = { query . displayColumn }
94- placeholder = "Defaults to 'Column'"
89+ < HaystackQueryInput
90+ query = { this . state . query }
91+ onChange = { ( e ) => this . onQueryChange ( e ) }
9592 />
96- </ InlineField >
97- </ div >
98- ) ;
99- } ;
93+ < InlineField label = "Column" >
94+ < Input
95+ width = { this . variableInputWidth }
96+ onChange = { ( e ) => this . onColumnChange ( e ) }
97+ value = { this . state . query . column }
98+ placeholder = "Defaults to 'id' or first column"
99+ />
100+ </ InlineField >
101+ < InlineField label = "Display Column" >
102+ < Input
103+ width = { this . variableInputWidth }
104+ onChange = { ( e ) => this . onDisplayColumnChange ( e ) }
105+ value = { this . state . query . displayColumn }
106+ placeholder = "Defaults to 'Column'"
107+ />
108+ </ InlineField >
109+ </ div >
110+ ) ;
111+ }
112+ }
0 commit comments