File tree Expand file tree Collapse file tree 3 files changed +28
-6
lines changed Expand file tree Collapse file tree 3 files changed +28
-6
lines changed Original file line number Diff line number Diff line change @@ -2,10 +2,12 @@ import type { Cons } from '../component'
22import { type OptionBuilder , applyAccessors } from '../optionBuilder'
33import { obtainSlot , optoinNullableMemberDecorator } from '../utils'
44
5- export const decorator = optoinNullableMemberDecorator ( function ( proto : any , name : string ) {
5+ export type RefConfig = null | string
6+
7+ export const decorator = optoinNullableMemberDecorator ( function ( proto : any , name : string , key ?: string ) {
68 const slot = obtainSlot ( proto )
79 const map = slot . obtainMap ( 'ref' )
8- map . set ( name , true )
10+ map . set ( name , typeof key === 'undefined' ? null : key )
911} )
1012
1113
@@ -16,9 +18,10 @@ export function build(cons: Cons, optionBuilder: OptionBuilder) {
1618 applyAccessors ( optionBuilder , ( ctx : any ) => {
1719 const data : Map < string , { get : ( ) => any , set : undefined } > = new Map
1820 names . forEach ( ( value , name ) => {
21+ const refKey = value === null ? name : value
1922 data . set ( name , {
2023 get : function ( this : any ) {
21- return ctx . $refs [ name ]
24+ return ctx . $refs [ refKey ]
2225 } ,
2326 set : undefined
2427 } )
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import type { HookConfig } from "./option/methodsAndHooks";
77import type { VModelConfig } from "./option/vmodel" ;
88import type { WatchConfig } from "./option/watch" ;
99import type { SetupConfig } from './option/setup'
10+ import type { RefConfig } from './option/ref' ;
1011const SlotSymbol = Symbol ( 'vue-facing-decorator-slot' )
1112
1213export type SlotMapTypes = {
@@ -19,7 +20,7 @@ export type SlotMapTypes = {
1920 hooks : Map < string , HookConfig >
2021 'v-model' : Map < string , VModelConfig >
2122 watch : Map < string , WatchConfig | WatchConfig [ ] >
22- ref : Map < string , boolean >
23+ ref : Map < string , RefConfig >
2324 setup : Map < string , SetupConfig >
2425}
2526
@@ -123,7 +124,7 @@ export function getSuperSlot(obj: any) {
123124// }
124125// return arr
125126// }
126- // export function
127+ // export function
127128// export function collect<>(slot: Slot,mapName:keyof SlotMapTypes,) {
128129// let currSlot: Slot | null = slot
129130// while (currSlot != null) {
Original file line number Diff line number Diff line change 22import { expect } from 'chai' ;
33import 'mocha' ;
44import { Component , Ref , Base } from '../../dist'
5+ import { mount } from '@vue/test-utils' ;
56
67@Component
78export class Comp extends Base {
89 @Ref
910 readonly refName ! : any
1011
12+ @Ref ( 'override' )
13+ readonly foo ! : any
1114}
1215const CompContext = Comp as any
1316
@@ -16,7 +19,22 @@ describe('decorator Ref',
1619 it ( 'default' , ( ) => {
1720 expect ( 'function' ) . to . equal ( typeof CompContext ?. beforeCreate )
1821 } )
22+
23+ it ( 'points to an element' , ( ) => {
24+ const component = mount ( {
25+ ...CompContext ,
26+ template : `
27+ <div>
28+ <div ref="refName">ref content</div>
29+ <div ref="override">foo</div>
30+ </div>
31+ ` ,
32+ } )
33+
34+ expect ( component . vm . refName . textContent ) . to . equal ( 'ref content' ) ;
35+ expect ( component . vm . foo . textContent ) . to . equal ( 'foo' ) ;
36+ } ) ;
1937 }
2038)
2139
22- export default { }
40+ export default { }
You can’t perform that action at this time.
0 commit comments