Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduced props accepted by ColumnHeader #2287

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
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
74 changes: 30 additions & 44 deletions packages/itwinui-react/src/core/Table/ColumnHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,27 @@ import {
SvgSortUp,
} from '../../utils/index.js';
import type {
ColumnInstance,
HeaderGroup,
TableKeyedProps,
TableState,
TableInstance,
} from '../../react-table/react-table.js';
import { SELECTION_CELL_ID } from './columns/index.js';
import { FilterToggle } from './filters/FilterToggle.js';
import { getCellStyle, getSubRowStyle, getStickyStyle } from './utils.js';
import cx from 'classnames';

type ColumnHeaderProps<
T extends Record<string, unknown> = Record<string, unknown>,
> = TableKeyedProps & {
columnRefs: React.MutableRefObject<Record<string, HTMLDivElement>>;
column: HeaderGroup<T>;
index: number;
areFiltersSet: boolean;
hasAnySubRows: boolean;
headers: HeaderGroup<T>[];
state: TableState<T>;
data: T[];
isResizable: boolean;
columnResizeMode: 'fit' | 'expand';
enableColumnReordering: boolean;
density: string | undefined;
visibleColumns: ColumnInstance<T>[];
columnHasExpanders: boolean;
isLast: boolean;
isTableEmpty: boolean;
instance: TableInstance<T>;
};

export const ColumnHeader = <
Expand All @@ -45,19 +40,16 @@ export const ColumnHeader = <
props: ColumnHeaderProps<T>,
): JSX.Element => {
const {
columnRefs,
column,
index,
areFiltersSet,
hasAnySubRows,
headers,
state,
data,
isResizable,
columnResizeMode,
enableColumnReordering,
density,
visibleColumns,
columnHasExpanders,
isLast,
isTableEmpty,
instance,
...rest
} = props;

Expand All @@ -69,20 +61,13 @@ export const ColumnHeader = <
};

const showFilterButton = (column: HeaderGroup<T>) =>
(data.length !== 0 || areFiltersSet) && column.canFilter && !!column.Filter;
(!isTableEmpty || areFiltersSet) && column.canFilter && !!column.Filter;

const showSortButton = (column: HeaderGroup<T>) =>
data.length !== 0 && column.canSort;
!isTableEmpty && column.canSort;

const { onClick, ...restSortProps } = column.getSortByToggleProps();

const columnHasExpanders =
hasAnySubRows &&
index ===
headers.findIndex(
(c) => c.id !== SELECTION_CELL_ID, // first non-selection column is the expander column
);

if ([undefined, 0].includes(column.minWidth)) {
// override "undefined" or zero min-width with default value
column.minWidth = columnHasExpanders
Expand All @@ -107,9 +92,9 @@ export const ColumnHeader = <
column.columnClassName,
),
style: {
...getCellStyle(column, !!state.isTableResizing),
...getCellStyle(column, !!instance.state.isTableResizing),
...(columnHasExpanders && getSubRowStyle({ density })),
...getStickyStyle(column, visibleColumns),
...getStickyStyle(column, instance.visibleColumns),
flexWrap: 'wrap',
columnGap: 'var(--iui-size-xs)',
},
Expand All @@ -121,15 +106,6 @@ export const ColumnHeader = <
{...rest}
key={columnProps.key}
title={undefined}
ref={React.useCallback(
(el: HTMLDivElement) => {
if (el) {
columnRefs.current[column.id] = el;
column.resizeWidth = el.getBoundingClientRect().width;
}
},
[column, columnRefs],
)}
onMouseDown={() => {
isHeaderDirectClick.current = true;
}}
Expand All @@ -140,6 +116,14 @@ export const ColumnHeader = <
isHeaderDirectClick.current = false;
}
}}
ref={React.useCallback(
(el: HTMLDivElement) => {
if (el) {
column.resizeWidth = el.getBoundingClientRect().width;
}
},
[column],
)}
tabIndex={showSortButton(column) ? 0 : undefined}
onKeyDown={(e) => {
if (e.key == 'Enter' && showSortButton(column)) {
Expand Down Expand Up @@ -183,7 +167,7 @@ export const ColumnHeader = <
)}
{isResizable &&
column.isResizerVisible &&
(index !== headers.length - 1 || columnResizeMode === 'expand') && (
(!isLast || columnResizeMode === 'expand') && (
<Box
{...column.getResizerProps()}
className='iui-table-resizer'
Expand All @@ -195,12 +179,14 @@ export const ColumnHeader = <
{enableColumnReordering && !column.disableReordering && (
<Box className='iui-table-reorder-bar' slot='resizers' />
)}
{column.sticky === 'left' && state.sticky.isScrolledToRight && (
<Box className='iui-table-cell-shadow-right' slot='shadows' />
)}
{column.sticky === 'right' && state.sticky.isScrolledToLeft && (
<Box className='iui-table-cell-shadow-left' slot='shadows' />
)}
{column.sticky === 'left' &&
instance.state.sticky.isScrolledToRight && (
<Box className='iui-table-cell-shadow-right' slot='shadows' />
)}
{column.sticky === 'right' &&
instance.state.sticky.isScrolledToLeft && (
<Box className='iui-table-cell-shadow-left' slot='shadows' />
)}
</>
</Box>
);
Expand Down
18 changes: 10 additions & 8 deletions packages/itwinui-react/src/core/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,6 @@ export const Table = <
gotoPage,
setPageSize,
flatHeaders,
visibleColumns,
setGlobalFilter,
} = instance;

Expand Down Expand Up @@ -1003,19 +1002,22 @@ export const Table = <
<ColumnHeader<T>
{...dragAndDropProps}
key={dragAndDropProps.key || column.id || index}
columnRefs={columnRefs}
column={column}
index={index}
areFiltersSet={areFiltersSet}
hasAnySubRows={hasAnySubRows}
headers={headerGroup.headers}
state={state}
data={data}
columnHasExpanders={
hasAnySubRows &&
index ===
headerGroup.headers.findIndex(
(c) => c.id !== SELECTION_CELL_ID, // first non-selection column is the expander column
)
}
isLast={index === headerGroup.headers.length - 1}
isTableEmpty={data.length === 0}
isResizable={isResizable}
columnResizeMode={columnResizeMode}
enableColumnReordering={enableColumnReordering}
density={density}
visibleColumns={visibleColumns}
instance={instance}
/>
);
})}
Expand Down
Loading