Skip to content

Commit

Permalink
fix: onMissingComponent prop to accept boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
theisel committed Sep 26, 2022
1 parent 0b86d85 commit 29815b8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
10 changes: 10 additions & 0 deletions .changeset/tall-kings-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"astro-portabletext": patch
---

Fixes PortableText `onMissingComponent` prop to accept boolean

```diff
- onMissingComponent?: MissingComponentHandler | false;
+ onMissingComponent?: MissingComponentHandler | boolean;
```
10 changes: 8 additions & 2 deletions astro-portabletext/components/PortableText.astro
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const {
value,
components: componentOverrides = {},
listNestingMode = LIST_NEST_MODE_HTML,
onMissingComponent = printWarning,
onMissingComponent = true,
class: astroClass,
} = Astro.props;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion astro-portabletext/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down

0 comments on commit 29815b8

Please sign in to comment.