-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[material-ui] Improve getReactElementRef() utils (#43022)
Co-authored-by: Aarón García Hervás <[email protected]>
- Loading branch information
1 parent
82a6448
commit 3c83c7d
Showing
21 changed files
with
116 additions
and
52 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
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
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
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
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
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
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
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
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
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
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
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
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
19 changes: 19 additions & 0 deletions
19
packages/mui-utils/src/getReactElementRef/getReactElementRef.spec.tsx
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,19 @@ | ||
import getReactElementRef from '@mui/utils/getReactElementRef'; | ||
import * as React from 'react'; | ||
|
||
// @ts-expect-error | ||
getReactElementRef(false); | ||
|
||
// @ts-expect-error | ||
getReactElementRef(null); | ||
|
||
// @ts-expect-error | ||
getReactElementRef(undefined); | ||
|
||
// @ts-expect-error | ||
getReactElementRef(1); | ||
|
||
// @ts-expect-error | ||
getReactElementRef([<div key="1" />, <div key="2" />]); | ||
|
||
getReactElementRef(<div />); |
39 changes: 39 additions & 0 deletions
39
packages/mui-utils/src/getReactElementRef/getReactElementRef.test.tsx
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,39 @@ | ||
import { expect } from 'chai'; | ||
import getReactElementRef from '@mui/utils/getReactElementRef'; | ||
import * as React from 'react'; | ||
|
||
describe('getReactElementRef', () => { | ||
it('should return undefined when not used correctly', () => { | ||
// @ts-expect-error | ||
expect(getReactElementRef(false)).to.equal(undefined); | ||
// @ts-expect-error | ||
expect(getReactElementRef()).to.equal(undefined); | ||
// @ts-expect-error | ||
expect(getReactElementRef(1)).to.equal(undefined); | ||
|
||
const children = [<div key="1" />, <div key="2" />]; | ||
// @ts-expect-error | ||
expect(getReactElementRef(children)).to.equal(undefined); | ||
}); | ||
|
||
it('should return the ref of a React element', () => { | ||
const ref = React.createRef<HTMLDivElement>(); | ||
const element = <div ref={ref} />; | ||
expect(getReactElementRef(element)).to.equal(ref); | ||
}); | ||
|
||
it('should return null for a fragment', () => { | ||
const element = ( | ||
<React.Fragment> | ||
<p>Hello</p> | ||
<p>Hello</p> | ||
</React.Fragment> | ||
); | ||
expect(getReactElementRef(element)).to.equal(null); | ||
}); | ||
|
||
it('should return null for element with no ref', () => { | ||
const element = <div />; | ||
expect(getReactElementRef(element)).to.equal(null); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
packages/mui-utils/src/getReactElementRef/getReactElementRef.ts
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,20 @@ | ||
import * as React from 'react'; | ||
|
||
/** | ||
* Returns the ref of a React element handling differences between React 19 and older versions. | ||
* It will throw runtime error if the element is not a valid React element. | ||
* | ||
* @param element React.ReactElement | ||
* @returns React.Ref<any> | null | undefined | ||
*/ | ||
export default function getReactElementRef( | ||
element: React.ReactElement, | ||
): React.Ref<any> | null | undefined { | ||
// 'ref' is passed as prop in React 19, whereas 'ref' is directly attached to children in older versions | ||
if (parseInt(React.version, 10) >= 19) { | ||
return element.props?.ref; | ||
} | ||
// @ts-expect-error element.ref is not included in the ReactElement type | ||
// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/70189 | ||
return 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 './getReactElementRef'; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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