forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[utils] Bring back getReactNodeRef (mui#44248)
- Loading branch information
1 parent
c8bf299
commit 9b9f562
Showing
3 changed files
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import * as React from 'react'; | ||
|
||
/** | ||
* Returns the ref of a React node handling differences between React 19 and older versions. | ||
* It will return null if the node is not a valid React element. | ||
* | ||
* @param element React.ReactNode | ||
* @returns React.Ref<any> | null | ||
* | ||
* @deprecated Use getReactElementRef instead | ||
*/ | ||
export default function getReactNodeRef(element: React.ReactNode): React.Ref<any> | null { | ||
if (!element || !React.isValidElement(element)) { | ||
return null; | ||
} | ||
|
||
// 'ref' is passed as prop in React 19, whereas 'ref' is directly attached to children in older versions | ||
return (element.props as any).propertyIsEnumerable('ref') | ||
? (element.props as any).ref | ||
: // @ts-expect-error element.ref is not included in the ReactElement type | ||
// We cannot check for it, but isValidElement is true at this point | ||
// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/70189 | ||
element.ref; | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './getReactNodeRef'; |
This file contains 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