Skip to content

Commit 2d2990d

Browse files
committed
+ Add v-model support on Vue component.
1 parent e73f7f7 commit 2d2990d

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

lib/directives/v-model.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,17 @@ const dealWithVModel = (
181181

182182
// v-model on component.
183183
if (!isHTMLElement) {
184-
// TODO: Add component support.
184+
const instance = getCurrentInstance()
185+
vNodeData.props.value = isString(bindingTarget)
186+
? instance[bindingTarget]
187+
: bindingTarget.value
188+
vNodeData.on.input = (value) => {
189+
if (isString(bindingTarget)) {
190+
instance[bindingTarget] = value
191+
} else {
192+
bindingTarget.value = value
193+
}
194+
}
185195
}
186196
}
187197

test/v-model.composition.spec.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,17 @@ describe('v-model composition API testing.', () => {
108108
expect(checkboxInputElement.checked).toBe(false)
109109

110110
// input -> value
111+
checkboxInputElement.checked = true
111112
checkboxInput.trigger('change')
112113
await sleep(10)
113-
expect(userInputRef.value).toBe('on')
114114
expect(checkboxInputElement.checked).toBe(true)
115+
expect(userInputRef.value).toBe('on')
115116

117+
checkboxInputElement.checked = false
116118
checkboxInput.trigger('change')
117119
await sleep(10)
118-
expect(userInputRef.value).toBe(undefined)
119120
expect(checkboxInputElement.checked).toBe(false)
121+
expect(userInputRef.value).toBe(undefined)
120122

121123
// value -> input
122124
userInputRef.value = 'on'

test/v-model.render.spec.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,13 @@ describe('v-model render function testing.', () => {
117117
expect(checkboxInputElement.checked).toBe(false)
118118

119119
// input -> value
120+
checkboxInputElement.checked = true
120121
checkboxInput.trigger('change')
121122
await sleep(10)
122123
expect(vm.userInput).toBe('on')
123124
expect(checkboxInputElement.checked).toBe(true)
124125

126+
checkboxInputElement.checked = false
125127
checkboxInput.trigger('change')
126128
await sleep(10)
127129
expect(vm.userInput).toBe(undefined)

0 commit comments

Comments
 (0)