forked from bvaughn/react-virtualized
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCellMeasurer.example.js
139 lines (121 loc) · 3.71 KB
/
CellMeasurer.example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import Immutable from 'immutable';
import PropTypes from 'prop-types';
import * as React from 'react';
import {
ContentBox,
ContentBoxHeader,
ContentBoxParagraph,
} from '../demo/ContentBox';
import AutoSizer from '../AutoSizer';
import clsx from 'clsx';
import styles from './CellMeasurer.example.css';
import DynamicWidthGrid from './CellMeasurer.DynamicWidthGrid.example.js';
import DynamicHeightGrid from './CellMeasurer.DynamicHeightGrid.example.js';
import DynamicWidthMultiGrid from './CellMeasurer.DynamicWidthMultiGrid.example.js';
import DynamicHeightList from './CellMeasurer.DynamicHeightList.example.js';
import DynamicHeightTableColumn from './CellMeasurer.DynamicHeightTableColumn.example.js';
const demoComponents = [
DynamicWidthGrid,
DynamicHeightGrid,
DynamicWidthMultiGrid,
DynamicHeightList,
DynamicHeightTableColumn,
];
export default class CellMeasurerExample extends React.PureComponent {
static contextTypes = {
list: PropTypes.instanceOf(Immutable.List).isRequired,
};
constructor(props, context) {
super(props, context);
this.state = {
currentTab: 0,
};
this._onClick = this._onClick.bind(this);
}
render() {
const {list} = this.context;
const {currentTab} = this.state;
const buttonProps = {
currentTab,
onClick: this._onClick,
};
const DemoComponent = demoComponents[currentTab];
return (
<ContentBox>
<ContentBoxHeader
text="CellMeasurer"
sourceLink="https://github.com/bvaughn/react-virtualized/blob/master/source/CellMeasurer/CellMeasurer.example.js"
docsLink="https://github.com/bvaughn/react-virtualized/blob/master/docs/CellMeasurer.md"
/>
<ContentBoxParagraph>
This component can be used to just-in-time measure dynamic content
(eg. messages in a chat interface).
</ContentBoxParagraph>
<AutoSizer disableHeight>
{({width}) => (
<div style={{width}}>
<div>
<strong>Grid</strong>:
<Tab id={0} {...buttonProps}>
dynamic width text
</Tab>
<Tab id={1} {...buttonProps}>
dynamic height text
</Tab>
<strong>MultiGrid</strong>:
<Tab id={2} {...buttonProps}>
dynamic width text
</Tab>
<strong>List</strong>:
<Tab id={3} {...buttonProps}>
dynamic height image
</Tab>
<strong>Table</strong>:
<Tab id={4} {...buttonProps}>
mixed fixed and dynamic height text
</Tab>
</div>
<DemoComponent
getClassName={getClassName}
getContent={getContent}
list={list}
width={width}
/>
</div>
)}
</AutoSizer>
</ContentBox>
);
}
_onClick(id) {
this.setState({
currentTab: id,
});
}
}
function getClassName({columnIndex, rowIndex}) {
const rowClass = rowIndex % 2 === 0 ? styles.evenRow : styles.oddRow;
return clsx(rowClass, styles.cell, {
[styles.centeredCell]: columnIndex > 2,
});
}
function getContent({index, datum, long = true}) {
switch (index % 3) {
case 0:
return datum.color;
case 1:
return datum.name;
case 2:
return long ? datum.randomLong : datum.random;
}
}
function Tab({children, currentTab, id, onClick}) {
const classNames = clsx(styles.Tab, {
[styles.ActiveTab]: currentTab === id,
});
return (
<button className={classNames} onClick={() => onClick(id)}>
{children}
</button>
);
}