|
1 | | -import {Component} from 'react' |
2 | | - |
3 | 1 | import {CollapseHandler} from '../CollapseHandler' |
4 | 2 | import {Key} from '../Key' |
5 | 3 | import {Level} from '../Level' |
6 | 4 | import {Punctuation} from '../Punctuation' |
7 | 5 | import {Value} from '../Value' |
8 | 6 |
|
9 | | -export class DataHandler extends Component<{ |
10 | | - theme?: 'gloom' | 'default' |
| 7 | +export function DataHandler({ |
| 8 | + data, |
| 9 | + outer = false, |
| 10 | + theme = 'default', |
| 11 | +}: { |
11 | 12 | data: unknown |
12 | | - outer: boolean |
13 | | -}> { |
14 | | - static displayName = 'ReactInspectDataHandler' |
15 | | - static defaultProps = { |
16 | | - outer: false, |
| 13 | + outer?: boolean |
| 14 | + theme?: 'gloom' | 'default' |
| 15 | +}) { |
| 16 | + if (typeof data == 'string') { |
| 17 | + return <Value type="string" theme={theme}>{`"${data}"`}</Value> |
17 | 18 | } |
18 | 19 |
|
19 | | - render() { |
20 | | - const {data, outer, theme = 'default'} = this.props |
21 | | - |
22 | | - if (typeof data == 'string') { |
23 | | - return <Value type="string" theme={theme}>{`"${data}"`}</Value> |
24 | | - } |
25 | | - |
26 | | - if (typeof data == 'number') { |
27 | | - return <Value type="number" theme={theme}>{`${data}`}</Value> |
28 | | - } |
29 | | - |
30 | | - if (typeof data == 'function') { |
31 | | - const value = ( |
32 | | - <Value type="function" theme={theme}> |
33 | | - {String(data).trim()} |
34 | | - </Value> |
35 | | - ) |
| 20 | + if (typeof data == 'number') { |
| 21 | + return <Value type="number" theme={theme}>{`${data}`}</Value> |
| 22 | + } |
36 | 23 |
|
37 | | - if (outer) { |
38 | | - return value |
39 | | - } |
| 24 | + if (typeof data == 'function') { |
| 25 | + const value = ( |
| 26 | + <Value type="function" theme={theme}> |
| 27 | + {String(data).trim()} |
| 28 | + </Value> |
| 29 | + ) |
40 | 30 |
|
41 | | - return ( |
42 | | - <CollapseHandler> |
43 | | - {(show) => |
44 | | - show ? value : {...value, props: {...value.props, children: 'fn'}} |
45 | | - } |
46 | | - </CollapseHandler> |
47 | | - ) |
| 31 | + if (outer) { |
| 32 | + return value |
48 | 33 | } |
49 | 34 |
|
50 | | - if (Array.isArray(data)) { |
51 | | - const value = data.map((x, i) => ( |
52 | | - <Level key={i}> |
53 | | - <DataHandler data={x} theme={theme} /> |
54 | | - </Level> |
55 | | - )) |
| 35 | + return ( |
| 36 | + <CollapseHandler> |
| 37 | + {(show) => |
| 38 | + show ? value : {...value, props: {...value.props, children: 'fn'}} |
| 39 | + } |
| 40 | + </CollapseHandler> |
| 41 | + ) |
| 42 | + } |
56 | 43 |
|
57 | | - return ( |
58 | | - <span> |
59 | | - <Punctuation theme={theme}>{'['}</Punctuation> |
60 | | - {outer ? ( |
61 | | - value |
62 | | - ) : ( |
63 | | - <CollapseHandler> |
64 | | - {(show) => |
65 | | - show ? ( |
66 | | - <>{value}</> |
67 | | - ) : ( |
68 | | - <Punctuation theme={theme}>...</Punctuation> |
69 | | - ) |
70 | | - } |
71 | | - </CollapseHandler> |
72 | | - )} |
73 | | - <Punctuation theme={theme}>{']'}</Punctuation> |
74 | | - </span> |
75 | | - ) |
76 | | - } |
| 44 | + if (Array.isArray(data)) { |
| 45 | + const value = data.map((x, i) => ( |
| 46 | + <Level key={i}> |
| 47 | + <DataHandler data={x} theme={theme} /> |
| 48 | + </Level> |
| 49 | + )) |
77 | 50 |
|
78 | | - if (isRecord(data)) { |
79 | | - const value = Object.keys(data).map((x) => ( |
80 | | - <Level key={x}> |
81 | | - <Key theme={theme}>{x}</Key> |
82 | | - <Punctuation theme={theme}>:</Punctuation>{' '} |
83 | | - <DataHandler data={data[x]} theme={theme} /> |
84 | | - </Level> |
85 | | - )) |
| 51 | + return ( |
| 52 | + <span> |
| 53 | + <Punctuation theme={theme}>{'['}</Punctuation> |
| 54 | + {outer ? ( |
| 55 | + value |
| 56 | + ) : ( |
| 57 | + <CollapseHandler> |
| 58 | + {(show) => |
| 59 | + show ? <>{value}</> : <Punctuation theme={theme}>...</Punctuation> |
| 60 | + } |
| 61 | + </CollapseHandler> |
| 62 | + )} |
| 63 | + <Punctuation theme={theme}>{']'}</Punctuation> |
| 64 | + </span> |
| 65 | + ) |
| 66 | + } |
86 | 67 |
|
87 | | - return ( |
88 | | - <span> |
89 | | - <Punctuation theme={theme}>{'{'}</Punctuation> |
90 | | - {outer ? ( |
91 | | - value |
92 | | - ) : ( |
93 | | - <CollapseHandler> |
94 | | - {(show) => |
95 | | - show ? ( |
96 | | - <>{value}</> |
97 | | - ) : ( |
98 | | - <Punctuation theme={theme}>...</Punctuation> |
99 | | - ) |
100 | | - } |
101 | | - </CollapseHandler> |
102 | | - )} |
103 | | - <Punctuation theme={theme}>{'}'}</Punctuation> |
104 | | - </span> |
105 | | - ) |
106 | | - } |
| 68 | + if (isRecord(data)) { |
| 69 | + const value = Object.keys(data).map((x) => ( |
| 70 | + <Level key={x}> |
| 71 | + <Key theme={theme}>{x}</Key> |
| 72 | + <Punctuation theme={theme}>:</Punctuation>{' '} |
| 73 | + <DataHandler data={data[x]} theme={theme} /> |
| 74 | + </Level> |
| 75 | + )) |
107 | 76 |
|
108 | | - return <Value type="keyword" theme={theme}>{`${data}`}</Value> |
| 77 | + return ( |
| 78 | + <span> |
| 79 | + <Punctuation theme={theme}>{'{'}</Punctuation> |
| 80 | + {outer ? ( |
| 81 | + value |
| 82 | + ) : ( |
| 83 | + <CollapseHandler> |
| 84 | + {(show) => |
| 85 | + show ? <>{value}</> : <Punctuation theme={theme}>...</Punctuation> |
| 86 | + } |
| 87 | + </CollapseHandler> |
| 88 | + )} |
| 89 | + <Punctuation theme={theme}>{'}'}</Punctuation> |
| 90 | + </span> |
| 91 | + ) |
109 | 92 | } |
| 93 | + |
| 94 | + return <Value type="keyword" theme={theme}>{`${data}`}</Value> |
110 | 95 | } |
111 | 96 |
|
| 97 | +DataHandler.displayName = 'ReactInspectDataHandler' |
| 98 | + |
112 | 99 | function isRecord(data: unknown): data is Record<string, unknown> { |
113 | 100 | return data != null && typeof data == 'object' |
114 | 101 | } |
0 commit comments