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

[Bug]: DOM refs are not working on Input component #259

Closed
devberkay opened this issue Jan 10, 2024 · 2 comments
Closed

[Bug]: DOM refs are not working on Input component #259

devberkay opened this issue Jan 10, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@devberkay
Copy link

devberkay commented Jan 10, 2024

Environment

Developement/Production OS: Windows 10 19043.1110
Node version: 16.0.0
Package manager: pnpm@8.6.0
Radix Vue version: 1.0.0
Shadcn Vue version: 1.0.0
Vue version: 3.0.0
Nuxt version: 3.0.0
Nuxt mode: universal
Nuxt target: server
CSS framework: tailwindcss@3.3.3
Client OS: Windows 10 19043.1110
Browser: Chrome 90.0.4430.212

Link to minimal reproduction

In shortly

Steps to reproduce

<script setup lang="ts">
const el = ref<HTMLInputElement | null>(null)
</script>

<template>
  <Input ref="el" />
</template>

Describe the bug

My aim is to set cursor position offset for an input tag
I am getting following error when i use DOM refs on Input component. and call a function of that HTMLElement:

value.setSelectionRange is not a function

Expected behavior

No response

Conext & Screenshots (if applicable)

No response

@devberkay devberkay added the bug Something isn't working label Jan 10, 2024
@devberkay
Copy link
Author

devberkay commented Jan 10, 2024

I have found the solution from vuejs docs. Since this is a child component you need to expose native input tag's DOM ref using defineExpose() function. https://vuejs.org/api/sfc-script-setup.html#defineexpose

Here is how the altered Input component looks after i added defineExpose():


<script setup lang="ts">
import { useAttrs } from 'vue'
import { useVModel } from '@vueuse/core'
import { cn } from '@/lib/utils'

const domRef = ref<HTMLInputElement | null>(null)

defineExpose({
  domRef
})

defineOptions({
  inheritAttrs: false,
})

const props = defineProps<{
  defaultValue?: string | number
  modelValue?: string | number
}>()

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

const { class: className, ...rest } = useAttrs()

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

<template>
  <input ref="domRef" v-model="modelValue" :class="cn('flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50', className ?? '')" v-bind="rest">
</template>

I am going to close this as completed since i don't think this is a bug rather it is a design decision by the shadcn-vue maintainers.

@sadeghbarati
Copy link
Collaborator

Related rfcs

vuejs/rfcs#258

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants