Skip to content

Commit

Permalink
chore: support fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Aug 17, 2023
1 parent e6a3306 commit 5348044
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/Body/BodyRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export function getCellProps<RecordType>(
);
}

let additionalCellProps: React.HTMLAttributes<HTMLElement>;
let additionalCellProps: React.HTMLAttributes<HTMLElement> = {};
if (column.onCell) {
additionalCellProps = column.onCell(record, index);
}
Expand Down
26 changes: 17 additions & 9 deletions src/StaticTable/BodyGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import classNames from 'classnames';
import VirtualList from 'rc-virtual-list';
import * as React from 'react';
import TableContext from '../context/TableContext';
import useFlattenRecords, { FlattenData } from '../hooks/useFlattenRecords';

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused import FlattenData.
import BodyLine from './BodyLine';
import StaticContext from './StaticContext';

Expand All @@ -13,17 +14,23 @@ export interface GridProps<RecordType = any> {
const Grid = React.forwardRef((props: GridProps, ref: any) => {
const { data } = props;

const { flattenColumns, onColumnResize, getRowKey, prefixCls } = useContext(TableContext, [
'flattenColumns',
'onColumnResize',
'getRowKey',
'prefixCls',
]);
const { flattenColumns, onColumnResize, getRowKey, expandedKeys, prefixCls, childrenColumnName } =
useContext(TableContext, [
'flattenColumns',
'onColumnResize',
'getRowKey',
'prefixCls',
'expandedKeys',
'childrenColumnName',
]);
const { scrollY, scrollX } = useContext(StaticContext);

// const context = useContext(TableContext);
// console.log('=>', context, scrollX, scrollY);

// =========================== Data ===========================
const flattenData = useFlattenRecords(data, childrenColumnName, expandedKeys, getRowKey);

// ========================== Column ==========================
const columnsWidth = React.useMemo<[key: React.Key, width: number][]>(
() => flattenColumns.map(({ width, key }) => [key, width as number]),
Expand All @@ -41,16 +48,17 @@ const Grid = React.forwardRef((props: GridProps, ref: any) => {

return (
<div ref={ref}>
<VirtualList
<VirtualList<FlattenData<any>>
className={classNames(tblPrefixCls, `${tblPrefixCls}-virtual`)}
height={scrollY}
itemHeight={24}
data={data}
data={flattenData}
itemKey={getRowKey}
scrollWidth={scrollX}
>
{(item, index, itemProps) => {
return <BodyLine record={item} index={index} {...itemProps} />;
const rowKey = getRowKey(item, index);
return <BodyLine data={item} rowKey={rowKey} index={index} {...itemProps} />;
}}
</VirtualList>
</div>
Expand Down
43 changes: 28 additions & 15 deletions src/StaticTable/BodyLine.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import { useContext } from '@rc-component/context';
import classNames from 'classnames';
import * as React from 'react';
import { getCellProps, useRowInfo } from '../Body/BodyRow';
import Cell from '../Cell';
import TableContext from '../context/TableContext';
import { getColumnsKey } from '../utils/valueUtil';
import type { FlattenData } from '../hooks/useFlattenRecords';
import StaticContext from './StaticContext';

export interface BodyLineProps<RecordType = any> {
record: RecordType;
data: FlattenData<RecordType>;
index: number;
className?: string;
style?: React.CSSProperties;
rowKey: React.Key;
}

const BodyLine = React.forwardRef<HTMLDivElement, BodyLineProps>((props, ref) => {
const { record, index, className, style, ...restProps } = props;
const { data, index, className, rowKey, style, ...restProps } = props;
const { record, indent } = data;

const { flattenColumns, prefixCls, fixedInfoList } = useContext(TableContext);
const { flattenColumns, prefixCls } = useContext(TableContext);
const { scrollX } = useContext(StaticContext, ['scrollX']);

const columnsKey = getColumnsKey(flattenColumns);
const rowInfo = useRowInfo(record, rowKey);

// ========================== Render ==========================
return (
Expand All @@ -32,23 +36,29 @@ const BodyLine = React.forwardRef<HTMLDivElement, BodyLineProps>((props, ref) =>
}}
>
{flattenColumns.map((column, colIndex) => {
const { render, dataIndex, className: columnClassName } = column;
const { render, dataIndex, className: columnClassName, width: colWidth } = column;

const key = columnsKey[colIndex];
const fixedInfo = fixedInfoList[colIndex];
const { key, fixedInfo, appendCellNode, additionalCellProps } = getCellProps(
rowInfo,
column,
colIndex,
indent,
index,
);

// return (
// <div key={index} className={`${prefixCls}-cell`} style={{ width: column.width }}>
// {value}
// </div>
// );
const { style: cellStyle } = additionalCellProps;
const mergedStyle = {
...cellStyle,
width: colWidth,
};

return (
<Cell
className={columnClassName}
ellipsis={column.ellipsis}
align={column.align}
scope={column.rowScope}
// component={column.rowScope ? scopeCellComponent : cellComponent}
component="div"
prefixCls={prefixCls}
key={key}
Expand All @@ -60,8 +70,11 @@ const BodyLine = React.forwardRef<HTMLDivElement, BodyLineProps>((props, ref) =>
render={render}
shouldCellUpdate={column.shouldCellUpdate}
{...fixedInfo}
// appendNode={appendCellNode}
// additionalProps={additionalCellProps}
appendNode={appendCellNode}
additionalProps={{
...additionalCellProps,
style: mergedStyle,
}}
/>
);
})}
Expand Down
3 changes: 3 additions & 0 deletions src/StaticTable/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import { INTERNAL_HOOKS } from '..';
import type { CustomizeScrollBody } from '../interface';
import Table, { type TableProps } from '../Table';
import Grid from './BodyGrid';
Expand Down Expand Up @@ -35,6 +36,8 @@ export default function StaticTable<RecordType>(props: StaticTableProps<RecordTy
body: renderBody,
}}
columns={filledWidthColumns}
internalHooks={INTERNAL_HOOKS}
hideScrollColumn
/>
</StaticContext.Provider>
);
Expand Down
10 changes: 8 additions & 2 deletions src/hooks/useFlattenRecords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ function flatRecord<T>(
return arr;
}

export interface FlattenData<RecordType> {
record: RecordType;
indent: number;
index: number;
}

/**
* flat tree data on expanded state
*
Expand All @@ -53,11 +59,11 @@ function flatRecord<T>(
* @returns flattened data
*/
export default function useFlattenRecords<T>(
data,
data: T[],
childrenColumnName: string,
expandedKeys: Set<Key>,
getRowKey: GetRowKey<T>,
) {
): FlattenData<T>[] {
const arr: { record: T; indent: number; index: number }[] = React.useMemo(() => {
if (expandedKeys?.size) {
let temp: { record: T; indent: number; index: number }[] = [];
Expand Down

0 comments on commit 5348044

Please sign in to comment.