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

Improve Combobox types to improve false positives #2411

Merged
merged 2 commits into from Apr 3, 2023
Merged

Conversation

RobinMalfait
Copy link
Collaborator

@RobinMalfait RobinMalfait commented Apr 3, 2023

If you use the default Combobox component then it defaults to Fragment. This also means that if you provide an additional prop that it would be forwarded to the Fragment but that won't work.

You do get a runtime error, but the types aren't 100% clear on what's going on. In fact, they make it very confusing because it will use the last "fallback" in all the Combobox definitions which marks the value as "multiple".

Concretely, this means:

let [value, setValue] = useState('Tom Cook')

<Combobox
  value={value}
  onChange={setValue}
  placeholder="Hello!"
/>

Starts complaining about the value and onChange not handling the
multiple case correctly. But it should complain about the placeholder.

Switching the order does solve this, but it is not the cleanest solution.

Maybe we should be explicit about the Fragment case somehow.

However, there is a use case where I don't think TypeScript will be able to help and it's a bit unfortunate.

let [value, setValue] = useState('Tom Cook')

<Combobox value={value} onChange={setValue} placeholder="Hello!">
  <div>
    {/* ... */}
  </div>
</Combobox>

This is valid because at runtime we will forward all the props to the div. So not 100% sure what we should do here instead.

Visually, these changes look like:
image

vs
image

Fixes: #2407

If you use the default `Combobox` component then it defaults to
`Fragment`. This also means that if you provide an additional prop that
it would be forwarded to the `Fragment` but that won't work.

You do get a runtime error, but the types aren't 100% clear on what's
going on. In fact, they make it very confusing because it will use the
last "fallback" in all the `Combobox` definitions which marks the value
as "multiple".

Concretely, this means:
```ts
let [value, setValue] = useState('Tom Cook')

<Combobox
  value={value}
  onChange={setValue}
  placeholder="Hello!"
/>
```

Starts complaining about the `value` and `onChange` not handling the
`multiple` case correctly. But it should complain about the `placeholder`.

Switching the order _does_ solve this, but it is not the cleanest
solution.

Maybe we should be explicit about the `Fragment` case somehow.

However, there is a use case where I don't think TypeScript will be able
to help and it's a bit unfortunate.

```ts
let [value, setValue] = useState('Tom Cook')

<Combobox value={value} onChange={setValue} placeholder="Hello!">
   <div>
      {/* ... */}
   </div>
</Combobox>
```

This is valid because at runtime we will forward all the props to the
`div`. So not 100% sure what we should do here instead.
@vercel
Copy link

vercel bot commented Apr 3, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
headlessui-react ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 3, 2023 3:44pm
headlessui-vue ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 3, 2023 3:44pm

): JSX.Element
<TValue, TTag extends ElementType = typeof DEFAULT_COMBOBOX_TAG>(
props: ComboboxProps<TValue, false, true, TTag> & RefProp<typeof ComboboxFn>
props: ComboboxProps<TValue, false, false, TTag> & RefProp<typeof ComboboxFn>
Copy link
Contributor

Choose a reason for hiding this comment

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

lol

Copy link
Contributor

Choose a reason for hiding this comment

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

The fact that this change fixes things amazes me. Thanks TypeScript 😂

Copy link
Contributor

@thecrypticace thecrypticace left a comment

Choose a reason for hiding this comment

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

LGTM

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.

Adding "placeholder" prop to ComboBox causes "multiple" to be true / wrong types
2 participants