-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathColumnSizer.example.js
148 lines (130 loc) · 4.28 KB
/
ColumnSizer.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
140
141
142
143
144
145
146
147
148
/**
* @flow
*/
import * as React from 'react';
import styles from './ColumnSizer.example.css';
import AutoSizer from '../AutoSizer';
import ColumnSizer from './ColumnSizer';
import Grid from '../Grid';
import {
ContentBox,
ContentBoxHeader,
ContentBoxParagraph,
} from '../demo/ContentBox';
import {LabeledInput, InputRow} from '../demo/LabeledInput';
export default class ColumnSizerExample extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
columnMaxWidth: 100,
columnMinWidth: 75,
columnCount: 10,
};
this._noColumnMaxWidthChange = this._noColumnMaxWidthChange.bind(this);
this._noColumnMinWidthChange = this._noColumnMinWidthChange.bind(this);
this._onColumnCountChange = this._onColumnCountChange.bind(this);
this._noContentRenderer = this._noContentRenderer.bind(this);
this._cellRenderer = this._cellRenderer.bind(this);
}
render() {
const {columnMaxWidth, columnMinWidth, columnCount} = this.state;
return (
<ContentBox>
<ContentBoxHeader
text="ColumnSizer"
sourceLink="https://github.com/bvaughn/react-virtualized/blob/master/source/ColumnSizer/ColumnSizer.example.js"
docsLink="https://github.com/bvaughn/react-virtualized/blob/master/docs/ColumnSizer.md"
/>
<ContentBoxParagraph>
This component decorates a <code>Grid</code> and calculates the width
of its columns based on the current (<code>Grid</code>) width.
</ContentBoxParagraph>
<InputRow>
<LabeledInput
label="Num Columns"
name="columnCount"
onChange={this._onColumnCountChange}
value={columnCount}
/>
<LabeledInput
label="Column Min Width"
name="columnMinWidth"
onChange={this._noColumnMinWidthChange}
value={columnMinWidth}
/>
<LabeledInput
label="Column Max Width"
name="columnMaxWidth"
onChange={this._noColumnMaxWidthChange}
value={columnMaxWidth}
/>
</InputRow>
<div>
<AutoSizer disableHeight>
{({width}) => (
<ColumnSizer
columnMaxWidth={columnMaxWidth}
columnMinWidth={columnMinWidth}
columnCount={columnCount}
key="GridColumnSizer"
width={width}>
{({adjustedWidth, columnWidth, registerChild}) => (
<div
className={styles.GridContainer}
style={{
height: 50,
width: adjustedWidth,
}}>
<Grid
ref={registerChild}
columnWidth={columnWidth}
columnCount={columnCount}
height={50}
noContentRenderer={this._noContentRenderer}
cellRenderer={this._cellRenderer}
rowHeight={50}
rowCount={1}
width={adjustedWidth}
/>
</div>
)}
</ColumnSizer>
)}
</AutoSizer>
</div>
</ContentBox>
);
}
_noColumnMaxWidthChange(event) {
let columnMaxWidth = parseInt(event.target.value, 10);
if (isNaN(columnMaxWidth)) {
columnMaxWidth = undefined;
} else {
columnMaxWidth = Math.min(1000, columnMaxWidth);
}
this.setState({columnMaxWidth});
}
_noColumnMinWidthChange(event) {
let columnMinWidth = parseInt(event.target.value, 10);
if (isNaN(columnMinWidth)) {
columnMinWidth = undefined;
} else {
columnMinWidth = Math.max(1, columnMinWidth);
}
this.setState({columnMinWidth});
}
_onColumnCountChange(event) {
this.setState({columnCount: parseInt(event.target.value, 10) || 0});
}
_noContentRenderer() {
return <div className={styles.noCells}>No cells</div>;
}
_cellRenderer({columnIndex, key, rowIndex, style}) {
const className = columnIndex === 0 ? styles.firstCell : styles.cell;
return (
<div className={className} key={key} style={style}>
{`R:${rowIndex}, C:${columnIndex}`}
</div>
);
}
}