Skip to content

Commit 783ee1a

Browse files
committed
✨ Allow configuring @Ref('...')
At the moment, the `@Ref` decorator only allows the property to match the `ref` value. This change allows configuring the `ref`, similarly to `@Emit`.
1 parent 95ab564 commit 783ee1a

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

src/option/ref.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import type { Cons } from '../component'
22
import { type OptionBuilder, applyAccessors } from '../optionBuilder'
33
import { 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
})

src/utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { HookConfig } from "./option/methodsAndHooks";
77
import type { VModelConfig } from "./option/vmodel";
88
import type { WatchConfig } from "./option/watch";
99
import type { SetupConfig } from './option/setup'
10+
import type { RefConfig } from './option/ref';
1011
const SlotSymbol = Symbol('vue-facing-decorator-slot')
1112

1213
export 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) {

test/option/ref.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
import { expect } from 'chai';
33
import 'mocha';
44
import { Component, Ref, Base } from '../../dist'
5+
import {mount} from '@vue/test-utils';
56

67
@Component
78
export class Comp extends Base {
89
@Ref
910
readonly refName!: any
1011

12+
@Ref('override')
13+
readonly foo!: any
1114
}
1215
const 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 {}

0 commit comments

Comments
 (0)