Skip to content

Commit bee15a8

Browse files
author
Claudéric Demers
authored
Merge pull request clauderic#271 from silvenon/examples-virtual-table-columns
Add example for sorting react-virtualized table columns
2 parents 90a06f4 + ab05126 commit bee15a8

File tree

5 files changed

+87
-10
lines changed

5 files changed

+87
-10
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
],
1616
"allowSamePrecedence": true
1717
}
18-
]
18+
],
19+
"quotes": ["error", "single"]
1920
}
2021
}

examples/drag-handle.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ class SortableComponent extends Component {
3333
items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6'],
3434
};
3535
onSortEnd = ({oldIndex, newIndex}) => {
36-
let {items} = this.state;
36+
const {items} = this.state;
3737

3838
this.setState({
3939
items: arrayMove(items, oldIndex, newIndex),
4040
});
4141
};
4242
render() {
43-
let {items} = this.state;
43+
const {items} = this.state;
4444

4545
return <SortableList items={items} onSortEnd={this.onSortEnd} useDragHandle={true} />;
4646
}

examples/infinite-list.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ class SortableComponent extends Component {
3333
],
3434
};
3535
onSortEnd = ({oldIndex, newIndex}) => {
36-
let {items} = this.state;
36+
const {items} = this.state;
3737

3838
this.setState({
3939
items: arrayMove(items, oldIndex, newIndex),
4040
});
4141
};
4242
render() {
43-
let {items} = this.state;
43+
const {items} = this.state;
4444

4545
return <SortableList items={items} onSortEnd={this.onSortEnd} />;
4646
}

examples/virtual-list.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const SortableItem = SortableElement(({value}) => {
1313

1414
class VirtualList extends Component {
1515
render() {
16-
let {items} = this.props;
16+
const {items} = this.props;
1717

1818
return (
1919
<List
@@ -22,7 +22,7 @@ class VirtualList extends Component {
2222
}}
2323
rowHeight={({index}) => items[index].height}
2424
rowRenderer={({index}) => {
25-
let {value} = items[index];
25+
const {value} = items[index];
2626
return <SortableItem index={index} value={value} />;
2727
}}
2828
rowCount={items.length}
@@ -53,21 +53,21 @@ class SortableComponent extends Component {
5353
};
5454
onSortEnd = ({oldIndex, newIndex}) => {
5555
if (oldIndex !== newIndex) {
56-
let {items} = this.state;
56+
const {items} = this.state;
5757

5858
this.setState({
5959
items: arrayMove(items, oldIndex, newIndex),
6060
});
6161

6262
// We need to inform React Virtualized that the items have changed heights
63-
let instance = this.SortableList.getWrappedInstance();
63+
const instance = this.SortableList.getWrappedInstance();
6464

6565
instance.List.recomputeRowHeights();
6666
instance.forceUpdate();
6767
}
6868
};
6969
render() {
70-
let {items} = this.state;
70+
const {items} = this.state;
7171

7272
return (
7373
<SortableList

examples/virtual-table-columns.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import React, {Component} from 'react';
2+
import {render} from 'react-dom';
3+
import {Table, Column} from 'react-virtualized';
4+
import {SortableContainer, SortableElement, arrayMove} from 'react-sortable-hoc';
5+
import 'react-virtualized/styles.css';
6+
7+
const ROW_HEIGHT = 30;
8+
const HEADER_ROW_HEIGHT = 20;
9+
const COL_WIDTH = 100;
10+
11+
const SortableHeader = SortableElement(({children, ...props}) =>
12+
React.cloneElement(children, props)
13+
);
14+
15+
const SortableHeaderRowRenderer = SortableContainer(
16+
({className, columns, style}) => (
17+
<div className={className} role="row" style={style}>
18+
{React.Children.map(columns, (column, index) => (
19+
<SortableHeader index={index}>
20+
{column}
21+
</SortableHeader>
22+
))}
23+
</div>
24+
)
25+
);
26+
27+
class TableWithSortableColumns extends Component {
28+
state = {
29+
cols: [
30+
{dataKey: 'col1', label: 'Column 1'},
31+
{dataKey: 'col2', label: 'Column 2'},
32+
{dataKey: 'col3', label: 'Column 3'},
33+
],
34+
rows: [
35+
{col1: 'row1 col1', col2: 'row1 col2', col3: 'row1 col3'},
36+
{col1: 'row2 col1', col2: 'row2 col2', col3: 'row2 col3'},
37+
{col1: 'row3 col1', col2: 'row3 col2', col3: 'row3 col3'},
38+
],
39+
};
40+
onSortEnd = ({oldIndex, newIndex}) => {
41+
this.setState(state => ({
42+
cols: arrayMove(state.cols, oldIndex, newIndex),
43+
}));
44+
};
45+
render() {
46+
const {rows, cols} = this.state;
47+
return (
48+
<Table
49+
width={COL_WIDTH * rows.length}
50+
height={HEADER_ROW_HEIGHT + (ROW_HEIGHT * rows.length)}
51+
headerHeight={ROW_HEIGHT}
52+
rowHeight={ROW_HEIGHT}
53+
rowCount={rows.length}
54+
rowGetter={({index}) => rows[index]}
55+
headerRowRenderer={params => (
56+
<SortableHeaderRowRenderer
57+
{...params}
58+
axis="x"
59+
lockAxis="x"
60+
onSortEnd={this.onSortEnd}
61+
/>
62+
)}
63+
>
64+
{cols.map(col => (
65+
<Column
66+
{...col}
67+
key={col.dataKey}
68+
width={COL_WIDTH}
69+
/>
70+
))}
71+
</Table>
72+
);
73+
}
74+
}
75+
76+
render(<TableWithSortableColumns />, document.getElementById('root'));

0 commit comments

Comments
 (0)