Skip to content

Commit 5452ee6

Browse files
authored
fix: pass key argument as prop in JSX transform (#14111)
1 parent b5b39e4 commit 5452ee6

File tree

5 files changed

+31
-2
lines changed

5 files changed

+31
-2
lines changed

.changeset/cruel-pandas-wonder.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes a bug that prevented "key" from being used as a prop for Astro components in MDX

packages/astro/src/jsx-runtime/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,15 @@ function transformSetDirectives(vnode: AstroVNode) {
7272
}
7373
}
7474

75-
function createVNode(type: any, props: Record<string, any>) {
75+
function createVNode(type: any, props: Record<string, any> = {}, key?: string | number): AstroVNode {
76+
if (key) {
77+
props.key = key;
78+
}
7679
const vnode: AstroVNode = {
7780
[Renderer]: 'astro:jsx',
7881
[AstroJSX]: true,
7982
type,
80-
props: props ?? {},
83+
props,
8184
};
8285
transformSetDirectives(vnode);
8386
transformSlots(vnode);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
export interface Props {
3+
key: string;
4+
}
5+
6+
const { key } = Astro.props
7+
8+
---
9+
10+
<p id="key-test">{key}</p>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
import '../styles.css'
2+
import KeyTest from '../components/KeyTest.astro'
23

34
# Hello page!
5+
6+
<KeyTest key="oranges" />

packages/integrations/mdx/test/mdx-page.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ describe('MDX Page', () => {
5454
const html = await fixture.readFile('/chinese-encoding-layout-manual/index.html');
5555
assert.doesNotMatch(html, /<meta charset="utf-8"/);
5656
});
57+
58+
it('renders MDX with key prop', async () => {
59+
const html = await fixture.readFile('/index.html');
60+
const { document } = parseHTML(html);
61+
62+
const keyTest = document.querySelector('#key-test');
63+
assert.equal(keyTest.textContent, 'oranges');
64+
});
5765
});
5866

5967
describe('dev', () => {

0 commit comments

Comments
 (0)