Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ jspm_packages
.node_repl_history

sample/react-native-draftjs-render/*

# VSCode
jsconfig.json

12 changes: 12 additions & 0 deletions sample/__tests__/getBlocks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,16 @@ describe('return specific component based on type', () => {
const result = getBlocks({ contentState: bodyData });
expect(result).toBe(null);
});

it('should use the optional customBlockHandler when handling custom block types', () => {
const bodyData = { blocks: [{ type: 'my-own-type' }] };
const myCustomComponent = jest.fn();
const customBlockHandler = jest.fn((item, params) => myCustomComponent);
const result = getBlocks({ contentState: bodyData, customBlockHandler });
expect(customBlockHandler.mock.calls.length).toBe(1);
expect(customBlockHandler.mock.calls[0][0].type).toBe('my-own-type');
expect(customBlockHandler.mock.calls[0][1].contentState).toBe(bodyData);
expect(customBlockHandler.mock.calls[0][1].customBlockHandler).toBe(customBlockHandler);
expect(result[0]).toBe(myCustomComponent);
});
});
4 changes: 3 additions & 1 deletion src/getBlocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type ParamsType = {
atomicHandler: Function,
navigate?: Function,
orderedListSeparator?: string,
customBlockHandler?: (Object, ParamsType) => any
};

const getBlocks = (params: ParamsType): ?Array<*> => {
Expand All @@ -31,6 +32,7 @@ const getBlocks = (params: ParamsType): ?Array<*> => {
customStyles,
navigate,
orderedListSeparator,
customBlockHandler,
} = params;
let { atomicHandler } = params;

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

default: {
const viewBefore = checkCounter(counters);
return (
return customBlockHandler ? customBlockHandler(item, params) : (
<View key={generateKey()}>
{viewBefore}
</View>
Expand Down