Skip to content

Commit 788b606

Browse files
committed
merge: Allow configuring @ref('...')
2 parents 7c5c910 + 783ee1a commit 788b606

File tree

7 files changed

+43
-8
lines changed

7 files changed

+43
-8
lines changed

changelog/v3.0.1.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1. Use class constructor name as default component name.
2+
3+
2. `Ref` decorator accepts `key` paramater. (doc updated)

docs/en/class-component/reference/code-usage.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Ref, Component, Vue, toNative } from 'vue-facing-decorator'
55
66
<template>
77
<div ref="refEl"></div>
8+
<div ref="fooEl"></div>
89
</template>
910
1011
*/
@@ -13,6 +14,9 @@ import { Ref, Component, Vue, toNative } from 'vue-facing-decorator'
1314
class MyComponent extends Vue {
1415
@Ref
1516
readonly refEl!: HTMLDivElement
17+
18+
@Ref('fooEl')
19+
refEl2!: HTMLDivElement
1620
}
1721

1822
export default toNative(MyComponent)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## Usage
22

3-
Use `Ref` decorator to define a property getter `this.$refs[name]` on vue component instance. The reference name is the name of property.
3+
Use `Ref` decorator to define a property getter `this.$refs[name]` on vue component instance. The reference name is the name of property or a passed name parameter.
44

55
[](./code-usage.ts ':include :type=code typescript')
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## 用法
22

3-
装饰器`Ref`在vue实力上定义一个指定引用`this.$refs[name]`的访问器。引用名是这个属性的名字
3+
装饰器`Ref`在vue实力上定义一个指定引用`this.$refs[name]`的访问器。引用名是这个属性的名字或者一个传入的名称作为参数
44

55
[](../../../en/class-component/reference/code-usage.ts ':include :type=code typescript')

src/option/ref.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import type { Cons } from '../component'
22
import { type OptionBuilder, applyAccessors } from '../optionBuilder'
33
import { obtainSlot, optoinNullableMemberDecorator } from '../utils'
4-
import { compatibleMemberDecorator } from '../deco3/utils'
5-
export const decorator = optoinNullableMemberDecorator(function (proto: any, name: string) {
4+
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

1214
export function build(cons: Cons, optionBuilder: OptionBuilder) {
1315
const slot = obtainSlot(cons.prototype)
16+
1417
const names = slot.getMap('ref')
1518
if (!names || names.size === 0) {
1619
return
@@ -19,11 +22,13 @@ export function build(cons: Cons, optionBuilder: OptionBuilder) {
1922
applyAccessors(optionBuilder, (ctx: any) => {
2023
const data: Map<string, { get: () => any, set: undefined }> = new Map
2124
names.forEach((value, name) => {
25+
const refKey = value === null ? name : value
2226
data.set(name, {
2327
get: function (this: any) {
24-
return ctx.$refs[name]
28+
return ctx.$refs[refKey]
2529
},
2630
set: undefined
31+
2732
})
2833
})
2934
return data

src/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import type { VModelConfig } from "./option/vmodel";
88
import type { WatchConfig } from "./option/watch";
99
import type { SetupConfig } from './option/setup'
1010
import type { Record as CustomDecoratorRecord } from './custom/custom'
11+
import type { RefConfig } from './option/ref';
1112
import { compatibleMemberDecorator } from './deco3/utils';
13+
1214
const SlotSymbol = Symbol('vue-facing-decorator-slot')
1315

1416
export type SlotMapTypes = {
@@ -21,7 +23,7 @@ export type SlotMapTypes = {
2123
hooks: Map<string, HookConfig>
2224
'v-model': Map<string, VModelConfig>
2325
watch: Map<string, WatchConfig | WatchConfig[]>
24-
ref: Map<string, boolean>
26+
ref: Map<string, RefConfig>
2527
setup: Map<string, SetupConfig>
2628
customDecorator: Map<string, CustomDecoratorRecord>
2729
}

test/option/ref.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11

22
import { expect } from 'chai';
33
import 'mocha';
4+
45
import { Component, Ref, Base, toNative } from '../../dist'
56

7+
import { mount } from '@vue/test-utils';
8+
9+
610
@Component
711
class Comp extends Base {
812
@Ref
913
readonly refName!: any
1014

15+
@Ref('override')
16+
readonly foo!: any
1117
}
1218
const CompContext = toNative(Comp) as any
1319

@@ -16,7 +22,22 @@ describe('decorator Ref',
1622
it('default', () => {
1723
expect('function').to.equal(typeof CompContext?.beforeCreate)
1824
})
25+
26+
it('points to an element', () => {
27+
const component = mount({
28+
...CompContext,
29+
template: `
30+
<div>
31+
<div ref="refName">ref content</div>
32+
<div ref="override">foo</div>
33+
</div>
34+
`,
35+
})
36+
37+
expect(component.vm.refName.textContent).to.equal('ref content');
38+
expect(component.vm.foo.textContent).to.equal('foo');
39+
});
1940
}
2041
)
2142

22-
export default {}
43+
export default {}

0 commit comments

Comments
 (0)