Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for a custom container (via getContainer prop) #37

Merged
merged 2 commits into from
Jul 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ More code examples are available [here](https://github.com/clauderic/react-sorta
| 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. |
| lockToContainerEdges | Boolean | `false` | You can lock movement of the sortable element to it's parent `SortableContainer` |
| 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%"]`). |
| 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. |

\* `OffsetValue` is either a finite `Number` or a `String` made-up of a number and a unit (`px` or `%`).
Examples: `10` (is the same as `"10px"`), `"50%"`
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@
"postcss-loader": "^0.9.1",
"qs": "^6.2.0",
"raw-loader": "^0.5.1",
"react": "^15.2.1",
"react-addons-pure-render-mixin": "^15.0.2",
"react-addons-shallow-compare": "^15.1.0",
"react-addons-test-utils": "^15.1.0",
"react-infinite": "^0.9.2",
"react-virtualized": "^7.3.1",
"react-virtualized": "^7.15.1",
"redux": "^3.5.2",
"rimraf": "^2.5.2",
"sass-loader": "^3.2.0",
Expand Down
54 changes: 53 additions & 1 deletion src/.stories/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, {Component, PropTypes} from 'react';
import ReactDOM from 'react-dom';
import {storiesOf} from '@kadira/storybook';
import style from './Storybook.scss';
import {SortableContainer, SortableElement, SortableHandle, arrayMove} from '../index';
import {VirtualScroll} from 'react-virtualized';
import {defaultFlexTableRowRenderer, FlexColumn, FlexTable, VirtualScroll} from 'react-virtualized';
import 'react-virtualized/styles.css';
import Infinite from 'react-infinite';
import range from 'lodash/range';
Expand Down Expand Up @@ -95,6 +96,50 @@ class VirtualList extends Component {
}
const SortableVirtualList = SortableContainer(VirtualList, {withRef: true});

const SortableFlexTable = SortableContainer(FlexTable, {withRef: true});
const SortableRowRenderer = SortableElement(defaultFlexTableRowRenderer);

class FlexTableWrapper extends Component {
render () {
const {
className,
height,
itemClass,
itemHeight,
items,
onSortEnd,
width
} = this.props

return (
<SortableFlexTable
getContainer={(wrappedInstance) => ReactDOM.findDOMNode(wrappedInstance.Grid)}
gridClassName={className}
headerHeight={itemHeight}
height={height}
onSortEnd={onSortEnd}
rowClassName={itemClass}
rowCount={items.length}
rowGetter={({ index }) => items[index]}
rowHeight={itemHeight}
rowRenderer={(props) => <SortableRowRenderer {...props} />}
width={width}
>
<FlexColumn
label='Index'
dataKey='value'
width={100}
/>
<FlexColumn
label='Height'
dataKey='height'
width={width - 100}
/>
</SortableFlexTable>
);
}
}

const SortableInfiniteList = SortableContainer(({className, items, itemClass, sortingIndex, useWindowAsScrollContainer}) => {
return (
<Infinite
Expand Down Expand Up @@ -224,6 +269,13 @@ storiesOf('React Virtualized', module)
</div>
);
})
.add('FlexTable usage', () => {
return (
<div className={style.root}>
<ListWrapper component={FlexTableWrapper} items={getItems(500, 40)} itemHeight={40} helperClass={style.stylizedHelper} />
</div>
);
})

storiesOf('React Infinite', module)
.add('Basic usage', () => {
Expand Down
5 changes: 3 additions & 2 deletions src/SortableContainer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default function SortableContainer(WrappedComponent, config = {withRef: f
PropTypes.string,
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.number, PropTypes.string]))
]),
getContainer: PropTypes.func
};
static childContextTypes = {
manager: PropTypes.object.isRequired
Expand All @@ -58,9 +59,9 @@ export default function SortableContainer(WrappedComponent, config = {withRef: f
};
}
componentDidMount() {
let {contentWindow} = this.props;
let {contentWindow, getContainer} = this.props;

this.container = ReactDOM.findDOMNode(this);
this.container = (typeof getContainer == 'function') ? getContainer(this.getWrappedInstance()) : ReactDOM.findDOMNode(this);
Copy link
Contributor Author

@bvaughn bvaughn Jul 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was on the fence about whether the getContainer prop should return a React element or a DOM node. Thoughts?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd lean towards DOM node, simply because it would enable the use of elements outside of React's scope, for instance, document.body

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I thought also. (So that's what it currently does.)

this.document = this.container.ownerDocument || document;
this.scrollContainer = (this.props.useWindowAsScrollContainer) ? this.document.body : this.container;
this.contentWindow = (typeof contentWindow == 'function') ? contentWindow() : contentWindow;
Expand Down