Skip to content

Commit 7b4e800

Browse files
committed
fix(useProxyModel): sync model back when apply rejects
1 parent c0710c7 commit 7b4e800

File tree

1 file changed

+18
-5
lines changed
  • packages/0/src/composables/useProxyModel

1 file changed

+18
-5
lines changed

packages/0/src/composables/useProxyModel/index.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515

1616
// Utilities
17-
import { isFunction } from '#v0/utilities'
17+
import { isArray, isFunction } from '#v0/utilities'
1818
import { onScopeDispose, toValue, watch } from 'vue'
1919

2020
// Transformers
@@ -102,20 +102,33 @@ export function useProxyModel (
102102
}
103103
}
104104

105+
let syncing = false
106+
107+
function shallowEqual (a: unknown, b: unknown): boolean {
108+
if (a === b) return true
109+
if (!isArray(a) || !isArray(b) || a.length !== b.length) return false
110+
return a.every((v, index) => v === b[index])
111+
}
112+
105113
const contextWatch = watch(context.selectedValues as Ref, val => {
106-
modelWatch.pause()
114+
if (syncing) return
107115

116+
modelWatch.pause()
108117
model.value = transformOut(Array.from(toValue(val)))
109-
110118
modelWatch.resume()
111119
}, { flush: 'sync' })
112120

113121
const modelWatch = watch(model, val => {
114-
contextWatch.pause()
122+
if (syncing) return
115123

124+
syncing = true
125+
contextWatch.pause()
116126
context.apply(transformIn(val), applyOptions)
117-
127+
// Sync model back to actual selection state (apply may have rejected due to disabled/mandatory)
128+
const actual = transformOut(Array.from(context.selectedValues.value))
129+
if (!shallowEqual(val, actual)) model.value = actual
118130
contextWatch.resume()
131+
syncing = false
119132
}, { flush: 'sync', deep: toValue(multiple) })
120133

121134
function onRegister (data: unknown) {

0 commit comments

Comments
 (0)