@@ -195,7 +195,6 @@ class Editor extends Component {
195
195
try {
196
196
this . props . onChange ( fromJson ( this . state . code ) ) ;
197
197
} catch ( err ) {
198
- console . error ( err ) ;
199
198
this . setState ( { valid : false , code } ) ;
200
199
}
201
200
} ) ;
@@ -274,6 +273,42 @@ function ThemeSelector({ theme, select }) {
274
273
) ;
275
274
}
276
275
276
+ class CopyLink extends Component {
277
+ onCopyClick = event => {
278
+ this . input . select ( ) ;
279
+ document . execCommand ( "copy" ) ;
280
+ } ;
281
+
282
+ render ( ) {
283
+ const { shareURL, onShare } = this . props ;
284
+ if ( ! shareURL ) {
285
+ return (
286
+ < button className = "btn btn-default" type = "button" onClick = { onShare } >
287
+ Share
288
+ </ button >
289
+ ) ;
290
+ }
291
+ return (
292
+ < div className = "input-group" >
293
+ < input
294
+ type = "text"
295
+ ref = { input => this . input = input }
296
+ className = "form-control"
297
+ defaultValue = { shareURL }
298
+ />
299
+ < span className = "input-group-btn" >
300
+ < button
301
+ className = "btn btn-default"
302
+ type = "button"
303
+ onClick = { this . onCopyClick } >
304
+ < i className = "glyphicon glyphicon-copy" />
305
+ </ button >
306
+ </ span >
307
+ </ div >
308
+ ) ;
309
+ }
310
+ }
311
+
277
312
class App extends Component {
278
313
constructor ( props ) {
279
314
super ( props ) ;
@@ -288,11 +323,21 @@ class App extends Component {
288
323
editor : "default" ,
289
324
theme : "default" ,
290
325
liveValidate : true ,
326
+ shareURL : null ,
291
327
} ;
292
328
}
293
329
294
330
componentDidMount ( ) {
295
- this . load ( samples . Simple ) ;
331
+ const hash = document . location . hash . match ( / # ( .* ) / ) ;
332
+ if ( hash && typeof hash [ 1 ] === "string" && hash [ 1 ] . length > 0 ) {
333
+ try {
334
+ this . load ( JSON . parse ( atob ( hash [ 1 ] ) ) ) ;
335
+ } catch ( err ) {
336
+ alert ( "Unable to load form setup data." ) ;
337
+ }
338
+ } else {
339
+ this . load ( samples . Simple ) ;
340
+ }
296
341
}
297
342
298
343
shouldComponentUpdate ( nextProps , nextState ) {
@@ -307,11 +352,11 @@ class App extends Component {
307
352
this . setState ( { ...data , form : true , ArrayFieldTemplate } ) ) ;
308
353
} ;
309
354
310
- onSchemaEdited = schema => this . setState ( { schema } ) ;
355
+ onSchemaEdited = schema => this . setState ( { schema, shareURL : null } ) ;
311
356
312
- onUISchemaEdited = uiSchema => this . setState ( { uiSchema } ) ;
357
+ onUISchemaEdited = uiSchema => this . setState ( { uiSchema, shareURL : null } ) ;
313
358
314
- onFormDataEdited = formData => this . setState ( { formData } ) ;
359
+ onFormDataEdited = formData => this . setState ( { formData, shareURL : null } ) ;
315
360
316
361
onThemeSelected = ( theme , { stylesheet, editor } ) => {
317
362
this . setState ( { theme, editor : editor ? editor : "default" } ) ;
@@ -323,7 +368,19 @@ class App extends Component {
323
368
324
369
setLiveValidate = ( { formData } ) => this . setState ( { liveValidate : formData } ) ;
325
370
326
- onFormDataChange = ( { formData } ) => this . setState ( { formData } ) ;
371
+ onFormDataChange = ( { formData } ) =>
372
+ this . setState ( { formData, shareURL : null } ) ;
373
+
374
+ onShare = ( ) => {
375
+ const { formData, schema, uiSchema } = this . state ;
376
+ const { location : { origin, pathname } } = document ;
377
+ try {
378
+ const hash = btoa ( JSON . stringify ( { formData, schema, uiSchema } ) ) ;
379
+ this . setState ( { shareURL : `${ origin } ${ pathname } #${ hash } ` } ) ;
380
+ } catch ( err ) {
381
+ this . setState ( { shareURL : null } ) ;
382
+ }
383
+ } ;
327
384
328
385
render ( ) {
329
386
const {
@@ -401,8 +458,21 @@ class App extends Component {
401
458
onBlur = { ( id , value ) =>
402
459
console . log ( `Touched ${ id } with value ${ value } ` ) }
403
460
transformErrors = { transformErrors }
404
- onError = { log ( "errors" ) }
405
- /> }
461
+ onError = { log ( "errors" ) } >
462
+ < div className = "row" >
463
+ < div className = "col-sm-3" >
464
+ < button className = "btn btn-primary" type = "submit" >
465
+ Submit
466
+ </ button >
467
+ </ div >
468
+ < div className = "col-sm-9 text-right" >
469
+ < CopyLink
470
+ shareURL = { this . state . shareURL }
471
+ onShare = { this . onShare }
472
+ />
473
+ </ div >
474
+ </ div >
475
+ </ Form > }
406
476
</ div >
407
477
</ div >
408
478
) ;
0 commit comments