-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[material-ui] Cherry pick ref accessing PRs (#44543)
- Loading branch information
1 parent
9172305
commit 6003187
Showing
19 changed files
with
114 additions
and
29 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(null); | ||
// @ts-expect-error | ||
expect(getReactElementRef()).to.equal(null); | ||
// @ts-expect-error | ||
expect(getReactElementRef(1)).to.equal(null); | ||
|
||
const children = [<div key="1" />, <div key="2" />]; | ||
// @ts-expect-error | ||
expect(getReactElementRef(children)).to.equal(null); | ||
}); | ||
|
||
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); | ||
}); | ||
}); |
18 changes: 18 additions & 0 deletions
18
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,18 @@ | ||
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 | ||
*/ | ||
export default function getReactElementRef(element: React.ReactElement): React.Ref<any> | null { | ||
// '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 as any)?.ref || null; | ||
} | ||
// @ts-expect-error element.ref is not included in the ReactElement type | ||
// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/70189 | ||
return element?.ref || null; | ||
} |
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 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