File tree Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change 8
8
import type React from 'react'
9
9
import { useCallback , useEffect , useState } from 'react'
10
10
11
+ // this url is copied from: https://beta.reactjs.org/learn/passing-props-to-a-component
12
+ const avatar = 'https://i.imgur.com/1bX5QH6.jpg'
13
+
11
14
function aPlusB ( a : number , b : number ) {
12
15
return a + b
13
16
}
@@ -45,6 +48,7 @@ const example = {
45
48
fn : aPlusB ,
46
49
string_number : '1234' ,
47
50
timer : 0 ,
51
+ avatar,
48
52
date : new Date ( 'Tue Sep 13 2022 14:07:44 GMT-0500 (Central Daylight Time)' )
49
53
}
50
54
@@ -100,6 +104,14 @@ const IndexPage: React.FC = () => {
100
104
indentWidth = { indent }
101
105
groupArraysAfterLength = { groupArraysAfterLength }
102
106
keyRenderer = { KeyRenderer }
107
+ valueTypes = { [
108
+ {
109
+ is : ( value ) : value is string => typeof value === 'string' && value . startsWith ( 'https://i.imgur.com' ) ,
110
+ Component : ( props ) => {
111
+ return < img height = '50px' src = { props . value } alt = { props . value } />
112
+ }
113
+ }
114
+ ] }
103
115
onChange = {
104
116
useCallback < JsonViewerOnChange > (
105
117
( path , oldValue , newValue ) => {
Original file line number Diff line number Diff line change @@ -17,12 +17,19 @@ export function registerType<Value> (type: DataType<Value>) {
17
17
}
18
18
19
19
export function matchTypeComponents < Value > ( value : Value ) : DataType < Value > {
20
+ let potential : DataType < Value > | undefined
20
21
for ( const T of typeRegistry ) {
21
22
if ( T . is ( value ) ) {
23
+ potential = T
24
+ }
25
+ if ( typeof value === 'object' && value === null ) {
22
26
return T
23
27
}
24
28
}
25
- throw new DevelopmentError ( 'this is not possible' )
29
+ if ( potential === undefined ) {
30
+ throw new DevelopmentError ( 'this is not possible' )
31
+ }
32
+ return potential
26
33
}
27
34
28
35
export function useTypeComponents ( value : unknown ) {
You can’t perform that action at this time.
0 commit comments