1
1
import defaults from 'lodash/defaults' ;
2
2
import React , { useState } from 'react' ;
3
3
import {
4
- Icon ,
5
4
InlineFieldRow ,
6
5
InlineField ,
7
6
Segment ,
@@ -12,14 +11,14 @@ import {
12
11
useTheme ,
13
12
InfoBox ,
14
13
} from '@grafana/ui' ;
15
- import { SelectableValue , FieldType , QueryEditorProps } from '@grafana/data' ;
14
+ import { QueryEditorProps } from '@grafana/data' ;
16
15
import { JsonApiQuery , JsonApiDataSourceOptions , defaultQuery } from '../types' ;
17
- import { JsonPathQueryField } from './JsonPathQueryField' ;
18
16
import { KeyValueEditor } from './KeyValueEditor' ;
19
17
import AutoSizer from 'react-virtualized-auto-sizer' ;
20
18
import { css } from 'emotion' ;
21
19
import { Pair } from '../types' ;
22
20
import { JsonDataSource } from 'datasource' ;
21
+ import { FieldEditor } from './FieldEditor' ;
23
22
24
23
// Display a warning message when user adds any of the following headers.
25
24
const sensitiveHeaders = [ 'authorization' , 'proxy-authorization' , 'x-api-key' ] ;
@@ -55,101 +54,20 @@ export const QueryEditor: React.FC<Props> = ({ onRunQuery, onChange, limitFields
55
54
onRunQuery ( ) ;
56
55
} ;
57
56
58
- const onChangePath = ( i : number ) => ( e : string ) => {
59
- const { fields } = query ;
60
- onChange ( {
61
- ...query ,
62
- fields : fields . map ( ( field , n ) => ( i === n ? { ...fields [ i ] , jsonPath : e } : field ) ) ,
63
- } ) ;
64
- onRunQuery ( ) ;
65
- } ;
66
-
67
- const onChangeType = ( i : number ) => ( e : SelectableValue < string > ) => {
68
- const { fields } = query ;
69
- onChange ( {
70
- ...query ,
71
- fields : fields . map ( ( field , n ) =>
72
- i === n ? { ...fields [ i ] , type : ( e . value === 'auto' ? undefined : e . value ) as FieldType } : field
73
- ) ,
74
- } ) ;
75
- onRunQuery ( ) ;
76
- } ;
77
-
78
- const addField = ( i : number ) => ( ) => {
79
- const { fields } = query ;
80
- if ( ! limitFields || fields . length < limitFields ) {
81
- onChange ( {
82
- ...query ,
83
- fields : [ ...fields . slice ( 0 , i + 1 ) , { name : '' , jsonPath : '' } , ...fields . slice ( i + 1 ) ] ,
84
- } ) ;
85
- onRunQuery ( ) ;
86
- }
87
- } ;
88
-
89
- const removeField = ( i : number ) => ( ) => {
90
- const { fields } = query ;
91
- onChange ( {
92
- ...query ,
93
- fields : [ ...fields . slice ( 0 , i ) , ...fields . slice ( i + 1 ) ] ,
94
- } ) ;
95
- onRunQuery ( ) ;
96
- } ;
97
-
98
57
const tabs = [
99
58
{
100
59
title : 'Fields' ,
101
- content : query . fields
102
- ? query . fields . map ( ( field , index ) => (
103
- < InlineFieldRow key = { index } >
104
- < InlineField
105
- label = "Field"
106
- tooltip = {
107
- < div >
108
- A < a href = "https://goessner.net/articles/JsonPath/" > JSON Path</ a > query that selects one or more
109
- values from a JSON object.
110
- </ div >
111
- }
112
- grow
113
- >
114
- < JsonPathQueryField
115
- onBlur = { onRunQuery }
116
- onChange = { onChangePath ( index ) }
117
- query = { field . jsonPath }
118
- onData = { ( ) => datasource . metadataRequest ( query , range ) }
119
- />
120
- </ InlineField >
121
- < InlineField
122
- label = "Type"
123
- tooltip = "If Auto is set, the JSON property type is used to detect the field type."
124
- >
125
- < Select
126
- value = { field . type ?? 'auto' }
127
- width = { 12 }
128
- onChange = { onChangeType ( index ) }
129
- options = { [
130
- { label : 'Auto' , value : 'auto' } ,
131
- { label : 'String' , value : 'string' } ,
132
- { label : 'Number' , value : 'number' } ,
133
- { label : 'Time' , value : 'time' } ,
134
- { label : 'Boolean' , value : 'boolean' } ,
135
- ] }
136
- />
137
- </ InlineField >
138
-
139
- { ( ! limitFields || query . fields . length < limitFields ) && (
140
- < a className = "gf-form-label" onClick = { addField ( index ) } >
141
- < Icon name = "plus" />
142
- </ a >
143
- ) }
144
-
145
- { query . fields . length > 1 ? (
146
- < a className = "gf-form-label" onClick = { removeField ( index ) } >
147
- < Icon name = "minus" />
148
- </ a >
149
- ) : null }
150
- </ InlineFieldRow >
151
- ) )
152
- : null ,
60
+ content : query . fields && (
61
+ < FieldEditor
62
+ value = { query . fields }
63
+ onChange = { ( value ) => {
64
+ onChange ( { ...query , fields : value } ) ;
65
+ onRunQuery ( ) ;
66
+ } }
67
+ limit = { limitFields }
68
+ onComplete = { ( ) => datasource . metadataRequest ( query , range ) }
69
+ />
70
+ ) ,
153
71
} ,
154
72
{
155
73
title : 'Path' ,
0 commit comments