Skip to content

Commit 03e29f7

Browse files
authored
Add sharing capability to the Playground. (rjsf-team#535)
1 parent 35b9806 commit 03e29f7

File tree

3 files changed

+80
-10
lines changed

3 files changed

+80
-10
lines changed

ISSUE_TEMPLATE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
2. [Second Step]
1414
3. [and so on...]
1515

16-
Ideally, I'm providing a [sample JSFiddle](https://jsfiddle.net/n1k0/f2y3fq7L/6/) demonstrating the issue.
16+
Ideally, I'm providing a [sample JSFiddle](https://jsfiddle.net/n1k0/f2y3fq7L/6/) or a [shared playground link](https://mozilla-services.github.io/react-jsonschema-form/) demonstrating the issue.
1717

1818
#### Expected behavior
1919

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ A [live playground](https://mozilla-services.github.io/react-jsonschema-form/) i
5252
- [Advanced customization](#advanced-customization)
5353
- [Field template](#field-template)
5454
- [Array Field Template](#array-field-template)
55-
- [Error list Template](#error-list-template)
55+
- [Error List template](#error-list-template)
5656
- [Custom widgets and fields](#custom-widgets-and-fields)
5757
- [Custom widget components](#custom-widget-components)
5858
- [Custom component registration](#custom-component-registration)

playground/app.js

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ class Editor extends Component {
195195
try {
196196
this.props.onChange(fromJson(this.state.code));
197197
} catch (err) {
198-
console.error(err);
199198
this.setState({ valid: false, code });
200199
}
201200
});
@@ -274,6 +273,42 @@ function ThemeSelector({ theme, select }) {
274273
);
275274
}
276275

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+
277312
class App extends Component {
278313
constructor(props) {
279314
super(props);
@@ -288,11 +323,21 @@ class App extends Component {
288323
editor: "default",
289324
theme: "default",
290325
liveValidate: true,
326+
shareURL: null,
291327
};
292328
}
293329

294330
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+
}
296341
}
297342

298343
shouldComponentUpdate(nextProps, nextState) {
@@ -307,11 +352,11 @@ class App extends Component {
307352
this.setState({ ...data, form: true, ArrayFieldTemplate }));
308353
};
309354

310-
onSchemaEdited = schema => this.setState({ schema });
355+
onSchemaEdited = schema => this.setState({ schema, shareURL: null });
311356

312-
onUISchemaEdited = uiSchema => this.setState({ uiSchema });
357+
onUISchemaEdited = uiSchema => this.setState({ uiSchema, shareURL: null });
313358

314-
onFormDataEdited = formData => this.setState({ formData });
359+
onFormDataEdited = formData => this.setState({ formData, shareURL: null });
315360

316361
onThemeSelected = (theme, { stylesheet, editor }) => {
317362
this.setState({ theme, editor: editor ? editor : "default" });
@@ -323,7 +368,19 @@ class App extends Component {
323368

324369
setLiveValidate = ({ formData }) => this.setState({ liveValidate: formData });
325370

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+
};
327384

328385
render() {
329386
const {
@@ -401,8 +458,21 @@ class App extends Component {
401458
onBlur={(id, value) =>
402459
console.log(`Touched ${id} with value ${value}`)}
403460
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>}
406476
</div>
407477
</div>
408478
);

0 commit comments

Comments
 (0)