Skip to content

Commit 7a78b12

Browse files
author
Raphael Freitas
committed
Add support to entityMap for atomic handlers
1 parent 87ca02d commit 7a78b12

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

sample/__tests__/getBlocks.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,19 @@ describe('return specific component based on type', () => {
201201
expect(result[0].type).toBe('atomic');
202202
});
203203

204+
it('calls atomicHandler with correctly params', () => {
205+
const bodyData = { blocks: [{ type: 'atomic' }], entityMap: {} };
206+
const atomicHandler = jest.fn();
207+
getBlocks({ contentState: bodyData, atomicHandler });
208+
expect(atomicHandler.mock.calls[0].length).toBe(2);
209+
});
210+
211+
it('returns the item if do not have a atomicHandler', () => {
212+
const bodyData = { blocks: [{ type: 'atomic', test: 'ok' }], entityMap: {} };
213+
const result = getBlocks({ contentState: bodyData });
214+
expect(result[0].test).toBe('ok');
215+
});
216+
204217
it('atomicHandler function when type atomic between lists', () => {
205218
const bodyData = {
206219
blocks: [

src/getBlocks.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ import OrderedListItem from './components/OrderedListItem';
1616
import generateKey from './utils/generateKey';
1717

1818
type ParamsType = {
19-
contentState: Object,
19+
contentState: {
20+
blocks: ?Array<*>,
21+
entityMap: Object,
22+
},
2023
customStyles: Object,
2124
atomicHandler: Function,
2225
navigate?: Function,
@@ -29,25 +32,21 @@ export const ViewAfterList = (props: Object): React$Element<*> => (
2932
<View {...props} />
3033
);
3134

32-
const getBlocks = (params: ParamsType): ?Array<*> => {
35+
const getBlocks = (params: ParamsType): ?Array<React$Element<*>> => {
3336
const {
3437
contentState,
3538
customStyles,
3639
navigate,
3740
orderedListSeparator,
3841
customBlockHandler,
3942
depthMargin,
43+
atomicHandler,
4044
} = params;
41-
let { atomicHandler } = params;
4245

4346
if (!contentState.blocks) {
4447
return null;
4548
}
4649

47-
if (typeof atomicHandler === 'undefined') {
48-
atomicHandler = (item: Object): any => item;
49-
}
50-
5150
const counters = {
5251
'unordered-list-item': {
5352
count: 0,
@@ -59,7 +58,7 @@ const getBlocks = (params: ParamsType): ?Array<*> => {
5958
},
6059
};
6160

62-
const checkCounter = (counter: Object): any => {
61+
const checkCounter = (counter: Object): ?React$Element<*> => {
6362
const myCounter = counter;
6463

6564
// list types
@@ -92,7 +91,7 @@ const getBlocks = (params: ParamsType): ?Array<*> => {
9291
};
9392

9493
return contentState.blocks
95-
.map((item: Object): any => {
94+
.map((item: Object): React$Element<*> => {
9695
const itemData = {
9796
key: item.key,
9897
text: item.text,
@@ -128,17 +127,20 @@ const getBlocks = (params: ParamsType): ?Array<*> => {
128127
}
129128

130129
case 'atomic': {
131-
const viewBefore = checkCounter(counters);
132-
const atomic = atomicHandler(item);
133-
if (viewBefore) {
134-
return (
135-
<View key={generateKey()}>
136-
{viewBefore}
137-
{atomic}
138-
</View>
139-
);
130+
if (atomicHandler) {
131+
const viewBefore = checkCounter(counters);
132+
const atomic = atomicHandler(item, contentState.entityMap);
133+
if (viewBefore) {
134+
return (
135+
<View key={generateKey()}>
136+
{viewBefore}
137+
{atomic}
138+
</View>
139+
);
140+
}
141+
return atomic;
140142
}
141-
return atomicHandler(item);
143+
return item;
142144
}
143145

144146
case 'blockquote': {

0 commit comments

Comments
 (0)