Skip to content

Commit

Permalink
Leverage forwardRef to remove findDOMNode from the block component (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Nov 2, 2018
1 parent ccb199e commit d3d6f93
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 20 deletions.
1 change: 1 addition & 0 deletions packages/editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Polish

- Remove `findDOMNode` usage from the `Inserter` component.
- Remove `findDOMNode` usage from the `Block` component.

## 6.1.0 (2018-10-30)

Expand Down
16 changes: 2 additions & 14 deletions packages/editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { get, reduce, size, first, last } from 'lodash';
/**
* WordPress dependencies
*/
import { Component, findDOMNode, Fragment } from '@wordpress/element';
import { Component, Fragment } from '@wordpress/element';
import {
focus,
isTextField,
Expand Down Expand Up @@ -102,24 +102,12 @@ export class BlockListBlock extends Component {
}

setBlockListRef( node ) {
// Disable reason: The root return element uses a component to manage
// event nesting, but the parent block list layout needs the raw DOM
// node to track multi-selection.
//
// eslint-disable-next-line react/no-find-dom-node
node = findDOMNode( node );

this.wrapperNode = node;

this.props.blockRef( node, this.props.clientId );
}

bindBlockNode( node ) {
// Disable reason: The block element uses a component to manage event
// nesting, but we rely on a raw DOM node for focusing.
//
// eslint-disable-next-line react/no-find-dom-node
this.node = findDOMNode( node );
this.node = node;
}

/**
Expand Down
15 changes: 10 additions & 5 deletions packages/editor/src/components/ignore-nested-events/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { reduce } from 'lodash';
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { Component, forwardRef } from '@wordpress/element';

/**
* Component which renders a div with passed props applied except the optional
Expand All @@ -22,7 +22,7 @@ import { Component } from '@wordpress/element';
*
* @type {Component}
*/
class IgnoreNestedEvents extends Component {
export class IgnoreNestedEvents extends Component {
constructor() {
super( ...arguments );

Expand Down Expand Up @@ -66,7 +66,7 @@ class IgnoreNestedEvents extends Component {
}

render() {
const { childHandledEvents = [], ...props } = this.props;
const { childHandledEvents = [], forwardedRef, ...props } = this.props;

const eventHandlers = reduce( [
...childHandledEvents,
Expand Down Expand Up @@ -96,8 +96,13 @@ class IgnoreNestedEvents extends Component {
return result;
}, {} );

return <div { ...props } { ...eventHandlers } />;
return <div ref={ forwardedRef } { ...props } { ...eventHandlers } />;
}
}

export default IgnoreNestedEvents;
const forwardedIgnoreNestedEvents = ( props, ref ) => {
return <IgnoreNestedEvents { ...props } forwardedRef={ ref } />;
};
forwardedIgnoreNestedEvents.displayName = 'IgnoreNestedEvents';

export default forwardRef( forwardedIgnoreNestedEvents );
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { mount } from 'enzyme';
/**
* Internal dependencies
*/
import IgnoreNestedEvents from '../';
import { IgnoreNestedEvents } from '../';

describe( 'IgnoreNestedEvents', () => {
it( 'passes props to its rendered div', () => {
Expand Down

0 comments on commit d3d6f93

Please sign in to comment.