Skip to content

Commit

Permalink
Fix focus on title on mount
Browse files Browse the repository at this point in the history
  • Loading branch information
Tug committed Apr 16, 2019
1 parent e88e4a6 commit ed9a548
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 26 deletions.
24 changes: 24 additions & 0 deletions jest/setup.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import { NativeModules } from 'react-native';

jest.mock( '../react-native-gutenberg-bridge', () => {
return {
addEventListener: jest.fn(),
Expand Down Expand Up @@ -36,3 +41,22 @@ if ( ! global.window.matchMedia ) {
removeListener: () => {},
} );
}

// Overwrite some native module mocks from `react-native` jest preset:
// https://github.com/facebook/react-native/blob/master/jest/setup.js
// to fix issue "TypeError: Cannot read property 'Commands' of undefined"
// raised when calling focus or blur on a native component
const mockNativeModules = {
UIManager: {
...NativeModules.UIManager,
getViewManagerConfig: jest.fn( () => ( { Commands: {} } ) ),
},
};

Object.keys( mockNativeModules ).forEach( ( module ) => {
try {
jest.doMock( module, () => mockNativeModules[ module ] ); // needed by FacebookSDK-test
} catch ( error ) {
jest.doMock( module, () => mockNativeModules[ module ], { virtual: true } );
}
} );
22 changes: 1 addition & 21 deletions src/app/AppContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
* External dependencies
*/
import React from 'react';
import { type EmitterSubscription, type InputText } from 'react-native';
import { type EmitterSubscription } from 'react-native';
import RNReactNativeGutenbergBridge, {
subscribeParentGetHtml,
subscribeParentToggleHTMLMode,
subscribeUpdateHtml,
subscribeSetFocusOnTitle,
subscribeSetTitle,
sendNativeEditorDidLayout,
} from 'react-native-gutenberg-bridge';
Expand Down Expand Up @@ -60,18 +59,14 @@ type PropsType = {
*/
class AppContainer extends React.Component<PropsType> {
post: PostType;
postTitleRef: ?InputText;
subscriptionParentGetHtml: ?EmitterSubscription;
subscriptionParentToggleHTMLMode: ?EmitterSubscription;
subscriptionParentUpdateHtml: ?EmitterSubscription;
subscriptionParentSetFocusOnTitle: ?EmitterSubscription;
subscriptionParentSetTitle: ?EmitterSubscription;

constructor( props: PropsType ) {
super( props );

( this: any ).setTitleRef = this.setTitleRef.bind( this );

this.post = props.post || {
id: 1,
title: {
Expand Down Expand Up @@ -104,12 +99,6 @@ class AppContainer extends React.Component<PropsType> {
this.updateHtmlAction( payload.html );
} );

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

this.subscriptionParentSetTitle = subscribeSetTitle( ( payload ) => {
this.props.editTitle( payload.title );
} );
Expand All @@ -128,10 +117,6 @@ class AppContainer extends React.Component<PropsType> {
this.subscriptionParentUpdateHtml.remove();
}

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

if ( this.subscriptionParentSetTitle ) {
this.subscriptionParentSetTitle.remove();
}
Expand Down Expand Up @@ -176,14 +161,9 @@ class AppContainer extends React.Component<PropsType> {
this.props.resetEditorBlocksWithoutUndoLevel( parsed );
}

setTitleRef( titleRef: ?InputText ) {
this.postTitleRef = titleRef;
}

render() {
return (
<MainScreen
setTitleRef={ this.setTitleRef }
onNativeEditorDidLayout={ sendNativeEditorDidLayout }
/>
);
Expand Down
4 changes: 1 addition & 3 deletions src/app/MainScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* External dependencies
*/
import React from 'react';
import { type InputText, LayoutChangeEvent, SafeAreaView } from 'react-native';
import { LayoutChangeEvent, SafeAreaView } from 'react-native';
import SafeArea from 'react-native-safe-area';

/**
Expand All @@ -26,7 +26,6 @@ type PropsType = {
isReady: boolean,
mode: string,
onNativeEditorDidLayout: () => ?mixed,
setTitleRef: ?InputText => void,
};

type StateType = {
Expand Down Expand Up @@ -116,7 +115,6 @@ class MainScreen extends React.Component<PropsType, StateType> {
isFullyBordered={ this.state.isFullyBordered }
rootViewHeight={ this.state.rootViewHeight }
safeAreaBottomInset={ this.state.safeAreaBottomInset }
setTitleRef={ this.props.setTitleRef }
/>
);
}
Expand Down
11 changes: 9 additions & 2 deletions src/app/VisualEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type PropsType = {
rootViewHeight: number,
safeAreaBottomInset: number,
isFullyBordered: boolean,
setTitleRef: ?InputText => void,
};

/*
Expand All @@ -40,6 +39,14 @@ type PropsType = {
* - `gutenberg/packages/edit-post/src/components/layout/index.js`
*/
class VisualEditor extends React.Component<PropsType> {
postTitleRef: ?InputText;

componentDidMount() {
if ( this.postTitleRef ) {
this.postTitleRef.focus();
}
}

blockHolderBorderStyle() {
return this.props.isFullyBordered ? styles.blockHolderFullBordered : styles.blockHolderSemiBordered;
}
Expand All @@ -52,7 +59,7 @@ class VisualEditor extends React.Component<PropsType> {

return (
<PostTitle
innerRef={ this.props.setTitleRef }
innerRef={ ( titleRef: ?InputText ) => this.postTitleRef = titleRef }
title={ title }
onUpdate={ editTitle }
placeholder={ __( 'Add title' ) }
Expand Down

0 comments on commit ed9a548

Please sign in to comment.