Skip to content

Commit

Permalink
feat(Template): remove support for react element
Browse files Browse the repository at this point in the history
  • Loading branch information
iam4x committed Mar 24, 2017
1 parent ce41cde commit ca2ab44
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 19 deletions.
10 changes: 5 additions & 5 deletions src/components/Template.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react';
import hogan from 'hogan.js';

import curry from 'lodash/curry';
import cloneDeep from 'lodash/cloneDeep';
import mapValues from 'lodash/mapValues';

import hogan from 'hogan.js';

import isEqual from 'lodash/isEqual';

import {isReactElement} from '../lib/utils.js';

export class PureTemplate extends React.Component {
shouldComponentUpdate(nextProps) {
return !isEqual(this.props.data, nextProps.data) || this.props.templateKey !== nextProps.templateKey;
Expand All @@ -31,8 +31,8 @@ export class PureTemplate extends React.Component {
return null;
}

if (React.isValidElement(content)) {
return <div {...this.props.rootProps}>{content}</div>;
if (isReactElement(content)) {
throw new Error('Support for templates as React elements has been removed, please use react-instantsearch');
}

return <div {...this.props.rootProps} dangerouslySetInnerHTML={{__html: content}} />;
Expand Down
9 changes: 4 additions & 5 deletions src/components/__tests__/Template-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ describe('Template', () => {
expect(tree).toMatchSnapshot();
});

it('supports templates as functions returning a React element', () => {
it('throws an error when templates as functions returning a React element', () => {
const props = getProps({
templates: {test: templateData => <p>it also works with {templateData.type}</p>},
templates: {test: templateData => <p>it doesnt works with {templateData.type}</p>},
data: {type: 'functions'},
});
const tree = renderer.create(
expect(() => renderer.create(
<PureTemplate {...props} />
).toJSON();
expect(tree).toMatchSnapshot();
)).toThrow();
});

it('can configure compilation options', () => {
Expand Down
9 changes: 0 additions & 9 deletions src/components/__tests__/__snapshots__/Template-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,6 @@ exports[`Template without helpers can configure compilation options 1`] = `
/>
`;

exports[`Template without helpers supports templates as functions returning a React element 1`] = `
<div>
<p>
it also works with
functions
</p>
</div>
`;

exports[`Template without helpers supports templates as functions returning a string 1`] = `
<div
dangerouslySetInnerHTML={
Expand Down
7 changes: 7 additions & 0 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export {
escapeRefinement,
unescapeRefinement,
checkRendering,
isReactElement,
};

/**
Expand Down Expand Up @@ -264,3 +265,9 @@ function checkRendering(rendering, usage) {
throw new Error(usage);
}
}

const REACT_ELEMENT_TYPE = typeof Symbol === 'function' && Symbol.for && Symbol.for('react.element') || 0xeac7;

function isReactElement(object) {
return typeof object === 'object' && object !== null && object.$$typeof === REACT_ELEMENT_TYPE;
}

0 comments on commit ca2ab44

Please sign in to comment.