Skip to content
This repository has been archived by the owner on Mar 27, 2018. It is now read-only.

Commit

Permalink
feat(mapper): add Paragraph and Paragraphs components as helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Wensink committed Sep 14, 2017
1 parent bc3b6bf commit 4d5cb54
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
33 changes: 33 additions & 0 deletions src/Paragraph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react';
import PropTypes from 'prop-types';
import site from './site';

const Paragraph = ({ mapper, uuid, page, index }) => {
const paragraph = site.getData(uuid);

if(!paragraph) {
return null;
}

const Component = typeof mapper === 'function' ? mapper(paragraph.type.target_id) : mapper[paragraph.type.target_id];
return (
<Component
type={paragraph.type.target_id}
page={page}
paragraph={paragraph}
index={index}
/>
);
};

Paragraph.propTypes = {
mapper: PropTypes.oneOfType([
PropTypes.shape(),
PropTypes.func,
]).isRequired,
uuid: PropTypes.string.isRequired,
page: PropTypes.shape({}),
index: PropTypes.number.isRequired,
};

export default Paragraph;
46 changes: 46 additions & 0 deletions src/Paragraphs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from 'react';
import PropTypes from 'prop-types';
import Paragraph from './Paragraph';

const Paragraphs = ({ mapper, paragraphs, page, Wrapper }) =>
paragraphs.map((uuid, index) => {
const paragraph = (
<Paragraph
key={uuid.target_uuid}
uuid={uuid.target_uuid}
page={page}
index={index}
/>
);
if(Wrapper) {
return (
<Wrapper>
{paragraph}
</Wrapper>
);
}
return paragraph;
});

Paragraphs.propTypes = {
mapper: PropTypes.oneOfType([
PropTypes.shape(),
PropTypes.func,
]).isRequired,
paragraphs: PropTypes.arrayOf(PropTypes.shape()),
page: PropTypes.shape({
uuid: PropTypes.shape({
target_uuid: PropTypes.string,
}).isRequired,
}),
Wrapper: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.node,
]),
};

Paragraphs.defaultProps = {
Wrapper: false,
};

export default Paragraphs;
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import DrupalPage from './DrupalPage';
import site from './site';
import Paragraph from './Paragraph';
import Paragraphs from './Paragraphs';

export {
DrupalPage,
site
site,
Paragraph,
Paragraphs,
};

0 comments on commit 4d5cb54

Please sign in to comment.