This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1386 from atom/aw/enzyme-up
Upgrade to Enzyme 3
- Loading branch information
Showing
24 changed files
with
612 additions
and
303 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,86 @@ | ||
import {Emitter} from 'event-kit'; | ||
|
||
/* | ||
* Allow child components to operate on refs captured by a parent component. | ||
* | ||
* React does not guarantee that refs are available until the component has finished mounting (before | ||
* componentDidMount() is called), but a component does not finish mounting until all of its children are mounted. This | ||
* causes problems when a child needs to consume a DOM node from its parent to interact with the Atom API, like we do in | ||
* the `Tooltip` and `Commands` components. | ||
* | ||
* To pass a ref to a child, capture it in a RefHolder in the parent, and pass the RefHolder to the child: | ||
* | ||
* class Parent extends React.Component { | ||
* constructor() { | ||
* this.theRef = new RefHolder(); | ||
* } | ||
* | ||
* render() { | ||
* return ( | ||
* <div ref={this.theRef.setter}> | ||
* <Child theRef={this.theRef} /> | ||
* </div> | ||
* ) | ||
* } | ||
* } | ||
* | ||
* In the child, use the `observe()` method to defer operations that need the DOM node to proceed: | ||
* | ||
* class Child extends React.Component { | ||
* | ||
* componentDidMount() { | ||
* this.props.theRef.observe(domNode => this.register(domNode)) | ||
* } | ||
* | ||
* render() { | ||
* return null; | ||
* } | ||
* | ||
* register(domNode) { | ||
* console.log('Hey look I have a real DOM node', domNode); | ||
* } | ||
* } | ||
*/ | ||
export default class RefHolder { | ||
constructor() { | ||
this.emitter = new Emitter(); | ||
this.value = undefined; | ||
} | ||
|
||
isEmpty() { | ||
return this.value === undefined; | ||
} | ||
|
||
get() { | ||
if (this.isEmpty()) { | ||
throw new Error('RefHolder is empty'); | ||
} | ||
return this.value; | ||
} | ||
|
||
setter = value => { | ||
if (value === null || value === undefined) { return; } | ||
const oldValue = this.value; | ||
this.value = value; | ||
if (value !== oldValue) { | ||
this.emitter.emit('did-update', value); | ||
} | ||
} | ||
|
||
observe(callback) { | ||
if (!this.isEmpty()) { | ||
callback(this.value); | ||
} | ||
return this.emitter.on('did-update', callback); | ||
} | ||
|
||
static on(valueOrHolder) { | ||
if (valueOrHolder instanceof this) { | ||
return valueOrHolder; | ||
} else { | ||
const holder = new this(); | ||
holder.setter(valueOrHolder); | ||
return holder; | ||
} | ||
} | ||
} |
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.