Commit cb51f13e by ryan

torkel feedback

parent 6494c17c
import React from 'react';
import debounce from 'lodash/debounce';
import { ParseConfig, parseCSV, ParseDetails } from '../../utils/processTableData';
import { parseCSV, TableParseOptions, TableParseDetails } from '../../utils/processTableData';
import { TableData } from '../../types/data';
import { AutoSizer } from 'react-virtualized';
interface Props {
config?: ParseConfig;
options?: TableParseOptions;
text: string;
onTableParsed: (table: TableData, text: string) => void;
}
......@@ -13,7 +13,7 @@ interface Props {
interface State {
text: string;
table: TableData;
details: ParseDetails;
details: TableParseDetails;
}
/**
......@@ -24,9 +24,9 @@ class TableInputCSV extends React.PureComponent<Props, State> {
super(props);
// Shoud this happen in onComponentMounted?
const { text, config, onTableParsed } = props;
const { text, options, onTableParsed } = props;
const details = {};
const table = parseCSV(text, config, details);
const table = parseCSV(text, options, details);
this.state = {
text,
table,
......@@ -37,13 +37,13 @@ class TableInputCSV extends React.PureComponent<Props, State> {
readCSV = debounce(() => {
const details = {};
const table = parseCSV(this.state.text, this.props.config, details);
const table = parseCSV(this.state.text, this.props.options, details);
this.setState({ table, details });
}, 150);
componentDidUpdate(prevProps: Props, prevState: State) {
const { text } = this.state;
if (text !== prevState.text || this.props.config !== prevProps.config) {
if (text !== prevState.text || this.props.options !== prevProps.options) {
this.readCSV();
}
// If the props text has changed, replace our local version
......@@ -56,7 +56,7 @@ class TableInputCSV extends React.PureComponent<Props, State> {
}
}
handleFooterClicked = (event: any) => {
onFooterClicked = (event: any) => {
console.log('Errors', this.state);
const message = this.state.details
.errors!.map(err => {
......@@ -66,7 +66,7 @@ class TableInputCSV extends React.PureComponent<Props, State> {
alert('CSV Parsing Errors:\n' + message);
};
handleChange = (event: any) => {
onTextChange = (event: any) => {
this.setState({ text: event.target.value });
};
......@@ -80,8 +80,8 @@ class TableInputCSV extends React.PureComponent<Props, State> {
<AutoSizer>
{({ height, width }) => (
<div className="gf-table-input-csv" style={{ width, height }}>
<textarea placeholder="Enter CSV here..." value={this.state.text} onChange={this.handleChange} />
<footer onClick={this.handleFooterClicked} className={footerClassNames}>
<textarea placeholder="Enter CSV here..." value={this.state.text} onChange={this.onTextChange} />
<footer onClick={this.onFooterClicked} className={footerClassNames}>
Rows:{table.rows.length}, Columns:{table.columns.length} &nbsp;
{hasErrors ? <i className="fa fa-exclamation-triangle" /> : <i className="fa fa-check-circle" />}
</footer>
......
......@@ -3,7 +3,7 @@ import { TableData, Column } from '../types/index';
import Papa, { ParseError, ParseMeta } from 'papaparse';
// Subset of all parse options
export interface ParseConfig {
export interface TableParseOptions {
headerIsFirstLine?: boolean; // Not a papa-parse option
delimiter?: string; // default: ","
newline?: string; // default: "\r\n"
......@@ -12,7 +12,7 @@ export interface ParseConfig {
comments?: boolean | string; // default: false
}
export interface ParseDetails {
export interface TableParseDetails {
meta?: ParseMeta;
errors?: ParseError[];
}
......@@ -87,11 +87,11 @@ function makeColumns(values: any[]): Column[] {
* Convert CSV text into a valid TableData object
*
* @param text
* @param config
* @param options
* @param details, if exists the result will be filled with debugging details
*/
export function parseCSV(text: string, config?: ParseConfig, details?: ParseDetails): TableData {
const results = Papa.parse(text, { ...config, dynamicTyping: true, skipEmptyLines: true });
export function parseCSV(text: string, options?: TableParseOptions, details?: TableParseDetails): TableData {
const results = Papa.parse(text, { ...options, dynamicTyping: true, skipEmptyLines: true });
const { data, meta, errors } = results;
// Fill the parse details for debugging
......@@ -121,7 +121,7 @@ export function parseCSV(text: string, config?: ParseConfig, details?: ParseDeta
}
// Assume the first line is the header unless the config says its not
const headerIsNotFirstLine = config && config.headerIsFirstLine === false;
const headerIsNotFirstLine = options && options.headerIsFirstLine === false;
const header = headerIsNotFirstLine ? [] : results.data.shift();
return matchRowSizes({
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment