Extracted renderToken function from class. Do not render additional <div>.

This commit is contained in:
Andrey Okonetchnikov 2016-09-22 17:44:25 +02:00
parent 2c483220b5
commit 7fe1a6f8b6
3 changed files with 106 additions and 111 deletions

View File

@ -21,9 +21,7 @@ const defaultRenderers = {
'unstyled': null, 'unstyled': null,
}; };
export default class MarkitupReactRenderer extends React.Component { function renderToken(token) {
renderToken = (token) => {
const { type, data, text, tokens } = token; const { type, data, text, tokens } = token;
const element = defaultRenderers[type]; const element = defaultRenderers[type];
@ -31,7 +29,7 @@ export default class MarkitupReactRenderer extends React.Component {
if (typeof element !== 'undefined') { if (typeof element !== 'undefined') {
let children = null; let children = null;
if (Array.isArray(tokens) && tokens.length) { if (Array.isArray(tokens) && tokens.length) {
children = tokens.map(this.renderToken); children = tokens.map(renderToken);
} else if (type === 'text') { } else if (type === 'text') {
children = text; children = text;
} }
@ -44,7 +42,9 @@ export default class MarkitupReactRenderer extends React.Component {
} }
} }
return null; return null;
} }
export default class MarkitupReactRenderer extends React.Component {
render() { render() {
const { value, syntax } = this.props; const { value, syntax } = this.props;
@ -57,9 +57,7 @@ export default class MarkitupReactRenderer extends React.Component {
const json = JSONUtils.encode(content); const json = JSONUtils.encode(content);
// console.log(JSON.stringify(json, null, 2)); // console.log(JSON.stringify(json, null, 2));
return ( return renderToken(json.token);
<div>{this.renderToken(json.token)}</div>
);
} }
} }

View File

@ -44,8 +44,7 @@ Text with **bold** & _em_ elements
}); });
it('should support custom syntax', () => { it('should support custom syntax', () => {
const value = ` const value = '';
`;
const component = renderer.create( const component = renderer.create(
<MarkitupReactRenderer <MarkitupReactRenderer
value={value} value={value}

View File

@ -1,6 +1,5 @@
exports[`MarkitupReactRenderer should render HTML 1`] = ` exports[`MarkitupReactRenderer should render HTML 1`] = `
<div> <article>
<article>
<p> <p>
Paragraph with Paragraph with
<em> <em>
@ -8,13 +7,11 @@ exports[`MarkitupReactRenderer should render HTML 1`] = `
</em> </em>
element element
</p> </p>
</article> </article>
</div>
`; `;
exports[`MarkitupReactRenderer should render markdown 1`] = ` exports[`MarkitupReactRenderer should render markdown 1`] = `
<div> <article>
<article>
<h1 <h1
id={null}> id={null}>
H1 H1
@ -85,6 +82,7 @@ exports[`MarkitupReactRenderer should render markdown 1`] = `
id={null}> id={null}>
H6 H6
</h6> </h6>
</article> </article>
</div>
`; `;
exports[`MarkitupReactRenderer should support custom syntax 1`] = `<article />`;