-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jsx-directive/v-model): support dynamic argument (#641)
* feat(jsx-directive/v-model): support dynamic argument * chore: add changeset
- Loading branch information
1 parent
b76b938
commit 3e790d5
Showing
12 changed files
with
268 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@vue-macros/jsx-directive": patch | ||
"@vue-macros/volar": patch | ||
--- | ||
|
||
support dynamic argument for v-model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { type MagicString, importHelperFn } from '@vue-macros/common' | ||
import type { JSXAttribute } from '@babel/types' | ||
|
||
export function transformVModel( | ||
attribute: JSXAttribute, | ||
s: MagicString, | ||
offset: number, | ||
) { | ||
if ( | ||
attribute.name.type === 'JSXNamespacedName' && | ||
attribute.value?.type === 'JSXExpressionContainer' | ||
) { | ||
const matched = attribute.name.name.name.match(/^\$(.*)\$(?:_(.*))?/) | ||
if (!matched) return | ||
|
||
let [, argument, modifiers] = matched | ||
const value = s.sliceNode(attribute.value.expression, { offset }) | ||
argument = `${importHelperFn(s, offset, 'unref')}(${argument})` | ||
modifiers = modifiers | ||
? `, [${argument} + "Modifiers"]: { ${modifiers | ||
.split('_') | ||
.map((key) => `${key}: true`) | ||
.join(', ')} }` | ||
: '' | ||
s.overwriteNode( | ||
attribute, | ||
`{...{[${argument}]: ${value}, ["onUpdate:" + ${argument}]: $event => ${value} = $event${modifiers}}}`, | ||
{ offset }, | ||
) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/jsx-directive/tests/__snapshots__/v-model.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`jsx-vue-directive > v-model > ./fixtures/v-model/index.vue 1`] = ` | ||
"<script setup lang="tsx"> | ||
import { unref as __MACROS_unref } from "vue"; | ||
import { ref } from 'vue' | ||
let foo = ref('') | ||
const value = ref('value') | ||
defineRender(() => ( | ||
<div> | ||
<input {...{[__MACROS_unref(value)]: foo, ["onUpdate:" + __MACROS_unref(value)]: $event => foo = $event, [__MACROS_unref(value) + "Modifiers"]: { trim: true, number: true }}} /> | ||
{foo} | ||
</div> | ||
)) | ||
</script> | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<script setup lang="tsx"> | ||
import { ref } from 'vue' | ||
let foo = ref('') | ||
const value = ref('value') | ||
defineRender(() => ( | ||
<div> | ||
<input v-model:$value$_trim_number={foo} /> | ||
{foo} | ||
</div> | ||
)) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { describe } from 'vitest' | ||
import { testFixtures } from '@vue-macros/test-utils' | ||
import { transformJsxDirective } from '../src/api' | ||
|
||
describe('jsx-vue-directive', () => { | ||
describe('v-model', async () => { | ||
await testFixtures( | ||
import.meta.glob('./fixtures/v-model/*.{vue,jsx,tsx}', { | ||
eager: true, | ||
as: 'raw', | ||
}), | ||
(_, id, code) => transformJsxDirective(code, id, 3)?.code, | ||
) | ||
}) | ||
}) |
Oops, something went wrong.