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) Properly handle focus and blur states for address hierarchy fields #745

Merged
merged 6 commits into from
Jul 3, 2023
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useMemo, useRef, useState } from 'react';
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { TextInput, Layer } from '@carbon/react';
import SelectionTick from './selection-tick.component';
import styles from '../input.scss';
Expand Down Expand Up @@ -26,18 +26,6 @@ const ComboInput: React.FC<ComboInputProps> = ({ entries, fieldProps, handleInpu
setHighlightedEntry(-1);
}, [setShowEntries, setHighlightedEntry]);

const handleBlur = useCallback(
(e) => {
// This check is in place to not hide the entries when an entry is clicked
// Else the onClick of the entry will not be counted.
if (!comboInputRef?.current?.contains(e.target)) {
setShowEntries(false);
}
setHighlightedEntry(-1);
},
[setShowEntries, setHighlightedEntry],
);

const filteredEntries = useMemo(() => {
if (!entries) {
return [];
Expand All @@ -60,6 +48,12 @@ const ComboInput: React.FC<ComboInputProps> = ({ entries, fieldProps, handleInpu
const handleKeyPress = useCallback(
(e: KeyboardEvent) => {
const totalResults = filteredEntries.length ?? 0;

if (e.key === 'Tab') {
setShowEntries(false);
setHighlightedEntry(-1);
}

if (e.key === 'ArrowUp') {
setHighlightedEntry((prev) => Math.max(-1, prev - 1));
} else if (e.key === 'ArrowDown') {
Expand All @@ -68,17 +62,32 @@ const ComboInput: React.FC<ComboInputProps> = ({ entries, fieldProps, handleInpu
handleOptionClick(filteredEntries[highlightedEntry], e);
}
},
[highlightedEntry, handleOptionClick, filteredEntries, setHighlightedEntry],
[highlightedEntry, handleOptionClick, filteredEntries, setHighlightedEntry, setShowEntries],
);

useEffect(() => {
const listener = (e) => {
if (!comboInputRef.current.contains(e.target as Node)) {
setShowEntries(false);
setHighlightedEntry(-1);
}
};
window.addEventListener('click', listener);
return () => {
window.removeEventListener('click', listener);
};
});

return (
<div className={styles.comboInput} ref={comboInputRef}>
<Layer>
<TextInput
{...fieldProps}
onChange={(e) => handleInputChange(e.target.value)}
onChange={(e) => {
setHighlightedEntry(-1);
handleInputChange(e.target.value);
}}
onFocus={handleFocus}
onBlur={handleBlur}
autoComplete={'off'}
onKeyDown={handleKeyPress}
/>
Expand All @@ -98,7 +107,7 @@ const ComboInput: React.FC<ComboInputProps> = ({ entries, fieldProps, handleInpu
aria-selected="true"
onClick={() => handleOptionClick(entry)}>
<div
className={`cds--list-box__menu-item__option ${
className={`cds--list-box__menu-item__option ${styles.comboInputItemOption} ${
entry === value && 'cds--list-box__menu-item--active'
}`}>
{entry}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,8 @@
.comboInputEntries {
position: relative;
}

.comboInputItemOption {
margin: 0;
padding-left: 1rem;
}