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 (159 loc) · 4.22 KB
/
index.tsx
File metadata and controls
173 lines (159 loc) · 4.22 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 * as React from 'react'
import { isObject, isArray } from './utils'
import { connect, Connect } from '../../app'
import {
Wrapper,
Key,
Bracket,
StringValue,
GenericValue,
NestedChildren,
InlineNested,
} from './elements'
type NestedProps = {
startBracket: string
endBracket: string
expandedPaths: string[]
path: string[]
keys: string[]
isArray: boolean
children: () => React.ReactNode
onToggleExpand: (path: string[]) => void
} & Connect
class Nested extends React.Component<NestedProps> {
hasMounted: boolean = false
componentDidMount() {
this.hasMounted = true
}
render() {
const {
keys,
expandedPaths,
path,
onToggleExpand,
startBracket,
endBracket,
children,
isArray,
} = this.props
const shouldCollapse =
(this.hasMounted && !expandedPaths.includes(path.join('.'))) ||
(!this.hasMounted && !expandedPaths.includes(path.join('.')))
if (shouldCollapse) {
return (
<InlineNested onClick={() => onToggleExpand(path)}>
{path.length ? (
<Key>{this.props.path[this.props.path.length - 1]}:</Key>
) : null}
{startBracket} {isArray ? keys.length : keys.slice(0, 3).join(', ')}{' '}
{endBracket}
</InlineNested>
)
}
return (
<React.Fragment>
<Bracket onClick={() => onToggleExpand(path)}>
{path.length ? (
<Key>{this.props.path[this.props.path.length - 1]}:</Key>
) : null}
{startBracket}
</Bracket>
<NestedChildren>{children()}</NestedChildren>
<Bracket>{endBracket}</Bracket>
</React.Fragment>
)
}
}
const ConnectedNested = connect(Nested)
type ValueComponentProps = {
value: string | number | boolean
path: string[]
}
class ValueComponent extends React.Component<ValueComponentProps> {
render() {
if (typeof this.props.value === 'string') {
return (
<StringValue>
{this.props.path.length ? (
<Key>{this.props.path[this.props.path.length - 1]}:</Key>
) : null}{' '}
"{this.props.value}"
</StringValue>
)
}
return (
<GenericValue>
{this.props.path.length ? (
<Key>{this.props.path[this.props.path.length - 1]}:</Key>
) : null}{' '}
{String(this.props.value)}
</GenericValue>
)
}
}
export type RenderPaths = {
[path: string]: (children: React.ReactChildren) => React.ReactNode
}
type InspectorProps = {
value: object
expandedPaths: string[]
small?: boolean
onToggleExpand: (path: string[]) => void
renderPaths?: RenderPaths
}
class Inspector extends React.Component<InspectorProps> {
renderValue(path: string[], value: any, renderPaths?: RenderPaths) {
const wrapper = renderPaths && renderPaths[path.join('.')]
let node
if (isObject(value)) {
node = (
<ConnectedNested
key={path.join('.')}
startBracket="{"
endBracket="}"
path={path}
expandedPaths={this.props.expandedPaths}
onToggleExpand={this.props.onToggleExpand}
keys={Object.keys(value)}
isArray={false}
>
{() =>
Object.keys(value).map((key) =>
this.renderValue(path.concat(key), value[key], renderPaths)
)
}
</ConnectedNested>
)
} else if (isArray(value)) {
node = (
<ConnectedNested
key={path.join('.')}
startBracket="["
endBracket="]"
path={path}
expandedPaths={this.props.expandedPaths}
onToggleExpand={this.props.onToggleExpand}
keys={Object.keys(value)}
isArray
>
{() =>
value.map((_, index) =>
this.renderValue(path.concat(index), value[index], renderPaths)
)
}
</ConnectedNested>
)
} else {
node = <ValueComponent key={path.join('.')} path={path} value={value} />
}
return wrapper ? wrapper(node) : node
}
render() {
return (
<Wrapper small={this.props.small}>
{this.renderValue([], this.props.value, this.props.renderPaths)}
</Wrapper>
)
}
}
export default Inspector