Skip to content

Commit

Permalink
Fix merging of ref props in addProps (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
skirtles-code authored Nov 18, 2022
1 parent b76cd58 commit bb82205
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
44 changes: 44 additions & 0 deletions src/__tests__/vue-vnode-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Fragment,
h,
isVNode,
ref,
Text,
type VNode,
type VNodeArrayChildren,
Expand Down Expand Up @@ -422,6 +423,49 @@ describe('addProps', () => {
expect(fragment[0]).toBe(spanNode)
expect(spanNode.props).toBe(null)
})

it('addProps - 8259', () => {
let count = 0

const spanRef = ref()
const otherRef = ref()
const spanNode = h('span', { ref: spanRef, class: 'bold', style: 'color: red' })
const startNodes = [spanNode]

const nodes = addProps(startNodes, () => {
count++

return {
ref: otherRef,
class: 'large',
style: 'line-height: 1'
}
})

expect(count).toBe(1)

expect(nodes).toHaveLength(1)

const newNode = nodes[0] as VNode
const props = newNode.props

expect(props?.class).toBe('bold large')
expect(props?.style.color).toBe('red')
expect(props?.style['line-height']).toBe('1')

const mergedRef = newNode.ref

expect(Array.isArray(mergedRef)).toBe(true)
expect(mergedRef).toHaveLength(2)

// Keep TS happy...
if (Array.isArray(mergedRef)) {
const refs = mergedRef.map(({ r }) => r)

expect(refs.includes(spanRef)).toBe(true)
expect(refs.includes(otherRef)).toBe(true)
}
})
})

describe('replaceChildren', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/vue-vnode-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export const addProps = (
}

if (props && !isEmptyObject(props)) {
return cloneVNode(vnode, props)
return cloneVNode(vnode, props, true)
}
}, options)
}
Expand Down

0 comments on commit bb82205

Please sign in to comment.