InputConfigEditor.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. // Types
  4. import { InputOptions } from './types';
  5. import { DataSourcePluginOptionsEditorProps, DataSourceSettings, TableInputCSV } from '@grafana/ui';
  6. import { DataFrame, MutableDataFrame } from '@grafana/data';
  7. import { dataFrameToCSV } from './utils';
  8. type InputSettings = DataSourceSettings<InputOptions>;
  9. interface Props extends DataSourcePluginOptionsEditorProps<InputSettings> {}
  10. interface State {
  11. text: string;
  12. }
  13. export class InputConfigEditor extends PureComponent<Props, State> {
  14. state = {
  15. text: '',
  16. };
  17. componentDidMount() {
  18. const { options } = this.props;
  19. if (options.jsonData.data) {
  20. const text = dataFrameToCSV(options.jsonData.data);
  21. this.setState({ text });
  22. }
  23. }
  24. onSeriesParsed = (data: DataFrame[], text: string) => {
  25. const { options, onOptionsChange } = this.props;
  26. if (!data) {
  27. data = [new MutableDataFrame()];
  28. }
  29. // data is a property on 'jsonData'
  30. const jsonData = {
  31. ...options.jsonData,
  32. data,
  33. };
  34. onOptionsChange({
  35. ...options,
  36. jsonData,
  37. });
  38. this.setState({ text });
  39. };
  40. render() {
  41. const { text } = this.state;
  42. return (
  43. <div>
  44. <div className="gf-form-group">
  45. <h4>Shared Data:</h4>
  46. <span>Enter CSV</span>
  47. <TableInputCSV text={text} onSeriesParsed={this.onSeriesParsed} width={'100%'} height={200} />
  48. </div>
  49. <div className="grafana-info-box">
  50. This data is stored in the datasource json and is returned to every user in the initial request for any
  51. datasource. This is an appropriate place to enter a few values. Large datasets will perform better in other
  52. datasources.
  53. <br />
  54. <br />
  55. <b>NOTE:</b> Changes to this data will only be reflected after a browser refresh.
  56. </div>
  57. </div>
  58. );
  59. }
  60. }