Skip to content

fix(types): add back object to replace option return type #1129

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

Merged
merged 1 commit into from
Oct 29, 2023
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
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