Commit eca3824e by ryan

cleanup

parent 40d7ba1e
import React from 'react';
import { storiesOf } from '@storybook/react'; import { storiesOf } from '@storybook/react';
import TableInputCSV from './TableInputCSV'; import TableInputCSV, { ParseResults } from './TableInputCSV';
import { withFullSizeStory } from '../../utils/storybook/withFullSizeStory'; //import { withFullSizeStory } from '../../utils/storybook/withFullSizeStory';
const TableInputStories = storiesOf('UI/Table/Input', module); const TableInputStories = storiesOf('UI/Table/Input', module);
TableInputStories.add('default', () => { TableInputStories.add('default', () => {
return withFullSizeStory(TableInputCSV, {}); //return withFullSizeStory(TableInputCSV, {});
return (
<div>
<TableInputCSV
width={'90%'}
height={'90vh'}
onTableParsed={(results: ParseResults) => {
console.log('GOT', results);
}}
/>
</div>
);
}); });
import React from 'react'; import React from 'react';
import renderer from 'react-test-renderer'; import renderer from 'react-test-renderer';
import TableInputCSV from './TableInputCSV'; import TableInputCSV, { ParseResults } from './TableInputCSV';
describe('TableInputCSV', () => { describe('TableInputCSV', () => {
it('renders correctly', () => { it('renders correctly', () => {
const tree = renderer.create(<TableInputCSV width={100} height={100} />).toJSON(); const tree = renderer
.create(
<TableInputCSV
width={100}
height={100}
onTableParsed={(results: ParseResults) => {
console.log('GOT', results);
}}
/>
)
.toJSON();
//expect(tree).toMatchSnapshot(); //expect(tree).toMatchSnapshot();
expect(tree).toBeDefined(); expect(tree).toBeDefined();
}); });
......
...@@ -3,7 +3,7 @@ import debounce from 'lodash/debounce'; ...@@ -3,7 +3,7 @@ import debounce from 'lodash/debounce';
import Papa, { ParseError, ParseMeta } from 'papaparse'; import Papa, { ParseError, ParseMeta } from 'papaparse';
import { TableData, Column } from '../../types/data'; import { TableData, Column } from '../../types/data';
// Subset of all parse configs // Subset of all parse options
export interface ParseConfig { export interface ParseConfig {
delimiter?: string; // default: "," delimiter?: string; // default: ","
newline?: string; // default: "\r\n" newline?: string; // default: "\r\n"
...@@ -12,7 +12,7 @@ export interface ParseConfig { ...@@ -12,7 +12,7 @@ export interface ParseConfig {
comments?: boolean | string; // default: false comments?: boolean | string; // default: false
} }
interface ParseResults { export interface ParseResults {
table: TableData; table: TableData;
meta: ParseMeta; meta: ParseMeta;
errors: ParseError[]; errors: ParseError[];
...@@ -106,8 +106,9 @@ export function parseCSV(text: string, config?: ParseConfig): ParseResults { ...@@ -106,8 +106,9 @@ export function parseCSV(text: string, config?: ParseConfig): ParseResults {
interface Props { interface Props {
config?: ParseConfig; config?: ParseConfig;
width: number; width: number | string;
height: number; height: number | string;
onTableParsed: (results: ParseResults) => void;
} }
interface State { interface State {
...@@ -127,37 +128,39 @@ class TableInputCSV extends React.PureComponent<Props, State> { ...@@ -127,37 +128,39 @@ class TableInputCSV extends React.PureComponent<Props, State> {
readCSV = debounce(() => { readCSV = debounce(() => {
const results = parseCSV(this.state.text, this.props.config); const results = parseCSV(this.state.text, this.props.config);
this.setState({ results }); this.setState({ results });
console.log('GOT:', results);
}, 150); }, 150);
componentDidUpdate(prevProps: Props, prevState: State) { componentDidUpdate(prevProps: Props, prevState: State) {
if (this.state.text !== prevState.text || this.props.config !== prevProps.config) { if (this.state.text !== prevState.text || this.props.config !== prevProps.config) {
this.readCSV(); this.readCSV();
} }
if (this.state.results !== prevState.results) {
this.props.onTableParsed(this.state.results);
}
} }
handleChange = (event: any) => { handleChange = (event: any) => {
this.setState({ text: event.target.value }); this.setState({ text: event.target.value });
}; };
handleBlur = (event: React.SyntheticEvent<HTMLTextAreaElement>) => {
// console.log('BLUR', event);
};
render() { render() {
const { width, height } = this.props; const { width, height } = this.props;
const { table, errors } = this.state.results; const { table, errors } = this.state.results;
let clazz = 'fa fa-check-circle';
errors.forEach(error => {
if (error.type === 'warning') {
clazz = 'fa fa-exclamation-triangle';
} else {
clazz = 'fa fa-times-circle';
}
});
return ( return (
<div <div className="gf-table-input-csv" style={{ width, height }}>
className={'gf-table-input-wrap'} <textarea placeholder="Enter CSV here..." value={this.state.text} onChange={this.handleChange} />
style={{
width: `${width}px`,
height: `${height}px`,
}}
>
<textarea value={this.state.text} onChange={this.handleChange} onBlur={this.handleBlur} />
<footer> <footer>
BAR: / ROWS:{table.rows.length} / COLS:{table.columns.length} / {JSON.stringify(errors)} Rows:{table.rows.length}, Columns:{table.columns.length} <i className={clazz} />
</footer> </footer>
</div> </div>
); );
......
.gf-table-input-wrap { .gf-table-input-csv {
width: 100%; position: relative;
} }
.gf-table-input-wrap textarea { .gf-table-input-csv textarea {
height: 100%; height: 100%;
width: 100%; width: 100%;
resize: none; resize: none;
} }
.gf-table-input-wrap footer { .gf-table-input-csv footer {
position: absolute; position: absolute;
bottom: 20px; bottom: 20px;
right: 20px; right: 20px;
border: 2px solid #222; border: 1px solid #222;
background: #ccc; background: #ccc;
padding: 4px 10px;
} }
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