Skip to content

Commit 7bac743

Browse files
committed
fix: should watch modelValue/value after onChange is available
1 parent 9763ccb commit 7bac743

File tree

1 file changed

+71
-71
lines changed

1 file changed

+71
-71
lines changed

src/Component.ts

Lines changed: 71 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -118,77 +118,6 @@ export default defineComponent({
118118
},
119119
)
120120

121-
watch(
122-
() => props[modelValueProp],
123-
(newModelValue: any) => {
124-
if (preventUpdatingContent.value) {
125-
preventUpdatingContent.value = false
126-
return
127-
}
128-
if (jsonEditor.value) {
129-
preventUpdatingModelValue.value = true
130-
jsonEditor.value.set(
131-
[undefined, ''].includes(newModelValue)
132-
// `undefined` is not accepted by vanilla-jsoneditor
133-
// The default value is `{ text: '' }`
134-
// Only default value can clear the editor
135-
? { text: '' }
136-
: { json: newModelValue },
137-
)
138-
}
139-
},
140-
{
141-
deep: true,
142-
},
143-
)
144-
145-
watch(
146-
() => props.mode,
147-
(mode) => {
148-
// `jsonEditor.value` could be `undefined` in Vue 2.6
149-
jsonEditor.value?.updateProps({
150-
mode,
151-
})
152-
},
153-
)
154-
155-
watch(
156-
() => Array.from(boolAttrs, boolAttr => props[boolAttr]),
157-
(values) => {
158-
jsonEditor.value?.updateProps(
159-
Object.fromEntries(Array.from(values, (v, i) => [boolAttrs[i], v]).filter(([, v]) => v !== undefined)),
160-
)
161-
},
162-
)
163-
164-
watch(
165-
() => attrs,
166-
(newAttrs) => {
167-
// Functions need to be merged again
168-
const defaultFunctionAttrs: {
169-
onChange?: (...args: any) => unknown
170-
onChangeMode?: (...args: any) => unknown
171-
} = {}
172-
if (newAttrs.onChange) {
173-
defaultFunctionAttrs.onChange = onChange
174-
}
175-
if (newAttrs.onChangeMode) {
176-
defaultFunctionAttrs.onChangeMode = onChangeMode
177-
}
178-
jsonEditor.value?.updateProps(
179-
Object.getOwnPropertyNames(defaultFunctionAttrs).length > 0
180-
? conclude([newAttrs, defaultFunctionAttrs], {
181-
type: Object,
182-
mergeFunction,
183-
})
184-
: newAttrs,
185-
)
186-
},
187-
{
188-
deep: true,
189-
},
190-
)
191-
192121
expose?.({ jsonEditor })
193122

194123
onUnmounted(() => {
@@ -201,6 +130,77 @@ export default defineComponent({
201130
props: initialAttrs,
202131
})
203132

133+
watch(
134+
() => props[modelValueProp],
135+
(newModelValue: any) => {
136+
if (preventUpdatingContent.value) {
137+
preventUpdatingContent.value = false
138+
return
139+
}
140+
if (jsonEditor.value) {
141+
preventUpdatingModelValue.value = true
142+
jsonEditor.value.set(
143+
[undefined, ''].includes(newModelValue)
144+
// `undefined` is not accepted by vanilla-jsoneditor
145+
// The default value is `{ text: '' }`
146+
// Only default value can clear the editor
147+
? { text: '' }
148+
: { json: newModelValue },
149+
)
150+
}
151+
},
152+
{
153+
deep: true,
154+
},
155+
)
156+
157+
watch(
158+
() => props.mode,
159+
(mode) => {
160+
// `jsonEditor.value` could be `undefined` in Vue 2.6
161+
jsonEditor.value?.updateProps({
162+
mode,
163+
})
164+
},
165+
)
166+
167+
watch(
168+
() => Array.from(boolAttrs, boolAttr => props[boolAttr]),
169+
(values) => {
170+
jsonEditor.value?.updateProps(
171+
Object.fromEntries(Array.from(values, (v, i) => [boolAttrs[i], v]).filter(([, v]) => v !== undefined)),
172+
)
173+
},
174+
)
175+
176+
watch(
177+
() => attrs,
178+
(newAttrs) => {
179+
// Functions need to be merged again
180+
const defaultFunctionAttrs: {
181+
onChange?: (...args: any) => unknown
182+
onChangeMode?: (...args: any) => unknown
183+
} = {}
184+
if (newAttrs.onChange) {
185+
defaultFunctionAttrs.onChange = onChange
186+
}
187+
if (newAttrs.onChangeMode) {
188+
defaultFunctionAttrs.onChangeMode = onChangeMode
189+
}
190+
jsonEditor.value?.updateProps(
191+
Object.getOwnPropertyNames(defaultFunctionAttrs).length > 0
192+
? conclude([newAttrs, defaultFunctionAttrs], {
193+
type: Object,
194+
mergeFunction,
195+
})
196+
: newAttrs,
197+
)
198+
},
199+
{
200+
deep: true,
201+
},
202+
)
203+
204204
// There's no `expose` in @vue/composition-api
205205
if (!expose) {
206206
expose = (exposed: Record<string, any> | undefined): void => {

0 commit comments

Comments
 (0)