Skip to content

Commit

Permalink
fix(Autocomplete): remove handler to open menu on focus (#3033)
Browse files Browse the repository at this point in the history
* fix(Autocomplete): remove handler to open menu on focus

* Create good-donuts-obey.md

* chore: fix test
  • Loading branch information
zchenwei committed Nov 21, 2022
1 parent ad433da commit c9f67b1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-donuts-obey.md
@@ -0,0 +1,5 @@
---
"@aws-amplify/ui-react": patch
---

fix(Autocomplete): remove handler to open menu on focus to fix #3030
4 changes: 0 additions & 4 deletions packages/react/src/primitives/Autocomplete/Autocomplete.tsx
Expand Up @@ -29,7 +29,6 @@ export const AutocompletePrimitive: Primitive<AutocompleteProps, 'input'> = (
onChange,
onClear,
onClick,
onFocus,
onSelect,
onSubmit,
renderOption,
Expand All @@ -46,7 +45,6 @@ export const AutocompletePrimitive: Primitive<AutocompleteProps, 'input'> = (
handleOnBlur,
handleOnClear,
handleOnClick,
handleOnFocus,
handleOnChange,
handleOnKeyDown,
isControlled,
Expand All @@ -67,7 +65,6 @@ export const AutocompletePrimitive: Primitive<AutocompleteProps, 'input'> = (
onChange,
onClear,
onClick,
onFocus,
onSelect,
onSubmit,
});
Expand Down Expand Up @@ -145,7 +142,6 @@ export const AutocompletePrimitive: Primitive<AutocompleteProps, 'input'> = (
onChange={handleOnChange}
onClear={handleOnClear}
onClick={handleOnClick}
onFocus={handleOnFocus}
onKeyDown={handleOnKeyDown}
ref={ref}
value={composedValue}
Expand Down
Expand Up @@ -65,6 +65,13 @@ describe('Autocomplete: ', () => {
userEvent.keyboard('{Esc}');
expect(textInput).toHaveValue('');

// Focus will not open menu
userEvent.click(document.body);
expect(textInput).not.toHaveFocus();
textInput.focus();
expect(textInput).toHaveFocus();
expect(screen.queryByRole('listbox')).not.toBeInTheDocument();

// Click will open menu
expect(textInput).toHaveFocus();
userEvent.click(textInput);
Expand Down Expand Up @@ -188,7 +195,7 @@ describe('Autocomplete: ', () => {
render(<Autocomplete label={label} options={options} isLoading />);

const textInput = await screen.findByRole('combobox');
textInput.focus();
userEvent.click(textInput);
const listbox = screen.queryByRole('listbox');
expect(listbox).not.toBeInTheDocument();
const loading = screen.getByText(ComponentText.Autocomplete.loadingText);
Expand All @@ -199,7 +206,7 @@ describe('Autocomplete: ', () => {
render(<Autocomplete label={label} options={options} />);

const textInput = await screen.findByRole('combobox');
textInput.focus();
userEvent.click(textInput);

const appleOption = screen.getByText('apple')
.parentElement as HTMLLIElement;
Expand Down Expand Up @@ -296,7 +303,7 @@ describe('Autocomplete: ', () => {
);

const textInput = await screen.findByRole('combobox');
textInput.focus();
userEvent.click(textInput);
expect(renderOption).toHaveBeenCalled();
});

Expand All @@ -312,7 +319,7 @@ describe('Autocomplete: ', () => {
);

const textInput = await screen.findByRole('combobox');
textInput.focus();
userEvent.click(textInput);

const loading = screen.getByText(LoadingIndicator);
expect(loading).toBeInTheDocument();
Expand All @@ -324,7 +331,7 @@ describe('Autocomplete: ', () => {
render(<Autocomplete label={label} options={[]} menuSlots={{ Empty }} />);

const textInput = await screen.findByRole('combobox');
textInput.focus();
userEvent.click(textInput);

const empty = screen.getByText(Empty);
expect(empty).toBeInTheDocument();
Expand All @@ -343,7 +350,7 @@ describe('Autocomplete: ', () => {
);

const textInput = await screen.findByRole('combobox');
textInput.focus();
userEvent.click(textInput);

const header = screen.getByText(Header);
expect(header).toBeInTheDocument();
Expand All @@ -362,7 +369,7 @@ describe('Autocomplete: ', () => {
expect(textInput).toHaveAttribute('aria-haspopup', 'listbox');
expect(textInput).toHaveAttribute('aria-expanded', 'false');

textInput.focus();
userEvent.click(textInput);
const menu = screen.getByRole('listbox').parentElement;
expect(textInput).toHaveAttribute('aria-expanded', 'true');
expect(textInput).toHaveAttribute('aria-controls', menu?.id);
Expand Down
13 changes: 0 additions & 13 deletions packages/react/src/primitives/Autocomplete/useAutocomplete.tsx
Expand Up @@ -21,7 +21,6 @@ export const useAutocomplete = ({
onChange,
onClear,
onClick,
onFocus,
onSelect,
onSubmit,
}: UseAutocompleteProps) => {
Expand Down Expand Up @@ -105,17 +104,6 @@ export const useAutocomplete = ({
[onClick]
);

const handleOnFocus: React.FocusEventHandler<HTMLInputElement> =
React.useCallback(
(event) => {
setIsMenuOpen(true);
if (isFunction(onFocus)) {
onFocus(event);
}
},
[onFocus]
);

const handleOnKeyDown: React.KeyboardEventHandler<HTMLInputElement> = (
event
) => {
Expand Down Expand Up @@ -241,7 +229,6 @@ export const useAutocomplete = ({
handleOnBlur,
handleOnClear,
handleOnClick,
handleOnFocus,
handleOnChange,
handleOnKeyDown,
isControlled,
Expand Down
2 changes: 0 additions & 2 deletions packages/react/src/primitives/types/autocomplete.ts
Expand Up @@ -143,6 +143,4 @@ export interface UseAutocompleteProps extends Partial<AutocompleteProps> {
onChange: React.ChangeEventHandler<HTMLInputElement>;

onClick: React.MouseEventHandler<HTMLInputElement>;

onFocus: React.FocusEventHandler<HTMLInputElement>;
}

0 comments on commit c9f67b1

Please sign in to comment.