|
1 | | -import parse, { HTMLReactParserOptions, domToReact, htmlToDOM } from 'html-react-parser'; |
| 1 | +import parse, { |
| 2 | + HTMLReactParserOptions, |
| 3 | + domToReact, |
| 4 | + htmlToDOM |
| 5 | +} from 'html-react-parser'; |
2 | 6 | import * as React from 'react'; |
3 | 7 |
|
4 | | -/* $ExpectType ReactElement | ReactElement[] */ |
5 | | -parse('<div>text</div>'); |
| 8 | +// $ExpectError |
| 9 | +parse(); |
6 | 10 |
|
7 | | -// `options.replace` |
| 11 | +// $ExpectType Element | Element[] |
| 12 | +parse(''); |
8 | 13 |
|
9 | | -// Return React.createElement from `replace` |
10 | | -parse('<p id="replace">text</p>', { |
| 14 | +// $ExpectType Element | Element[] |
| 15 | +parse('string'); |
| 16 | + |
| 17 | +// $ExpectType Element | Element[] |
| 18 | +parse('<p>text</p>'); |
| 19 | + |
| 20 | +// $ExpectType Element | Element[] |
| 21 | +parse('<li>1</li><li>2</li>'); |
| 22 | + |
| 23 | +// $ExpectType Element | Element[] |
| 24 | +parse('<br id="replace">', { |
11 | 25 | replace: domNode => { |
12 | 26 | if (domNode.attribs && domNode.attribs.id === 'replace') { |
13 | | - return React.createElement('span', {}, 'replaced'); |
| 27 | + return <span>replaced</span>; |
14 | 28 | } |
15 | 29 | } |
16 | 30 | }); |
17 | 31 |
|
18 | | -// Return ReactElement |
19 | | -const options: HTMLReactParserOptions = { |
| 32 | +// $ExpectType Element | Element[] |
| 33 | +parse('<br id="remove">', { |
20 | 34 | replace({ attribs }) { |
21 | | - return attribs && attribs.id === 'remove' && <React.Fragment />; |
| 35 | + return attribs && attribs.id === 'remove' && <></>; |
22 | 36 | } |
23 | | -}; |
| 37 | +}); |
24 | 38 |
|
25 | | -parse('<p><br id="remove"></p>', options); |
| 39 | +let options: HTMLReactParserOptions; |
26 | 40 |
|
27 | | -// Return domhandler node |
28 | | -parse('<a id="header" href="#">Heading</a>', { |
| 41 | +options = { |
29 | 42 | replace: node => { |
30 | 43 | if (node.attribs && node.attribs.id === 'header') { |
31 | | - return { |
32 | | - type: 'h1', |
33 | | - props: { children: 'Heading' } |
34 | | - }; |
| 44 | + return; |
35 | 45 | } |
36 | 46 | } |
| 47 | +}; |
| 48 | + |
| 49 | +// $ExpectType Element | Element[] |
| 50 | +parse('<a id="header" href="#">Heading</a>', options); |
| 51 | + |
| 52 | +// $ExpectType Element | Element[] |
| 53 | +parse('<hr>', { |
| 54 | + library: { |
| 55 | + cloneElement: (element, props, children) => |
| 56 | + React.cloneElement(element, props, children), |
| 57 | + createElement: (type, props, children) => |
| 58 | + React.createElement(type, props, children), |
| 59 | + isValidElement: element => React.isValidElement(element) |
| 60 | + } |
37 | 61 | }); |
38 | 62 |
|
39 | 63 | // $ExpectType DomElement[] |
40 | | -const dom = htmlToDOM('<div>text</div>'); |
| 64 | +const domNodes = htmlToDOM('<div>text</div>'); |
41 | 65 |
|
42 | | -/* $ExpectType ReactElement | ReactElement[] */ |
43 | | -domToReact(dom); |
| 66 | +// $ExpectType Element | Element[] |
| 67 | +domToReact(domNodes); |
0 commit comments