From 29815b880e39bdd5db66ff8f0206034afc93dbed Mon Sep 17 00:00:00 2001 From: Tom Theisel Date: Mon, 26 Sep 2022 09:50:46 +0100 Subject: [PATCH] fix: `onMissingComponent` prop to accept boolean --- .changeset/tall-kings-rush.md | 10 ++++++++++ astro-portabletext/components/PortableText.astro | 10 ++++++++-- astro-portabletext/src/types.ts | 2 +- 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 .changeset/tall-kings-rush.md diff --git a/.changeset/tall-kings-rush.md b/.changeset/tall-kings-rush.md new file mode 100644 index 0000000..152c65a --- /dev/null +++ b/.changeset/tall-kings-rush.md @@ -0,0 +1,10 @@ +--- +"astro-portabletext": patch +--- + +Fixes PortableText `onMissingComponent` prop to accept boolean + +```diff +- onMissingComponent?: MissingComponentHandler | false; ++ onMissingComponent?: MissingComponentHandler | boolean; +``` diff --git a/astro-portabletext/components/PortableText.astro b/astro-portabletext/components/PortableText.astro index 5082fd9..971bac6 100644 --- a/astro-portabletext/components/PortableText.astro +++ b/astro-portabletext/components/PortableText.astro @@ -55,7 +55,7 @@ const { value, components: componentOverrides = {}, listNestingMode = LIST_NEST_MODE_HTML, - onMissingComponent = printWarning, + onMissingComponent = true, class: astroClass, } = Astro.props; @@ -99,7 +99,13 @@ const components = mergeComponents( ) as PortableTextComponents; const noop = () => {}; -const missingComponentHandler = onMissingComponent || noop; + +const missingComponentHandler = ((handler: unknown) => { + if (typeof handler === "function") { + return handler; + } + return !handler ? noop : printWarning; +})(onMissingComponent); const emitter = mitt<{ unknownType: string; diff --git a/astro-portabletext/src/types.ts b/astro-portabletext/src/types.ts index d96b75d..2b4ce3d 100644 --- a/astro-portabletext/src/types.ts +++ b/astro-portabletext/src/types.ts @@ -43,7 +43,7 @@ export interface PortableTextProps< * Prints a warning message to the console by default. * Pass `false` to disable. */ - onMissingComponent?: MissingComponentHandler | false; + onMissingComponent?: MissingComponentHandler | boolean; /** * `html` or `direct`