Skip to content

Commit a7169de

Browse files
Matteo DelleaClaudéric Demers
authored andcommitted
Allow getContainer to return a promise to get parent container (clauderic#155)
1 parent bc9ad96 commit a7169de

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

src/SortableContainer/index.js

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
103103

104104
componentDidMount() {
105105
const {
106-
getContainer,
107106
useWindowAsScrollContainer,
108107
} = this.props;
109108

@@ -113,27 +112,29 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
113112
* https://github.com/clauderic/react-sortable-hoc/issues/249
114113
*/
115114

116-
this.container = typeof getContainer === 'function'
117-
? getContainer(this.getWrappedInstance())
118-
: findDOMNode(this);
119-
this.document = this.container.ownerDocument || document;
120-
121-
const contentWindow = this.props.contentWindow || this.document.defaultView || window;
122-
123-
this.contentWindow = typeof contentWindow === 'function'
124-
? contentWindow()
125-
: contentWindow;
126-
this.scrollContainer = useWindowAsScrollContainer
127-
? this.document.scrollingElement || this.document.documentElement
128-
: this.container;
129-
130-
for (const key in this.events) {
131-
if (this.events.hasOwnProperty(key)) {
132-
events[key].forEach(eventName =>
133-
this.container.addEventListener(eventName, this.events[key], false)
134-
);
115+
const container = this.getContainer();
116+
117+
Promise.resolve(container).then((containerNode) => {
118+
this.container = containerNode;
119+
this.document = this.container.ownerDocument || document;
120+
121+
const contentWindow = this.props.contentWindow || this.document.defaultView || window;
122+
123+
this.contentWindow = typeof contentWindow === 'function'
124+
? contentWindow()
125+
: contentWindow;
126+
this.scrollContainer = useWindowAsScrollContainer
127+
? this.document.scrollingElement || this.document.documentElement
128+
: this.container;
129+
130+
for (const key in this.events) {
131+
if (this.events.hasOwnProperty(key)) {
132+
events[key].forEach(eventName =>
133+
this.container.addEventListener(eventName, this.events[key], false)
134+
);
135+
}
135136
}
136-
}
137+
});
137138
}
138139

139140
componentWillUnmount() {
@@ -747,9 +748,20 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
747748
config.withRef,
748749
'To access the wrapped instance, you need to pass in {withRef: true} as the second argument of the SortableContainer() call'
749750
);
751+
750752
return this.refs.wrappedInstance;
751753
}
752754

755+
getContainer() {
756+
const {getContainer} = this.props;
757+
758+
if (typeof getContainer !== 'function') {
759+
return findDOMNode(this);
760+
}
761+
762+
return getContainer(config.withRef ? this.getWrappedInstance() : undefined);
763+
}
764+
753765
render() {
754766
const ref = config.withRef ? 'wrappedInstance' : null;
755767

0 commit comments

Comments
 (0)