+
+ {topHideHeight ?
: null}
+ {visibleRows.current}
+ {bottomHideHeight ?
: null}
+
- return (
-
-
- {topHideHeight ?
: null}
- {visibleRows.current}
- {bottomHideHeight ?
: null}
+
+ {renderScrollbar()}
+
+ );
+ };
-
- {renderScrollbar()}
-
-
+ const contextValue = React.useMemo(
+ () => ({
+ classPrefix,
+ translateDOMPositionXY: translateDOMPositionXY.current,
+ rtl,
+ isTree,
+ hasCustomTreeCol
+ }),
+ [classPrefix, hasCustomTreeCol, isTree, rtl]
);
- };
- const contextValue = React.useMemo(
- () => ({
- classPrefix,
- translateDOMPositionXY: translateDOMPositionXY.current,
- rtl,
- isTree,
- hasCustomTreeCol
- }),
- [classPrefix, hasCustomTreeCol, isTree, rtl]
- );
-
- return (
-
-
- {showHeader && renderTableHeader(headerCells, rowWidth)}
- {children && renderTableBody(bodyCells, rowWidth)}
- {showHeader && (
-
- )}
-
-
- );
-});
+ return (
+
+
+ {showHeader && renderTableHeader(headerCells, rowWidth)}
+ {children && renderTableBody(bodyCells, rowWidth)}
+ {showHeader && (
+
+ )}
+
+
+ );
+ }
+);
Table.displayName = 'Table';
Table.propTypes = {
@@ -1207,7 +1249,8 @@ Table.propTypes = {
onTouchEnd: PropTypes.func
};
-export interface TableInstance
extends React.FunctionComponent> {
+export interface TableInstance
+ extends React.FunctionComponent> {
/** The table body element */
readonly body: HTMLDivElement;
/** The table root element */
@@ -1220,6 +1263,6 @@ export interface TableInstance extends React.FunctionComponent void;
}
-export default Table as (
+export default Table as (
props: TableProps & React.RefAttributes>
) => React.ReactElement;
diff --git a/src/utils/children.ts b/src/utils/children.ts
new file mode 100644
index 0000000..ae86ed7
--- /dev/null
+++ b/src/utils/children.ts
@@ -0,0 +1,19 @@
+import React from 'react';
+import * as ReactIS from 'react-is';
+
+export const flattenChildren = (
+ children: React.ReactNode | React.ReactNode[],
+ flattened: React.ReactNode[] = []
+) => {
+ for (const child of React.Children.toArray(children)) {
+ if (ReactIS.isFragment(child)) {
+ const childEl = child as React.ReactElement;
+ if (childEl.props?.children) {
+ flattenChildren(childEl.props.children, flattened);
+ }
+ } else {
+ flattened.push(child);
+ }
+ }
+ return flattened;
+};
diff --git a/src/utils/getColumnProps.ts b/src/utils/getColumnProps.ts
index 9fc974b..67dbb89 100644
--- a/src/utils/getColumnProps.ts
+++ b/src/utils/getColumnProps.ts
@@ -1,5 +1,6 @@
+import React from 'react';
+import { RowDataType } from '../@types/common';
import { ColumnProps } from '../Column';
-export type Column = React.ReactElement;
/**
* Get the union of the props of the column itself and the props of the custom column
@@ -15,7 +16,9 @@ export type Column = React.ReactElement;
*
*
*/
-export default function getColumnProps(column: Column): ColumnProps {
+export default function getColumnProps(
+ column: React.ReactElement
+): ColumnProps {
const columnDefaultProps = column['type']?.['render']?.()?.props || {};
return { ...columnDefaultProps, ...column?.props };
diff --git a/src/utils/getTotalByColumns.ts b/src/utils/getTotalByColumns.ts
index 6f11091..6ae67af 100644
--- a/src/utils/getTotalByColumns.ts
+++ b/src/utils/getTotalByColumns.ts
@@ -1,15 +1,19 @@
import React from 'react';
import isPlainObject from 'lodash/isPlainObject';
-import getColumnProps, { Column } from './getColumnProps';
+import getColumnProps from './getColumnProps';
+import { RowDataType } from '../@types/common';
+import { ColumnProps } from '../Column';
-function getTotalByColumns(columns: Column | React.ReactNode[]) {
+function getTotalByColumns(
+ columns: React.ReactElement> | React.ReactElement>[]
+) {
let totalFlexGrow = 0;
let totalWidth = 0;
const count = (items: React.ReactNode[]) => {
Array.from(items).forEach(column => {
if (React.isValidElement(column)) {
- const { flexGrow, width = 0 } = getColumnProps(column as Column);
+ const { flexGrow, width = 0 } = getColumnProps(column);
totalFlexGrow += flexGrow || 0;
totalWidth += flexGrow ? 0 : width;
} else if (Array.isArray(column)) {
diff --git a/src/utils/useCellDescriptor.ts b/src/utils/useCellDescriptor.ts
index 5a00c69..ce1444e 100644
--- a/src/utils/useCellDescriptor.ts
+++ b/src/utils/useCellDescriptor.ts
@@ -16,7 +16,7 @@ import flushSync from './flushSync';
import useMount from './useMount';
interface CellDescriptorProps {
- children: React.ReactNode;
+ children: React.ReactNode[];
rtl: boolean;
minScrollX: React.MutableRefObject;
scrollX: React.MutableRefObject;
@@ -192,9 +192,9 @@ const useCellDescriptor = (
const columns = getTableColumns(children) as React.ReactElement[];
const count = columns.length;
- const { totalFlexGrow, totalWidth } = getTotalByColumns(columns);
+ const { totalFlexGrow, totalWidth } = getTotalByColumns(columns);
- React.Children.forEach(columns, (column: React.ReactElement, index) => {
+ React.Children.forEach(columns, (column: React.ReactElement>, index) => {
if (React.isValidElement(column)) {
const columnChildren = column.props.children as React.ReactNode[];
const columnProps = getColumnProps(column);
diff --git a/test/Table.test.tsx b/test/Table.test.tsx
index e03ce4c..96ac764 100644
--- a/test/Table.test.tsx
+++ b/test/Table.test.tsx
@@ -1,6 +1,7 @@
import React from 'react';
import { expectType } from 'ts-expect';
import Table, { TableInstance } from '../src/Table';
+import Cell from '../src/Cell';
type Row = {
id: number;
@@ -61,7 +62,57 @@ const ref = React.createRef>();
;
+
+ {({ Column, HeaderCell, Cell }) => (
+ <>
+
+ Id
+ {({ id }) => id} |
+
+ >
+ )}
+
;
+
ref.current?.body;
ref.current?.root;
ref.current?.scrollLeft(100);
ref.current?.scrollTop(100);
+
+interface InventoryItem {
+ id: string;
+ name: string;
+}
+
+const table = React.createRef>();
+
+ ref={table}>
+ {({ Column, HeaderCell, Cell }) => (
+ <>
+
+ Name
+ {row => row.name} |
+
+
+ Type
+ {/** @ts-expect-error Property 'type' does not exist on type 'InventoryItem' */}
+ {row => row.type} |
+
+ >
+ )}
+
;
+
+interface ImageCellProps> {
+ rowData: TRow;
+ dataKey: TKey;
+ // ... any other props
+}
+
+export const ImageCell = >({
+ rowData,
+ dataKey,
+ ...rest
+}: ImageCellProps) => (
+ {...rest}>
+
+ |
+);
diff --git a/test/TableSpec.js b/test/TableSpec.js
index 41658a1..7858131 100644
--- a/test/TableSpec.js
+++ b/test/TableSpec.js
@@ -29,6 +29,26 @@ describe('Table', () => {
);
+ expect(instance.querySelectorAll('.rs-table-cell')).to.be.length(2);
+ });
+
+ it('Should accept render prop', () => {
+ const instance = getDOMNode(
+
+ {({ Column, HeaderCell, Cell }) => (
+ <>
+
+ 11
+ 12 |
+
+
+ 11
+ 12 |
+
+ >
+ )}
+
+ );
expect(instance.querySelectorAll('.rs-table-cell')).to.be.length(2);
});