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

Issue with mat-autocomplete #109

Open
dhutaryan opened this issue Mar 24, 2021 · 3 comments
Open

Issue with mat-autocomplete #109

dhutaryan opened this issue Mar 24, 2021 · 3 comments

Comments

@dhutaryan
Copy link

Hello,

I use keyboard with mat-autocomplete field. But when I click keyboard buttons, autocomplete options won't appear

@ronan-guillamet
Copy link

Hi! Did you find any way to solve this issue ? Have the same...

@dhutaryan
Copy link
Author

@ronan-guillamet Hi,
I opened autocomplete options manually.

<input
  #autocompleteTrigger="matAutocompleteTrigger"
/>
@ViewChild('autocompleteTrigger')
private autocompleteTrigger: MatAutocompleteTrigger;

So, and then I listened field changes, send request and opened options manually after getting result. Simple example:

this.users$ = this.checkInForm.get('name').valueChanges.pipe(
  switchMap((name) => {
    return this.usersService.search({ name }).pipe(
        tap(() => { this.autocompleteTrigger.openPanel(); }),
    );
  }),
);

@seco-plr
Copy link

seco-plr commented Mar 15, 2022

Currently, I solved it by calling stopPropagation on the click event of the keyboard. The following code is a custom version of the MatKeyboardService which you can add as provider, replacing the original one:

@Injectable()
export class CustomKeyboardService extends MatKeyboardService {

  public override openFromComponent(layoutOrLocale: string, config: MatKeyboardConfig): MatKeyboardRef<MatKeyboardComponent> {
    const keyboardRef = super.openFromComponent(layoutOrLocale, config);
    this.setClickEventPropagation(true);
    const dismissFunction = keyboardRef.dismiss.bind(keyboardRef);
    keyboardRef.dismiss = () => {
      this.setClickEventPropagation(false);
      dismissFunction();
    }
    return keyboardRef;
  }

  private setClickEventPropagation(enabled: boolean): void {
    const action = (element: Element) => (enabled ? Element.prototype.addEventListener : Element.prototype.removeEventListener)
      .call(element, 'click', Function.prototype.apply.bind(Event.prototype.stopPropagation));
    Array.from(document.getElementsByClassName('mat-keyboard-wrapper'))
      .forEach(action);
  }
}

You might want to track the input-event of the input field. This service will not open the autocomplete popup if you're starting from an empty input field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants