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

fix(Combobox): infer modelValue emit based on multiple #1309

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

iamandrewluca
Copy link

No description provided.

@sunnylost
Copy link
Contributor

sunnylost commented Sep 28, 2024

Turns out we can infer the value of v-model without using multiple.

// This is incorrect, T can never be an array
export type ComboboxRootEmits<T = AcceptableValue> = {
  'update:modelValue': [value: T]

So we should update T to make it can be an array.

type ModelValue = AcceptableValue | Array<AcceptableValue>

// change context
export const [injectComboboxRootContext, provideComboboxRootContext]
  = createContext<ComboboxRootContext<ModelValue>>('ComboboxRoot')

// change emits
export type ComboboxRootEmits<T = ModelValue> = {
  'update:modelValue': [value: T]
  // ...
}

// also change props
export interface ComboboxRootProps<T = ModelValue> extends PrimitiveProps {
  modelValue?: T
  defaultValue?: T
  // ...

Finally, we should update Vue's generic:

<script setup lang="ts" generic="T extends ModelValue = AcceptableValue">

image

@zernonia
Copy link
Member

Thanks for the PR @iamandrewluca ! The solution suggested by @sunnylost is make sense and straightforward. Can you give it a try? 😁

@zernonia zernonia linked an issue Sep 30, 2024 that may be closed by this pull request
@iamandrewluca
Copy link
Author

iamandrewluca commented Sep 30, 2024

@zernonia I started looking into it locally, but then I thought, can it be the user does not want to use multiple but the modelValue is an Array? 🤔

PS: Or it actually does not matter 👀

@iamandrewluca
Copy link
Author

iamandrewluca commented Oct 13, 2024

It seems to be a little complicated. TS will infer the type based on different usage, and we might need to use NoInfer, which might be a breaking change 🤔

PS: I think we might still need to infer based on the multiple prop

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

Successfully merging this pull request may close these issues.

[Bug]: v-model should infer T or T[] based on multiple value
3 participants