From 13c7dfe10dc4a8c02621d08bc1b959d647c083fb Mon Sep 17 00:00:00 2001 From: "Xiao(Bill) Li" Date: Thu, 7 Jul 2022 13:39:16 -0400 Subject: [PATCH] fix(table): fix an issue when key in columnDefinition doesn't exist in TableItems (#146) fix: remove unused variables. --- packages/table/package.json | 2 +- packages/table/src/table/index.tsx | 8 +++-- packages/table/src/utils/tableHelpers.spec.ts | 29 ++++++++++++++++--- packages/table/src/utils/tableHelpers.tsx | 20 ++++++------- 4 files changed, 41 insertions(+), 18 deletions(-) diff --git a/packages/table/package.json b/packages/table/package.json index 84860a379..d1c7ed39c 100644 --- a/packages/table/package.json +++ b/packages/table/package.json @@ -3,7 +3,7 @@ "publishConfig": { "access": "public" }, - "version": "1.3.0", + "version": "1.4.0", "description": "IoT Application Kit - Table component", "license": "Apache-2.0", "main": "./dist/index.cj.js", diff --git a/packages/table/src/table/index.tsx b/packages/table/src/table/index.tsx index 3dc2f0824..f20b1094c 100644 --- a/packages/table/src/table/index.tsx +++ b/packages/table/src/table/index.tsx @@ -5,14 +5,16 @@ import { TableProps } from '../utils'; import { defaultI18nStrings, getDefaultColumnDefinitions } from '../utils/tableHelpers'; export const Table: FunctionComponent = (props) => { - const { items, useCollectionOption = { sorting: {} }, columnDefinitions } = props; - const { collectionProps, propertyFilterProps } = useCollection(items, useCollectionOption); + const { items: initalItems, useCollectionOption = { sorting: {} }, columnDefinitions } = props; + const { items, collectionProps, propertyFilterProps } = useCollection(initalItems, useCollectionOption); + const { propertyFiltering } = useCollectionOption; return ( } + filter={propertyFiltering && } /> ); }; diff --git a/packages/table/src/utils/tableHelpers.spec.ts b/packages/table/src/utils/tableHelpers.spec.ts index efebc97eb..2cc6db824 100644 --- a/packages/table/src/utils/tableHelpers.spec.ts +++ b/packages/table/src/utils/tableHelpers.spec.ts @@ -42,8 +42,12 @@ describe('default cell function', () => { key: 'data', header: 'Data', }, + { + key: 'notExist', + header: 'Not Exist', + }, ]; - const columnDef = getDefaultColumnDefinitions(userColumnDefinitions)[0]; + const [firstColumnDef, secondColumnDef] = getDefaultColumnDefinitions(userColumnDefinitions); it("returns item's value", () => { const item: TableItem = { @@ -54,7 +58,7 @@ describe('default cell function', () => { valueOf: jest.fn(), }, }; - const cell = columnDef.cell(item) as ReactElement; + const cell = firstColumnDef.cell(item) as ReactElement; act(() => { root.render(cell); }); @@ -62,6 +66,23 @@ describe('default cell function', () => { expect(container.textContent).toContain('10'); }); + it('return empty when property not found in tableItem', () => { + const item: TableItem = { + data: { + value: 10, + error: undefined, + isLoading: undefined, + valueOf: jest.fn(), + }, + }; + const cell = secondColumnDef.cell(item) as ReactElement; + act(() => { + root.render(cell); + }); + + expect(container.textContent).toBeEmpty(); + }); + it('returns error message when in error state', () => { const item: TableItem = { data: { @@ -73,7 +94,7 @@ describe('default cell function', () => { valueOf: jest.fn(), }, }; - const cell = columnDef.cell(item) as ReactElement; + const cell = firstColumnDef.cell(item) as ReactElement; act(() => { root.render(cell); }); @@ -91,7 +112,7 @@ describe('default cell function', () => { }, }; - const cell = columnDef.cell(item) as ReactElement; + const cell = firstColumnDef.cell(item) as ReactElement; act(() => { root.render(cell); }); diff --git a/packages/table/src/utils/tableHelpers.tsx b/packages/table/src/utils/tableHelpers.tsx index faa6f3563..a7ec98b4d 100644 --- a/packages/table/src/utils/tableHelpers.tsx +++ b/packages/table/src/utils/tableHelpers.tsx @@ -7,26 +7,26 @@ import { getIcons } from './iconUtils'; import { LoadingSpinner } from './spinner'; export const getDefaultColumnDefinitions: ( - useColumnDefinitions: ColumnDefinition[] -) => AWSUITableProps.ColumnDefinition[] = (useColumnDefinitions) => { - return useColumnDefinitions.map((colDef) => ({ + columnDefinitions: ColumnDefinition[] +) => AWSUITableProps.ColumnDefinition[] = (columnDefinitions) => { + return columnDefinitions.map((colDef) => ({ cell: (item: TableItem) => { + if (!(colDef.key in item)) { + return ; + } + const { error, isLoading, value, threshold } = item[colDef.key]; const { color = 'unset', icon } = threshold || {}; if (error) { return ( -
+ <> {getIcons(StatusIcon.ERROR)} {error.msg} -
+ ); } if (isLoading) { - return ( -
- -
- ); + return ; } if (colDef.formatter && value) {