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

[Feature]: Allow more types for modelValue in Select #772

Open
1 of 2 tasks
wouterlms opened this issue Mar 19, 2024 · 8 comments
Open
1 of 2 tasks

[Feature]: Allow more types for modelValue in Select #772

wouterlms opened this issue Mar 19, 2024 · 8 comments
Labels

Comments

@wouterlms
Copy link

wouterlms commented Mar 19, 2024

Describe the feature

Currently, the only supported value is a string, which can be limiting. It would be beneficial to expand this functionality to accommodate additional types, akin to how it's implemented in a Combobox.

Additional information

  • I intend to submit a PR for this feature.
  • I have already implemented and/or tested this feature.
@wouterlms wouterlms changed the title [Feature]: Allow custom modelValue for Select [Feature]: Allow more types for modelValue in Select Mar 19, 2024
@kylemilloy
Copy link

kylemilloy commented Mar 28, 2024

I did a bit of digging on this. While setting this to any would be the quick fix I think the better long term solution would be to use a generic for the model value. That said, the package is set to support Vue v3.2 and generics were introduced in v3.3.

I suspect the reason it's only string would be because when you get values out of FormData.entries() it casts them all to strings. Either that or the base value for HTML form elements is string.

@wouterlms
Copy link
Author

@kylemilloy, casting the type to any wouldn't suffice because the logic for checking if a value is selected is structured like this:

const isSelected = computed(() => rootContext.modelValue?.value === props.value)

However, I managed to "fix" it by wrapping the SelectRoot and SelectItem in custom components where the value is being stringified before passing it.

@wouterlms
Copy link
Author

This is my workaround for those who are interested:

SelectItem is quite straightforward

<SelectItem :value="JSON.Stringify(value)">
  <slot />
</SelectItem>

And SelectRoot

<script setup lang="ts" generic="TValue">
import { SelectRoot } from 'radix-vue'
import { computed } from 'vue'

const props = defineProps<{
  modelValue: TValue | undefined
}>()

const emit = defineEmits<{
  'update:modelValue': [value: TValue |  undefined]
}>()

const model = computed<string | undefined>({
  get: () => {
    return JSON.stringify(props.modelValue)
  },
  set: (value) => {
    if (value === undefined) {
      emit('update:modelValue', undefined)
      return
    }

    emit('update:modelValue', JSON.parse(value))
  },
})
</script>

<template>
  <SelectRoot v-model="model" >
    <slot />
  </SelectRoot>
</template>

@kilobyte2007
Copy link

@wouterlms This hack mostly works but you'll also want to handle the '' empty string to be able to reset the value.

@zernonia are there any plans to expand the types for this component (and others, like Radio Group, Checkbox Group, Toggle Group)?

I think it's implemented quite greatly in the Combobox component so it would probably make sense to replicate this across other components to make it consistent.

@hi-reeve
Copy link

hi-reeve commented May 9, 2024

no update on this one?

@zernonia zernonia added the v2 label May 10, 2024
@zernonia
Copy link
Collaborator

As this is highly requested feature, and it involves breaking changes. This will be included in v2

@MrSunshyne
Copy link

@wouterlms

This is my workaround for those who are interested:

thank you for this solution 🙏 it worked for me!

@peter-emad99
Copy link

is this feature will be one select component only or it will include the other components aslo ( Radio Group, Checkbox , Toggle Group) ??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Backlog
Development

No branches or pull requests

7 participants