forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
173 lines (160 loc) · 3.98 KB
/
index.tsx
File metadata and controls
173 lines (160 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { createElement, SFC } from 'react'
import { isObject, isArray } from './utils'
import * as styles from './styles'
import { css } from 'emotion'
type NestedProps = {
startBracket: string
endBracket: string
expandedPaths: string[]
path: string[]
keys: string[]
isArray: boolean
children: () => React.ReactNode
onToggleExpand: (path: string[]) => void
}
const Nested: SFC<NestedProps> = ({
keys,
expandedPaths,
path,
onToggleExpand,
startBracket,
endBracket,
children,
isArray,
}) => {
const shouldCollapse = !expandedPaths.includes(path.join('.'))
if (shouldCollapse) {
return (
<div
className={styles.inlineNested}
onClick={(event) => {
event.stopPropagation()
onToggleExpand(path)
}}
>
{path.length ? (
<span className={styles.key}>{path[path.length - 1]}:</span>
) : null}
{startBracket}
<span className={styles.keyCount}>
{isArray
? keys.length + ' items'
: keys.slice(0, 3).join(', ') + '...'}
</span>
{endBracket}
</div>
)
}
return (
<div>
<div
className={styles.bracket(true)}
onClick={(event) => {
event.stopPropagation()
onToggleExpand(path)
}}
>
{path.length ? (
<span className={styles.key}>{path[path.length - 1]}:</span>
) : null}
{startBracket}
</div>
<div className={styles.nestedChildren}>{children()}</div>
<div className={styles.bracket(false)}>{endBracket}</div>
</div>
)
}
type ValueComponentProps = {
value: string | number | boolean
path: string[]
}
const ValueComponent: SFC<ValueComponentProps> = ({ value, path }) => {
if (typeof value === 'string') {
return (
<div className={styles.stringValue}>
{path.length ? (
<span className={styles.key}>{path[path.length - 1]}:</span>
) : null}{' '}
"{value}"
</div>
)
}
return (
<div className={styles.genericValue}>
{path.length ? (
<span className={styles.key}>{path[path.length - 1]}:</span>
) : null}{' '}
{String(value)}
</div>
)
}
export type RenderPaths = {
[path: string]: (children: React.ReactChildren) => React.ReactNode
}
type InspectorProps = {
value: object
expandedPaths: string[]
small?: boolean
onToggleExpand: (path: string[]) => void
renderPaths?: RenderPaths
}
const Inspector: SFC<InspectorProps> = ({
value,
expandedPaths,
small,
onToggleExpand,
renderPaths,
}) => {
function renderValue(path: string[], value: any, renderPaths?: RenderPaths) {
const wrapper = renderPaths && renderPaths[path.join('.')]
let node
if (isObject(value)) {
node = (
<Nested
key={path.join('.')}
startBracket="{"
endBracket="}"
path={path}
expandedPaths={expandedPaths}
onToggleExpand={onToggleExpand}
keys={Object.keys(value)}
isArray={false}
>
{() =>
Object.keys(value).map((key) =>
renderValue(path.concat(key), value[key], renderPaths)
)
}
</Nested>
)
} else if (isArray(value)) {
node = (
<Nested
key={path.join('.')}
startBracket="["
endBracket="]"
path={path}
expandedPaths={expandedPaths}
onToggleExpand={onToggleExpand}
keys={Object.keys(value)}
isArray
>
{() =>
value.map((_, index) =>
renderValue(path.concat(index), value[index], renderPaths)
)
}
</Nested>
)
} else {
node = <ValueComponent key={path.join('.')} path={path} value={value} />
}
return wrapper ? wrapper(node) : node
}
return (
<div className={css(small ? styles.smallWrapper : styles.wrapper)}>
{renderValue([], value, renderPaths)}
</div>
)
}
export default Inspector