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'
2
2
import { type OptionBuilder , applyAccessors } from '../optionBuilder'
3
3
import { obtainSlot , optoinNullableMemberDecorator } from '../utils'
4
4
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 ) {
6
8
const slot = obtainSlot ( proto )
7
9
const map = slot . obtainMap ( 'ref' )
8
- map . set ( name , true )
10
+ map . set ( name , typeof key === 'undefined' ? null : key )
9
11
} )
10
12
11
13
@@ -16,9 +18,10 @@ export function build(cons: Cons, optionBuilder: OptionBuilder) {
16
18
applyAccessors ( optionBuilder , ( ctx : any ) => {
17
19
const data : Map < string , { get : ( ) => any , set : undefined } > = new Map
18
20
names . forEach ( ( value , name ) => {
21
+ const refKey = value === null ? name : value
19
22
data . set ( name , {
20
23
get : function ( this : any ) {
21
- return ctx . $refs [ name ]
24
+ return ctx . $refs [ refKey ]
22
25
} ,
23
26
set : undefined
24
27
} )
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import type { HookConfig } from "./option/methodsAndHooks";
7
7
import type { VModelConfig } from "./option/vmodel" ;
8
8
import type { WatchConfig } from "./option/watch" ;
9
9
import type { SetupConfig } from './option/setup'
10
+ import type { RefConfig } from './option/ref' ;
10
11
const SlotSymbol = Symbol ( 'vue-facing-decorator-slot' )
11
12
12
13
export type SlotMapTypes = {
@@ -19,7 +20,7 @@ export type SlotMapTypes = {
19
20
hooks : Map < string , HookConfig >
20
21
'v-model' : Map < string , VModelConfig >
21
22
watch : Map < string , WatchConfig | WatchConfig [ ] >
22
- ref : Map < string , boolean >
23
+ ref : Map < string , RefConfig >
23
24
setup : Map < string , SetupConfig >
24
25
}
25
26
@@ -123,7 +124,7 @@ export function getSuperSlot(obj: any) {
123
124
// }
124
125
// return arr
125
126
// }
126
- // export function
127
+ // export function
127
128
// export function collect<>(slot: Slot,mapName:keyof SlotMapTypes,) {
128
129
// let currSlot: Slot | null = slot
129
130
// while (currSlot != null) {
Original file line number Diff line number Diff line change 2
2
import { expect } from 'chai' ;
3
3
import 'mocha' ;
4
4
import { Component , Ref , Base } from '../../dist'
5
+ import { mount } from '@vue/test-utils' ;
5
6
6
7
@Component
7
8
export class Comp extends Base {
8
9
@Ref
9
10
readonly refName ! : any
10
11
12
+ @Ref ( 'override' )
13
+ readonly foo ! : any
11
14
}
12
15
const CompContext = Comp as any
13
16
@@ -16,7 +19,22 @@ describe('decorator Ref',
16
19
it ( 'default' , ( ) => {
17
20
expect ( 'function' ) . to . equal ( typeof CompContext ?. beforeCreate )
18
21
} )
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
+ } ) ;
19
37
}
20
38
)
21
39
22
- export default { }
40
+ export default { }
You can’t perform that action at this time.
0 commit comments