Skip to content

Commit

Permalink
Merge pull request #971 from robbieaverill/children-reactnode
Browse files Browse the repository at this point in the history
Theme: Children interface is now ReactNode, allows `guard()` and `render()` to accept JSX
  • Loading branch information
sserrata authored Sep 24, 2024
2 parents 87958a4 + 2fcfe32 commit 296475b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 4 additions & 0 deletions packages/docusaurus-plugin-openapi-docs/src/markdown/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
* LICENSE file in the root directory of this source tree.
* ========================================================================== */

/**
* Children in the plugin does not accept DOM elements, when compared with Children in the theme.
* It is designed for rendering HTML a strings.
*/
export type Children = string | undefined | (string | string[] | undefined)[];

export type Props = Record<string, any> & { children?: Children };
Expand Down
13 changes: 7 additions & 6 deletions packages/docusaurus-theme-openapi-docs/src/markdown/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

import { ReactNode } from "react";

export type Children = ReactNode | string | undefined | (string | undefined)[];
/** @deprecated use ReactNode from React instead */
export type Children = ReactNode;

export type Props = Record<string, any> & { children?: Children };
export type Props = Record<string, any> & { children?: ReactNode };

export function create(tag: string, props: Props): string {
const { children, ...rest } = props;
Expand All @@ -24,20 +25,20 @@ export function create(tag: string, props: Props): string {

export function guard<T>(
value: T | undefined | string,
cb: (value: T) => Children
): string {
cb: (value: T) => ReactNode
) {
if (!!value || value === 0) {
const children = cb(value as T);
return render(children);
}
return "";
}

export function render(children: Children): string {
export function render(children: ReactNode) {
if (Array.isArray(children)) {
return children.filter((c) => c !== undefined).join("");
}
return (children as string) ?? "";
return children ?? "";
}

export function toString(value: any): string | undefined {
Expand Down

0 comments on commit 296475b

Please sign in to comment.