Skip to content

Commit

Permalink
feat: support listItemHeight
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Aug 22, 2023
1 parent 9bdcdd4 commit 28d54a6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/VirtualTable/BodyGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
'expandedKeys',
'childrenColumnName',
]);
const { scrollY, scrollX } = useContext(StaticContext);
const { scrollY, scrollX, listItemHeight } = useContext(StaticContext);

// =========================== Ref ============================
const listRef = React.useRef<ListRef>();
Expand Down Expand Up @@ -182,7 +182,7 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
ref={listRef}
className={classNames(tblPrefixCls, `${tblPrefixCls}-virtual`)}
height={scrollY}
itemHeight={24}
itemHeight={listItemHeight || 24}
data={flattenData}
itemKey={item => getRowKey(item.record)}
scrollWidth={scrollX}
Expand Down
1 change: 1 addition & 0 deletions src/VirtualTable/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createContext } from '@rc-component/context';
export interface StaticContextProps {
scrollX: number;
scrollY: number;
listItemHeight: number;
}

export const StaticContext = createContext<StaticContextProps>(null);
Expand Down
15 changes: 11 additions & 4 deletions src/VirtualTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ const renderBody: CustomizeScrollBody<any> = (rawData, props) => {
return <Grid ref={ref} data={rawData as any} onScroll={onScroll} />;
};

export interface StaticTableProps<RecordType> extends Omit<TableProps<RecordType>, 'scroll'> {
export interface VirtualTableProps<RecordType> extends Omit<TableProps<RecordType>, 'scroll'> {
scroll: {
x: number;
y: number;
};
listItemHeight?: number;
}

const PRESET_COLUMN_WIDTH = 100;

function VirtualTable<RecordType>(props: StaticTableProps<RecordType>) {
const { columns, scroll, prefixCls = DEFAULT_PREFIX, className } = props;
function VirtualTable<RecordType>(props: VirtualTableProps<RecordType>) {
const { columns, scroll, prefixCls = DEFAULT_PREFIX, className, listItemHeight } = props;

const { x: scrollX, y: scrollY } = scroll || {};
let mergedScrollX = scrollX;
Expand All @@ -40,9 +41,15 @@ function VirtualTable<RecordType>(props: StaticTableProps<RecordType>) {
}
}

// ========================= Context ==========================
const context = React.useMemo(
() => ({ scrollX: mergedScrollX, scrollY, listItemHeight }),
[mergedScrollX, scrollY, listItemHeight],
);

// ========================== Render ==========================
return (
<StaticContext.Provider value={{ scrollX: mergedScrollX, scrollY }}>
<StaticContext.Provider value={context}>
<Table
{...props}
className={classNames(className, `${prefixCls}-virtual`)}
Expand Down

0 comments on commit 28d54a6

Please sign in to comment.