-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheditor.tsx
65 lines (56 loc) · 1.79 KB
/
editor.tsx
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import * as React from 'react'
import cn from 'classnames'
import { HTML5Backend } from 'react-dnd-html5-backend'
import { DndProvider } from 'react-dnd'
import { Sidebar } from './editor/sidebar'
import { useProvidedStoreContext, StoreContext } from './editor/store'
import { Form } from './editor/models'
import { FormElements } from './editor/form'
import { SerializedForm } from './data'
const FormPanel: React.FC = () => {
return (
<div className="form-panel">
<FormElements />
</div>
)
}
export interface FormRef {
readonly form: Form
clear(): void
update(form: SerializedForm): void
}
export interface EditorProps {
className?: string
defaultValue?: SerializedForm
formRef?: React.MutableRefObject<FormRef | null>
}
export const EditorPanel: React.FC<EditorProps> = ({
className, formRef, defaultValue,
}) => {
const ctx = useProvidedStoreContext(defaultValue)
React.useEffect(() => {
if (formRef) {
formRef.current = {
get form() { return ctx.store.form },
clear() { ctx.dispatch({ type: 'CLEAR' }) },
update(form: SerializedForm) {
ctx.dispatch({ type: 'REPLACE_FORM', form, controls: ctx.store.controls })
},
}
}
return () => {
if (formRef) { formRef.current = null }
}
}, [formRef, ctx.store.form])
return (
<StoreContext.Provider value={ctx}>
<div className={cn('formial-editor', className)}>
<FormPanel />
<Sidebar />
</div>
</StoreContext.Provider>
)
}
export const Editor: React.FC<EditorProps> = ( props ) => {
return <DndProvider backend={HTML5Backend}><EditorPanel {...props} /></DndProvider>
}