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(InputMenu/Select/SelectMenu): improve types #2471

Merged
merged 9 commits into from
Oct 28, 2024

Conversation

yassilah
Copy link
Contributor

@yassilah yassilah commented Oct 27, 2024

πŸ”— Linked issue

Resolves #2470

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

To resolve the issue, I added a few type helpers common to all "select-like" components that allow for a better inference of the actual items. I've made sure it works with both Item[] and Item[][] to preserve the correct types when having multiple groups. I've also added a expectEmitPayloadType test helper to ensure the actual types that are being emitted based on the props are correct.

Before:
Capture d’écran 2024-10-28 aΜ€ 08 29 01

After:
Capture d’écran 2024-10-27 aΜ€ 16 12 56

It also handles mixed items array, default valueKey: "value" for USelect and full object item when valueKey isn't set on USelectMenu and UInputMenu.

<USelect :items="['foo']" /> // modelValue: string
<USelect :items="[{ value: 'foo' }]" /> // modelValue: string
<USelect :items="['foo', { value: 'foo' }]" /> // modelValue: string

<USelectMenu :items="[{ value: 'foo' }]" /> // modelValue: { value: string }
<USelectMenu :items="['foo', { value: 'foo' }]" /> // modelValue: string | { value: string }
<USelectMenu :items="['foo', { value: 'foo' }]" value-key="value" /> // modelValue: string
<USelectMenu :items="['foo', { value: 'foo' }]"  multiple /> // modelValue: (string | { value: string })[]
<USelectMenu :items="['foo', { value: 'foo' }]" value-key="value" multiple /> // modelValue: string[]

<UInputMenu :items="[{ value: 'foo' }]" /> // modelValue: { value: string }
<UInputMenu :items="['foo', { value: 'foo' }]" /> // modelValue: string | { value: string }
<UInputMenu :items="['foo', { value: 'foo' }]" value-key="value" /> // modelValue: string
<UInputMenu :items="['foo', { value: 'foo' }]"  multiple /> // modelValue: (string | { value: string })[]
<UInputMenu :items="['foo', { value: 'foo' }]" value-key="value" multiple /> // modelValue: string[]

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

Copy link
Member

@benjamincanac benjamincanac left a comment

Choose a reason for hiding this comment

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

Thanks a lot for the work!! 😊

src/runtime/types/utils.ts Show resolved Hide resolved
@benjamincanac benjamincanac changed the title Select input types fix(InputMenu/Select/SelectMenu): improve types Oct 28, 2024
@benjamincanac benjamincanac linked an issue Oct 28, 2024 that may be closed by this pull request
Copy link

pkg-pr-new bot commented Oct 28, 2024

pnpm add https://pkg.pr.new/@nuxt/ui@2471

commit: 311b61c

@benjamincanac
Copy link
Member

Just tried it out and I don't get autocomplete inside value-key of Select component πŸ€”

@benjamincanac benjamincanac added the v3 #1289 label Oct 28, 2024
@yassilah
Copy link
Contributor Author

Sorry yeah I noticed the autocomplete wouldn't work because it's hard to infer the actual key while also defaulting to 'value' when none is set in USelect πŸ˜… But it should now work properly.

Please also note when using USelect that if you try to pass a list of object items that doesn't match { value: any }[] without specifying another value-key prop, then the modelValue will of course be never

@benjamincanac
Copy link
Member

That's awesome! Thanks a lot @yassilah 😊

@benjamincanac
Copy link
Member

benjamincanac commented Oct 28, 2024

It seems there is an issue in the documentation typecheck about value-key for the SelectMenu.

But it might be because items are not typed correctly 😬

@benjamincanac benjamincanac merged commit db8111d into nuxt:v3 Oct 28, 2024
2 checks passed
ineshbose pushed a commit to ineshbose/nuxt-ui that referenced this pull request Oct 28, 2024
@yassilah yassilah deleted the select-input-types branch October 28, 2024 22:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v3 #1289
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[v3]: wrong types on select inputs components
2 participants