Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Utilize Vue's built-in defineModel for model implementation #906

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions apps/www/src/lib/registry/default/ui/textarea/Textarea.vue
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
<script setup lang="ts">
import type { HTMLAttributes } from 'vue'
import { cn } from '@/lib/utils'
import { useVModel } from '@vueuse/core'

const props = defineProps<{
class?: HTMLAttributes['class']
defaultValue?: string | number
modelValue?: string | number
}>()

const emits = defineEmits<{
(e: 'update:modelValue', payload: string | number): void
}>()

const modelValue = useVModel(props, 'modelValue', emits, {
passive: true,
defaultValue: props.defaultValue,
})
const modelValue = defineModel<string | number>()
modelValue.value = props.defaultValue
Comment on lines +10 to +11
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const modelValue = defineModel<string | number>()
modelValue.value = props.defaultValue
const modelValue = defineModel<string | number>({
default: props.defaultValue
})

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It cannot be done like that, already tried it 😁
"[@vue/compiler-sfc] defineModel() in <script setup> cannot reference locally declared variables because it will be hoisted outside of the setup() function."

</script>

<template>
Expand Down
12 changes: 2 additions & 10 deletions apps/www/src/lib/registry/new-york/ui/textarea/Textarea.vue
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
<script setup lang="ts">
import type { HTMLAttributes } from 'vue'
import { cn } from '@/lib/utils'
import { useVModel } from '@vueuse/core'

const props = defineProps<{
class?: HTMLAttributes['class']
defaultValue?: string | number
modelValue?: string | number
}>()

const emits = defineEmits<{
(e: 'update:modelValue', payload: string | number): void
}>()

const modelValue = useVModel(props, 'modelValue', emits, {
passive: true,
defaultValue: props.defaultValue,
})
const modelValue = defineModel<string | number>()
modelValue.value = props.defaultValue
</script>

<template>
Expand Down