Skip to content

Commit c92a847

Browse files
authored
Improve Combobox types to improve false positives (#2411)
* swap `Combobox` type order 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. * update changelog
1 parent f4e9710 commit c92a847

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

packages/@headlessui-react/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Fix "Can't perform a React state update on an unmounted component." when using the `Transition` component ([#2374](https://github.com/tailwindlabs/headlessui/pull/2374))
1414
- Add `FocusTrap` event listeners once document has loaded ([#2389](https://github.com/tailwindlabs/headlessui/pull/2389))
1515
- Fix `className` hydration for `<Transition appear>` ([#2390](https://github.com/tailwindlabs/headlessui/pull/2390))
16+
- Improve `Combobox` types to improve false positives ([#2411](https://github.com/tailwindlabs/headlessui/pull/2411))
1617

1718
### Added
1819

packages/@headlessui-react/src/components/combobox/combobox.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,10 +1426,10 @@ interface ComponentCombobox extends HasDisplayName {
14261426
props: ComboboxProps<TValue, true, false, TTag> & RefProp<typeof ComboboxFn>
14271427
): JSX.Element
14281428
<TValue, TTag extends ElementType = typeof DEFAULT_COMBOBOX_TAG>(
1429-
props: ComboboxProps<TValue, false, false, TTag> & RefProp<typeof ComboboxFn>
1429+
props: ComboboxProps<TValue, false, true, TTag> & RefProp<typeof ComboboxFn>
14301430
): JSX.Element
14311431
<TValue, TTag extends ElementType = typeof DEFAULT_COMBOBOX_TAG>(
1432-
props: ComboboxProps<TValue, false, true, TTag> & RefProp<typeof ComboboxFn>
1432+
props: ComboboxProps<TValue, false, false, TTag> & RefProp<typeof ComboboxFn>
14331433
): JSX.Element
14341434
}
14351435

0 commit comments

Comments
 (0)