From 40d7ba1e6a78a29a3696bbdb94a5d22b04be6456 Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 8 Mar 2019 18:02:52 -0800 Subject: [PATCH] adding toolbar --- .../components/Table/TableInputCSV.story.tsx | 7 +- .../components/Table/TableInputCSV.test.tsx | 2 +- .../src/components/Table/TableInputCSV.tsx | 97 ++++++++++++------- .../src/components/Table/_TableInputCSV.scss | 17 ++++ .../__snapshots__/TableInputCSV.test.tsx.snap | 11 --- packages/grafana-ui/src/components/index.scss | 1 + .../src/utils/storybook/withFullSizeStory.tsx | 23 +++++ 7 files changed, 104 insertions(+), 54 deletions(-) create mode 100644 packages/grafana-ui/src/components/Table/_TableInputCSV.scss delete mode 100644 packages/grafana-ui/src/components/Table/__snapshots__/TableInputCSV.test.tsx.snap create mode 100644 packages/grafana-ui/src/utils/storybook/withFullSizeStory.tsx diff --git a/packages/grafana-ui/src/components/Table/TableInputCSV.story.tsx b/packages/grafana-ui/src/components/Table/TableInputCSV.story.tsx index ab2f09f2b74..b894de2b4f7 100644 --- a/packages/grafana-ui/src/components/Table/TableInputCSV.story.tsx +++ b/packages/grafana-ui/src/components/Table/TableInputCSV.story.tsx @@ -1,12 +1,9 @@ import { storiesOf } from '@storybook/react'; -import { withCenteredStory } from '../../utils/storybook/withCenteredStory'; -import { renderComponentWithTheme } from '../../utils/storybook/withTheme'; import TableInputCSV from './TableInputCSV'; +import { withFullSizeStory } from '../../utils/storybook/withFullSizeStory'; const TableInputStories = storiesOf('UI/Table/Input', module); -TableInputStories.addDecorator(withCenteredStory); - TableInputStories.add('default', () => { - return renderComponentWithTheme(TableInputCSV, {}); + return withFullSizeStory(TableInputCSV, {}); }); diff --git a/packages/grafana-ui/src/components/Table/TableInputCSV.test.tsx b/packages/grafana-ui/src/components/Table/TableInputCSV.test.tsx index 70dba9e0b9d..af3d764f7a9 100644 --- a/packages/grafana-ui/src/components/Table/TableInputCSV.test.tsx +++ b/packages/grafana-ui/src/components/Table/TableInputCSV.test.tsx @@ -5,7 +5,7 @@ import TableInputCSV from './TableInputCSV'; describe('TableInputCSV', () => { it('renders correctly', () => { - const tree = renderer.create().toJSON(); + const tree = renderer.create().toJSON(); //expect(tree).toMatchSnapshot(); expect(tree).toBeDefined(); }); diff --git a/packages/grafana-ui/src/components/Table/TableInputCSV.tsx b/packages/grafana-ui/src/components/Table/TableInputCSV.tsx index 6b98c1c742f..4f7e6c202f7 100644 --- a/packages/grafana-ui/src/components/Table/TableInputCSV.tsx +++ b/packages/grafana-ui/src/components/Table/TableInputCSV.tsx @@ -18,6 +18,35 @@ interface ParseResults { errors: ParseError[]; } +// This mutates the table structure! +export function checkAndFix(table: TableData): number { + let cols = table.columns.length; + let different = 0; + table.rows.forEach(row => { + if (cols !== row.length) { + different++; + cols = Math.max(cols, row.length); + } + }); + if (different > 0) { + if (cols !== table.columns.length) { + const diff = cols - table.columns.length; + for (let i = 0; i < diff; i++) { + table.columns.push({ + text: 'Column ' + table.columns.length, + }); + } + } + table.rows.forEach(row => { + const diff = cols - row.length; + for (let i = 0; i < diff; i++) { + row.push(null); + } + }); + } + return different; +} + export function parseCSV(text: string, config?: ParseConfig): ParseResults { const results = Papa.parse(text, { ...config, dynamicTyping: true, skipEmptyLines: true }); @@ -44,47 +73,32 @@ export function parseCSV(text: string, config?: ParseConfig): ParseResults { }; } - let same = true; - let cols = data[0].length; - data.forEach(row => { - if (cols !== row.length) { - same = false; - cols = Math.max(cols, row.length); - } - }); - - // Use a second pass to update the sizes - if (!same) { + const first = results.data.shift(); + const table = { + columns: first.map((v: any, index: number) => { + if (!v) { + v = 'Column ' + (index + 1); + } + return { + text: v.toString().trim(), + } as Column; + }), + rows: results.data, + type: 'table', + columnMap: {}, + } as TableData; + + const changed = checkAndFix(table); + if (changed > 0) { errors.push({ type: 'warning', // A generalization of the error - message: 'not all rows have the same width', + message: 'not all rows have the same width. Changed:' + changed, code: 'width', row: 0, }); - // Add null values to the end of all short arrays - data.forEach(row => { - const diff = cols - row.length; - for (let i = 0; i < diff; i++) { - row.push(null); - } - }); } - - const first = results.data.shift(); return { - table: { - columns: first.map((v: any, index: number) => { - if (!v) { - v = 'Column ' + (index + 1); - } - return { - text: v.toString().trim(), - } as Column; - }), - rows: results.data, - type: 'table', - columnMap: {}, - } as TableData, + table, meta, errors, }; @@ -92,6 +106,8 @@ export function parseCSV(text: string, config?: ParseConfig): ParseResults { interface Props { config?: ParseConfig; + width: number; + height: number; } interface State { @@ -128,14 +144,21 @@ class TableInputCSV extends React.PureComponent { }; render() { + const { width, height } = this.props; const { table, errors } = this.state.results; return ( -
+