Skip to content

Commit

Permalink
Fix focus title on new posts regression (#17180)
Browse files Browse the repository at this point in the history
  • Loading branch information
etoledom authored Aug 26, 2019
1 parent a7bb5b3 commit fe4c02f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
21 changes: 21 additions & 0 deletions packages/edit-post/src/editor.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import memize from 'memize';
import { size, map, without } from 'lodash';
import { subscribeSetFocusOnTitle } from 'react-native-gutenberg-bridge';

/**
* WordPress dependencies
Expand Down Expand Up @@ -31,6 +32,8 @@ class Editor extends Component {
this.getEditorSettings = memize( this.getEditorSettings, {
maxSize: 1,
} );

this.setTitleRef = this.setTitleRef.bind( this );
}

getEditorSettings(
Expand Down Expand Up @@ -66,6 +69,24 @@ class Editor extends Component {
return settings;
}

componentDidMount() {
this.subscriptionParentSetFocusOnTitle = subscribeSetFocusOnTitle( () => {
if ( this.postTitleRef ) {
this.postTitleRef.focus();
}
} );
}

componentWillUnmount() {
if ( this.subscriptionParentSetFocusOnTitle ) {
this.subscriptionParentSetFocusOnTitle.remove();
}
}

setTitleRef( titleRef ) {
this.postTitleRef = titleRef;
}

render() {
const {
settings,
Expand Down
17 changes: 0 additions & 17 deletions packages/editor/src/components/provider/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import RNReactNativeGutenbergBridge, {
subscribeParentGetHtml,
subscribeParentToggleHTMLMode,
subscribeUpdateHtml,
subscribeSetFocusOnTitle,
subscribeSetTitle,
} from 'react-native-gutenberg-bridge';

Expand All @@ -28,8 +27,6 @@ class NativeEditorProvider extends Component {

// Keep a local reference to `post` to detect changes
this.post = props.post;

this.setTitleRef = this.setTitleRef.bind( this );
}

componentDidMount() {
Expand All @@ -48,12 +45,6 @@ class NativeEditorProvider extends Component {
this.subscriptionParentUpdateHtml = subscribeUpdateHtml( ( payload ) => {
this.updateHtmlAction( payload.html );
} );

this.subscriptionParentSetFocusOnTitle = subscribeSetFocusOnTitle( () => {
if ( this.postTitleRef ) {
this.postTitleRef.focus();
}
} );
}

componentWillUnmount() {
Expand All @@ -72,10 +63,6 @@ class NativeEditorProvider extends Component {
if ( this.subscriptionParentUpdateHtml ) {
this.subscriptionParentUpdateHtml.remove();
}

if ( this.subscriptionParentSetFocusOnTitle ) {
this.subscriptionParentSetFocusOnTitle.remove();
}
}

componentDidUpdate( prevProps ) {
Expand All @@ -87,10 +74,6 @@ class NativeEditorProvider extends Component {
}
}

setTitleRef( titleRef ) {
this.postTitleRef = titleRef;
}

serializeToNativeAction() {
if ( this.props.mode === 'text' ) {
this.updateHtmlAction( this.props.getEditedPostContent() );
Expand Down

0 comments on commit fe4c02f

Please sign in to comment.