-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
426f6f2
commit 8eb028e
Showing
9 changed files
with
252 additions
and
63 deletions.
There are no files selected for viewing
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
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
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
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,76 @@ | ||
import * as React from 'react' | ||
import { Button } from 'semantic-ui-react' | ||
|
||
interface JsonEditorProps { | ||
jsonStr?: string | ||
jsonObj?: any | ||
onChange: (json: object) => void | ||
} | ||
|
||
export const JsonEditor: React.FunctionComponent<JsonEditorProps> = ({ | ||
jsonStr, | ||
jsonObj, | ||
onChange, | ||
}) => { | ||
const [innerJsonValue, setInnerJsonValue] = React.useState<string>( | ||
jsonObj ? JSON.stringify(jsonObj, null, 2) : jsonStr || '' | ||
) | ||
const [error, setError] = React.useState<string | undefined>(undefined) | ||
|
||
React.useEffect(() => { | ||
if (jsonObj) { | ||
setInnerJsonValue(JSON.stringify(jsonObj, null, 2)) | ||
return | ||
} | ||
if (jsonStr) { | ||
setInnerJsonValue(jsonStr) | ||
} | ||
}, [jsonStr, jsonObj]) | ||
|
||
const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => { | ||
const value = e.target.value | ||
setInnerJsonValue(value) | ||
try { | ||
const newJson = JSON.parse(value) | ||
setError(undefined) | ||
} catch (e: any) { | ||
setError(e.message) | ||
} | ||
} | ||
|
||
const submit = () => { | ||
try { | ||
const newJson = JSON.parse(innerJsonValue) | ||
onChange(newJson) | ||
} catch (e: any) { | ||
setError(e.message) | ||
} | ||
} | ||
|
||
return ( | ||
<div> | ||
<textarea | ||
value={innerJsonValue} | ||
onChange={handleChange} | ||
style={{ | ||
width: '100%', | ||
height: '400px', | ||
border: !!error ? '3px solid var(--color-border-red)' : '1px solid #ccc', | ||
padding: '10px', | ||
fontFamily: | ||
'Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace', | ||
}} | ||
/> | ||
{error && ( | ||
<p> | ||
<em style={{ color: 'var(--color-text-red)' }}>{error}</em> | ||
</p> | ||
)} | ||
<p> | ||
<Button onClick={submit} disabled={!!error}> | ||
Apply | ||
</Button> | ||
</p> | ||
</div> | ||
) | ||
} |
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
Oops, something went wrong.