This repository was archived by the owner on Mar 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
feat(Ref): support of forwardRef()
API
#491
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
62929d9
feat(Ref): support of `forwardRef()` API
layershifter 9882478
fix styling
layershifter 46c6913
Merge branches 'feat/ref-forward' and 'master' of https://github.com/…
layershifter 17cbc97
update yarn.lock
layershifter 33c95c1
add entry to changelog
layershifter 57faf87
rename examples
layershifter bb93b77
Merge branch 'master' of https://github.com/stardust-ui/react into fe…
layershifter 306ca3b
Merge branches 'feat/ref-forward' and 'master' of https://github.com/…
layershifter 97594a7
fix changelog
layershifter c418eec
clean up test
layershifter 747931c
Merge branches 'feat/ref-forward' and 'master' of https://github.com/…
layershifter d11dcc4
regenerate lock
layershifter 1501b0d
Merge branches 'feat/ref-forward' and 'master' of https://github.com/…
layershifter 0d75b11
Merge branches 'feat/ref-forward' and 'master' of https://github.com/…
layershifter 6f0039f
fix review comments
layershifter 1c77d7b
Merge branch 'master' of https://github.com/stardust-ui/react into fe…
layershifter 31de50a
fix tests
layershifter 5642eab
Merge branches 'feat/ref-forward' and 'master' of https://github.com/…
layershifter bce1d43
fix types
layershifter 098ee71
Merge branch 'master' of https://github.com/stardust-ui/react into fe…
layershifter a2f7fd1
Merge branches 'feat/ref-forward' and 'master' of https://github.com/…
layershifter 8e41c02
add entry to changelog
layershifter b8a3384
Merge branch 'master' of https://github.com/stardust-ui/react into fe…
layershifter e1d73e1
update changelog
layershifter 921f35a
Merge branches 'feat/ref-forward' and 'master' of https://github.com/…
layershifter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
54 changes: 54 additions & 0 deletions
54
docs/src/examples/components/Ref/Types/RefForwardingExample.tsx
This file contains hidden or 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,54 @@ | ||
import React from 'react' | ||
import { Grid, Ref, Segment } from '@stardust-ui/react' | ||
|
||
const ExampleButton = React.forwardRef<HTMLButtonElement>((props, ref) => ( | ||
<div> | ||
<button {...props} ref={ref} /> | ||
</div> | ||
)) | ||
|
||
class RefForwardingExample extends React.Component { | ||
forwardedRef = React.createRef<HTMLButtonElement>() | ||
state = { isMounted: false } | ||
|
||
componentDidMount() { | ||
this.setState({ isMounted: true }) | ||
} | ||
|
||
render() { | ||
const { isMounted } = this.state | ||
const buttonNode = this.forwardedRef.current | ||
|
||
return ( | ||
<Grid columns={2}> | ||
<Segment> | ||
<p> | ||
A button below uses <code>forwardRef</code> API. | ||
</p> | ||
|
||
<Ref innerRef={this.forwardedRef}> | ||
<ExampleButton>A button</ExampleButton> | ||
</Ref> | ||
</Segment> | ||
|
||
{isMounted && ( | ||
<code style={{ margin: 10 }}> | ||
<pre> | ||
{JSON.stringify( | ||
{ | ||
nodeName: buttonNode.nodeName, | ||
nodeType: buttonNode.nodeType, | ||
textContent: buttonNode.textContent, | ||
}, | ||
null, | ||
2, | ||
)} | ||
</pre> | ||
</code> | ||
)} | ||
</Grid> | ||
) | ||
} | ||
} | ||
|
||
export default RefForwardingExample |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,35 @@ | ||
import * as PropTypes from 'prop-types' | ||
import * as React from 'react' | ||
import { findDOMNode } from 'react-dom' | ||
|
||
import { ChildrenComponentProps, handleRef } from '../../lib' | ||
|
||
export interface RefFindNodeProps extends ChildrenComponentProps<React.ReactElement<any>> { | ||
/** | ||
* Called when a child component will be mounted or updated. | ||
* | ||
* @param {HTMLElement} node - Referred node. | ||
*/ | ||
innerRef?: React.Ref<any> | ||
} | ||
|
||
export default class RefFindNode extends React.Component<RefFindNodeProps> { | ||
static propTypes = { | ||
children: PropTypes.element.isRequired, | ||
innerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]), | ||
} | ||
|
||
componentDidMount() { | ||
handleRef(this.props.innerRef, findDOMNode(this)) | ||
} | ||
|
||
componentWillUnmount() { | ||
handleRef(this.props.innerRef, null) | ||
} | ||
|
||
render() { | ||
const { children } = this.props | ||
|
||
return children | ||
} | ||
} |
This file contains hidden or 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,36 @@ | ||
import * as PropTypes from 'prop-types' | ||
import * as React from 'react' | ||
|
||
import { ChildrenComponentProps, handleRef } from '../../lib' | ||
|
||
export interface RefForwardProps | ||
extends ChildrenComponentProps<React.ReactElement<any> & { ref: React.Ref<any> }> { | ||
/** | ||
* Called when a child component will be mounted or updated. | ||
* | ||
* @param {HTMLElement} node - Referred node. | ||
*/ | ||
innerRef?: React.Ref<any> | ||
} | ||
|
||
export default class RefForward extends React.Component<RefForwardProps> { | ||
static propTypes = { | ||
children: PropTypes.element.isRequired, | ||
innerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]), | ||
} | ||
|
||
private handleRefOverride = (node: HTMLElement) => { | ||
const { children, innerRef } = this.props | ||
|
||
handleRef(children.ref, node) | ||
handleRef(innerRef, node) | ||
} | ||
|
||
render() { | ||
const { children } = this.props | ||
|
||
return React.cloneElement(children, { | ||
ref: this.handleRefOverride, | ||
}) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,52 @@ | ||
import { mount } from 'enzyme' | ||
import * as React from 'react' | ||
|
||
import Ref from 'src/components/Ref/Ref' | ||
import { CompositeClass, CompositeFunction, DOMClass, DOMFunction } from './fixtures' | ||
|
||
const testInnerRef = Component => { | ||
const innerRef = jest.fn() | ||
const node = mount( | ||
<Ref innerRef={innerRef}> | ||
<Component /> | ||
</Ref>, | ||
).getDOMNode() | ||
|
||
expect(innerRef).toHaveBeenCalledTimes(1) | ||
expect(innerRef).toHaveBeenCalledWith(node) | ||
} | ||
|
||
describe('Ref', () => { | ||
describe('innerRef', () => { | ||
it('returns node from a functional component with DOM node', () => { | ||
testInnerRef(DOMFunction) | ||
}) | ||
|
||
it('returns node from a functional component', () => { | ||
testInnerRef(CompositeFunction) | ||
}) | ||
|
||
it('returns node from a class component with DOM node', () => { | ||
testInnerRef(DOMClass) | ||
}) | ||
|
||
it('returns node from a class component', () => { | ||
testInnerRef(CompositeClass) | ||
}) | ||
|
||
it('returns "null" after unmount', () => { | ||
const innerRef = jest.fn() | ||
const wrapper = mount( | ||
<Ref innerRef={innerRef}> | ||
<CompositeClass /> | ||
</Ref>, | ||
) | ||
|
||
innerRef.mockClear() | ||
wrapper.unmount() | ||
|
||
expect(innerRef).toHaveBeenCalledTimes(1) | ||
expect(innerRef).toHaveBeenCalledWith(null) | ||
}) | ||
}) | ||
}) |
This file contains hidden or 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,21 @@ | ||
import { mount } from 'enzyme' | ||
import * as React from 'react' | ||
|
||
import RefForward from 'src/components/Ref/RefForward' | ||
import { ForwardedRef } from './fixtures' | ||
|
||
describe('RefForward', () => { | ||
describe('innerRef', () => { | ||
it('works with "forwardRef" API', () => { | ||
const forwardedRef = React.createRef<HTMLButtonElement>() | ||
const innerRef = React.createRef() | ||
|
||
mount( | ||
<RefForward innerRef={innerRef}>{<ForwardedRef ref={forwardedRef} /> as any}</RefForward>, | ||
) | ||
|
||
expect(forwardedRef.current).toBeInstanceOf(Element) | ||
expect(innerRef.current).toBeInstanceOf(Element) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(!) A new dependency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍