Skip to content

Commit 97d82c6

Browse files
authored
feat(define-model)!: support runtime (#104)
* feat(define-model)!: support runtime * chore: add playground * chore: add changeset * docs: add * refactor: improve perf * test: update
1 parent 6162a9c commit 97d82c6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+789
-317
lines changed

.changeset/shy-maps-jog.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@vue-macros/define-model': minor
3+
'unplugin-vue-macros': minor
4+
'@vue-macros/volar': minor
5+
'@vue-macros/common': patch
6+
---
7+
8+
add runtime defineModel, requires `@vueuse/core`.
9+
10+
⚠️ BREAKING CHANGE: original `defineModel` renamed to `$defineModel`.

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"Unocss",
88
"unplugin",
99
"vitest",
10-
"vmodel"
10+
"vmodel",
11+
"Vueuse"
1112
]
1213
}

docs/macros/define-model.md

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,42 @@ Declaring and mutate `v-model` props as the same as normal variable using the `d
55
| Features | Supported |
66
| :----------------: | :----------------: |
77
| Vue 3 | :white_check_mark: |
8-
| Vue 2 | :x: |
8+
| Vue 2 | :white_check_mark: |
99
| TypeScript / Volar | :white_check_mark: |
1010

11+
## Usage
12+
13+
```vue
14+
<script setup lang="ts">
15+
const { modelValue, count } = defineModel<{
16+
modelValue: string
17+
count: number
18+
}>()
19+
20+
console.log(modelValue.value)
21+
modelValue.value = 'newValue'
22+
</script>
23+
```
24+
25+
::: warning ❌ Object declaring is not supported.
26+
27+
```vue
28+
<script setup lang="ts">
29+
const { modelValue } = defineModel({
30+
modelValue: String,
31+
})
32+
</script>
33+
```
34+
35+
:::
36+
37+
## With Reactivity Transform
38+
1139
::: warning
1240

1341
[Reactivity Transform](https://vuejs.org/guide/extras/reactivity-transform.html) is required. You should enable it first. Otherwise, it will lose the reactivity connection.
1442

15-
Unfortunately Reactivity Transform is not implemented in Vue 2, so this macros doesn't support Vue 2 now.
43+
Unfortunately Reactivity Transform is not implemented in Vue 2, so this macro doesn't support Vue 2 now.
1644

1745
:::
1846

@@ -22,11 +50,9 @@ Assignment expression is only supported in `<script setup>` block. In other word
2250

2351
:::
2452

25-
## Basic Usage
26-
27-
```vue
53+
```vue{7-9}
2854
<script setup lang="ts">
29-
let { modelValue, count } = defineModel<{
55+
let { modelValue, count } = $defineModel<{
3056
modelValue: string
3157
count: number
3258
}>()

packages/common/src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export const DEFINE_PROPS = 'defineProps'
22
export const DEFINE_EMITS = 'defineEmits'
33
export const DEFINE_OPTIONS = 'defineOptions'
44
export const DEFINE_MODEL = 'defineModel'
5+
export const DEFINE_MODEL_DOLLAR = '$defineModel'
56
export const DEFINE_SETUP_COMPONENT = 'defineSetupComponent'
67
export const DEFINE_RENDER = 'defineRender'
78

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import type { defineModel as _defineModel } from './macros'
1+
import type {
2+
$defineModel as _$defineModel,
3+
defineModel as _defineModel,
4+
} from './macros'
25

36
declare global {
47
const defineModel: typeof _defineModel
8+
const $defineModel: typeof _$defineModel
59
}
610

711
export {}

packages/define-model/macros.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
export const defineModel: <T>() => T
1+
import type { WritableComputedRef } from 'vue'
2+
3+
export const defineModel: <T>() => {
4+
[K in keyof T]: WritableComputedRef<T[K]>
5+
}
6+
export const $defineModel: <T>() => T

packages/define-model/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@
6767
"build": "tsup && tsx ../../scripts/postbuild.mts",
6868
"dev": "DEV=1 tsup"
6969
},
70+
"peerDependencies": {
71+
"@vueuse/core": "^9.0.0"
72+
},
73+
"peerDependenciesMeta": {
74+
"@vueuse/core": {
75+
"optional": true
76+
}
77+
},
7078
"dependencies": {
7179
"@rollup/pluginutils": "^4.2.1",
7280
"@vue-macros/common": "workspace:*",
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1-
export const emitHelperId = '/plugin-define-model/emit-helper'
1+
export const helperPrefix = '/plugin-define-model'
2+
3+
export const emitHelperId = `${helperPrefix}/emit-helper`
24
export const emitHelperCode = `export default (emitFn, key, value, ...args) => {
35
emitFn(key, value)
46
return args.length > 0 ? args[0] : value
57
}`
8+
9+
export const useVmodelHelperId = `${helperPrefix}/use-vmodel`
10+
export const useVmodelHelperCode = `import { getCurrentInstance } from 'vue';
11+
import { useVModel } from '@vueuse/core';
12+
export default (...keys) => {
13+
const props = getCurrentInstance().proxy.$props
14+
const ret = {}
15+
for (const [key, eventName] of keys)
16+
ret[key] = useVModel(props, key, undefined, { eventName })
17+
return ret
18+
}
19+
`

0 commit comments

Comments
 (0)