Skip to content

Commit

Permalink
Fix column id check in DefaultCell (#2218)
Browse files Browse the repository at this point in the history
  • Loading branch information
veekeys authored Sep 6, 2024
1 parent 547c780 commit 06f08f7
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-badgers-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@itwin/itwinui-react': patch
---

Fix `Table` cell not taking full width when custom `Cell` implementation is passed.
39 changes: 39 additions & 0 deletions packages/itwinui-react/src/core/Table/Table.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3994,3 +3994,42 @@ it('should pass custom props to different parts of Table', () => {
expect(emptyTableContent).toBeTruthy();
expect(emptyTableContent.style.fontSize).toBe('12px');
});

it('should apply clamp, if cell is string value and no custom Cell is rendered', () => {
const data = [{ name: 'name' }];
const columns: Column<TestDataType>[] = [
{
Header: 'Name',
accessor: 'name',
cellClassName: 'test-cell',
},
];
const { container } = renderComponent({
columns,
data,
});
const host = container.querySelector('.test-cell');
expect(host?.shadowRoot).toBeTruthy();
const lineClamp = host?.shadowRoot?.querySelector('div');
expect(lineClamp).toBeTruthy();
});

it('should not apply clamp, if custom Cell is used', () => {
const data = [{ name: 'name' }];
const columns: Column<TestDataType>[] = [
{
Header: 'Name',
accessor: 'name',
cellClassName: 'test-cell',
Cell: () => 'my custom content',
},
];
const { container } = renderComponent({
columns,
data,
});
const host = container.querySelector('.test-cell');
expect(host?.shadowRoot).toBeTruthy();
const lineClamp = host?.shadowRoot?.querySelector('div');
expect(lineClamp).toBeNull();
});
5 changes: 2 additions & 3 deletions packages/itwinui-react/src/core/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type {
ActionType,
TableInstance,
Column,
ColumnInstance,
} from '../../react-table/react-table.js';
import { ProgressRadial } from '../ProgressIndicators/ProgressRadial.js';
import {
Expand Down Expand Up @@ -906,9 +907,7 @@ export const Table = <
}, []);

return (
<TableColumnsContext.Provider
value={columns as Column<Record<string, unknown>>[]}
>
<TableColumnsContext.Provider value={instance.columns as ColumnInstance[]}>
<Box
ref={useMergedRefs<HTMLDivElement>(
tableRef,
Expand Down
10 changes: 5 additions & 5 deletions packages/itwinui-react/src/core/Table/cells/DefaultCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import * as React from 'react';
import { defaultColumn } from 'react-table';
import type { CellRendererProps } from '../../../react-table/react-table.js';
import cx from 'classnames';
import { Box, LineClamp, ShadowRoot } from '../../../utils/index.js';
Expand Down Expand Up @@ -45,13 +46,12 @@ export type DefaultCellProps<T extends Record<string, unknown>> = {
export const DefaultCell = <T extends Record<string, unknown>>(
props: DefaultCellProps<T>,
) => {
const columnsProp = React.useContext(TableColumnsContext);
const instanceColumns = React.useContext(TableColumnsContext);
const isCustomCell = React.useMemo(
() =>
columnsProp
.find(({ id }) => props.cellProps.column.id === id)
?.hasOwnProperty('Cell'),
[props.cellProps.column.id, columnsProp],
instanceColumns.find(({ id }) => props.cellProps.column.id === id)
?.Cell !== defaultColumn.Cell,
[instanceColumns, props.cellProps.column.id],
);

const {
Expand Down
6 changes: 2 additions & 4 deletions packages/itwinui-react/src/core/Table/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import * as React from 'react';
import type { ColumnInstance, Column } from '../../react-table/react-table.js';
import type { ColumnInstance } from '../../react-table/react-table.js';

export const getCellStyle = <T extends Record<string, unknown>>(
column: ColumnInstance<T>,
Expand Down Expand Up @@ -82,6 +82,4 @@ export const getSubRowStyle = ({ density = 'default', depth = 1 }) => {
} satisfies React.CSSProperties;
};

export const TableColumnsContext = React.createContext<
Column<Record<string, unknown>>[]
>([]);
export const TableColumnsContext = React.createContext<ColumnInstance[]>([]);

0 comments on commit 06f08f7

Please sign in to comment.