feat(core): allow passing explicit refs via props

This commit is contained in:
Evan You
2019-11-06 12:51:06 -05:00
parent e79c918676
commit d9c6ff372c
7 changed files with 76 additions and 18 deletions

View File

@@ -1,5 +1,9 @@
import { isObject, toRawType } from '@vue/shared'
import { mutableHandlers, readonlyHandlers } from './baseHandlers'
import {
mutableHandlers,
readonlyHandlers,
readonlyPropsHandlers
} from './baseHandlers'
import {
mutableCollectionHandlers,
readonlyCollectionHandlers
@@ -80,6 +84,23 @@ export function readonly<T extends object>(
)
}
// @internal
// Return a readonly-copy of a props object, without unwrapping refs at the root
// level. This is intended to allow explicitly passing refs as props.
// Technically this should use different global cache from readonly(), but
// since it is only used on internal objects so it's not really necessary.
export function readonlyProps<T extends object>(
target: T
): Readonly<{ [K in keyof T]: UnwrapNestedRefs<T[K]> }> {
return createReactiveObject(
target,
rawToReadonly,
readonlyToRaw,
readonlyPropsHandlers,
readonlyCollectionHandlers
)
}
function createReactiveObject(
target: unknown,
toProxy: WeakMap<any, any>,