Skip to content

Commit 6c6b8ec

Browse files
author
David Bailey
committed
refactor(dom-to-react): change how library option works
- Update README - Add typings - Add a new test for Preact - Restore old peerDependencies
1 parent d09eb38 commit 6c6b8ec

File tree

5 files changed

+34
-23
lines changed

5 files changed

+34
-23
lines changed

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,31 @@ parse('<p><br id="remove"></p>', {
196196

197197
### library
198198

199-
The `library` option allows you to specify which component library is used to create elements (currently either React or Preact). React is used by default if this option is not specified, or if the given option is unknown.
199+
The `library` option allows you to specify which component library is used to create elements. React is used by default if this option is not specified.
200200

201201
Here's an example showing how to use Preact:
202202

203203
```js
204204
parse('<br>', {
205-
library: 'preact'
205+
library: require('preact')
206+
});
207+
```
208+
209+
Or, using a custom library:
210+
211+
```js
212+
parse('<br>', {
213+
library: {
214+
cloneElement: () => {
215+
/* ... */
216+
},
217+
createElement: () => {
218+
/* ... */
219+
},
220+
isValidElement: () => {
221+
/* ... */
222+
}
223+
}
206224
});
207225
```
208226

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import htmlToDOM from 'html-dom-parser';
88
export interface HTMLReactParserOptions {
99
// TODO: Replace `object` by type for objects like `{ type: 'h1', props: { children: 'Heading' } }`
1010
replace(domNode: DomElement): React.ReactElement | object | undefined | false;
11+
library?: object;
1112
}
1213

1314
/**

lib/dom-to-react.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,6 @@
11
var attributesToProps = require('./attributes-to-props');
22
var utilities = require('./utilities');
33

4-
/**
5-
* Requires the appropriate component library (React or Preact).
6-
*
7-
* @param {Object} [options] - The additional options.
8-
* @return {Object}
9-
*/
10-
function requireReact(options) {
11-
options = options || {};
12-
13-
if (options.library === 'preact') {
14-
return require('preact');
15-
}
16-
17-
return require('react');
18-
}
19-
204
/**
215
* Converts DOM nodes to React elements.
226
*
@@ -26,13 +10,13 @@ function requireReact(options) {
2610
* @return {ReactElement|Array}
2711
*/
2812
function domToReact(nodes, options) {
29-
var React = requireReact(options);
13+
options = options || {};
3014

15+
var React = options.library || require('react');
3116
var cloneElement = React.cloneElement;
3217
var createElement = React.createElement;
3318
var isValidElement = React.isValidElement;
3419

35-
options = options || {};
3620
var result = [];
3721
var node;
3822
var hasReplace = typeof options.replace === 'function';

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"lint-staged": "^8.2.1",
5555
"mocha": "^6.1.4",
5656
"nyc": "^14.1.1",
57+
"preact": "10.0.4",
5758
"prettier": "^1.18.2",
5859
"react": "^16",
5960
"react-dom": "^16",
@@ -64,9 +65,8 @@
6465
"rollup-plugin-uglify": "^6.0.2",
6566
"standard-version": "^6.0.1"
6667
},
67-
"optionalPeerDependencies": {
68-
"react": "^0.14 || ^15 || ^16",
69-
"preact": "^8 || ^10"
68+
"peerDependencies": {
69+
"react": "^0.14 || ^15 || ^16"
7070
},
7171
"files": [
7272
"/dist",

test/dom-to-react.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const assert = require('assert');
22
const React = require('react');
3+
const Preact = require('preact');
34
const htmlToDOM = require('html-dom-parser');
45
const domToReact = require('../lib/dom-to-react');
56
const { data, render } = require('./helpers/');
@@ -159,6 +160,13 @@ describe('dom-to-react parser', () => {
159160
);
160161
});
161162

163+
it('handles using a custom component library', () => {
164+
const html = data.html.single;
165+
const preactElement = domToReact(htmlToDOM(html), { library: Preact });
166+
167+
assert.deepEqual(preactElement, Preact.createElement('p', {}, 'foo'));
168+
});
169+
162170
it('does not modify keys for replacement if it has one', () => {
163171
const html = [data.html.single, data.html.customElement].join('');
164172

0 commit comments

Comments
 (0)