-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathview.tsx
More file actions
41 lines (35 loc) · 1.05 KB
/
view.tsx
File metadata and controls
41 lines (35 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import * as React from 'react'
import Markdown from 'react-markdown'
interface Props {
markdown: string;
onSave: (newMarkdown: string) => Promise<void>;
}
export const View: React.FC<Props> = (props) => {
const [phase, setPhase] = React.useState<'saving' | 'rendering' | 'editing'>('rendering')
const [rawText, setRawText] = React.useState(props.markdown)
function storeMarkdown () {
setPhase('saving')
props.onSave(rawText).then(() => {
setPhase('rendering')
})
}
if (phase === 'saving') {
return <span aria-busy={true}>Loading…</span>
}
if (phase === 'editing') {
return (
<form onSubmit={(e) => { e.preventDefault(); storeMarkdown() }}>
<textarea
onChange={(e) => { setRawText(e.target.value) }}
defaultValue={rawText}/>
<button type="submit">RENDER</button>,
</form>
)
}
return (
<form onSubmit={(event) => { event.preventDefault(); setPhase('editing') }}>
<Markdown source={rawText}/>
<button type="submit">EDIT</button>
</form>
)
}