Skip to content

Commit

Permalink
fix(vue2): augmentedCreateElement should pass HTML attributes (#6282)
Browse files Browse the repository at this point in the history
fix(vue2): `augmentedCreateElement` does not pass HTML attributes properly

Co-authored-by: Haroen Viaene <hello@haroen.me>
  • Loading branch information
aymeric-giraudet and Haroenv authored Jul 11, 2024
1 parent 60c3f20 commit e0d4c68
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
52 changes: 52 additions & 0 deletions packages/vue-instantsearch/src/util/__tests__/renderCompat.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @jest-environment jsdom
*/

import { mount } from '../../../test/utils';
import { renderCompat } from '../vue-compat';

describe('renderCompat', () => {
it('should handle function components that return multiple children', () => {
const wrapper = mount({
render: renderCompat((h) => {
function Component({ text }) {
return h(
'div',
{ attrs: { 'data-text': text } },
h('span', {}, ['1']),
h('span', {}, '2')
);
}
return h(Component, { text: 'Hello world' });
}),
});

expect(wrapper.html()).toMatchInlineSnapshot(`
<div data-text="Hello world">
<span>
1
</span>
<span>
2
</span>
</div>
`);
});

it('should map props to attrs when using a HTML tag', () => {
const wrapper = mount({
render: renderCompat((h) => {
return h('img', {
src: 'http://algolia.com/image.png',
alt: 'Some image',
});
}),
});

expect(wrapper.html()).toMatchInlineSnapshot(`
<img src="http://algolia.com/image.png"
alt="Some image"
>
`);
});
});
17 changes: 17 additions & 0 deletions packages/vue-instantsearch/src/util/vue-compat/index-vue2.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ const augmentCreateElement =
);
}

if (typeof tag === 'string') {
const { on, style, attrs, domProps, nativeOn, key, ...rest } = props;
return createElement(
tag,
{
class: className || props.class,
attrs: attrs || rest,
on,
nativeOn,
style,
domProps,
key,
},
children
);
}

return createElement(
tag,
Object.assign(props, { class: className || props.class }),
Expand Down

0 comments on commit e0d4c68

Please sign in to comment.