Skip to content

Commit bb82205

Browse files
Fix merging of ref props in addProps (#3)
1 parent b76cd58 commit bb82205

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

Diff for: src/__tests__/vue-vnode-utils.spec.ts

+44
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
Fragment,
1111
h,
1212
isVNode,
13+
ref,
1314
Text,
1415
type VNode,
1516
type VNodeArrayChildren,
@@ -422,6 +423,49 @@ describe('addProps', () => {
422423
expect(fragment[0]).toBe(spanNode)
423424
expect(spanNode.props).toBe(null)
424425
})
426+
427+
it('addProps - 8259', () => {
428+
let count = 0
429+
430+
const spanRef = ref()
431+
const otherRef = ref()
432+
const spanNode = h('span', { ref: spanRef, class: 'bold', style: 'color: red' })
433+
const startNodes = [spanNode]
434+
435+
const nodes = addProps(startNodes, () => {
436+
count++
437+
438+
return {
439+
ref: otherRef,
440+
class: 'large',
441+
style: 'line-height: 1'
442+
}
443+
})
444+
445+
expect(count).toBe(1)
446+
447+
expect(nodes).toHaveLength(1)
448+
449+
const newNode = nodes[0] as VNode
450+
const props = newNode.props
451+
452+
expect(props?.class).toBe('bold large')
453+
expect(props?.style.color).toBe('red')
454+
expect(props?.style['line-height']).toBe('1')
455+
456+
const mergedRef = newNode.ref
457+
458+
expect(Array.isArray(mergedRef)).toBe(true)
459+
expect(mergedRef).toHaveLength(2)
460+
461+
// Keep TS happy...
462+
if (Array.isArray(mergedRef)) {
463+
const refs = mergedRef.map(({ r }) => r)
464+
465+
expect(refs.includes(spanRef)).toBe(true)
466+
expect(refs.includes(otherRef)).toBe(true)
467+
}
468+
})
425469
})
426470

427471
describe('replaceChildren', () => {

Diff for: src/vue-vnode-utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ export const addProps = (
232232
}
233233

234234
if (props && !isEmptyObject(props)) {
235-
return cloneVNode(vnode, props)
235+
return cloneVNode(vnode, props, true)
236236
}
237237
}, options)
238238
}

0 commit comments

Comments
 (0)