Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add element override for text nodes #42

Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@
"react": ">= 16.0.0"
},
"dependencies": {
"@contentful/rich-text-types": "^12.1.2"
"@contentful/rich-text-types": "^15.12.0"
}
}
25 changes: 25 additions & 0 deletions src/components/RichText/RichText.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { create } from 'react-test-renderer';
import PropTypes from 'prop-types';

import {
createDocument,
Expand Down Expand Up @@ -27,4 +28,28 @@ describe('RichText', () => {
expect(actual).toMatchSnapshot();
});
});

describe('styles', () => {
it('should allow to override normal text', () => {
const Text = ({ children, ...props }) => (
<span {...props}>{children}</span>
);
Text.propTypes = {
children: PropTypes.node
};

const richText = createDocument([paragraph]);
const actual = create(
<RichText
richText={richText}
overrides={{
text: {
component: Text
}
}}
/>
);
expect(actual).toMatchSnapshot();
});
});
});
24 changes: 24 additions & 0 deletions src/components/RichText/__snapshots__/RichText.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`RichText styles should allow to override normal text 1`] = `
<p>
<span>
This is normal text.
</span>
<strong>
This is bold text.
</strong>
<em>
This is italic text.
</em>
<u>
This is underlined text.
</u>
<a
href="https://acme.com"
>
<span>
This is a hyperlink.
</span>
</a>
</p>
`;

exports[`RichText styles should render with default styles 1`] = `
Array [
<h1>
Expand Down
12 changes: 11 additions & 1 deletion src/rich-text-to-jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,17 @@ export function textNodeToJsx(node, options, key) {
const { overrides, createElement } = options;

if (isEmpty(marks)) {
return value;
const element = getElement('text', overrides);

if (!element) {
return value;
}

const props = getProps('text', overrides, {
...data,
key
});
return createElement(element, props, value);
}

return marks.reduce((children, mark, markKey) => {
Expand Down
Loading