[Combobox] `onChange` type incorrectly determined
See original GitHub issueWhat package within Headless UI are you using?
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:
- Created a year ago
- Comments:6
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You are right, I didn’t add a
tsconfig.json
in the sandbox, and it works fine in strict mode 👌Hey @JesusTheHun
I don’t think the Combobox is wrong in this case, the actual
value
has the wrong type. It is currently juststring
instead ofstring | null
, and therefore the Combobox thinks it is juststring
as well.I think this is because
strict
mode is set tofalse
in your tsconfig.json. Once you set that totrue
you will see this:Which results in the correct types.
Even if the type was just
string
, because of thenullable={true}
prop, you will still getstring | null
in theonChange
.If you set
nullable={false}
it will bestring
instead ofstring | null
.But again, this requires
"strict": true
otherwise you will see the incorrect types again:I think once
strict
mode is on, that it will be solved.