-
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.
- Loading branch information
1 parent
292c595
commit 2743e97
Showing
8 changed files
with
142 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import * as React from 'react'; | ||
import { DOMElement } from 'react'; | ||
|
||
export interface RefProps { | ||
[key: string]: any; | ||
|
||
/** Primary content. */ | ||
children: React.ReactNode; | ||
|
||
/** | ||
* Called when componentDidMount. | ||
* | ||
* @param {DOMElement} node - Referred node. | ||
*/ | ||
innerRef: (node: DOMElement) => void; | ||
} | ||
|
||
declare const Ref: React.ComponentClass<RefProps>; | ||
|
||
export default Ref; |
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 PropTypes from 'prop-types' | ||
import { Children, Component } from 'react' | ||
import { findDOMNode } from 'react-dom' | ||
|
||
import { META } from '../../lib' | ||
|
||
/** | ||
* This component exposes a callback prop that always returns the DOM node of both functional and class component | ||
* children. | ||
*/ | ||
export default class Ref extends Component { | ||
static propTypes = { | ||
children: PropTypes.element.isRequired, | ||
|
||
/** | ||
* Called when componentDidMount. | ||
* | ||
* @param {DOMElement} node - Referred node. | ||
*/ | ||
innerRef: PropTypes.func.isRequired, | ||
} | ||
|
||
static _meta = { | ||
name: 'Ref', | ||
type: META.TYPES.ADDON, | ||
} | ||
|
||
componentDidMount() { | ||
const { innerRef } = this.props | ||
const node = findDOMNode(this) | ||
|
||
innerRef(node) | ||
} | ||
|
||
render() { | ||
const { children } = this.props | ||
|
||
return Children.only(children) | ||
} | ||
} |
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, RefProps } from './Ref'; |
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 './Ref' |
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,60 @@ | ||
import _ from 'lodash' | ||
import React from 'react' | ||
|
||
import Ref from 'src/addons/Ref/Ref' | ||
import * as common from 'test/specs/commonTests' | ||
import { sandbox } from 'test/utils' | ||
import { CompositeClass, CompositeFunction, DOMClass, DOMFunction } from './fixtures' | ||
|
||
const nodeMount = (Component, innerRef) => ( | ||
mount( | ||
<Ref innerRef={innerRef}> | ||
<Component /> | ||
</Ref> | ||
) | ||
.find('#node') | ||
.getDOMNode() | ||
) | ||
|
||
describe('Ref', () => { | ||
common.hasValidTypings(Ref, null, { | ||
requiredProps: { | ||
children: <div />, | ||
innerRef: _.noop, | ||
}, | ||
}) | ||
|
||
describe('innerRef', () => { | ||
it('returns node from a functional component with DOM node', () => { | ||
const innerRef = sandbox.spy() | ||
const node = nodeMount(DOMFunction, innerRef) | ||
|
||
innerRef.should.have.been.calledOnce() | ||
innerRef.should.have.been.calledWithMatch(node) | ||
}) | ||
|
||
it('returns node from a functional component', () => { | ||
const innerRef = sandbox.spy() | ||
const node = nodeMount(CompositeFunction, innerRef) | ||
|
||
innerRef.should.have.been.calledOnce() | ||
innerRef.should.have.been.calledWithMatch(node) | ||
}) | ||
|
||
it('returns node from a class component with DOM node', () => { | ||
const innerRef = sandbox.spy() | ||
const node = nodeMount(DOMClass, innerRef) | ||
|
||
innerRef.should.have.been.calledOnce() | ||
innerRef.should.have.been.calledWithMatch(node) | ||
}) | ||
|
||
it('returns node from a class component', () => { | ||
const innerRef = sandbox.spy() | ||
const node = nodeMount(CompositeClass, innerRef) | ||
|
||
innerRef.should.have.been.calledOnce() | ||
innerRef.should.have.been.calledWithMatch(node) | ||
}) | ||
}) | ||
}) |
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 @@ | ||
/* eslint-disable react/no-multi-comp */ | ||
import React, { Component } from 'react' | ||
|
||
export const DOMFunction = (props) => <div {...props} id='node' /> | ||
|
||
export const CompositeFunction = (props) => <DOMFunction {...props} /> | ||
|
||
export class DOMClass extends Component { | ||
render() { | ||
return <div {...this.props} id='node' /> | ||
} | ||
} | ||
|
||
export class CompositeClass extends Component { | ||
render() { | ||
return <DOMClass {...this.props} /> | ||
} | ||
} |