From 760f588dcd5e2d26034c036b6e8af36e57271140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Gr=C5=88o?= Date: Mon, 7 May 2018 20:43:37 +0200 Subject: [PATCH] [react-table] Add support for Functional Rendering (#25347) * Added type definitions for functional rendering https://www.npmjs.com/package/react-table#functional-rendering * fixed trailing whitespace * add test for functional rendering * add missing props to FinalState * restricted some of the `any` types * corrected semicolons --- types/react-table/index.d.ts | 46 +++++++++++++++++++++++++ types/react-table/react-table-tests.tsx | 29 ++++++++++++++-- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/types/react-table/index.d.ts b/types/react-table/index.d.ts index d748fcffb2..ebe531ecfa 100644 --- a/types/react-table/index.d.ts +++ b/types/react-table/index.d.ts @@ -177,6 +177,13 @@ export interface TableProps extends /** Server-side callbacks */ onFetchData: (state: any, instance: any) => void; + + /** Control callback for functional rendering */ + children: ( + state: FinalState, + makeTable: () => React.ReactElement, + instance: Instance + ) => React.ReactNode; } export interface ControlledStateOverrideProps { @@ -254,6 +261,14 @@ export interface ExpandedRows { [idx: number]: boolean | ExpandedRows; } +export interface DerivedDataObject { + _index: number; + _nestingLevel: number; + _subRows: any; + _original: any; + [p: string]: any; +} + export interface ControlledStateCallbackProps { /** Called when the page index is changed by the user */ onPageChange: PageChangeFunction; @@ -648,15 +663,46 @@ export interface RowInfo { } export interface FinalState extends TableProps { + frozen: boolean; startRow: number; endRow: number; pageRows: number; padRows: number; hasColumnFooter: boolean; + hasHeaderGroups: boolean; canPrevious: boolean; canNext: boolean; rowMinWidth: number; + + allVisibleColumns: Column[]; + allDecoratedColumns: Column[]; + resolvedData: DerivedDataObject[]; + sortedData: DerivedDataObject[]; + headerGroups: any[]; } export const ReactTableDefaults: TableProps; export default class ReactTable extends React.Component> { } + +export interface Instance extends ReactTable { + context: any; + props: Partial; + refs: any; + state: FinalState; + filterColumn(...props: any[]): any; + filterData(...props: any[]): any; + fireFetchData(...props: any[]): any; + getDataModel(...props: any[]): any; + getMinRows(...props: any[]): any; + getPropOrState(...props: any[]): any; + getResolvedState(...props: any[]): any; + getSortedData(...props: any[]): any; + getStateOrProp(...props: any[]): any; + onPageChange: PageChangeFunction; + onPageSizeChange: PageSizeChangeFunction; + resizeColumnEnd(...props: any[]): any; + resizeColumnMoving(...props: any[]): any; + resizeColumnStart(...props: any[]): any; + sortColumn(...props: any[]): any; + sortData(...props: any[]): any; +} diff --git a/types/react-table/react-table-tests.tsx b/types/react-table/react-table-tests.tsx index f03bb5e167..b8380125b3 100644 --- a/types/react-table/react-table-tests.tsx +++ b/types/react-table/react-table-tests.tsx @@ -2,7 +2,7 @@ import * as React from 'react'; import * as ReactDOM from 'react-dom'; // Import React Table -import ReactTable, { Column } from "react-table"; +import ReactTable, { Column, FinalState, Instance } from "react-table"; import "react-table/react-table.css"; const columns: Column[] = [ @@ -154,7 +154,32 @@ const Component = (props: {}) => { console.log(newSorted); } }} - /> + > + {( + state: FinalState, + makeTable: () => React.ReactChild, + instance: Instance + ) => { + return ( +
+
+                
+                  state.allVisibleColumns ==={" "}
+                  {JSON.stringify(state.allVisibleColumns, null, 4)}
+                
+              
+ {makeTable()} +
+ ); + }} +
);