File tree Expand file tree Collapse file tree 3 files changed +26
-12
lines changed Expand file tree Collapse file tree 3 files changed +26
-12
lines changed Original file line number Diff line number Diff line change 66 BlockMapType ,
77 MapPageUrl ,
88 MapImageUrl ,
9- BlockValueTypeKeys ,
10- CustomComponent
9+ CustomComponents ,
10+ BlockValueProp
1111} from "./types" ;
1212import Asset from "./components/asset" ;
1313import Code from "./components/code" ;
@@ -64,7 +64,7 @@ interface Block {
6464
6565 fullPage ?: boolean ;
6666 hideHeader ?: boolean ;
67- customComponents ?: Record < BlockValueTypeKeys , CustomComponent > ;
67+ customComponents ?: CustomComponents ;
6868}
6969
7070export const Block : React . FC < Block > = props => {
@@ -83,10 +83,15 @@ export const Block: React.FC<Block> = props => {
8383
8484 // render a custom component first if passed.
8585 if ( customComponents && customComponents [ blockValue ?. type ] ) {
86- const CustomComponent = customComponents [ blockValue ?. type ] as any ;
87- return < CustomComponent blockValue = { blockValue } /> ;
86+ const CustomComponent = customComponents [ blockValue ?. type ] ! ;
87+ return (
88+ < CustomComponent
89+ blockValue = { blockValue as BlockValueProp < typeof blockValue . type > }
90+ >
91+ { children }
92+ </ CustomComponent >
93+ ) ;
8894 }
89-
9095 switch ( blockValue ?. type ) {
9196 case "page" :
9297 if ( level === 0 ) {
Original file line number Diff line number Diff line change 33 BlockMapType ,
44 MapPageUrl ,
55 MapImageUrl ,
6- CustomComponent
6+ CustomComponents
77} from "./types" ;
88import { Block } from "./block" ;
99import { defaultMapImageUrl , defaultMapPageUrl } from "./utils" ;
@@ -17,7 +17,7 @@ export interface NotionRendererProps {
1717
1818 currentId ?: string ;
1919 level ?: number ;
20- customComponents ?: Record < string , CustomComponent > ;
20+ customComponents ?: CustomComponents ;
2121}
2222
2323export const NotionRenderer : React . FC < NotionRendererProps > = ( {
Original file line number Diff line number Diff line change @@ -294,7 +294,7 @@ export type BlockValueType =
294294 | CollectionValueType
295295 | TweetType ;
296296
297- export type BlockValueTypeKeys = Pick < BlockValueType , "type" > [ "type" ] ;
297+ export type BlockValueTypeKeys = BlockValueType [ "type" ] ;
298298
299299export interface BlockType {
300300 role : string ;
@@ -342,8 +342,17 @@ export interface LoadPageChunkData {
342342export type MapPageUrl = ( pageId : string ) => string ;
343343export type MapImageUrl = ( image : string ) => string ;
344344
345- export interface CustomComponentProps {
346- blockValue : BlockValueType ;
345+ export type BlockValueProp < T > = Extract < BlockValueType , { type : T } > ;
346+
347+ export interface CustomComponentProps < T extends BlockValueTypeKeys > {
348+ blockValue : T extends BlockValueType
349+ ? Extract < BlockValueType , { type : T } >
350+ : BaseValueType ;
347351}
348352
349- export type CustomComponent = FC < CustomComponentProps > ;
353+ export type CustomComponent < T extends BlockValueTypeKeys > = FC <
354+ CustomComponentProps < T >
355+ > ;
356+ export type CustomComponents = {
357+ [ K in BlockValueTypeKeys ] ?: CustomComponent < K > ;
358+ } ;
You can’t perform that action at this time.
0 commit comments