From e604908247bd92e3b60e606f70e6e3187f51c5eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Viande?= Date: Tue, 14 May 2019 12:33:00 +0200 Subject: [PATCH 1/2] Implement render empty blocks --- src/components/DraftJsText.js | 4 ++-- src/components/types.js | 4 ++++ src/getBlocks.js | 6 ++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/components/DraftJsText.js b/src/components/DraftJsText.js index 79d0c8c..f06bdb1 100644 --- a/src/components/DraftJsText.js +++ b/src/components/DraftJsText.js @@ -15,10 +15,10 @@ import defaultStyles from './defaultStyles'; import type { DraftJsTextPropsType } from './types'; const DraftJsText = (props: DraftJsTextPropsType): any => { - const { text } = props; + const { text, renderEmptyBlocks } = props; let textElements = text; - if (textElements) { + if (textElements || renderEmptyBlocks) { textElements = loadAttributes({ text: props.text, customStyles: props.customStyles, diff --git a/src/components/types.js b/src/components/types.js index 757de7e..c90e2ae 100644 --- a/src/components/types.js +++ b/src/components/types.js @@ -15,6 +15,7 @@ export type BlockQuotePropsType = { entityRanges: Array, entityMap: ?Object, textProps: ?Object, + renderEmptyBlocks: boolean, }; export type DraftJsTextPropsType = { @@ -27,6 +28,7 @@ export type DraftJsTextPropsType = { entityMap: ?Object, navigate?: Function, textProps: ?Object, + renderEmptyBlocks: boolean, }; export type OrderedListItemPropsType = { @@ -42,6 +44,7 @@ export type OrderedListItemPropsType = { depth: number, defaultMarginLeft: number, textProps: ?Object, + renderEmptyBlocks: boolean, }; export type UnorderedListItemPropsType = { @@ -55,6 +58,7 @@ export type UnorderedListItemPropsType = { depth: number, defaultMarginLeft: number, textProps: ?Object, + renderEmptyBlocks: boolean, }; export type TextStyledPropsType = { diff --git a/src/getBlocks.js b/src/getBlocks.js index 6b73c61..d1974c0 100644 --- a/src/getBlocks.js +++ b/src/getBlocks.js @@ -20,6 +20,7 @@ type ParamsType = { blocks: ?Array<*>, entityMap: Object, }, + renderEmptyBlocks: boolean, customStyles: Object, atomicHandler: Function, navigate?: Function, @@ -36,6 +37,7 @@ export const ViewAfterList = (props: Object): React$Element<*> => ( const getBlocks = (params: ParamsType): ?Array> => { const { contentState, + renderEmptyBlocks, customStyles, navigate, orderedListSeparator, @@ -123,6 +125,7 @@ const getBlocks = (params: ParamsType): ?Array> => { {viewBefore} > => { customStyles={customStyles} navigate={navigate} textProps={textProps} + renderEmptyBlocks={renderEmptyBlocks} /> ); @@ -178,6 +182,7 @@ const getBlocks = (params: ParamsType): ?Array> => { navigate={navigate} defaultMarginLeft={depthMargin} textProps={textProps} + renderEmptyBlocks={renderEmptyBlocks} /> ); @@ -217,6 +222,7 @@ const getBlocks = (params: ParamsType): ?Array> => { navigate={navigate} defaultMarginLeft={depthMargin} textProps={textProps} + renderEmptyBlocks={renderEmptyBlocks} /> ); From 53a130345c179adf6d72768af5be4e1e52a7fd0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Viande?= Date: Tue, 14 May 2019 12:37:42 +0200 Subject: [PATCH 2/2] Render empty blocks add test --- .../__tests__/components/DraftJsText.test.js | 13 +++++++++++++ .../__snapshots__/DraftJsText.test.js.snap | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/sample/__tests__/components/DraftJsText.test.js b/sample/__tests__/components/DraftJsText.test.js index a809b72..54d3a43 100644 --- a/sample/__tests__/components/DraftJsText.test.js +++ b/sample/__tests__/components/DraftJsText.test.js @@ -115,3 +115,16 @@ it('renders text-align: center', () => { />).toJSON(); expect(tree).toMatchSnapshot(); }); + +it('renders empty blocks', () => { + const text = ''; + const tree = renderer.create().toJSON(); + expect(tree).toMatchSnapshot(); +}); diff --git a/sample/__tests__/components/__snapshots__/DraftJsText.test.js.snap b/sample/__tests__/components/__snapshots__/DraftJsText.test.js.snap index 744a24d..b7d2b0b 100644 --- a/sample/__tests__/components/__snapshots__/DraftJsText.test.js.snap +++ b/sample/__tests__/components/__snapshots__/DraftJsText.test.js.snap @@ -62,6 +62,25 @@ exports[`renders correctly with a text 1`] = ` `; +exports[`renders empty blocks 1`] = ` + + + +`; + exports[`renders null without a text 1`] = `null`; exports[`renders text-align: center 1`] = `