| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- import React from 'react';
- import debounce from 'lodash/debounce';
- import { DataFrame } from '../../types/data';
- import { CSVConfig, readCSV } from '../../utils/csv';
- interface Props {
- config?: CSVConfig;
- text: string;
- width: string | number;
- height: string | number;
- onSeriesParsed: (data: DataFrame[], text: string) => void;
- }
- interface State {
- text: string;
- data: DataFrame[];
- }
- /**
- * Expects the container div to have size set and will fill it 100%
- */
- export class TableInputCSV extends React.PureComponent<Props, State> {
- constructor(props: Props) {
- super(props);
- const { text, config } = props;
- this.state = {
- text,
- data: readCSV(text, { config }),
- };
- }
- readCSV = debounce(() => {
- const { config } = this.props;
- const { text } = this.state;
- this.setState({ data: readCSV(text, { config }) });
- }, 150);
- componentDidUpdate(prevProps: Props, prevState: State) {
- const { text } = this.state;
- if (text !== prevState.text || this.props.config !== prevProps.config) {
- this.readCSV();
- }
- // If the props text has changed, replace our local version
- if (this.props.text !== prevProps.text && this.props.text !== text) {
- this.setState({ text: this.props.text });
- }
- if (this.state.data !== prevState.data) {
- this.props.onSeriesParsed(this.state.data, this.state.text);
- }
- }
- onTextChange = (event: any) => {
- this.setState({ text: event.target.value });
- };
- render() {
- const { width, height } = this.props;
- const { data } = this.state;
- return (
- <div className="gf-table-input-csv">
- <textarea
- style={{ width, height }}
- placeholder="Enter CSV here..."
- value={this.state.text}
- onChange={this.onTextChange}
- className="gf-form-input"
- />
- {data && (
- <footer>
- {data.map((series, index) => {
- return (
- <span key={index}>
- Rows:{series.rows.length}, Columns:{series.fields.length}
- <i className="fa fa-check-circle" />
- </span>
- );
- })}
- </footer>
- )}
- </div>
- );
- }
- }
- export default TableInputCSV;
|