1
- import { Input , InputNumber , Select } from 'antd' ;
2
- import React , { useContext , useEffect } from 'react' ;
3
- import { getQuoteAddress , getTypeString , typeMap } from './util' ;
4
- import AddItem from './AddItem' ;
5
- import { ConfigContext } from './store' ;
6
- import { JsonEditorProps } from '.' ;
7
- import ArrayView from './ArrayView' ;
8
- import ToolsView from './Tools' ;
1
+ import { Input , InputNumber , Select } from 'antd'
2
+ import React , { useContext , useEffect } from 'react'
3
+ import { getQuoteAddress , getTypeString , typeMap } from './util'
4
+ import AddItem from './AddItem'
5
+ import { ConfigContext } from './store'
6
+ import { JsonEditorProps } from '.'
7
+ import ArrayView from './ArrayView'
8
+ import ToolsView from './Tools'
9
9
10
10
function JsonView ( props : JsonEditorProps ) {
11
- const { editObject, setEditObject } = useContext ( ConfigContext ) ;
11
+ const { editObject, setEditObject } = useContext ( ConfigContext )
12
12
13
13
useEffect ( ( ) => {
14
- props . onChange ( editObject ) ;
15
- } , [ editObject ] ) ;
14
+ props . onChange ( editObject )
15
+ } , [ editObject ] )
16
16
17
17
const syncData = ( data : Record < string , any > ) => {
18
- setEditObject ( { ...data } ) ;
19
- } ;
18
+ setEditObject ( { ...data } )
19
+ }
20
20
21
21
const onClickDelete = ( key : string , sourceData : any ) => {
22
22
if ( Array . isArray ( sourceData ) ) {
23
- sourceData . splice ( + key , 1 ) ;
23
+ sourceData . splice ( + key , 1 )
24
24
} else {
25
- Reflect . deleteProperty ( sourceData , key ) ;
25
+ Reflect . deleteProperty ( sourceData , key )
26
26
}
27
- syncData ( editObject ) ;
28
- } ;
27
+ syncData ( editObject )
28
+ }
29
29
30
30
const onChangeType = ( type : string , fieldValue : any ) => {
31
- const newEditObject = getQuoteAddress (
32
- fieldValue ,
33
- typeMap [ type ] ,
34
- editObject
35
- ) ;
36
- syncData ( newEditObject ) ;
37
- } ;
31
+ const newEditObject = getQuoteAddress ( fieldValue , typeMap [ type ] , editObject )
32
+ syncData ( newEditObject )
33
+ }
38
34
39
35
const onChangeKey = (
40
36
event : React . ChangeEvent < HTMLInputElement > ,
41
37
currentKey : string ,
42
38
source : Record < string , any >
43
39
) => {
44
- const newValue : Record < string , any > = { } ;
40
+ const newValue : Record < string , any > = { }
45
41
for ( const key in source ) {
46
42
if ( Object . prototype . hasOwnProperty . call ( source , key ) ) {
47
43
if ( key === currentKey ) {
48
- newValue [ event . target . value ] = source [ key ] ;
44
+ newValue [ event . target . value ] = source [ key ]
49
45
} else {
50
- newValue [ key ] = source [ key ] ;
46
+ newValue [ key ] = source [ key ]
51
47
}
52
48
}
53
49
}
54
- const newTotalData = getQuoteAddress ( source , newValue , editObject ) ;
55
- syncData ( newTotalData ) ;
56
- } ;
50
+ const newTotalData = getQuoteAddress ( source , newValue , editObject )
51
+ syncData ( newTotalData )
52
+ }
57
53
58
54
const onChangeValue = (
59
55
value : any ,
60
56
key : string ,
61
57
source : Record < string , any >
62
58
) => {
63
- source [ key ] = value ;
64
- syncData ( editObject ) ;
65
- } ;
59
+ source [ key ] = value
60
+ syncData ( editObject )
61
+ }
66
62
67
63
const getValue = (
68
64
fieldValue : any ,
@@ -71,7 +67,7 @@ function JsonView(props: JsonEditorProps) {
71
67
deepLevel : number ,
72
68
deepLevelJoin : string
73
69
) => {
74
- const thatType = getTypeString ( fieldValue ) ;
70
+ const thatType = getTypeString ( fieldValue )
75
71
switch ( thatType ) {
76
72
case 'array' :
77
73
return (
@@ -86,14 +82,14 @@ function JsonView(props: JsonEditorProps) {
86
82
getValue = { getValue }
87
83
/>
88
84
</ span >
89
- ) ;
85
+ )
90
86
case 'object' :
91
87
return (
92
88
< span style = { { marginBottom : '10px' } } >
93
89
< b > Object{ `{${ Object . keys ( fieldValue ) . length } }` } :</ b >
94
90
{ renderJsonConfig ( fieldValue , deepLevel + 1 , deepLevelJoin ) }
95
91
</ span >
96
- ) ;
92
+ )
97
93
case 'string' :
98
94
return (
99
95
< Input
@@ -104,7 +100,7 @@ function JsonView(props: JsonEditorProps) {
104
100
onChangeValue ( event . target . value , fieldKey , sourceData )
105
101
}
106
102
/>
107
- ) ;
103
+ )
108
104
case 'null' :
109
105
case 'number' :
110
106
return (
@@ -113,17 +109,17 @@ function JsonView(props: JsonEditorProps) {
113
109
placeholder = { fieldValue }
114
110
value = { fieldValue }
115
111
onChange = { ( value : number ) => {
116
- onChangeValue ( value , fieldKey , sourceData ) ;
112
+ onChangeValue ( value , fieldKey , sourceData )
117
113
} }
118
114
/>
119
- ) ;
115
+ )
120
116
case 'boolean' :
121
117
return (
122
118
< Select
123
119
style = { { width : '100px' } }
124
120
defaultValue = { true }
125
121
onChange = { ( value : boolean ) => {
126
- onChangeValue ( value , fieldKey , sourceData ) ;
122
+ onChangeValue ( value , fieldKey , sourceData )
127
123
} }
128
124
>
129
125
< Select . Option value = { true } label = "true" >
@@ -133,15 +129,15 @@ function JsonView(props: JsonEditorProps) {
133
129
false
134
130
</ Select . Option >
135
131
</ Select >
136
- ) ;
132
+ )
137
133
}
138
- } ;
134
+ }
139
135
const renderJsonConfig = (
140
136
sourceData : any ,
141
137
deepLevel : number = 0 ,
142
138
deepLevelJoin : string = `${ deepLevel } `
143
139
) => {
144
- const keyList = Object . keys ( sourceData ) ;
140
+ const keyList = Object . keys ( sourceData )
145
141
if ( ! keyList . length ) {
146
142
return (
147
143
< div style = { { marginLeft : '20px' } } >
@@ -151,14 +147,14 @@ function JsonView(props: JsonEditorProps) {
151
147
sourceData = { sourceData }
152
148
/>
153
149
</ div >
154
- ) ;
150
+ )
155
151
}
156
152
return (
157
153
< div className = "blockContent" >
158
154
< div style = { { marginTop : '10px' } } >
159
155
{ keyList . map ( ( fieldKey , index ) => {
160
- const uniqueKey = `${ deepLevelJoin } -${ index } ` ;
161
- const fieldValue = sourceData [ fieldKey ] ;
156
+ const uniqueKey = `${ deepLevelJoin } -${ index } `
157
+ const fieldValue = sourceData [ fieldKey ]
162
158
return (
163
159
< div key = { uniqueKey } className = "indexLine" >
164
160
< span className = "jsonKey" >
@@ -188,7 +184,7 @@ function JsonView(props: JsonEditorProps) {
188
184
}
189
185
</ span >
190
186
</ div >
191
- ) ;
187
+ )
192
188
} ) }
193
189
</ div >
194
190
< div >
@@ -200,8 +196,8 @@ function JsonView(props: JsonEditorProps) {
200
196
/>
201
197
</ div >
202
198
</ div >
203
- ) ;
204
- } ;
199
+ )
200
+ }
205
201
206
202
return (
207
203
< ConfigContext . Provider
@@ -214,7 +210,7 @@ function JsonView(props: JsonEditorProps) {
214
210
>
215
211
< div > { renderJsonConfig ( editObject ) } </ div >
216
212
</ ConfigContext . Provider >
217
- ) ;
213
+ )
218
214
}
219
215
220
- export default JsonView ;
216
+ export default JsonView
0 commit comments