Skip to content

Commit

Permalink
fix(useVModel): return default value when it's nil only (#1567)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmkx committed May 5, 2022
1 parent f31de43 commit 50d8a4c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"publish:ci": "esno scripts/publish.ts",
"install-fonts": "gfi install Inter && gfi install Fira Code",
"release": "esno scripts/release.ts && git push --follow-tags",
"size": " esno scripts/export-size.ts",
"size": "esno scripts/export-size.ts",
"test": "nr test:3",
"test:2": "vue-demi-switch 2 vue2 && vitest run --silent",
"test:3": "vue-demi-switch 3 && vitest run",
Expand Down
12 changes: 12 additions & 0 deletions packages/core/useVModel/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,22 @@ describe('useVModel', () => {
it('should work with user define defaultValue', () => {
const props = {
...defaultProps(),
data: undefined as string | undefined,
a: 0,
b: '',
c: false,
}
const emitMock = vitest.fn()

const data = useVModel(props, 'data', emitMock, { defaultValue: 'default-data' })
const dataA = useVModel(props, 'a', emitMock)
const dataB = useVModel(props, 'b', emitMock)
const dataC = useVModel(props, 'c', emitMock)

expect(data.value).toBe('default-data')
expect(dataA.value).toBe(0)
expect(dataB.value).toBe('')
expect(dataC.value).toBe(false)
})

it('should work with user define defaultValue with passive', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/useVModel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function useVModel<P extends object, K extends keyof P, Name extends stri
event = eventName || event || `update:${key}`

if (passive) {
const proxy = ref<P[K]>(props[key!] || defaultValue!)
const proxy = ref<P[K]>(props[key!] ?? defaultValue!)

watch(() => props[key!], v => proxy.value = v as UnwrapRef<P[K]>)

Expand Down

0 comments on commit 50d8a4c

Please sign in to comment.