-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding separate dialog so that we can handle state with useState
- Loading branch information
Showing
2 changed files
with
68 additions
and
28 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
packages/editor-sample/src/App/TemplatePanel/ImportJson/ImportJsonDialog.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import React, { useState } from 'react'; | ||
|
||
import { Button, Dialog, DialogActions, DialogContent, DialogTitle, Input } from '@mui/material'; | ||
|
||
import { resetDocument } from '../../../documents/editor/EditorContext'; | ||
|
||
type ImportJsonDialogProps = { | ||
onClose: () => void; | ||
}; | ||
export default function ImportJsonDialog({ onClose }: ImportJsonDialogProps) { | ||
const [value, setValue] = useState(''); | ||
const [error, setError] = useState<string | null>(null); | ||
|
||
const handleChange: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (ev) => { | ||
const v = ev.currentTarget.value; | ||
setValue(v); | ||
try { | ||
JSON.parse(v); | ||
setError(null); | ||
} catch { | ||
setError('There was a parsing error'); | ||
} | ||
}; | ||
|
||
return ( | ||
<Dialog open onClose={onClose}> | ||
<DialogTitle>Import JSON</DialogTitle> | ||
<form | ||
onSubmit={(ev) => { | ||
ev.preventDefault(); | ||
try { | ||
const doc = JSON.parse(value); | ||
resetDocument(doc); | ||
onClose(); | ||
} catch {} | ||
}} | ||
> | ||
<DialogContent> | ||
<Input | ||
error={error !== null} | ||
value={value} | ||
onChange={handleChange} | ||
type="text" | ||
fullWidth | ||
rows={10} | ||
multiline | ||
/> | ||
</DialogContent> | ||
<DialogActions> | ||
<Button type="button" onClick={onClose}> | ||
Cancel | ||
</Button> | ||
<Button type="submit" disabled={error !== null}> | ||
Save | ||
</Button> | ||
</DialogActions> | ||
</form> | ||
</Dialog> | ||
); | ||
} |
36 changes: 8 additions & 28 deletions
36
packages/editor-sample/src/App/TemplatePanel/ImportJson/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,21 @@ | ||
import React, { useState } from 'react'; | ||
|
||
import { Button, Dialog, DialogActions, DialogContent, DialogTitle, Input } from '@mui/material'; | ||
import { Button } from '@mui/material'; | ||
|
||
import { resetDocument } from '../../../documents/editor/EditorContext'; | ||
import ImportJsonDialog from './ImportJsonDialog'; | ||
|
||
export default function ImportJson() { | ||
const [open, setOpen] = useState(false); | ||
|
||
let dialog = null; | ||
if (open) { | ||
dialog = <ImportJsonDialog onClose={() => setOpen(false)} />; | ||
} | ||
|
||
return ( | ||
<> | ||
<Button onClick={() => setOpen(true)}>Import JSON</Button> | ||
<Dialog open={open} onClose={() => setOpen(false)}> | ||
<DialogTitle>Import JSON</DialogTitle> | ||
<form | ||
onSubmit={(ev) => { | ||
ev.preventDefault(); | ||
const data = new FormData(ev.currentTarget); | ||
const json = data.get('json'); | ||
if (typeof json !== 'string') { | ||
return; | ||
} | ||
try { | ||
const doc = JSON.parse(json); | ||
resetDocument(doc); | ||
setOpen(false); | ||
} catch {} | ||
}} | ||
> | ||
<DialogContent> | ||
<Input name="json" type="text" fullWidth rows={10} multiline /> | ||
</DialogContent> | ||
<DialogActions> | ||
<Button type="button">Cancel</Button> | ||
<Button type="submit">Save</Button> | ||
</DialogActions> | ||
</form> | ||
</Dialog> | ||
{dialog} | ||
</> | ||
); | ||
} |