-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(react-motions): improve error reporting on invalid elements (#2…
- Loading branch information
1 parent
0f56cc9
commit 3a5bf3f
Showing
5 changed files
with
67 additions
and
3 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
37 changes: 37 additions & 0 deletions
37
packages/react-components/react-motions-preview/src/utils/getChildElement.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,37 @@ | ||
import * as React from 'react'; | ||
import { getChildElement } from './getChildElement'; | ||
|
||
describe('getChildElement', () => { | ||
it('throws if multiple elements are passed', () => { | ||
expect(() => { | ||
getChildElement([<div key="1" />, <div key="2" />] as unknown as React.ReactElement); | ||
}).toThrow('@fluentui/react-motions: Invalid child element'); | ||
}); | ||
|
||
it('throws if passed element does not support ref forwarding', () => { | ||
const TestA = () => <div />; | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
function TestB() { | ||
return <div />; | ||
} | ||
|
||
expect(() => { | ||
getChildElement(<TestA />); | ||
}).toThrow('@fluentui/react-motions: Invalid child element'); | ||
expect(() => { | ||
getChildElement(<TestB />); | ||
}).toThrow('@fluentui/react-motions: Invalid child element'); | ||
}); | ||
|
||
it('does not throw if passed element does supports ref forwarding', () => { | ||
const Test = React.forwardRef(() => <div />); | ||
|
||
expect(() => { | ||
getChildElement(<Test />); | ||
}).not.toThrow(); | ||
expect(() => { | ||
getChildElement(<div />); | ||
}).not.toThrow(); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
packages/react-components/react-motions-preview/src/utils/getChildElement.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,24 @@ | ||
import * as React from 'react'; | ||
import * as ReactIs from 'react-is'; | ||
|
||
export function getChildElement(children: React.ReactElement) { | ||
try { | ||
const child = React.Children.only(children) as React.ReactElement & { ref: React.Ref<HTMLElement> }; | ||
|
||
if (typeof child.type === 'string' || ReactIs.isForwardRef(child)) { | ||
return child as React.ReactElement & { ref: React.Ref<HTMLElement> }; | ||
} | ||
|
||
// We don't need to do anything here: we catch the exception from React to throw a more meaningful error | ||
// eslint-disable-next-line no-empty | ||
} catch {} | ||
|
||
throw new Error( | ||
[ | ||
'@fluentui/react-motions: Invalid child element.', | ||
'\n', | ||
'Motion factories require a single child element to be passed. ', | ||
'That element element should support ref forwarding i.e. it should be either an intrinsic element (e.g. div) or a component that uses React.forwardRef().', | ||
].join(''), | ||
); | ||
} |