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

feat(Cell): make Cell, CellProps and InnerCellProps generic #458

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
298 changes: 151 additions & 147 deletions src/Cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@ import { useClassNames } from './utils';
import TableContext from './TableContext';
import ArrowRight from '@rsuite/icons/ArrowRight';
import ArrowDown from '@rsuite/icons/ArrowDown';
import { StandardProps, RowDataType } from './@types/common';
import { StandardProps } from './@types/common';
import { columnHandledProps } from './Column';

export interface CellProps extends StandardProps {
export interface CellProps<T = any> extends StandardProps {
/** Data binding key, but also a sort of key */
dataKey?: string;

/** Row Number */
rowIndex?: number;

/** Row Data */
rowData?: any;
rowData?: T;
}

export interface InnerCellProps extends Omit<CellProps, 'children'> {
export interface InnerCellProps<RowData = any> extends Omit<CellProps<RowData>, 'children'> {
align?: 'left' | 'center' | 'right';
verticalAlign?: 'top' | 'middle' | 'bottom';
isHeaderCell?: boolean;
width?: number;
height?: number | ((rowData: RowDataType) => number);
height?: number | ((rowData: RowData) => number);
left?: number;
headerHeight?: number;
style?: React.CSSProperties;
fullText?: boolean;
firstColumn?: boolean;
lastColumn?: boolean;
hasChildren?: boolean;
children?: React.ReactNode | ((rowData: RowDataType, rowIndex?: number) => React.ReactNode);
children?: React.ReactNode | ((rowData: RowData, rowIndex?: number) => React.ReactNode);
rowKey?: string | number;
rowSpan?: number;
depth?: number;
Expand All @@ -48,13 +48,13 @@ export interface InnerCellProps extends Omit<CellProps, 'children'> {
onTreeToggle?: (
rowKey?: string | number,
rowIndex?: number,
rowData?: RowDataType,
rowData?: RowData,
event?: React.MouseEvent
) => void;

renderTreeToggle?: (
expandButton: React.ReactNode,
rowData?: RowDataType,
rowData?: RowData,
expanded?: boolean
) => React.ReactNode;
renderCell?: (contentChildren: any) => React.ReactNode;
Expand All @@ -69,154 +69,158 @@ const groupKeys = [
'renderSortIcon'
];

const Cell = React.forwardRef((props: InnerCellProps, ref: React.Ref<HTMLDivElement>) => {
const {
classPrefix = 'cell',
width = 0,
left = 0,
headerHeight = ROW_HEADER_HEIGHT,
depth = 0,
height = ROW_HEIGHT,
style,
className,
fullText,
firstColumn,
lastColumn,
isHeaderCell,
align,
children,
rowData,
dataKey,
rowIndex,
removed,
rowKey,
rowSpan,
wordWrap,
verticalAlign,
expanded,
treeCol,
hasChildren,
predefinedStyle,
renderCell,
renderTreeToggle,
onClick,
onTreeToggle,
...rest
} = props;

const { rtl, hasCustomTreeCol, isTree } = React.useContext(TableContext);

const isTreeCol = treeCol || (!hasCustomTreeCol && firstColumn && isTree);
const cellHeight = typeof height === 'function' ? height(rowData) : height;

if (isTreeCol && !isHeaderCell && !rowData) {
throw new Error('[Table.Cell]: `rowData` is required for tree column');
}
const Cell = React.forwardRef(
<RowData = any,>(props: InnerCellProps<RowData>, ref: React.Ref<HTMLDivElement>) => {
const {
classPrefix = 'cell',
width = 0,
left = 0,
headerHeight = ROW_HEADER_HEIGHT,
depth = 0,
height = ROW_HEIGHT,
style,
className,
fullText,
firstColumn,
lastColumn,
isHeaderCell,
align,
children,
rowData,
dataKey,
rowIndex,
removed,
rowKey,
rowSpan,
wordWrap,
verticalAlign,
expanded,
treeCol,
hasChildren,
predefinedStyle,
renderCell,
renderTreeToggle,
onClick,
onTreeToggle,
...rest
} = props;

const handleTreeToggle = useCallback(
(event: React.MouseEvent) => {
onTreeToggle?.(rowKey, rowIndex, rowData, event);
},
[onTreeToggle, rowData, rowIndex, rowKey]
);

const { withClassPrefix, merge, prefix } = useClassNames(classPrefix);
const classes = merge(
className,
withClassPrefix({
expanded: expanded && isTreeCol,
first: firstColumn,
last: lastColumn,
rowspan: rowSpan && !isHeaderCell,
'full-text': fullText
})
);

const nextHeight = isHeaderCell ? headerHeight : cellHeight;
const styles = {
...predefinedStyle,
[fullText ? 'minWidth' : 'width']: width,
height: nextHeight,
zIndex: depth,
[rtl ? 'right' : 'left']: left
};

const paddingKey = rtl ? 'paddingRight' : 'paddingLeft';
const contentStyles: React.CSSProperties = {
...style,
width: fullText ? width - 1 : width,
height: nextHeight,
textAlign: align,
[paddingKey]: isTreeCol ? depth * LAYER_WIDTH + 10 : style?.[paddingKey] || style?.padding
};

if (verticalAlign) {
contentStyles.display = 'table-cell';
contentStyles.verticalAlign = verticalAlign;
}
const { rtl, hasCustomTreeCol, isTree } = React.useContext(TableContext);

if (wordWrap) {
contentStyles.wordBreak = typeof wordWrap === 'boolean' ? 'break-all' : wordWrap;
contentStyles.overflowWrap = wordWrap === 'break-word' ? wordWrap : undefined;
}
const isTreeCol = treeCol || (!hasCustomTreeCol && firstColumn && isTree);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const cellHeight = typeof height === 'function' ? height(rowData!) : height;

let cellContent = isNil(children) && rowData && dataKey ? get(rowData, dataKey) : children;
if (isTreeCol && !isHeaderCell && !rowData) {
throw new Error('[Table.Cell]: `rowData` is required for tree column');
}

if (typeof children === 'function') {
cellContent = children(rowData, rowIndex);
}
const handleTreeToggle = useCallback(
(event: React.MouseEvent) => {
onTreeToggle?.(rowKey, rowIndex, rowData, event);
},
[onTreeToggle, rowData, rowIndex, rowKey]
);

const { withClassPrefix, merge, prefix } = useClassNames(classPrefix);
const classes = merge(
className,
withClassPrefix({
expanded: expanded && isTreeCol,
first: firstColumn,
last: lastColumn,
rowspan: rowSpan && !isHeaderCell,
'full-text': fullText
})
);

const renderTreeNodeExpandIcon = () => {
const ExpandIconComponent = expanded ? ArrowDown : ArrowRight;
const expandButton = <ExpandIconComponent className={prefix('expand-icon')} />;

if (isTreeCol && hasChildren) {
return (
<span
role="button"
tabIndex={-1}
className={prefix('expand-wrapper')}
onClick={handleTreeToggle}
>
{renderTreeToggle ? renderTreeToggle(expandButton, rowData, expanded) : expandButton}
</span>
);
const nextHeight = isHeaderCell ? headerHeight : cellHeight;
const styles = {
...predefinedStyle,
[fullText ? 'minWidth' : 'width']: width,
height: nextHeight,
zIndex: depth,
[rtl ? 'right' : 'left']: left
};

const paddingKey = rtl ? 'paddingRight' : 'paddingLeft';
const contentStyles: React.CSSProperties = {
...style,
width: fullText ? width - 1 : width,
height: nextHeight,
textAlign: align,
[paddingKey]: isTreeCol ? depth * LAYER_WIDTH + 10 : style?.[paddingKey] || style?.padding
};

if (verticalAlign) {
contentStyles.display = 'table-cell';
contentStyles.verticalAlign = verticalAlign;
}

return null;
};

const content = wordWrap ? (
<div className={prefix('wrap')}>
{renderTreeNodeExpandIcon()}
{renderCell ? renderCell(cellContent) : cellContent}
</div>
) : (
<>
{renderTreeNodeExpandIcon()}
{renderCell ? renderCell(cellContent) : cellContent}
</>
);

if (removed) {
return null;
}
if (wordWrap) {
contentStyles.wordBreak = typeof wordWrap === 'boolean' ? 'break-all' : wordWrap;
contentStyles.overflowWrap = wordWrap === 'break-word' ? wordWrap : undefined;
}

let cellContent = isNil(children) && rowData && dataKey ? get(rowData, dataKey) : children;

return (
<div
ref={ref}
role={isHeaderCell ? 'columnheader' : 'gridcell'}
{...omit(rest, [...groupKeys, ...columnHandledProps])}
onClick={onClick}
className={classes}
style={styles}
>
<div className={prefix('content')} style={contentStyles}>
{content}
if (typeof children === 'function') {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
cellContent = children(rowData!, rowIndex);
}

const renderTreeNodeExpandIcon = () => {
const ExpandIconComponent = expanded ? ArrowDown : ArrowRight;
const expandButton = <ExpandIconComponent className={prefix('expand-icon')} />;

if (isTreeCol && hasChildren) {
return (
<span
role="button"
tabIndex={-1}
className={prefix('expand-wrapper')}
onClick={handleTreeToggle}
>
{renderTreeToggle ? renderTreeToggle(expandButton, rowData, expanded) : expandButton}
</span>
);
}

return null;
};

const content = wordWrap ? (
<div className={prefix('wrap')}>
{renderTreeNodeExpandIcon()}
{renderCell ? renderCell(cellContent) : cellContent}
</div>
</div>
);
});
) : (
<>
{renderTreeNodeExpandIcon()}
{renderCell ? renderCell(cellContent) : cellContent}
</>
);

if (removed) {
return null;
}

return (
<div
ref={ref}
role={isHeaderCell ? 'columnheader' : 'gridcell'}
{...omit(rest, [...groupKeys, ...columnHandledProps])}
onClick={onClick}
className={classes}
style={styles}
>
<div className={prefix('content')} style={contentStyles}>
{content}
</div>
</div>
);
}
);

Cell.displayName = 'Table.Cell';
Cell.propTypes = {
Expand Down
Loading