From 432fc99f3cafb3d651c70563bf82463d5c557ddc Mon Sep 17 00:00:00 2001 From: Juan Tejada Date: Thu, 28 Jul 2016 14:05:37 -0700 Subject: [PATCH] fix(mail-merge): Skip row in table data if its empty - Don't show annoying error if entire row is empty - Just show error if just the email cell is empty --- .../table/table-data-source-spec.jsx | 38 +++++++++++++++++- spec/fixtures/table-data.es6 | 12 ++++++ src/components/table/table-data-source.es6 | 39 ++++++++++++++++++- src/pro | 2 +- 4 files changed, 87 insertions(+), 4 deletions(-) diff --git a/spec/components/table/table-data-source-spec.jsx b/spec/components/table/table-data-source-spec.jsx index f74df7068..0790cb92b 100644 --- a/spec/components/table/table-data-source-spec.jsx +++ b/spec/components/table/table-data-source-spec.jsx @@ -1,4 +1,9 @@ -import {testData, testDataSource, testDataSourceUneven} from '../../fixtures/table-data' +import { + testData, + testDataSource, + testDataSourceEmpty, + testDataSourceUneven, +} from '../../fixtures/table-data' describe('TableDataSource', function describeBlock() { @@ -41,6 +46,37 @@ describe('TableDataSource', function describeBlock() { }); }); + describe('isEmpty', () => { + it('throws if no args passed', () => { + expect(() => testDataSource.isEmpty()).toThrow() + }); + + it('throws if row does not exist', () => { + expect(() => testDataSource.isEmpty({rowIdx: 100})).toThrow() + }); + + it('throws if col does not exist', () => { + expect(() => testDataSource.isEmpty({colIdx: 100})).toThrow() + }); + + it('returns correct value when checking cell', () => { + expect(testDataSourceEmpty.isEmpty({rowIdx: 2, colIdx: 1})).toBe(true) + expect(testDataSourceEmpty.isEmpty({rowIdx: 3, colIdx: 1})).toBe(true) + expect(testDataSourceEmpty.isEmpty({rowIdx: 0, colIdx: 0})).toBe(false) + }); + + it('returns correct value when checking col', () => { + expect(testDataSourceEmpty.isEmpty({colIdx: 2})).toBe(true) + expect(testDataSourceEmpty.isEmpty({colIdx: 0})).toBe(false) + }); + + it('returns correct value when checking row', () => { + expect(testDataSourceEmpty.isEmpty({rowIdx: 2})).toBe(true) + expect(testDataSourceEmpty.isEmpty({rowIdx: 3})).toBe(true) + expect(testDataSourceEmpty.isEmpty({rowIdx: 1})).toBe(false) + }); + }); + describe('rows', () => { it('returns all rows', () => { expect(testDataSource.rows()).toBe(testData.rows) diff --git a/spec/fixtures/table-data.es6 b/spec/fixtures/table-data.es6 index 6871ea4b1..0314e72a2 100644 --- a/spec/fixtures/table-data.es6 +++ b/spec/fixtures/table-data.es6 @@ -18,6 +18,16 @@ export const testDataUneven = { ], } +export const testDataEmpty = { + columns: ['col1', 'col2', ''], + rows: [ + [1, 2], + [4, 5, 6], + ['', ''], + [], + ], +} + class TestSource extends Table.TableDataSource { setRows(rows) { const data = { @@ -32,6 +42,8 @@ export const testDataSource = new TestSource(testData) export const testDataSourceUneven = new TestSource(testDataUneven) +export const testDataSourceEmpty = new TestSource(testDataEmpty) + export const selection = {colIdx: 0, rowIdx: 0, key: null} export const cellProps = {tableDataSource: testDataSource, selection, colIdx: 0, rowIdx: 0, onSetSelection: () => {}, onCellEdited: () => {}} diff --git a/src/components/table/table-data-source.es6 b/src/components/table/table-data-source.es6 index b4d495505..db97a46bc 100644 --- a/src/components/table/table-data-source.es6 +++ b/src/components/table/table-data-source.es6 @@ -45,7 +45,8 @@ export default class TableDataSource { * @method colAt */ colAt(colIdx) { - return this._tableData.columns[colIdx] || null + const col = this._tableData.columns[colIdx] + return col != null ? col : null } /** @@ -90,7 +91,41 @@ export default class TableDataSource { return this.colAt(colIdx) } const row = this.rowAt(rowIdx) - return row ? row[colIdx] || null : null + const cell = row ? row[colIdx] : null + return cell != null ? cell : null + } + + /** + * Returns true if the given cell, column, or row is empty + * + * ``` + * source.isEmpty({rowIdx: 1}) // true if row 1 is empty + * ``` + * + * @param {object} arg + * @param {number} arg.rowIdx - Row index of cell + * @param {number} arg.colIdx - Col index of cell + * @return {any} - value for cell at given indices or null if it does not exist + * @method cellAt + */ + isEmpty({rowIdx, colIdx} = {}) { + if (rowIdx == null && colIdx == null) { + throw new Error('TableDataSource::isEmpty - Must provide rowIdx and/or colIdx') + } + if (rowIdx == null) { + const col = this.colAt(colIdx) + if (col == null) { + throw new Error('TableDataSource::isEmpty - Must provide a valid colIdx') + } + } + const row = this.rowAt(rowIdx) + if (!row) { + throw new Error('TableDataSource::isEmpty - Must provide a valid rowIdx') + } + if (colIdx == null) { + return row.every((el) => !el) + } + return !this.cellAt({rowIdx, colIdx}) } /** diff --git a/src/pro b/src/pro index e04a35d00..0e0a398fb 160000 --- a/src/pro +++ b/src/pro @@ -1 +1 @@ -Subproject commit e04a35d00a2320a915d4164c886c6d7dd3f0d8f3 +Subproject commit 0e0a398fbcb4445c10d053da8636b552e0700201