CSVInputWrapper.tsx 956 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import React, { Component } from 'react';
  2. import coreModule from 'app/core/core_module';
  3. import { TableInputCSV, SeriesData, toCSV } from '@grafana/ui';
  4. interface Props {
  5. data: SeriesData[];
  6. onParsed: (data: SeriesData[]) => void;
  7. }
  8. interface State {
  9. data: SeriesData[];
  10. text: string;
  11. }
  12. /**
  13. * Angular wrapper around TableInputCSV
  14. */
  15. class Wraper extends Component<Props, State> {
  16. constructor(props) {
  17. super(props);
  18. this.state = {
  19. text: toCSV(props.data),
  20. data: props.data,
  21. };
  22. }
  23. onSeriesParsed = (data: SeriesData[], text: string) => {
  24. this.setState({ data, text });
  25. this.props.onParsed(data);
  26. };
  27. render() {
  28. const { text } = this.state;
  29. return <TableInputCSV text={text} onSeriesParsed={this.onSeriesParsed} width={'100%'} height={300} />;
  30. }
  31. }
  32. coreModule.directive('csvInput', [
  33. 'reactDirective',
  34. reactDirective => {
  35. return reactDirective(Wraper, ['data', 'onParsed']);
  36. },
  37. ]);