Skip to content
This repository has been archived by the owner on Jan 5, 2023. It is now read-only.

fix(Table): Fixes headless Table as previously it started to throw an error #648

Merged
merged 5 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/iTwinUI-react/src/core/Table/Table.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2880,3 +2880,24 @@ it('should render selectable rows without select column', () => {
expect(rows[2].classList).not.toContain('iui-selected');
expect(onRowClick).toHaveBeenCalledTimes(3);
});

it('should not throw on headless table', () => {
const columns: Column<TestDataType>[] = [
{
id: 'name',
Header: 'Name',
accessor: 'name',
},
{
id: 'description',
Header: 'Description',
accessor: 'description',
},
];
const { container } = renderComponent({
columns,
});

expect(container.querySelector('.iui-table-header .iui-row')).toBeFalsy();
expect(container.querySelector('.iui-table-body')).toBeTruthy();
});
28 changes: 21 additions & 7 deletions packages/iTwinUI-react/src/core/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,23 @@ export type TableProps<
enableColumnReordering?: boolean;
} & Omit<CommonProps, 'title'>;

// Original type for some reason is missing sub-columns
type ColumnType<
T extends Record<string, unknown> = Record<string, unknown>
> = Column<T> & {
columns: ColumnType[];
};
const flattenColumns = (columns: ColumnType[]): ColumnType[] => {
const flatColumns: ColumnType[] = [];
columns.forEach((column) => {
flatColumns.push(column);
if (column.columns) {
flatColumns.push(...flattenColumns(column.columns));
}
});
return flatColumns;
};

/**
* Table based on [react-table](https://react-table.tanstack.com/docs/api/overview).
* @example
Expand Down Expand Up @@ -351,13 +368,10 @@ export const Table = <
onRowInViewportRef.current = onRowInViewport;
}, [onBottomReached, onRowInViewport]);

// Original type for some reason is missing sub-columns
type ColumnType = Column<T> & {
columns: Column<T>[];
};
const hasManualSelectionColumn = (columns as ColumnType[])[0]?.columns.some(
(column) => column.id === SELECTION_CELL_ID,
);
const hasManualSelectionColumn = React.useMemo(() => {
const flatColumns = flattenColumns(columns as ColumnType[]);
return flatColumns.some((column) => column.id === SELECTION_CELL_ID);
}, [columns]);

const tableStateReducer = React.useCallback(
(
Expand Down
10 changes: 9 additions & 1 deletion packages/iTwinUI-react/src/core/Table/cells/EditableCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ export type EditableCellProps<
export const EditableCell = <T extends Record<string, unknown>>(
props: EditableCellProps<T>,
) => {
const { cellElementProps, cellProps, onCellEdit, children, ...rest } = props;
const {
cellElementProps,
cellProps,
onCellEdit,
children,
isDisabled,
...rest
} = props;
isDisabled; // To omit and prevent eslint error.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cant we omit this prop before it even reaches editable cell? Looks weird now..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want to ask our users not to pass {...props}? 😀
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh... thats weird..
Cant editable cell be disabled, afterall? :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then it would be just a default cell, doesn't make sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats fine. Users would not need to switch between cells in their custom implementations.
They just pass all the props and be calm about it.


const sanitizeString = (text: string) => {
return text.replace(/(\r\n|\n|\r)+/gm, ' ');
Expand Down