-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(MountNode): support refs as value for
node
prop (#3449)
- Loading branch information
1 parent
aaee3b1
commit 527863b
Showing
15 changed files
with
131 additions
and
113 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import _ from 'lodash' | ||
import { isBrowser, isRefObject } from '../../../lib' | ||
|
||
const toRef = _.memoize(node => ({ current: node })) | ||
|
||
/** | ||
* Given `this.props`, return a `node` value or undefined. | ||
* | ||
* @param {object|React.RefObject} props Component's props | ||
* @return {React.RefObject|undefined} | ||
*/ | ||
const getNodeRefFromProps = (props) => { | ||
const { node } = props | ||
|
||
if (isBrowser()) { | ||
if (isRefObject(node)) return node | ||
return _.isNil(node) ? toRef(document.body) : toRef(node) | ||
} | ||
} | ||
|
||
export default getNodeRefFromProps |
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 was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
test/specs/addons/MountNode/lib/getNodeRefFromProps-test.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 getNodeRefFromProps from 'src/addons/MountNode/lib/getNodeRefFromProps' | ||
import isBrowser from 'src/lib/isBrowser' | ||
|
||
describe('getNodeRefFromProps', () => { | ||
describe('browser', () => { | ||
it('returns a ref to node when it defined', () => { | ||
const node = document.createElement('div') | ||
const nodeRef = getNodeRefFromProps({ node }) | ||
|
||
nodeRef.should.have.property('current', node) | ||
}) | ||
|
||
it('returns node when it defined as React.Ref object', () => { | ||
const inputRef = { current: document.createElement('div') } | ||
const outputRef = getNodeRefFromProps({ node: inputRef }) | ||
|
||
outputRef.should.equal(inputRef) | ||
}) | ||
|
||
it('returns document.body by default', () => { | ||
getNodeRefFromProps({}).should.have.property('current', document.body) | ||
}) | ||
}) | ||
|
||
describe('browser', () => { | ||
before(() => { | ||
isBrowser.override = false | ||
}) | ||
|
||
after(() => { | ||
isBrowser.override = null | ||
}) | ||
|
||
it('always returns null', () => { | ||
expect(getNodeRefFromProps({ node: 'foo' })).to.be.a('undefined') | ||
expect(getNodeRefFromProps({})).to.be.a('undefined') | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.