/
ConfigEditor.tsx
83 lines (74 loc) · 2.34 KB
/
ConfigEditor.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import React, { ChangeEvent, PureComponent } from 'react';
import { LegacyForms } from '@grafana/ui';
import { DataSourcePluginOptionsEditorProps } from '@grafana/data';
import { MyDataSourceOptions, MySecureJsonData } from './types';
const { SecretFormField, FormField } = LegacyForms;
interface Props extends DataSourcePluginOptionsEditorProps<MyDataSourceOptions> {}
interface State {}
export class ConfigEditor extends PureComponent<Props, State> {
onPathChange = (event: ChangeEvent<HTMLInputElement>) => {
const { onOptionsChange, options } = this.props;
const jsonData = {
...options.jsonData,
path: event.target.value,
};
onOptionsChange({ ...options, jsonData });
};
// Secure field (only sent to the backend)
onAPIKeyChange = (event: ChangeEvent<HTMLInputElement>) => {
const { onOptionsChange, options } = this.props;
onOptionsChange({
...options,
secureJsonData: {
apiKey: event.target.value,
},
});
};
onResetAPIKey = () => {
const { onOptionsChange, options } = this.props;
onOptionsChange({
...options,
secureJsonFields: {
...options.secureJsonFields,
apiKey: false,
},
secureJsonData: {
...options.secureJsonData,
apiKey: '',
},
});
};
render() {
const { options } = this.props;
const { jsonData, secureJsonFields } = options;
const secureJsonData = (options.secureJsonData || {}) as MySecureJsonData;
return (
<div className="gf-form-group">
<div className="gf-form">
<FormField
label="Path"
labelWidth={6}
inputWidth={20}
onChange={this.onPathChange}
value={jsonData.path || ''}
placeholder="json field returned to frontend"
/>
</div>
<div className="gf-form-inline">
<div className="gf-form">
<SecretFormField
isConfigured={(secureJsonFields && secureJsonFields.apiKey) as boolean}
value={secureJsonData.apiKey || ''}
label="API Key"
placeholder="secure json field (backend only)"
labelWidth={6}
inputWidth={20}
onReset={this.onResetAPIKey}
onChange={this.onAPIKeyChange}
/>
</div>
</div>
</div>
);
}
}