InputConfigEditor.tsx 1.9 KB

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