From 39c5ea72ab7e971b5ca4ea3d4164b6b1a9f00b88 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Fri, 14 Jun 2019 16:24:01 +0100 Subject: [PATCH] Support forwardRef components in save functions (#16180) --- packages/element/src/serialize.js | 7 +++++++ packages/element/src/test/serialize.js | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/packages/element/src/serialize.js b/packages/element/src/serialize.js index 5c2c2a8011d2a7..b700831f1dccd0 100644 --- a/packages/element/src/serialize.js +++ b/packages/element/src/serialize.js @@ -49,10 +49,14 @@ import { createContext, Fragment, StrictMode, + forwardRef, } from './react'; import RawHTML from './raw-html'; const { Provider, Consumer } = createContext(); +const ForwardRef = forwardRef( () => { + return null; +} ); /** * Valid attribute types. @@ -406,6 +410,9 @@ export function renderElement( element, context, legacyContext = {} ) { case Consumer.$$typeof: return renderElement( props.children( context || type._currentValue ), context, legacyContext ); + + case ForwardRef.$$typeof: + return renderElement( type.render( props ), context, legacyContext ); } return ''; diff --git a/packages/element/src/test/serialize.js b/packages/element/src/test/serialize.js index 7fe4251666decd..b6a8c906751d97 100644 --- a/packages/element/src/test/serialize.js +++ b/packages/element/src/test/serialize.js @@ -12,6 +12,7 @@ import { createElement, Fragment, StrictMode, + forwardRef, } from '../react'; import RawHTML from '../raw-html'; import serialize, { @@ -84,6 +85,20 @@ describe( 'serialize()', () => { ); } ); + it( 'should render with forwardRef', () => { + const ForwardedComponent = forwardRef( () => { + return
test
; + } ); + + const result = serialize( + + ); + + expect( result ).toBe( + '
test
' + ); + } ); + describe( 'empty attributes', () => { it( 'should not render a null attribute value', () => { const result = serialize(