@sanity/assist 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -39,9 +39,6 @@ export function AssistDocumentForm(props: ObjectInputProps) {
|
|
|
39
39
|
const id = value?._id
|
|
40
40
|
const fields = value?.fields
|
|
41
41
|
|
|
42
|
-
// need this to not fire onChange twice in React strict mode
|
|
43
|
-
const onChangeCalled = useRef(false)
|
|
44
|
-
|
|
45
42
|
const targetDocumentType = useMemo(() => {
|
|
46
43
|
if (!id) {
|
|
47
44
|
return undefined
|
|
@@ -91,27 +88,7 @@ export function AssistDocumentForm(props: ObjectInputProps) {
|
|
|
91
88
|
}
|
|
92
89
|
}, [title, documentSchema, onChange, id])
|
|
93
90
|
|
|
94
|
-
const fieldExists = fields?.some((f) => f._key === pathKey)
|
|
95
|
-
useEffect(() => {
|
|
96
|
-
if (onChangeCalled.current || fieldExists || activePath || !pathKey) {
|
|
97
|
-
return
|
|
98
|
-
}
|
|
99
|
-
onChange([
|
|
100
|
-
setIfMissing([], ['fields']),
|
|
101
|
-
insert(
|
|
102
|
-
[
|
|
103
|
-
typed<AssistField>({
|
|
104
|
-
_key: pathKey,
|
|
105
|
-
_type: assistFieldTypeName,
|
|
106
|
-
path: pathKey,
|
|
107
|
-
}),
|
|
108
|
-
],
|
|
109
|
-
'after',
|
|
110
|
-
['fields', -1]
|
|
111
|
-
),
|
|
112
|
-
])
|
|
113
|
-
onChangeCalled.current = true
|
|
114
|
-
}, [activePath, onChange, pathKey, fieldExists])
|
|
91
|
+
const fieldExists = !!fields?.some((f) => f._key === pathKey)
|
|
115
92
|
|
|
116
93
|
const {onPathOpen, ...formCallbacks} = useFormCallbacks()
|
|
117
94
|
|
|
@@ -144,6 +121,13 @@ export function AssistDocumentForm(props: ObjectInputProps) {
|
|
|
144
121
|
return (
|
|
145
122
|
<SelectedFieldContextProvider value={context}>
|
|
146
123
|
<Stack space={5}>
|
|
124
|
+
<FieldsInitializer
|
|
125
|
+
key={pathKey}
|
|
126
|
+
pathKey={pathKey}
|
|
127
|
+
activePath={activePath}
|
|
128
|
+
fieldExists={fieldExists}
|
|
129
|
+
onChange={onChange}
|
|
130
|
+
/>
|
|
147
131
|
{instruction && <BackToInstructionListLink />}
|
|
148
132
|
|
|
149
133
|
{activePath && (
|
|
@@ -191,3 +175,40 @@ function useSelectedSchema(
|
|
|
191
175
|
return currentSchema
|
|
192
176
|
}, [documentSchema, fieldPath])
|
|
193
177
|
}
|
|
178
|
+
|
|
179
|
+
function FieldsInitializer({
|
|
180
|
+
pathKey,
|
|
181
|
+
activePath,
|
|
182
|
+
fieldExists,
|
|
183
|
+
onChange,
|
|
184
|
+
}: {
|
|
185
|
+
pathKey?: string
|
|
186
|
+
activePath?: Path
|
|
187
|
+
fieldExists: boolean
|
|
188
|
+
onChange: ObjectInputProps['onChange']
|
|
189
|
+
}) {
|
|
190
|
+
// need this to not fire onChange twice in React strict mode
|
|
191
|
+
const initialized = useRef(false)
|
|
192
|
+
useEffect(() => {
|
|
193
|
+
if (initialized.current || fieldExists || activePath || !pathKey) {
|
|
194
|
+
return
|
|
195
|
+
}
|
|
196
|
+
onChange([
|
|
197
|
+
setIfMissing([], ['fields']),
|
|
198
|
+
insert(
|
|
199
|
+
[
|
|
200
|
+
typed<AssistField>({
|
|
201
|
+
_key: pathKey,
|
|
202
|
+
_type: assistFieldTypeName,
|
|
203
|
+
path: pathKey,
|
|
204
|
+
}),
|
|
205
|
+
],
|
|
206
|
+
'after',
|
|
207
|
+
['fields', -1]
|
|
208
|
+
),
|
|
209
|
+
])
|
|
210
|
+
initialized.current = true
|
|
211
|
+
}, [activePath, onChange, pathKey, fieldExists])
|
|
212
|
+
|
|
213
|
+
return null
|
|
214
|
+
}
|