Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core): multiple ref elements with the same name #12268

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions packages/runtime-core/__tests__/helpers/useTemplateRef.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,28 @@ describe('useTemplateRef', () => {
__DEV__ = true
}
})

// 12246
test('should set last element if multiple with same key are defined', () => {
let tRef: ShallowRef
let key = 'refKey'

const Comp = {
setup() {
tRef = useTemplateRef(key)
},
render() {
return h('div', [
h('div', { ref: key }),
h('div', { ref: key }),
h('span', { ref: key }),
])
},
}

const root = nodeOps.createElement('div')
render(h(Comp), root)

expect(tRef!.value.tag).toBe('span')
})
})
24 changes: 24 additions & 0 deletions packages/runtime-core/__tests__/rendererTemplateRef.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
type ShallowRef,
defineComponent,
h,
nextTick,
Expand Down Expand Up @@ -538,4 +539,27 @@ describe('api: template refs', () => {
'<div><div>[object Object],[object Object]</div><ul><li>2</li><li>3</li></ul></div>',
)
})

// 12246
test('should set last element if multiple with same key are defined', () => {
let refKey: ShallowRef

const Comp = {
setup() {
refKey = ref()
},
render() {
return h('div', [
h('div', { ref: refKey }),
h('div', { ref: refKey }),
h('span', { ref: refKey }),
])
},
}

const root = nodeOps.createElement('div')
render(h(Comp), root)

expect(refKey!.value.tag).toBe('span')
})
})
16 changes: 10 additions & 6 deletions packages/runtime-core/src/rendererTemplateRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,11 @@ export function setRef(
} else {
if (!isArray(existing)) {
if (_isString) {
refs[ref] = [refValue]
if (canSetSetupRef(ref)) {
setupState[ref] = refs[ref]
if (oldRef !== ref) {
refs[ref] = [refValue]
if (canSetSetupRef(ref)) {
setupState[ref] = refs[ref]
}
}
} else {
ref.value = [refValue]
Expand All @@ -128,9 +130,11 @@ export function setRef(
}
}
} else if (_isString) {
refs[ref] = value
if (canSetSetupRef(ref)) {
setupState[ref] = value
if (oldRef !== ref) {
Copy link
Member

@edison1105 edison1105 Oct 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this solution is OK.
Maybe we can issue a runtime warning when oldRef === ref.

refs[ref] = value
if (canSetSetupRef(ref)) {
setupState[ref] = value
}
}
} else if (_isRef) {
ref.value = value
Expand Down