@@ -20,10 +20,10 @@ export default class EditRowDialog extends React.Component {
20
20
super ( props ) ;
21
21
22
22
const { selectedObject } = this . props ;
23
- const { currentObject, openObjectPickers } = this . initializeState (
23
+ const { currentObject, openObjectPickers, expandedTextAreas } = this . initializeState (
24
24
selectedObject
25
25
) ;
26
- this . state = { currentObject, openObjectPickers } ;
26
+ this . state = { currentObject, openObjectPickers, expandedTextAreas } ;
27
27
28
28
this . updateCurrentObject = this . updateCurrentObject . bind ( this ) ;
29
29
this . handleChange = this . handleChange . bind ( this ) ;
@@ -37,42 +37,46 @@ export default class EditRowDialog extends React.Component {
37
37
const newSelectedObject = props . selectedObject ;
38
38
const previousSelectedObject = this . props . selectedObject ;
39
39
if ( newSelectedObject . id !== previousSelectedObject . id ) {
40
- const { currentObject, openObjectPickers } = this . initializeState (
40
+ const { currentObject, openObjectPickers, expandedTextAreas } = this . initializeState (
41
41
newSelectedObject
42
42
) ;
43
- this . setState ( { currentObject, openObjectPickers } ) ;
43
+ this . setState ( { currentObject, openObjectPickers, expandedTextAreas } ) ;
44
+ } else if ( newSelectedObject . updatedAt !== previousSelectedObject . updatedAt ) {
45
+ this . updateCurrentObjectFromProps ( newSelectedObject ) ;
44
46
}
45
47
}
46
48
47
49
initializeState ( newObject ) {
48
50
const { columns } = this . props ;
49
51
const currentObject = { ...newObject } ;
50
52
const openObjectPickers = { } ;
53
+ const expandedTextAreas = { } ;
51
54
columns . forEach ( column => {
52
55
const { name, type } = column ;
53
56
if ( [ 'Array' , 'Object' ] . indexOf ( type ) >= 0 ) {
54
- currentObject [ name ] = JSON . stringify ( currentObject [ name ] , null , 2 ) ;
57
+ const stringifyValue = JSON . stringify ( currentObject [ name ] , null , 4 ) ;
58
+ currentObject [ name ] = stringifyValue ;
59
+ const rows = stringifyValue ? stringifyValue . split ( '\n' ) . length : 1 ;
60
+ expandedTextAreas [ name ] = { rows : rows , expanded : false } ;
55
61
}
56
62
if ( type === 'Polygon' ) {
57
- currentObject [ name ] = JSON . stringify (
63
+ const stringifyValue = JSON . stringify (
58
64
( currentObject [ name ] && currentObject [ name ] . coordinates ) || [
59
65
[ 'lat' , 'lon' ]
60
66
] ,
61
67
null ,
62
- 2
68
+ 4
63
69
) ;
70
+ currentObject [ name ] = stringifyValue ;
71
+ const rows = stringifyValue ? stringifyValue . split ( '\n' ) . length : 1 ;
72
+ expandedTextAreas [ name ] = { rows : rows , expanded : false } ;
64
73
}
65
- if ( type === 'Pointer' ) {
66
- currentObject [ name ] =
67
- ( currentObject [ name ] && currentObject [ name ] . id ) || '' ;
68
- openObjectPickers [ name ] = false ;
69
- }
70
- if ( type === 'Relation' ) {
74
+ if ( [ 'Pointer' , 'Relation' ] . indexOf ( type ) >= 0 ) {
71
75
openObjectPickers [ name ] = false ;
72
76
}
73
77
} ) ;
74
78
75
- return { currentObject, openObjectPickers } ;
79
+ return { currentObject, openObjectPickers, expandedTextAreas } ;
76
80
}
77
81
78
82
updateCurrentObject ( newValue , name ) {
@@ -81,6 +85,36 @@ export default class EditRowDialog extends React.Component {
81
85
this . setState ( { currentObject } ) ;
82
86
}
83
87
88
+ updateCurrentObjectFromProps ( newObject ) {
89
+ const { columns } = this . props ;
90
+ const { currentObject, expandedTextAreas } = this . state ;
91
+ columns . forEach ( column => {
92
+ const { name, type } = column ;
93
+ if ( [ 'String' , 'Number' ] . indexOf ( type ) >= 0 ) {
94
+ currentObject [ name ] = newObject [ name ] ;
95
+ }
96
+ if ( [ 'Array' , 'Object' ] . indexOf ( type ) >= 0 ) {
97
+ const stringifyValue = JSON . stringify ( newObject [ name ] , null , 4 ) ;
98
+ currentObject [ name ] = stringifyValue ;
99
+ const rows = stringifyValue ? stringifyValue . split ( '\n' ) . length : 1 ;
100
+ expandedTextAreas [ name ] . rows = rows ;
101
+ }
102
+ if ( type === 'Polygon' ) {
103
+ const stringifyValue = JSON . stringify (
104
+ ( newObject [ name ] && newObject [ name ] . coordinates ) || [
105
+ [ 'lat' , 'lon' ]
106
+ ] ,
107
+ null ,
108
+ 4
109
+ ) ;
110
+ currentObject [ name ] = stringifyValue ;
111
+ const rows = stringifyValue ? stringifyValue . split ( '\n' ) . length : 1 ;
112
+ expandedTextAreas [ name ] . rows = rows ;
113
+ }
114
+ } ) ;
115
+ this . setState ( { currentObject, expandedTextAreas } ) ;
116
+ }
117
+
84
118
handleChange ( newValue , name , type , targetClass , toDelete ) {
85
119
if ( name == 'password' ) {
86
120
if ( newValue === '' ) {
@@ -113,8 +147,22 @@ export default class EditRowDialog extends React.Component {
113
147
this . toggleObjectPicker ( name , false ) ;
114
148
} else {
115
149
if ( [ 'Array' , 'Object' , 'Polygon' ] . indexOf ( type ) >= 0 ) {
116
- const { currentObject } = this . state ;
117
- currentObject [ name ] = JSON . stringify ( newValue , null , 2 ) ;
150
+ const { selectedObject } = this . props ;
151
+ const { currentObject, expandedTextAreas } = this . state ;
152
+ const oldStringifyValue = JSON . stringify (
153
+ type === 'Polygon'
154
+ ? selectedObject [ name ] . coordinates
155
+ : selectedObject [ name ] ,
156
+ null ,
157
+ 4
158
+ ) ;
159
+ const stringifyValue = JSON . stringify ( newValue , null , 4 ) ;
160
+ if ( oldStringifyValue === stringifyValue ) {
161
+ return ;
162
+ }
163
+ currentObject [ name ] = stringifyValue ;
164
+ const rows = stringifyValue ? stringifyValue . split ( '\n' ) . length : 1 ;
165
+ expandedTextAreas [ name ] . rows = rows ;
118
166
if ( type === 'Polygon' ) {
119
167
newValue = {
120
168
__type : type ,
@@ -162,9 +210,15 @@ export default class EditRowDialog extends React.Component {
162
210
this . setState ( { openObjectPickers } ) ;
163
211
}
164
212
213
+ toggleExpandTextArea ( name ) {
214
+ const { expandedTextAreas } = this . state ;
215
+ expandedTextAreas [ name ] . expanded = ! expandedTextAreas [ name ] . expanded ;
216
+ this . setState ( { expandedTextAreas } ) ;
217
+ }
218
+
165
219
render ( ) {
166
220
const { selectedObject, className, columns, onClose, schema } = this . props ;
167
- const { currentObject, openObjectPickers } = this . state ;
221
+ const { currentObject, openObjectPickers, expandedTextAreas } = this . state ;
168
222
169
223
const fields = columns . map ( column => {
170
224
const { name, type, targetClass } = column ;
@@ -226,6 +280,11 @@ export default class EditRowDialog extends React.Component {
226
280
inputComponent = (
227
281
< TextInput
228
282
multiline = { true }
283
+ rows = {
284
+ expandedTextAreas [ name ] &&
285
+ expandedTextAreas [ name ] . expanded &&
286
+ expandedTextAreas [ name ] . rows
287
+ }
229
288
disabled = { isDisabled }
230
289
value = { currentObject [ name ] }
231
290
onChange = { newValue => this . updateCurrentObject ( newValue , name ) }
@@ -354,13 +413,29 @@ export default class EditRowDialog extends React.Component {
354
413
inputComponent = < div /> ;
355
414
}
356
415
416
+ const description = (
417
+ < span >
418
+ { targetClass ? `${ type } <${ targetClass } >` : type }
419
+ < div style = { { marginTop : '2px' } } >
420
+ { expandedTextAreas [ name ] && expandedTextAreas [ name ] . rows > 3 && (
421
+ < a
422
+ style = { { color : '#169cee' } }
423
+ onClick = { ( ) => this . toggleExpandTextArea ( name ) }
424
+ >
425
+ { expandedTextAreas [ name ] . expanded ? 'collapse' : 'expand' }
426
+ </ a >
427
+ ) }
428
+ </ div >
429
+ </ span >
430
+ ) ;
431
+
357
432
return (
358
433
< Field
359
434
key = { name }
360
435
label = {
361
436
< Label
362
437
text = { name }
363
- description = { targetClass ? ` ${ type } < ${ targetClass } >` : type }
438
+ description = { description }
364
439
/>
365
440
}
366
441
labelWidth = { 33 }
0 commit comments