Add typings for react-virtualized (#11148)

This commit is contained in:
Ian Ker-Seymer
2016-09-14 21:57:21 +09:00
committed by Masahiro Wakame
parent 5f23898c6f
commit 3cbdc08f81
2 changed files with 432 additions and 0 deletions
@@ -0,0 +1,309 @@
/// <reference path="react-virtualized.d.ts"/>
/// <reference path="../react/react.d.ts"/>
/// <reference path="../react/react-dom.d.ts"/>
import * as React from "react";
import * as ReactDOM from "react-dom";
import {
Collection,
FlexTable,
FlexColumn,
SortDirection,
Grid,
VirtualScroll,
ArrowKeyStepper,
AutoSizer,
CellMeasurer,
ColumnSizer,
InfiniteLoader,
ScrollSync,
WindowScroller,
} from "react-virtualized";
/*
* Collection
*/
function CollectionTest() {
const list = [
{ name: "Brian Vaughn", x: 13, y: 34, width: 123, size: 234, height: 123 }
];
// Render your grid
ReactDOM.render(
<Collection
cellCount={list.length}
cellRenderer={({ index }) => list[index].name}
cellSizeAndPositionGetter={({ index }) => {
const datum = list[index];
return {
height: datum.height,
width: datum.width,
x: datum.x,
y: datum.y
};
} }
height={300}
width={300}
/>,
document.getElementById("example")
);
}
function FlexTableTest() {
const list = [
{ name: 'Brian Vaughn', description: 'Software engineer' }
// And so on...
];
// Render your table
ReactDOM.render(
<FlexTable
width={300}
height={300}
headerHeight={20}
rowHeight={30}
rowCount={list.length}
rowGetter={
({ index }) => list[index]
}
>
<FlexColumn
label="Name"
dataKey="name"
width={100}
/>
<FlexColumn
width={200}
label="Description"
dataKey="description"
/>
</FlexTable>,
document.getElementById("example")
);
}
function GridTest() {
// Grid data as an array of arrays
const list = [
['Brian Vaughn', 'Software Engineer', 'San Jose', 'CA', 95125 /* ... */]
// And so on...
];
// Render your grid
ReactDOM.render(
<Grid
width={300}
height={300}
columnWidth={100}
rowHeight={30}
columnCount={list[0].length}
rowCount={list.length}
cellRenderer={({ columnIndex, isScrolling, rowIndex }) => list[rowIndex][columnIndex]}
/>,
document.getElementById('example')
);
}
function VirtualScrollTest() {
// List data as an array of strings
const list = [
'Brian Vaughn'
// And so on...
];
// Render your list
ReactDOM.render(
<VirtualScroll
width={300}
height={300}
rowCount={list.length}
rowHeight={20}
rowRenderer={({ index, isScrolling }) => list[index]}
/>,
document.getElementById('example')
);
}
function ArrowKeyStepperTest() {
const columnCount = 12;
const rowCount = 3;
ReactDOM.render(
<ArrowKeyStepper columnCount={columnCount} rowCount={rowCount}>
{({ onSectionRendered, scrollToColumn, scrollToRow }) => (
<Grid
columnCount={columnCount}
onSectionRendered={onSectionRendered}
rowCount={rowCount}
scrollToColumn={scrollToColumn}
scrollToRow={scrollToRow}
/>
) }
</ArrowKeyStepper>,
document.getElementById('example')
);
}
function AutoSizerTest() {
// List data as an array of strings
const list = [
'Brian Vaughn'
// And so on...
];
// Render your list
ReactDOM.render(
<AutoSizer>
{({ height, width }) => (
<VirtualScroll
width={width}
height={height}
rowCount={list.length}
rowHeight={20}
rowRenderer={
({ index }) => list[index] // Could also be a DOM element
}
/>
) }
</AutoSizer>,
document.getElementById('example')
);
}
function CellMeasurerTest() {
const columnCount = 12;
const rowCount = 3;
const cellRenderer = ({ columnIndex, rowIndex }) => `${rowIndex}, ${columnIndex}`;
const fixedRowHeight = 42;
const height = 12;
const width = 12;
ReactDOM.render(
<CellMeasurer
cellRenderer={cellRenderer}
columnCount={columnCount}
height={fixedRowHeight}
rowCount={rowCount}
>
{({ getColumnWidth }) => (
<Grid
columnCount={columnCount}
columnWidth={getColumnWidth}
height={height}
cellRenderer={cellRenderer}
rowCount={rowCount}
rowHeight={fixedRowHeight}
width={width}
/>
) }
</CellMeasurer>,
document.getElementById('example')
);
}
function ColumnSizerTest() {
ReactDOM.render(
<ColumnSizer
columnMaxWidth={100}
columnMinWidth={50}
columnCount={12}
width={120}
>
{({ adjustedWidth, getColumnWidth, registerChild }) => (
<Grid
ref={registerChild}
columnWidth={getColumnWidth}
columnCount={12}
height={12}
cellRenderer={() => "test"}
rowHeight={50}
rowCount={12}
width={adjustedWidth}
/>
) }
</ColumnSizer>,
document.getElementById('example')
);
}
function InfiniteLoaderTest() {
const list: string[] = [];
function isRowLoaded({ index }) {
return !!list[index];
}
function loadMoreRows({ startIndex, stopIndex }) {
return list.push('test');
}
// Render your list
ReactDOM.render(
<InfiniteLoader
isRowLoaded={isRowLoaded}
loadMoreRows={loadMoreRows}
rowCount={12}
>
{({ onRowsRendered, registerChild }) => (
<VirtualScroll
ref={registerChild}
width={300}
height={200}
onRowsRendered={onRowsRendered}
rowCount={list.length}
rowHeight={20}
rowRenderer={
({ index }) => list[index] // Could also be a DOM element
}
/>
) }
</InfiniteLoader>,
document.getElementById('example')
);
}
function ScrollSyncTest() {
return (
<ScrollSync>
{({ clientHeight, clientWidth, onScroll, scrollHeight, scrollLeft, scrollTop, scrollWidth }) => (
<div className='Table'>
<div className='LeftColumn'>
<VirtualScroll
rowRenderer={() => "test"}
rowCount={42}
height={12}
width={12}
rowHeight={120}
scrollTop={scrollTop}/>
/>
</div>
<div className='RightColumn'>
<Grid
onScroll={onScroll}/>
</div>
</div>
) }
</ScrollSync>
);
}
function WindowScrollerTest() {
ReactDOM.render(
<WindowScroller>
{({ height, isScrolling, scrollTop }) => (
<VirtualScroll
autoHeight
height={height}
rowCount={12}
rowHeight={12}
rowRenderer={() => 'test'}
scrollTop={scrollTop}
width={120}/>
) }
</WindowScroller>,
document.getElementById('example')
);
}
+123
View File
@@ -0,0 +1,123 @@
// Type definitions for react-virtualized
// Project: https://github.com/bvaughn/react-virtualized
// Definitions by: Ian Ker-Seymer <https://github.com/ianks>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="./../react/react.d.ts" />
declare module "react-virtualized" {
import * as React from "react";
/*
* Components
*/
interface VirtualScrollProps {
className?: string;
autoHeight?: boolean;
estimatedRowSize?: number;
height: number;
noRowsRenderer?: Function;
onRowsRendered?: (info: { overscanStartIndex: number, overscanStopIndex: number, startIndex: number, stopIndex: number }) => void;
onScroll?: (info: { clientHeight: number, scrollHeight: number, scrollTop: number }) => void;
overscanRowCount?: number;
rowHeight: number | ((info: { index: number }) => number);
rowRenderer: (info: { index: number, isScrolling: boolean }) => React.ReactNode;
rowClassName?: string | ((info: { index: number }) => string);
rowCount: number;
rowStyle?: React.CSSProperties | ((info: { index: number }) => React.CSSProperties);
scrollToAlignment?: string;
scrollToIndex?: number;
scrollTop?: number;
style?: React.CSSProperties;
tabIndex?: number;
width: number;
}
export class VirtualScroll extends React.Component<VirtualScrollProps, {}> { }
type CollectionProps = any;
export class Collection extends React.Component<CollectionProps, {}> { }
type FlexTableProps = any;
export class FlexTable extends React.Component<FlexTableProps, {}> { }
type FlexColumnProps = any;
export class FlexColumn extends React.Component<FlexColumnProps, {}> { }
type SortDirectionProps = any;
export class SortDirection extends React.Component<SortDirectionProps, {}> { }
type GridProps = any;
export class Grid extends React.Component<GridProps, {}> { }
/*
* Higher-Order Components
*/
interface AutoSizerProps {
disableHeight?: boolean;
disableWidth?: boolean;
onResize?: (info: { height: number, width: number }) => any;
}
export class AutoSizer extends React.Component<AutoSizerProps, {}> { }
interface ArrowKeyStepperProps {
children?: React.StatelessComponent<{
onSectionRendered: Function,
scrollToColumn: number,
scrollToRow: number
}>;
className?: string;
columnCount: number;
rowCount: number;
}
export class ArrowKeyStepper extends React.Component<ArrowKeyStepperProps, {}> { }
interface CellMeasurerProps {
cellRenderer: (info: { columnIndex: number, rowIndex: number }) => React.ReactNode;
cellSizeCache?: {
clearAllColumnWidths(): void;
clearAllRowHeights(): void;
clearColumnWidth(index: number): void;
clearRowHeight(index: number): void;
getColumnWidth(index: number): number;
getRowHeight(index: number): number;
hasColumnWidth(index: number): boolean;
hasRowHeight(index: number): boolean;
setColumnWidth(index: number, width: number): void;
setRowHeight(index: number, height: number): void;
};
children?: React.StatelessComponent<{
getColumnWidth: () => number,
getRowHeight: () => number,
resetMeasurements: () => any,
resetMeasurementsForColumn: (index: number) => any,
resetMeasurementsForRow: (index: number) => any,
}>;
columnCount: number;
container?: React.ReactType;
height?: number;
rowCount: number;
width?: number;
}
export class CellMeasurer extends React.Component<CellMeasurerProps, {}> { }
interface ColumnSizerProps {
children?: React.StatelessComponent<{ adjustedWidth: number, getColumnWidth: () => number, registerChild: any }>;
columnMaxWidth?: number;
columnMinWidth?: number;
columnCount?: number;
width: number;
}
export class ColumnSizer extends React.Component<ColumnSizerProps, {}> { }
type InfiniteLoaderProps = any;
export class InfiniteLoader extends React.Component<InfiniteLoaderProps, {}> { }
type ScrollSyncProps = any;
export class ScrollSync extends React.Component<ScrollSyncProps, {}> { }
type WindowScrollerProps = any;
export class WindowScroller extends React.Component<WindowScrollerProps, {}> { }
}