Skip to content

Commit 762f768

Browse files
goviraphaelpor
authored andcommitted
Adding a custom block handler (#23)
* added custom blocks handler * simplify custom block handling * added a test to cover the custom handler path
1 parent 212f0a6 commit 762f768

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,7 @@ jspm_packages
4242
.node_repl_history
4343

4444
sample/react-native-draftjs-render/*
45+
46+
# VSCode
47+
jsconfig.json
48+

sample/__tests__/getBlocks.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,16 @@ describe('return specific component based on type', () => {
8282
const result = getBlocks({ contentState: bodyData });
8383
expect(result).toBe(null);
8484
});
85+
86+
it('should use the optional customBlockHandler when handling custom block types', () => {
87+
const bodyData = { blocks: [{ type: 'my-own-type' }] };
88+
const myCustomComponent = jest.fn();
89+
const customBlockHandler = jest.fn((item, params) => myCustomComponent);
90+
const result = getBlocks({ contentState: bodyData, customBlockHandler });
91+
expect(customBlockHandler.mock.calls.length).toBe(1);
92+
expect(customBlockHandler.mock.calls[0][0].type).toBe('my-own-type');
93+
expect(customBlockHandler.mock.calls[0][1].contentState).toBe(bodyData);
94+
expect(customBlockHandler.mock.calls[0][1].customBlockHandler).toBe(customBlockHandler);
95+
expect(result[0]).toBe(myCustomComponent);
96+
});
8597
});

src/getBlocks.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type ParamsType = {
2323
atomicHandler: Function,
2424
navigate?: Function,
2525
orderedListSeparator?: string,
26+
customBlockHandler?: (Object, ParamsType) => any
2627
};
2728

2829
const getBlocks = (params: ParamsType): ?Array<*> => {
@@ -31,6 +32,7 @@ const getBlocks = (params: ParamsType): ?Array<*> => {
3132
customStyles,
3233
navigate,
3334
orderedListSeparator,
35+
customBlockHandler,
3436
} = params;
3537
let { atomicHandler } = params;
3638

@@ -176,7 +178,7 @@ const getBlocks = (params: ParamsType): ?Array<*> => {
176178

177179
default: {
178180
const viewBefore = checkCounter(counters);
179-
return (
181+
return customBlockHandler ? customBlockHandler(item, params) : (
180182
<View key={generateKey()}>
181183
{viewBefore}
182184
</View>

0 commit comments

Comments
 (0)