Skip to content
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
Expand Up @@ -38,14 +38,22 @@ export function AutoCompleteSelect({
}: AutoCompleteSelectProps) {
const { isOpen, onToggle, closeSelect } = useSelectToggle();
const [typeahead, setTypeahead] = useState(selectedOption);
// When isTyping is true, autocomplete results will not be displayed. This prevents
// a clunky UX where the dropdown results and the user text get out of sync.
const [isTyping, setIsTyping] = useState(false);
Copy link
Copy Markdown
Contributor

@pedrottimark pedrottimark Nov 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Judgment call whether to summarize the description as a comment about component behavior.


const autocompleteCallback = useCallback(() => {
const shouldMakeRequest = isOpen && autocompleteProvider;
if (shouldMakeRequest) {
return autocompleteProvider(typeahead);
const { request, cancel } = autocompleteProvider(typeahead);
request.finally(() => setIsTyping(false));
return { request, cancel };
}
return {
request: Promise.resolve([]),
request: new Promise<string[]>((resolve) => {
setIsTyping(false);
resolve([]);
}),
cancel: () => {},
};
}, [isOpen, autocompleteProvider, typeahead]);
Expand Down Expand Up @@ -73,14 +81,18 @@ export function AutoCompleteSelect({
variant="typeahead"
isCreatable
isOpen={isOpen}
onFilter={() => getOptions(OptionComponent, data)}
onFilter={() => getOptions(OptionComponent, isTyping ? [] : data)}
onToggle={onToggle}
onTypeaheadInputChanged={updateTypeahead}
onTypeaheadInputChanged={(val: string) => {
setIsTyping(true);
updateTypeahead(val);
}}
onBlur={() => updateTypeahead(selectedOption)}
selections={selectedOption}
onSelect={onSelect}
isDisabled={isDisabled}
>
{getOptions(OptionComponent, data)}
{getOptions(OptionComponent, isTyping ? [] : data)}
</Select>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ function RuleSelector({
// because PatternFly will pass a `ref` to it
const OptionComponent = forwardRef(
(
props: { className: string; children: ReactNode },
props: {
className: string;
children: ReactNode;
onClick: (...args: unknown[]) => void;
},
ref: ForwardedRef<HTMLButtonElement | null>
) => (
<button className={props.className} type="button" ref={ref}>
<button className={props.className} onClick={props.onClick} type="button" ref={ref}>
<ResourceIcon kind={entityType} />
{props.children}
</button>
Expand Down