-
Notifications
You must be signed in to change notification settings - Fork 290
Allow ReorderCell to work in React v19 #745
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -240,13 +240,30 @@ class ReorderCell extends React.PureComponent { | |
contents: this.cellRef.current, | ||
}; | ||
|
||
ReactDOM.render( | ||
// Since we're effectively rendering the proxy in a separate VDOM root, we cannot directly pass in our context. | ||
// To solve this, we use ExternalContextProvider to pass down the context value. | ||
// ExternalContextProvider also ensures that even if our cell gets unmounted, the dragged cell still receives updates from context. | ||
// Since we're effectively rendering the proxy in a separate VDOM root, we cannot directly pass in our context. | ||
// To solve this, we use ExternalContextProvider to pass down the context value. | ||
// ExternalContextProvider also ensures that even if our cell gets unmounted, the dragged cell still receives updates from context. | ||
const proxy = ( | ||
<ExternalContextProvider value={this.context}> | ||
<DragProxy {...this.props} {...additionalProps} /> | ||
</ExternalContextProvider>, | ||
</ExternalContextProvider> | ||
); | ||
|
||
if (this.props.__useReactRoot) { | ||
const flushSync = ReactDOM.flushSync || ((fn) => fn()); // ReactDOM.flushSync doesn't exist in older versions of React | ||
// flushSync is required to ensure that the drag proxy gets mounted synchronously in newer version of React | ||
flushSync(() => { | ||
const root = this.props.__useReactRoot(this.getDragContainer()); | ||
this.dragContainer.root = root; | ||
root.render(proxy); | ||
}); | ||
// we consider our cell to be in a reordering state as soon as the drag proxy gets mounted | ||
this.setState({ isReordering: true }); | ||
return; | ||
} | ||
|
||
ReactDOM.render( | ||
proxy, | ||
this.getDragContainer(), | ||
// we consider our cell in a reordering state as soon as the drag proxy gets mounted | ||
() => this.setState({ isReordering: true }) | ||
|
@@ -287,7 +304,11 @@ class ReorderCell extends React.PureComponent { | |
|
||
removeDragContainer = () => { | ||
// since the drag container is going to be removed, also unmount the drag proxy | ||
ReactDOM.unmountComponentAtNode(this.dragContainer); | ||
if (this.props.__useReactRoot) { | ||
this.dragContainer.root.unmount(); | ||
} else { | ||
ReactDOM.unmountComponentAtNode(this.dragContainer); | ||
} | ||
|
||
this.dragContainer.remove(); | ||
this.dragContainer = null; | ||
|
@@ -363,6 +384,25 @@ ReorderCell.propTypes = { | |
* ``` | ||
*/ | ||
onColumnReorderEnd: PropTypes.func.isRequired, | ||
|
||
/** | ||
* HACK to make this plugin work in a React v19 environment by letting the client pass the `createRoot` function from `react-dom/client`. | ||
* | ||
* Example usage: | ||
* ``` | ||
* import { createRoot } from 'react-dom/client'; | ||
* | ||
* const reorderCell = ( | ||
* <ReorderCell | ||
* __useReactRoot={createRoot} | ||
* /> | ||
* ``` | ||
* | ||
* See https://github.com/schrodinger/fixed-data-table-2/issues/743) for more information. | ||
* | ||
* @deprecated This'll be removed in future major version updates of FDT. | ||
*/ | ||
__useReactRoot: PropTypes.func, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: maybe we just call this something very explicit like |
||
}; | ||
|
||
export default ReorderCell; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unmountComponentAtNode
also doesn't exist anymore in React v19.