Skip to content

Commit 719342c

Browse files
author
Igor Canedo
committed
changing rn draftjs return to return the list of elements without a react element wrapper
1 parent 047c960 commit 719342c

File tree

3 files changed

+40
-57
lines changed

3 files changed

+40
-57
lines changed

index.js

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,6 @@
66

77
// @flow
88

9-
import React from 'react';
10-
import {
11-
View,
12-
} from 'react-native';
9+
import getRNDraftJSBlocks from './src/getBlocks';
1310

14-
import getBlocks from './src/getBlocks';
15-
16-
const styles = {
17-
container: {
18-
flex: 1,
19-
},
20-
};
21-
22-
type RnDraftJsRenderPropsType = {
23-
contentState: Object,
24-
customStyles?: Object,
25-
atomicHandler: Function,
26-
navigate?: Function,
27-
orderedListSeparator?: string,
28-
};
29-
30-
const RNDraftJSRender = (props: RnDraftJsRenderPropsType): any => {
31-
const blocks = getBlocks(
32-
props.contentState, props.customStyles, props.atomicHandler,
33-
props.navigate, props.orderedListSeparator);
34-
35-
return (
36-
<View style={styles.container}>
37-
{ blocks }
38-
</View>
39-
);
40-
};
41-
42-
RNDraftJSRender.defaultProps = {
43-
customStyles: {},
44-
atomicHandler: (): any => null,
45-
navigate: undefined,
46-
orderedListSeparator: '.',
47-
};
48-
49-
module.exports = RNDraftJSRender;
11+
module.exports = getRNDraftJSBlocks;

sample/src/index.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ import {
1414
View,
1515
} from 'react-native';
1616

17-
import RNDraftJS from '../react-native-draftjs-render';
17+
import getRNDraftJSBlocks from '../react-native-draftjs-render';
1818

1919
import data from './resourceMock.json';
2020

2121
const styles = StyleSheet.create({
2222
container: {
2323
flex: 1,
2424
paddingHorizontal: 16,
25-
backgroundColor: '#F5FCFF',
25+
backgroundColor: '#f4f4f4',
2626
},
2727
});
2828

@@ -107,13 +107,16 @@ const atomicHandler = (item: Object): any => {
107107
};
108108

109109
export default function App(): any {
110+
const params = {
111+
contentState: data,
112+
customStyles,
113+
atomicHandler,
114+
};
115+
const blocks = getRNDraftJSBlocks(params);
116+
110117
return (
111118
<ScrollView style={styles.container}>
112-
<RNDraftJS
113-
contentState={data}
114-
customStyles={customStyles}
115-
atomicHandler={atomicHandler}
116-
/>
119+
{blocks}
117120
</ScrollView>
118121
);
119122
}

src/getBlocks.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,31 @@ import UnorderedListItem from './components/UnorderedListItem';
1717
import OrderedListItem from './components/OrderedListItem';
1818
import generateKey from './utils/generateKey';
1919

20-
const getBlocks = (
21-
bodyData: Object = {},
22-
customStyles: Object = {},
20+
type ParamsType = {
21+
contentState: Object,
22+
customStyles: Object,
2323
atomicHandler: Function,
2424
navigate?: Function,
25-
orderedListSeparator?: string): any => {
26-
if (!bodyData.blocks) {
25+
orderedListSeparator?: string,
26+
};
27+
28+
const getBlocks = (params: ParamsType): ?Array<*> => {
29+
const {
30+
contentState,
31+
customStyles,
32+
navigate,
33+
orderedListSeparator,
34+
} = params;
35+
let { atomicHandler } = params;
36+
37+
if (!contentState.blocks) {
2738
return null;
2839
}
2940

41+
if (typeof atomicHandler === 'undefined') {
42+
atomicHandler = (item: Object): any => item;
43+
}
44+
3045
const counters = {
3146
'unordered-list-item': {
3247
count: 0,
@@ -45,6 +60,8 @@ const getBlocks = (
4560
function checkCounter(counter: Object): any {
4661
const myCounter = counter;
4762

63+
64+
// list types
4865
if (myCounter.count >= 0) {
4966
if (myCounter.count > 0) {
5067
myCounter.count = 0;
@@ -53,6 +70,7 @@ const getBlocks = (
5370
return null;
5471
}
5572

73+
// non list types
5674
if (myCounter['unordered-list-item'].count > 0 || myCounter['ordered-list-item'].count > 0) {
5775
myCounter['unordered-list-item'].count = 0;
5876
myCounter['ordered-list-item'].count = 0;
@@ -62,7 +80,7 @@ const getBlocks = (
6280
return null;
6381
}
6482

65-
return bodyData.blocks
83+
return contentState.blocks
6684
.map((item: Object): any => {
6785
const itemData = {
6886
key: item.key,
@@ -88,7 +106,7 @@ const getBlocks = (
88106
{viewBefore}
89107
<DraftJsText
90108
{...itemData}
91-
entityMap={bodyData.entityMap}
109+
entityMap={contentState.entityMap}
92110
customStyles={customStyles}
93111
navigate={navigate}
94112
/>
@@ -110,7 +128,7 @@ const getBlocks = (
110128
{viewBefore}
111129
<BlockQuote
112130
{...itemData}
113-
entityMap={bodyData.entityMap}
131+
entityMap={contentState.entityMap}
114132
customStyles={customStyles}
115133
navigate={navigate}
116134
/>
@@ -126,7 +144,7 @@ const getBlocks = (
126144
{viewBefore}
127145
<UnorderedListItem
128146
{...itemData}
129-
entityMap={bodyData.entityMap}
147+
entityMap={contentState.entityMap}
130148
customStyles={customStyles}
131149
navigate={navigate}
132150
/>
@@ -144,7 +162,7 @@ const getBlocks = (
144162
{...itemData}
145163
separator={orderedListSeparator}
146164
counter={counters[item.type].count}
147-
entityMap={bodyData.entityMap}
165+
entityMap={contentState.entityMap}
148166
customStyles={customStyles}
149167
navigate={navigate}
150168
/>

0 commit comments

Comments
 (0)