Skip to content

Commit

Permalink
fix(table): fix an issue when key in columnDefinition doesn't exist i…
Browse files Browse the repository at this point in the history
…n TableItems (#146)

fix: remove unused variables.
  • Loading branch information
square-li committed Sep 13, 2022
1 parent ba18720 commit 13c7dfe
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/table/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 5 additions & 3 deletions packages/table/src/table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import { TableProps } from '../utils';
import { defaultI18nStrings, getDefaultColumnDefinitions } from '../utils/tableHelpers';

export const Table: FunctionComponent<TableProps> = (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 (
<AWSUITable
{...props}
items={items}
{...collectionProps}
columnDefinitions={getDefaultColumnDefinitions(columnDefinitions)}
filter={<PropertyFilter {...propertyFilterProps} i18nStrings={defaultI18nStrings} />}
filter={propertyFiltering && <PropertyFilter {...propertyFilterProps} i18nStrings={defaultI18nStrings} />}
/>
);
};
29 changes: 25 additions & 4 deletions packages/table/src/utils/tableHelpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -54,14 +58,31 @@ describe('default cell function', () => {
valueOf: jest.fn(),
},
};
const cell = columnDef.cell(item) as ReactElement<HTMLSpanElement>;
const cell = firstColumnDef.cell(item) as ReactElement<HTMLSpanElement>;
act(() => {
root.render(cell);
});

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<HTMLSpanElement>;
act(() => {
root.render(cell);
});

expect(container.textContent).toBeEmpty();
});

it('returns error message when in error state', () => {
const item: TableItem = {
data: {
Expand All @@ -73,7 +94,7 @@ describe('default cell function', () => {
valueOf: jest.fn(),
},
};
const cell = columnDef.cell(item) as ReactElement<HTMLSpanElement>;
const cell = firstColumnDef.cell(item) as ReactElement<HTMLSpanElement>;
act(() => {
root.render(cell);
});
Expand All @@ -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);
});
Expand Down
20 changes: 10 additions & 10 deletions packages/table/src/utils/tableHelpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ import { getIcons } from './iconUtils';
import { LoadingSpinner } from './spinner';

export const getDefaultColumnDefinitions: (
useColumnDefinitions: ColumnDefinition<TableItem>[]
) => AWSUITableProps.ColumnDefinition<TableItem>[] = (useColumnDefinitions) => {
return useColumnDefinitions.map((colDef) => ({
columnDefinitions: ColumnDefinition<TableItem>[]
) => AWSUITableProps.ColumnDefinition<TableItem>[] = (columnDefinitions) => {
return columnDefinitions.map((colDef) => ({
cell: (item: TableItem) => {
if (!(colDef.key in item)) {
return <span />;
}

const { error, isLoading, value, threshold } = item[colDef.key];
const { color = 'unset', icon } = threshold || {};
if (error) {
return (
<div className="error">
<>
{getIcons(StatusIcon.ERROR)} {error.msg}
</div>
</>
);
}

if (isLoading) {
return (
<div className="loading-wrapper">
<LoadingSpinner size={16} />
</div>
);
return <LoadingSpinner size={16} />;
}

if (colDef.formatter && value) {
Expand Down

0 comments on commit 13c7dfe

Please sign in to comment.