Skip to content

Commit

Permalink
Register component directly in render for more flexibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Tug committed Apr 2, 2020
1 parent c7f28a0 commit 76866b5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
6 changes: 4 additions & 2 deletions packages/edit-post/src/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ let blocksRegistered = false;
* that can be registered with `AppRegistry.registerComponent`
*/
export function initializeEditor( {
id,
initialHtml,
initialTitle,
initialHtmlModeEnabled,
Expand All @@ -35,12 +36,13 @@ export function initializeEditor( {
registerCoreBlocks();
blocksRegistered = true;

return render(
render(
<Editor
initialHtml={ initialHtml }
initialHtmlModeEnabled={ initialHtmlModeEnabled }
initialTitle={ initialTitle }
postType={ postType }
/>
/>,
id
);
}
13 changes: 8 additions & 5 deletions packages/element/src/react-platform.native.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* External dependencies
*/
import { AppRegistry } from "react-native";
import { isEmpty, omit } from 'lodash';

/**
Expand All @@ -13,23 +14,25 @@ import { applyFilters, doAction } from '@wordpress/hooks';
*/
import { cloneElement } from './react';

const render = ( element ) => ( propsFromNative ) => {
doAction( 'native.render', propsFromNative );
const render = ( element, id ) => AppRegistry.registerComponent( id, () => ( propsFromNative ) => {
const nativeProps = omit( propsFromNative || {}, [ 'rootTag' ] );

doAction( 'native.render', nativeProps );

// if we have not received props from a parent native app
// just render the element as it is
if ( isEmpty( omit( propsFromNative, [ 'rootTag' ] ) ) ) {
if ( isEmpty( nativeProps ) ) {
return element;
}

// Otherwise overwrite the existing props using a filter hook
const filteredProps = applyFilters(
'native.block_editor_props',
propsFromNative
nativeProps
);

return cloneElement( element, filteredProps );
};
} );

/**
* Render a given element on Native.
Expand Down
7 changes: 4 additions & 3 deletions packages/react-native-editor/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { AppRegistry, I18nManager } from 'react-native';
import { I18nManager } from 'react-native';

/**
* Internal dependencies
Expand Down Expand Up @@ -36,7 +36,8 @@ const gutenbergSetup = () => {
setupInitHooks();

const initializeEditor = require( '@wordpress/edit-post' ).initializeEditor;
return initializeEditor( {
initializeEditor( {
id: 'gutenberg',
initialHtml,
initialHtmlModeEnabled: false,
initialTitle: 'Welcome to Gutenberg!',
Expand Down Expand Up @@ -89,4 +90,4 @@ const setupLocale = ( locale, extraTranslations ) => {

reactNativeSetup();

AppRegistry.registerComponent( 'gutenberg', gutenbergSetup );
gutenbergSetup();

0 comments on commit 76866b5

Please sign in to comment.