Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: When escaping inside an autocomplete, the displayvalue was shown while input was focused #179

Merged
merged 6 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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