Extracting type mapping into shared module

This commit is contained in:
Jeff Avallone 2019-01-27 08:50:03 -05:00
parent f16a51abcb
commit a118519c3a
3 changed files with 10 additions and 15 deletions

View File

@ -1,16 +1,10 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import SVG from 'rendering/SVG'; import nodeTypes from 'rendering/types';
import Text from 'rendering/Text';
import style from './style.module.css'; import style from './style.module.css';
const nodeTypes = {
SVG,
Text
};
const render = (data, extraProps) => { const render = (data, extraProps) => {
if (typeof data === 'string') { if (typeof data === 'string') {
return data; return data;
@ -21,7 +15,7 @@ const render = (data, extraProps) => {
(data, key) => render(data, { key })); (data, key) => render(data, { key }));
return React.createElement( return React.createElement(
nodeTypes[type] || type, nodeTypes[type] ? nodeTypes[type].default : type,
{ ...extraProps, ...props }, { ...extraProps, ...props },
children.length === 1 ? children[0] : children); children.length === 1 ? children[0] : children);
}; };

View File

@ -1,13 +1,7 @@
import React from 'react'; import React from 'react';
import ReactDOM from 'react-dom'; import ReactDOM from 'react-dom';
import * as SVG from 'rendering/SVG'; import nodeTypes from 'rendering/types';
import * as Text from 'rendering/Text';
const nodeTypes = {
SVG,
Text
};
const layout = data => { const layout = data => {
if (typeof data == 'string') { if (typeof data == 'string') {

7
src/rendering/types.js Normal file
View File

@ -0,0 +1,7 @@
import * as SVG from 'rendering/SVG';
import * as Text from 'rendering/Text';
export default {
SVG,
Text
};