Skip to content

Commit 3b80c2e

Browse files
fix: improved root element detection
1 parent 6d82457 commit 3b80c2e

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ function getRootBlock(
2020
rootChild &&
2121
tag !== 'formkit' &&
2222
tag !== 'form-kit' &&
23-
rootChild.isSelfClosing === false
23+
tag !== 'formkitschema' &&
24+
tag !== 'form-kit-schema' &&
25+
!rootChild.isSelfClosing
2426
) {
2527
// In this case the component has a root node that is not formkit and is
2628
// not self-closing, like, perhaps, a div. We need to move the provider
@@ -114,7 +116,6 @@ function injectProviderComponent(
114116
const content = code.substring(startInsertAt, template.loc.end.offset - 11)
115117
const after = code.substring(template.loc.end.offset - 11)
116118
code = `${before}\n${open}\n${content}\n${close}\n${after}`
117-
118119
return { code, map: null }
119120
}
120121

test/__snapshots__/index.test.ts.snap

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,30 @@ exports[`index > injects inside root node if there is one 1`] = `
3232
</template>"
3333
`;
3434

35+
exports[`index > injects inside root node with full sfc 1`] = `
36+
"<script lang=\\"ts\\" setup>
37+
import { FormKitLazyProvider } from '@formkit/vue'
38+
function handleLoginSubmit(values: any) {
39+
window.alert(\\"You are logged in. Credentials:
40+
\\" + JSON.stringify(values));
41+
}
42+
</script>
43+
44+
<template>
45+
<div>
46+
47+
<FormKitLazyProvider>
48+
<FormKit type=\\"form\\" submit-label=\\"login\\" @submit=\\"handleLoginSubmit\\">
49+
<FormKit type=\\"email\\" label=\\"Email\\" name=\\"email\\" />
50+
<FormKit type=\\"password\\" label=\\"Password\\" name=\\"password\\" />
51+
</FormKi
52+
</FormKitLazyProvider>
53+
t>
54+
</div>
55+
</template>
56+
"
57+
`;
58+
3559
exports[`index > injects setup block when using options api 1`] = `
3660
"<script setup lang=\\"ts\\">import { FormKitLazyProvider } from '@formkit/vue'</script>
3761
<script lang=\\"ts\\">

test/index.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,31 @@ describe('index', () => {
4242
).toMatchSnapshot()
4343
})
4444

45+
it('injects inside root node with full sfc', async () => {
46+
expect(
47+
(
48+
await plugin.transform(
49+
`<script lang="ts" setup>
50+
function handleLoginSubmit(values: any) {
51+
window.alert("You are logged in. Credentials: \n" + JSON.stringify(values));
52+
}
53+
</script>
54+
55+
<template>
56+
<div>
57+
<FormKit type="form" submit-label="login" @submit="handleLoginSubmit">
58+
<FormKit type="email" label="Email" name="email" />
59+
<FormKit type="password" label="Password" name="password" />
60+
</FormKit>
61+
</div>
62+
</template>
63+
`,
64+
'test.vue',
65+
)
66+
).code,
67+
).toMatchSnapshot()
68+
})
69+
4570
it('injects import into script setup block', async () => {
4671
expect(
4772
(await plugin.transform(aboutSFCFile, 'about.vue')).code,

0 commit comments

Comments
 (0)