Skip to content
This repository was archived by the owner on Jun 19, 2018. It is now read-only.

Commit f6f27a6

Browse files
committed
style: switch DOM props to spread
- Replace `wrapperProps` with an object spread that automatically removes the custom props from the remaining object - Export component from root
1 parent 5e26edf commit f6f27a6

File tree

4 files changed

+31
-16
lines changed

4 files changed

+31
-16
lines changed

src/Content/RawHtml.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
import { createElement, Component } from 'react';
2-
import { object, string } from 'prop-types';
2+
import { string } from 'prop-types';
33

44
export default class RawHtml extends Component {
55
static propTypes = {
66
sanitizedRawHtml: string.isRequired,
7-
wrapperTag: string,
8-
wrapperProps: object
7+
wrapperTag: string
98
};
109

1110
static defaultProps = {
1211
wrapperTag: 'span',
13-
wrapperProps: {
14-
className: 'peregrine-raw-html'
15-
}
12+
className: 'peregrine-raw-html'
1613
};
1714

1815
render() {
19-
// JSX tags must be in PascalCase
20-
const WrapperTag = this.props.wrapperTag;
16+
const {
17+
wrapperTag: WrapperTag,
18+
sanitizedRawHtml,
19+
...domProps
20+
} = this.props;
2121

2222
return (
2323
<WrapperTag
24-
{...this.props.wrapperProps}
24+
{...domProps}
2525
dangerouslySetInnerHTML={{
26-
__html: this.props.sanitizedRawHtml
26+
__html: sanitizedRawHtml
2727
}}
2828
/>
2929
);

src/Content/__docs__/RawHtml.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class ItemDescription extends React.Component {
3434
| ------------------ | :-------: | --------------------------------------------------: |
3535
| `sanitizedRawHtml` | ✅ | A string of raw HTML to display. Ignored if `children` are also passed and are not null.
3636
| `wrapperTag` | | Optional HTML tag to use as a custom wrapper element. Default `span.`.
37-
| `wrapperProps` | | Optional object to be passed as props to the `wrapperTag`. Default `{ className: "peregrine-raw-html" }`.
37+
38+
Additional DOM properties set on `RawHTML` will be passed to the wrapper tag.
3839

3940
## Notes
4041

@@ -52,14 +53,14 @@ class ItemDescription extends React.Component {
5253
</span>
5354
```
5455

55-
Use the `wrapperTag` string prop and `wrapperProps` object prop to customize
56-
the parent tag:
56+
Use the `wrapperTag` string prop additional DOM props to customize
57+
the parent tag:
5758

5859
```html
5960
<RawHtml
6061
sanitizedRawHtml="<h2>Header</h2><p>body text</p>"
6162
wrapperTag="section"
62-
wrapperProps={{ className: "product-description loading" }}
63+
className="product-description loading"
6364
/>
6465

6566
<!-- emits this HTML: -->

src/Content/__stories__/RawHtml.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ storiesOf('RawHtml', module)
1313
<RawHtml
1414
sanitizedRawHtml="<p><em>HTML fallback content</em></p><img src='https://placebeard.it/320/240'/>"
1515
wrapperTag="article"
16-
wrapperProps={{ style: { background: 'whitesmoke' } }}
16+
style={{ background: 'whitesmoke' }}
1717
/>
1818
));

src/Content/__tests__/RawHtml.test.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test('Renders raw HTML in a wrapper element', () => {
1414
);
1515
});
1616

17-
test('Takes DOM properties for a custom wrapper element', () => {
17+
test('Takes a prop for a custom wrapper element', () => {
1818
const wrapper = mount(
1919
<RawHtml
2020
sanitizedRawHtml="<h1 id='o-no'>Raw!!!</h1>"
@@ -25,3 +25,17 @@ test('Takes DOM properties for a custom wrapper element', () => {
2525
'<article class="peregrine-raw-html"><h1 id="o-no">Raw!!!</h1></article>'
2626
);
2727
});
28+
29+
test('Takes DOM properties for the custom wrapper element', () => {
30+
const wrapper = mount(
31+
<RawHtml
32+
sanitizedRawHtml="<h1 id='o-no'>Raw!!!</h1>"
33+
wrapperTag="article"
34+
className="custom-class-name"
35+
style={{ background: 'blue' }}
36+
/>
37+
);
38+
expect(wrapper.html()).toEqual(
39+
'<article class="custom-class-name" style="background: blue;"><h1 id="o-no">Raw!!!</h1></article>'
40+
);
41+
});

0 commit comments

Comments
 (0)