File tree 3 files changed +26
-12
lines changed
3 files changed +26
-12
lines changed Original file line number Diff line number Diff line change 6
6
BlockMapType ,
7
7
MapPageUrl ,
8
8
MapImageUrl ,
9
- BlockValueTypeKeys ,
10
- CustomComponent
9
+ CustomComponents ,
10
+ BlockValueProp
11
11
} from "./types" ;
12
12
import Asset from "./components/asset" ;
13
13
import Code from "./components/code" ;
@@ -64,7 +64,7 @@ interface Block {
64
64
65
65
fullPage ?: boolean ;
66
66
hideHeader ?: boolean ;
67
- customComponents ?: Record < BlockValueTypeKeys , CustomComponent > ;
67
+ customComponents ?: CustomComponents ;
68
68
}
69
69
70
70
export const Block : React . FC < Block > = props => {
@@ -83,10 +83,15 @@ export const Block: React.FC<Block> = props => {
83
83
84
84
// render a custom component first if passed.
85
85
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
+ ) ;
88
94
}
89
-
90
95
switch ( blockValue ?. type ) {
91
96
case "page" :
92
97
if ( level === 0 ) {
Original file line number Diff line number Diff line change 3
3
BlockMapType ,
4
4
MapPageUrl ,
5
5
MapImageUrl ,
6
- CustomComponent
6
+ CustomComponents
7
7
} from "./types" ;
8
8
import { Block } from "./block" ;
9
9
import { defaultMapImageUrl , defaultMapPageUrl } from "./utils" ;
@@ -17,7 +17,7 @@ export interface NotionRendererProps {
17
17
18
18
currentId ?: string ;
19
19
level ?: number ;
20
- customComponents ?: Record < string , CustomComponent > ;
20
+ customComponents ?: CustomComponents ;
21
21
}
22
22
23
23
export const NotionRenderer : React . FC < NotionRendererProps > = ( {
Original file line number Diff line number Diff line change @@ -294,7 +294,7 @@ export type BlockValueType =
294
294
| CollectionValueType
295
295
| TweetType ;
296
296
297
- export type BlockValueTypeKeys = Pick < BlockValueType , "type" > [ "type" ] ;
297
+ export type BlockValueTypeKeys = BlockValueType [ "type" ] ;
298
298
299
299
export interface BlockType {
300
300
role : string ;
@@ -342,8 +342,17 @@ export interface LoadPageChunkData {
342
342
export type MapPageUrl = ( pageId : string ) => string ;
343
343
export type MapImageUrl = ( image : string ) => string ;
344
344
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 ;
347
351
}
348
352
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