-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
2,759 additions
and
1,509 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"version": "4.9.0-alpha.2", | ||
"version": "4.9.0-alpha.3", | ||
"packages": [ | ||
"packages/*" | ||
], | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,40 @@ | ||
import {forwardRef, useId, useImperativeHandle, useRef} from 'react'; | ||
|
||
const WithRef = (WrappedComponent) => { | ||
const HoC = forwardRef(function (props, ref) { | ||
const {['data-withref-id']: givenId, outermostRef, referrerName, ...rest} = props; | ||
const divRef = useRef(); | ||
const generatedId = useId(); | ||
const id = givenId || generatedId; | ||
|
||
useImperativeHandle(outermostRef, () => { | ||
const refNode = divRef.current; | ||
const attributeSelector = `[data-withref-id="${refNode.getAttribute('data-withref-target')}"]`; | ||
/* The intended code is to search for the referrer element via a single querySelector call. But unit tests cannot handle :has() properly. | ||
const selector = `:scope ${attributeSelector}, :scope :has(${attributeSelector})`; | ||
return refNode?.parentElement?.querySelector(selector) || null; | ||
*/ | ||
const targetNode = refNode?.parentElement?.querySelector(attributeSelector) || null; | ||
for (let current = targetNode; current; current = current.parentElement) { | ||
if (current?.parentElement === refNode?.parentElement) { | ||
return current; | ||
} | ||
} | ||
return null; | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<WrappedComponent {...rest} data-withref-id={id} ref={ref} /> | ||
<div data-withref-target={id} data-withref-referrer={referrerName} ref={divRef} style={{display: 'none'}} /> | ||
</> | ||
); | ||
}); | ||
|
||
HoC.displayName = 'WithRef'; | ||
|
||
return HoC; | ||
}; | ||
|
||
export default WithRef; | ||
export {WithRef}; |
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,3 @@ | ||
{ | ||
"main": "WithRef.js" | ||
} |
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 {createRef, Component as ReactComponent} from 'react'; | ||
import {render, screen} from '@testing-library/react'; | ||
|
||
import {WithRef} from '../WithRef'; | ||
|
||
describe('WithRef', () => { | ||
const DummyComponent = class extends ReactComponent { | ||
static displayName = 'DummyComponent'; | ||
|
||
render () { | ||
return (<div {...this.props}>Dummy</div>); | ||
} | ||
}; | ||
|
||
test('should return a DOM node of the wrapped component via outermostRef', () => { | ||
const ComponentWithRef = WithRef(DummyComponent); | ||
const id = 'test-wrapped'; | ||
const divRef = createRef(); | ||
render( | ||
<ComponentWithRef outermostRef={divRef} data-testid={id} /> | ||
); | ||
const expectedNode = screen.getByTestId(id); | ||
|
||
expect(divRef.current).toBe(expectedNode); | ||
}); | ||
|
||
test('should pass ref', () => { | ||
const ComponentWithRef = WithRef('div'); | ||
const id = 'test-wrapped'; | ||
const divRef = createRef(); | ||
|
||
render( | ||
<ComponentWithRef ref={divRef} data-testid={id} /> | ||
); | ||
const expectedNode = screen.getByTestId(id); | ||
|
||
expect(divRef.current).toBe(expectedNode); | ||
}); | ||
}); |
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
Oops, something went wrong.