Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(runtime-core): unwrap nested refs on the component #738

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions packages/runtime-core/__tests__/apiSetupContext.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,30 @@ describe('api: setup context', () => {
// object exposed as-is
object: reactive({ msg: 'bar' }),
// primitive value exposed as-is
value: 'baz'
value: 'baz',
// nested ref at the end should auto-unwrap
nested: { ref: ref('qux') },
// ref with nested should auto-unwrap
refNested: ref({
nested: {
ref: ref('foo')
}
}),
// nested ref in the middle should auto-unwrap
refMiddleNested: {
nested: ref({
msg: 'bar'
})
}
}
},
render() {
return `${this.ref} ${this.object.msg} ${this.value}`
return `${this.ref} ${this.object.msg} ${this.value} ${
this.nested.ref
} ${this.refNested.nested.ref} ${this.refMiddleNested.nested.msg}`
}
})
expect(renderToString(h(Comp))).toMatch(`foo bar baz`)
expect(renderToString(h(Comp))).toMatch(`foo bar baz qux foo bar`)
})

it('should support returning render function', () => {
Expand Down
19 changes: 16 additions & 3 deletions packages/runtime-core/src/componentProxy.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import { ComponentInternalInstance, Data, Emit } from './component'
import { nextTick, queueJob } from './scheduler'
import { instanceWatch } from './apiWatch'
import { EMPTY_OBJ, hasOwn, isGloballyWhitelisted, NOOP } from '@vue/shared'
import {
EMPTY_OBJ,
hasOwn,
isGloballyWhitelisted,
NOOP,
isObject
} from '@vue/shared'
import {
ExtractComputedReturns,
ComponentOptionsBase,
ComputedOptions,
MethodOptions
} from './apiOptions'
import { UnwrapRef, ReactiveEffect, isRef, isReactive } from '@vue/reactivity'
import {
UnwrapRef,
ReactiveEffect,
isRef,
isReactive,
reactive
} from '@vue/reactivity'
import { warn } from './warning'
import { Slots } from './componentSlots'
import {
Expand Down Expand Up @@ -72,7 +84,8 @@ const enum AccessTypes {
OTHER
}

const unwrapRef = (val: unknown) => (isRef(val) ? val.value : val)
const unwrapRef = (val: unknown) =>
isRef(val) ? val.value : isObject(val) ? reactive(val) : val

export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
get(target: ComponentInternalInstance, key: string) {
Expand Down