-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonEditorListComponent.tsx
160 lines (156 loc) · 9.96 KB
/
JsonEditorListComponent.tsx
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
import React from "react"
import 'prismjs/components/prism-json'
import 'prismjs/themes/prism.css'
import JsonEditorComponent from "./JsonEditorComponent";
import ScrollPane from "./html/ScrollPane";
class JsonEditorListComponent extends React.Component<{
schema: any,
data?: any,
onChange: (data: any) => void
}, {
selectedIndex: number
}> {
render() {
const {schema, data, onChange} = this.props
const {selectedIndex} = this.state ?? {}
const entries: any[] = Array.isArray(data?.entries) ? data.entries : []
return <div className="flex-fill d-flex flex-column">
<div className="flex-fill separator-bottom">
<ScrollPane>
<div className="p-2">
{(() => {
const headerFields: string[] = schema?.properties?.entries?.items?.headerFields ?? []
return <table className="table table-sm table-hover">
<thead style={{
position: 'sticky',
top: '0px',
backgroundColor: 'white'
}}>
<tr>
{headerFields.map(field => <th key={field}>{field}</th>)}
<th className="align-middle" style={{width: 0}}>
<div className="d-flex flex-row align-items-center justify-content-end gap-2">
<i className="list-button oi oi-plus"
onClick={event => {
event.stopPropagation()
onChange({
...data,
entries: (data?.entries ?? []).concat([{}])
})
}}/>
</div>
</th>
</tr>
</thead>
<tbody>
{entries.map((entry, i) => {
const isSelected = selectedIndex === i
return <tr
key={i + '=' + JSON.stringify(entry)}
onClick={() => {
this.setState(state => ({...state, selectedIndex: i}))
}}
className={'mx-2' + (isSelected ? ' table-active' : '')}
style={{cursor: 'pointer'}}>
{headerFields.map(field => <td
key={field}>{(entry[field] ?? '').toString()}</td>)}
<td className="align-middle">
<div
className="d-flex flex-row align-items-center justify-content-end gap-2">
{i > 0 ?
<i className="list-button oi oi-caret-top"
onClick={event => {
event.stopPropagation()
onChange({
...data,
entries: (() => {
const entries = (data?.entries ?? [])
let moveEntry: any = null
return entries.flatMap((entry: any, index: number) => {
if (moveEntry != null) {
const insertEntry = moveEntry
moveEntry = null
return [entry, insertEntry]
} else if (index == i - 1) {
moveEntry = entry
return []
} else {
return [entry]
}
})
})()
})
}}/> : null}
{i + 1 < entries.length ?
<i className="list-button oi oi-caret-bottom"
onClick={event => {
event.stopPropagation()
onChange({
...data,
entries: (() => {
const entries = (data?.entries ?? [])
let moveEntry: any = null
return entries.flatMap((entry: any, index: number) => {
if (moveEntry != null) {
const insertEntry = moveEntry
moveEntry = null
return [entry, insertEntry]
} else if (index == i) {
moveEntry = entry
return []
} else {
return [entry]
}
})
})()
})
}}/> : null}
<i className="list-button oi oi-layers"
onClick={event => {
event.stopPropagation()
onChange({
...data,
entries: (data?.entries ?? []).flatMap((entry: any, index: number) => index == i ? [entry, entry] : entry)
})
}}/>
<i className="list-button oi oi-trash"
onClick={event => {
event.stopPropagation()
onChange({
...data,
entries: (data?.entries ?? []).filter((entry: any, index: number) => index != i)
})
}}/>
</div>
</td>
</tr>
})}
</tbody>
</table>
})()}
</div>
</ScrollPane>
</div>
<div className="flex-fill list-json-editor-panel">
<ScrollPane>
<div className="p-2">
{selectedIndex != null && data?.entries?.[selectedIndex] != null ?
<JsonEditorComponent
schema={{$schema: schema?.$schema, ...(schema?.properties?.entries?.items ?? {})}}
data={data?.entries?.[selectedIndex]}
onChange={entryData => onChange({
...data,
entries: (data?.entries ?? []).map((entry: any, i: number) =>
selectedIndex == i ?
{...entryData, $schema: undefined} :
entry
)
})}/> :
null}
</div>
</ScrollPane>
</div>
</div>
}
}
export default JsonEditorListComponent