Skip to content

Commit

Permalink
fix: When escaping inside an autocomplete, the displayvalue was shown…
Browse files Browse the repository at this point in the history
… while input was focused (#179)

* fix: When escaping or pressing enter inside autocompletes, displayvalue was shown and user could no longer type

* Update

* Update package.json

* Remove handler for opening/closing the autocompletes

* Update
  • Loading branch information
aurorascharff authored Oct 4, 2024
1 parent 6f553e8 commit c3d8420
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 12 deletions.
7 changes: 1 addition & 6 deletions packages/react/src/formElements/MdAutocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const MdAutocomplete = React.forwardRef<HTMLInputElement, MdAutocompleteProps>(
const [autocompleteValue, setAutocompleteValue] = useState('');
const [results, setResults] = useState<MdAutocompleteOption[]>([]);
const dropdownRef = useRef<HTMLDivElement>(null);
useDropdown(dropdownRef, open, setOpen);
useDropdown(dropdownRef, open, setOpen, 'autocomplete');

const autocompleteId = id && id !== '' ? id : uuidv4();

Expand Down Expand Up @@ -202,11 +202,6 @@ const MdAutocomplete = React.forwardRef<HTMLInputElement, MdAutocompleteProps>(
className={inputClassNames}
value={open ? autocompleteValue : displayValue}
tabIndex={0}
onKeyDown={e => {
if (e.key === 'Enter') {
setOpen(!open);
}
}}
onChange={e => {
setAutocompleteValue(e.target.value);
if (e.target.value && e.target.value !== '') {
Expand Down
7 changes: 1 addition & 6 deletions packages/react/src/formElements/MdMultiAutocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const MdMultiAutocomplete = React.forwardRef<HTMLInputElement, MdMultiAutocomple
const [autocompleteValue, setAutocompleteValue] = useState('');
const [results, setResults] = useState<MdMultiAutocompleteOption[]>([]);
const dropdownRef = useRef<HTMLDivElement>(null);
useDropdown(dropdownRef, open, setOpen);
useDropdown(dropdownRef, open, setOpen, 'autocomplete');

const multiAutocompleteId = id && id !== '' ? id : uuidv4();

Expand Down Expand Up @@ -225,11 +225,6 @@ const MdMultiAutocomplete = React.forwardRef<HTMLInputElement, MdMultiAutocomple
className={inputClassNames}
value={open ? autocompleteValue : displayValue}
tabIndex={0}
onKeyDown={e => {
if (e.key === 'Enter') {
setOpen(!open);
}
}}
onChange={e => {
setAutocompleteValue(e.target.value);
if (e.target.value && e.target.value !== '') {
Expand Down
4 changes: 4 additions & 0 deletions packages/react/src/hooks/useDropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ export default function useDropdown(
ref: React.RefObject<HTMLDivElement>,
open: boolean,
setOpen: (_open: boolean) => void,
elementType?: 'autocomplete' | 'select',
) {
const onKeyDown = useCallback((e: KeyboardEvent) => {
const focusableElements = ref.current?.querySelectorAll<HTMLElement>(focusableHtmlElements);

if (e.key === 'Escape') {
const element = focusableElements?.[0];
element?.focus();
if (elementType === 'autocomplete') {
element?.blur();
}
document.removeEventListener('keydown', onKeyDown, false);
setOpen(false);
}
Expand Down

0 comments on commit c3d8420

Please sign in to comment.