Skip to content

Commit 6d4c5da

Browse files
committed
Added support for a custom container (via getContainer prop)
This PR also updates the version of 'react-virtualized' used in 'devDependencies' to 7.15 so that a FlexTable example could be added using the new getContainer prop. The PR also adds 'react' to 'devDependencies' since it was missing.
1 parent 8ff6601 commit 6d4c5da

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ More code examples are available [here](https://github.com/clauderic/react-sorta
104104
| hideSortableGhost | Boolean | `true` | Whether to auto-hide the ghost element. By default, as a convenience, React Sortable List will automatically hide the element that is currently being sorted. Set this to false if you would like to apply your own styling. |
105105
| lockToContainerEdges | Boolean | `false` | You can lock movement of the sortable element to it's parent `SortableContainer` |
106106
| lockOffset | `OffsetValue`\* \| [`OffsetValue`\*, `OffsetValue`\*] | `"50%"` | When `lockToContainerEdges` is set to `true`, this controls the offset distance between the sortable helper and the top/bottom edges of it's parent `SortableContainer`. Percentage values are relative to the height of the item currently being sorted. If you wish to specify different behaviours for locking to the *top* of the container vs the *bottom*, you may also pass in an `array` (For example: `["0%", "100%"]`). |
107+
| getContainer | Function | | Optional function to return the scrollable container element. This property defaults to the `SortableContainer` element itself or (if `useWindowAsScrollContainer` is true) the window. Use this function to specify a custom container object (eg this is useful for integrating with certain 3rd party components such as `FlexTable`). This function is passed a single parameter (the `wrappedInstance` React element) and it is expected to return a DOM element. |
107108

108109
\* `OffsetValue` is either a finite `Number` or a `String` made-up of a number and a unit (`px` or `%`).
109110
Examples: `10` (is the same as `"10px"`), `"50%"`

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@
8686
"postcss-loader": "^0.9.1",
8787
"qs": "^6.2.0",
8888
"raw-loader": "^0.5.1",
89+
"react": "^15.2.1",
8990
"react-addons-pure-render-mixin": "^15.0.2",
9091
"react-addons-shallow-compare": "^15.1.0",
9192
"react-addons-test-utils": "^15.1.0",
9293
"react-infinite": "^0.9.2",
93-
"react-virtualized": "^7.3.1",
94+
"react-virtualized": "^7.15.0",
9495
"redux": "^3.5.2",
9596
"rimraf": "^2.5.2",
9697
"sass-loader": "^3.2.0",

src/.stories/index.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React, {Component, PropTypes} from 'react';
2+
import ReactDOM from 'react-dom';
23
import {storiesOf} from '@kadira/storybook';
34
import style from './Storybook.scss';
45
import {SortableContainer, SortableElement, SortableHandle, arrayMove} from '../index';
5-
import {VirtualScroll} from 'react-virtualized';
6+
import {defaultFlexTableRowRenderer, FlexColumn, FlexTable, VirtualScroll} from 'react-virtualized';
67
import 'react-virtualized/styles.css';
78
import Infinite from 'react-infinite';
89
import range from 'lodash/range';
@@ -95,6 +96,50 @@ class VirtualList extends Component {
9596
}
9697
const SortableVirtualList = SortableContainer(VirtualList, {withRef: true});
9798

99+
const SortableFlexTable = SortableContainer(FlexTable, {withRef: true});
100+
const SortableRowRenderer = SortableElement(defaultFlexTableRowRenderer);
101+
102+
class FlexTableWrapper extends Component {
103+
render () {
104+
const {
105+
className,
106+
height,
107+
itemClass,
108+
itemHeight,
109+
items,
110+
onSortEnd,
111+
width
112+
} = this.props
113+
114+
return (
115+
<SortableFlexTable
116+
getContainer={(wrappedInstance) => ReactDOM.findDOMNode(wrappedInstance._grid)}
117+
gridClassName={className}
118+
headerHeight={itemHeight}
119+
height={height}
120+
onSortEnd={onSortEnd}
121+
rowClassName={itemClass}
122+
rowCount={items.length}
123+
rowGetter={({ index }) => items[index]}
124+
rowHeight={itemHeight}
125+
rowRenderer={(props) => <SortableRowRenderer {...props} />}
126+
width={width}
127+
>
128+
<FlexColumn
129+
label='Index'
130+
dataKey='value'
131+
width={100}
132+
/>
133+
<FlexColumn
134+
label='Height'
135+
dataKey='height'
136+
width={width - 100}
137+
/>
138+
</SortableFlexTable>
139+
);
140+
}
141+
}
142+
98143
const SortableInfiniteList = SortableContainer(({className, items, itemClass, sortingIndex, useWindowAsScrollContainer}) => {
99144
return (
100145
<Infinite
@@ -224,6 +269,13 @@ storiesOf('React Virtualized', module)
224269
</div>
225270
);
226271
})
272+
.add('FlexTable usage', () => {
273+
return (
274+
<div className={style.root}>
275+
<ListWrapper component={FlexTableWrapper} items={getItems(500, 40)} itemHeight={40} helperClass={style.stylizedHelper} />
276+
</div>
277+
);
278+
})
227279

228280
storiesOf('React Infinite', module)
229281
.add('Basic usage', () => {

src/SortableContainer/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export default function SortableContainer(WrappedComponent, config = {withRef: f
4747
PropTypes.string,
4848
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.number, PropTypes.string]))
4949
]),
50+
getContainer: PropTypes.func
5051
};
5152
static childContextTypes = {
5253
manager: PropTypes.object.isRequired
@@ -58,9 +59,9 @@ export default function SortableContainer(WrappedComponent, config = {withRef: f
5859
};
5960
}
6061
componentDidMount() {
61-
let {contentWindow} = this.props;
62+
let {contentWindow, getContainer} = this.props;
6263

63-
this.container = ReactDOM.findDOMNode(this);
64+
this.container = (typeof getContainer == 'function') ? getContainer(this.getWrappedInstance()) : ReactDOM.findDOMNode(this);
6465
this.document = this.container.ownerDocument || document;
6566
this.scrollContainer = (this.props.useWindowAsScrollContainer) ? this.document.body : this.container;
6667
this.contentWindow = (typeof contentWindow == 'function') ? contentWindow() : contentWindow;

0 commit comments

Comments
 (0)