Skip to content

Commit

Permalink
fix(types): add back object to replace option return type
Browse files Browse the repository at this point in the history
Fixes #1126

Maintains backward compatibility for invalid return type
  • Loading branch information
remarkablemark committed Oct 29, 2023
1 parent 6f9ae44 commit 88eea66
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
21 changes: 13 additions & 8 deletions __tests__/dom-to-react.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ describe('library option', () => {
});

describe('replace option', () => {
it.each([undefined, null, 0, 1, true, false, {}])(
'does not replace for invalid return value %p',
(value) => {
const reactElement = domToReact(htmlToDOM('<br>'), {
replace: () => value,
}) as JSX.Element;
expect(reactElement).toEqual(<br />);
},
);

it("does not set key if there's a single node", () => {
const reactElement = domToReact(htmlToDOM(html.single), {
replace: () => <div />,
Expand Down Expand Up @@ -213,17 +223,12 @@ describe('replace option', () => {
const options: HTMLReactParserOptions = {
replace(domNode) {
if (domNode instanceof Element) {
return <>{domToReact(domNode.children as DOMNode[], options)}</>;
return domToReact(domNode.children as DOMNode[], options);
}
},
};

const reactElement = domToReact(
htmlToDOM(html.single),
options,
) as JSX.Element;

expect(reactElement).toBeInstanceOf(Object);
const reactElement = domToReact(htmlToDOM('<div>test</div>'), options);
expect(reactElement).toEqual(<div>test</div>);
});
});

Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export interface HTMLReactParserOptions {
[key: string]: any;
};

replace?: (domNode: DOMNode) => JSX.Element | string | null | boolean | void;
replace?: (
domNode: DOMNode,
) => JSX.Element | string | null | boolean | object | void;

transform?: (
reactNode: ReactNode,
Expand Down

0 comments on commit 88eea66

Please sign in to comment.