Skip to content

Commit 8e576df

Browse files
authored
Merge pull request #559 from components-ai/combobox-improvement
Improve enter handling for combobox
2 parents 2b21148 + c53ca4b commit 8e576df

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

.changeset/brown-steaks-sit.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@compai/css-gui': patch
3+
---
4+
5+
Improve enter handling for combobox

packages/gui/src/components/primitives/Combobox.tsx

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { useCombobox } from 'downshift'
2-
import { useEffect, useId, useRef, useState } from 'react'
2+
import {
3+
ChangeEvent,
4+
KeyboardEvent,
5+
useEffect,
6+
useId,
7+
useRef,
8+
useState,
9+
} from 'react'
310

411
interface ComboboxInterface {
512
onFilterItems: (filterValue: string) => string[]
@@ -59,21 +66,42 @@ export function Combobox({
5966
setFilterValue(clearOnSelect ? '' : selectedItem)
6067
}
6168

69+
const handleEnter = () => {
70+
if (items.includes(filterValue)) {
71+
handleItemSelected(filterValue)
72+
toggleMenu()
73+
}
74+
}
75+
6276
return (
6377
<div {...getComboboxProps()}>
6478
<input
6579
type="text"
6680
{...getInputProps({
6781
ref: inputRef,
68-
onChange: (e: any) => setFilterValue(e.target.value),
82+
onChange: (e: ChangeEvent<HTMLInputElement>) => {
83+
setFilterValue(e.target.value)
84+
},
85+
onKeyDown: (e: KeyboardEvent<HTMLInputElement>) => {
86+
if (e.key === 'Enter') {
87+
handleEnter()
88+
}
89+
},
6990
})}
7091
onFocus={() => {
7192
if (!isOpen) {
7293
toggleMenu()
7394
handleFilterItems('')
7495
}
7596
}}
76-
sx={{ WebkitAppearance: 'none', appearance: 'none', width: '100%', border: '1px solid', borderRadius: '6px', p: 1 }}
97+
sx={{
98+
WebkitAppearance: 'none',
99+
appearance: 'none',
100+
width: '100%',
101+
border: '1px solid',
102+
borderRadius: '6px',
103+
p: 1,
104+
}}
77105
/>
78106
<div
79107
sx={{

0 commit comments

Comments
 (0)