-
Notifications
You must be signed in to change notification settings - Fork 411
/
SchemaForm.js
235 lines (210 loc) · 6.49 KB
/
SchemaForm.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// @flow
import React from 'react'
import styles from './styles/SchemaForm.css'
import Header from './Header'
import Inspector from './Inspector'
import {save, restore} from '../lib/persist'
import sourceSchemas from '../schemas'
import Schema from '@sanity/schema'
import {FormBuilder} from '../../../src'
import {parseParams, preventDefault} from '../lib/utils'
import MyCustomLatLonInput from './custom/MyCustomLatLonInput'
import MyCustomValidationList from './custom/MyCustomValidationList'
import MyCustomImageInput from './custom/MyCustomImageInput'
import MyCustomFileInput from './custom/MyCustomFileInput'
import MyCustomSlugInput from './custom/MyCustomSlugInput'
import applyPatch from '../../../src/simplePatch'
import resolveReferenceInput from './custom/resolveReferenceInput'
import {arrayToJSONMatchPath} from '@sanity/mutator'
import InputWithCustomState from './custom/InputWithCustomState'
import {set, unset} from '../../../src/utils/patches'
import {resolvePreviewComponent} from '../../../src/defaultConfig'
const SCHEMA_NAMES = Object.keys(sourceSchemas)
const params = parseParams(document.location.pathname)
const schema =
params.schemaName && params.typeName && Schema.compile(sourceSchemas[params.schemaName])
const PERSISTKEY = `form-builder-value-${params.schemaName}-${params.typeName}`
const schemaType = schema && schema.get(params.typeName)
function logPatch(patch) {
const {type, path, ...rest} = patch
console.log(
// eslint-disable-line no-console
'%c%s%c %s =>',
'color:#2097ac',
type,
'color:inherit',
arrayToJSONMatchPath(path || []),
rest
)
}
function resolveInputComponent(type) {
if (type.component) {
return type.component
}
if (type.name === 'latlon') {
return MyCustomLatLonInput
}
if (type.name === 'reference') {
return resolveReferenceInput(type)
}
if (type.name === 'image') {
return MyCustomImageInput
}
if (type.name === 'customPatchHandlingExampleType') {
return InputWithCustomState
}
if (type.name === 'file') {
return MyCustomFileInput
}
if (type.name === 'slug') {
return MyCustomSlugInput
}
return type.inputComponent || undefined // signal to use default
}
function resolveValidationComponent() {
return MyCustomValidationList
}
export default class Main extends React.Component {
state = {
inspect: false,
value: restore(PERSISTKEY),
saved: false
}
patchChannel = FormBuilder.createPatchChannel()
componentDidUpdate(prevProps, prevState) {
if (prevState.inspect !== this.state.inspect) {
document.body.style.overflow = this.state.inspect === 'fullscreen' ? 'hidden' : ''
}
}
handleChange = event => {
this.receivePatches(event.patches)
}
receivePatches(patches) {
const nextValue = patches.reduce((prev, patch) => applyPatch(prev, patch), this.state.value)
this.patchChannel.receivePatches({patches, snapshot: nextValue})
this.setState({
value: nextValue,
saved: false
})
}
cmdSave(event) {
save(PERSISTKEY, this.state.value)
this.setState({saved: true})
}
cmdLog(event) {
console.log(this.state.value) // eslint-disable-line no-console
}
cmdClear(event) {
this.receivePatches([unset()])
}
cmdRevert(event) {
this.receivePatches([set(restore(PERSISTKEY))])
}
cmdInspectLive(event) {
this.setState({inspect: event.currentTarget.checked ? 'docked' : false})
}
handleDispatchCommand = event => {
const command = event.currentTarget.getAttribute('data-cmd')
const methodName = `cmd${command}`
if (typeof this[methodName] !== 'function') {
throw new Error(`Invalid command: ${JSON.stringify(command)}`)
}
this[methodName](event)
}
CommandButton = props => {
return (
<button
type="button"
className={styles.toolbarButton}
data-cmd={props.command}
onClick={this.handleDispatchCommand}
>
{props.children}
</button>
)
}
renderToolbar() {
const {inspect, saved} = this.state
return (
<div className={styles.toolbar}>
<this.CommandButton command="Save">{saved ? '✓ Saved' : ' Save'}</this.CommandButton>
<this.CommandButton command="Revert">Load saved</this.CommandButton>
<this.CommandButton command="Clear">Clear</this.CommandButton>
<this.CommandButton command="Log">console.log value</this.CommandButton>{' '}
<label>
<input
data-cmd="InspectLive"
checked={inspect}
type="checkbox"
onChange={this.handleDispatchCommand}
/>{' '}
Live inspection
</label>
</div>
)
}
renderInspect() {
const {value, inspect} = this.state
return (
<div className={styles[inspect === 'docked' ? 'inspectPane' : 'inspectPaneFullScreen']}>
<button
className={styles.closeInspectPaneButton}
onClick={() => this.setState({inspect: false})}
>
x
</button>
{inspect === 'docked' && (
<button
className={styles.fullscreenInspectPaneButton}
onClick={() => this.setState({inspect: 'fullscreen'})}
>
↑
</button>
)}
{inspect === 'fullscreen' && (
<button
className={styles.dockedInspectPaneButton}
onClick={() => this.setState({inspect: 'docked'})}
>
↓
</button>
)}
<div
className={
styles[inspect === 'docked' ? 'inspectPaneInner' : 'inspectPaneInnerFullScreen']
}
>
<Inspector inspect={value} />
</div>
</div>
)
}
render() {
const {value, inspect, validation} = this.state
return (
<div className={styles.root}>
<Header
schemaNames={SCHEMA_NAMES}
typeNames={schema && schema.getTypeNames()}
selectedSchemaName={params.schemaName}
selectedTypeName={params.typeName}
/>
{this.renderToolbar()}
<div className={styles.inner}>
<form onSubmit={preventDefault}>
<FormBuilder
patchChannel={this.patchChannel}
resolveInputComponent={resolveInputComponent}
resolvePreviewComponent={resolvePreviewComponent}
value={value}
type={schemaType}
validation={validation}
onChange={this.handleChange}
/>
</form>
</div>
{inspect && this.renderInspect()}
</div>
)
}
}