question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Combobox] `onChange` type incorrectly determined

See original GitHub issue

What package within Headless UI are you using?

@headlessui/react

What version of that package are you using?

v1.6.1

What browser are you using?

N/A

Reproduction URL

https://stackblitz.com/edit/react-ts-jldzdg?file=MyCombobox.tsx

Describe your issue

The Combobox component determined the type of the argument of the onChange function solely based on the type of the value property it receives. Unfortunately that is not accurate nor safe.

// 1. Nullable
<Combobox value={'hi'} onChange={(selection) => console.log("changed")} nullable={true} />

// Wrong type
type SelectionActual = string;
type SelectionExpected = string | null;

// 2. Multiple

/* 
Looking at the two cases below you cannot tell if there should be an error or not.
Maybe the option value is a `[string, string]`and maybe it's just a `string`.
Also, when `multiple & nullable = true` then `selection` cannot be null. So the type should reflect that.
*/

<Combobox value={['a', 'b']} onChange={(selection) => console.log("changed")} nullable={true} multiple={false} />
<Combobox value={['a', 'b']} onChange={(selection) => console.log("changed")} nullable={true} multiple={true} />

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
JesusTheHuncommented, Aug 23, 2022

You are right, I didn’t add a tsconfig.json in the sandbox, and it works fine in strict mode 👌

1reaction
RobinMalfaitcommented, Aug 23, 2022

Hey @JesusTheHun

I don’t think the Combobox is wrong in this case, the actual value has the wrong type. It is currently just string instead of string | null, and therefore the Combobox thinks it is just string as well.

image

image

I think this is because strict mode is set to false in your tsconfig.json. Once you set that to true you will see this:

image

image

Which results in the correct types.

Even if the type was just string, because of the nullable={true} prop, you will still get string | null in the onChange.

image

If you set nullable={false} it will be string instead of string | null.

image

But again, this requires "strict": true otherwise you will see the incorrect types again:

image


Forcing the generic type does not fix the issue.

I think once strict mode is on, that it will be solved.

Without strict mode With strict mode
image image
Read more comments on GitHub >

github_iconTop Results From Across the Web

Solved: Problems with ComboBox.OnChange triggering too oft...
It appears that the ComboBox.OnChange event gets triggered with a "weird timing" and more often than, say, TextInput.OnChange. Hence, whenever I ...
Read more >
c# - What event catches a change of value in a combobox in a ...
The problem is that the above catches the DataGridViewEditingControlShowingEvent and it does not catch the value changed. So it will fire every time...
Read more >
react-select: Select.onChange typings wrong (?) · Issue #32553
The first argument of the onChange handler cannot simply be of type OptionType , since it can be either one-, multiple- or no...
Read more >
This Excel Dependent Combo Box Solves an Annoying ...
In this video you'll learn a lot about combo box drop-down lists in Excel:1. How to create a combo box drop-down list2.
Read more >
Excel Dependent Combo Box Solves an Annoying Problem
A useful approach is to construct the formula in a normal worksheet cell; once it is determined that the formula is performing as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found