What is the trick to maintaining the chiplist autocomplete suggestions visible even after inserting or deleting a chip?

After creating an autocomplete chiplist component in Angular, I noticed that when a user adds or removes an item, the suggestion list disappears and the input field is cleared.

However, I would prefer to keep the autocomplete list open (or reopen it) so that the user does not have to trigger it by typing in the input field again.

Currently, the autocomplete only opens when the user starts typing;

chip-list.component.ts

constructor() {
  this.filteredItems = this.itemCtrl.valueChanges.pipe(
    map((item: string | T) => {
      if (typeof item === 'string') {
        return this._filter(item);
      }
      return this.allItems.filter(
        (x) => this.selectedItems.indexOf(x) === -1
      );
    })
  );
}

chip-list.component.html

<mat-form-field [formGroup]="group" class="mat-form-field-prefix">
  <mat-chip-list #chipList aria-label="Item selection">
    <mat-icon *ngIf="iconName" matPrefix>{{ iconName }}</mat-icon>
    <mat-chip
      *ngFor="let item of selectedItems"
      [selectable]="false"
      [removable]="true"
      (removed)="remove(item)"
      (click)="remove(item)"
    >
      {{ this.getItemName(item) }}
      <mat-icon matChipRemove>cancel</mat-icon>
    </mat-chip>
    <input
      matInput
      placeholder=""
      #itemInput
      (click)="itemCtrl.valueChanges.emit()"
      [formControl]="itemCtrl"
      [matAutocomplete]="auto"
      [matChipInputFor]="chipList"
      [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
      class="mat-form-field-infix"
    />
  </mat-chip-list>
  <mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
    <mat-option
      *ngFor="let item of filteredItems | async"
      [value]="item"
    >
      {{ this.getItemName(item) }}
    </mat-option>
  </mat-autocomplete>
</mat-form-field>

Answer №1

execute the function openPanel(). You can name the reference #operationTrigger as you like.

<input
      matInput
      placeholder=""
      #itemInput
      #operationTrigger="matAutocompleteTrigger"
      (click)="itemCtrl.valueChanges.emit()"
      [formControl]="itemCtrl"
      [matAutocomplete]="auto"
      [matChipInputFor]="chipList"
      [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
      class="mat-form-field-infix"
    />

and in your mat-option within mat-autocomplete, call a function when clicked like this.

(click)="operationTrigger.openPanel()"

<mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
    <mat-option
      *ngFor="let item of filteredItems | async"
      [value]="item"
      (click)="operationTrigger.openPanel()">
      {{ this.getItemName(item) }}
    </mat-option>
  </mat-autocomplete>

I hope this solution works well for you.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Struggling with Primeng's KeyFilter functionality?

I've implemented the KeyFilter Module of primeng in my project. Check out the code snippet below: <input type="text" pInputText [(ngModel)]="price.TintCost" [pKeyFilter]="patternDecimal" name="tintCost" required="true" /> Take a look at my Typ ...

Struggling with hashtags and ampersands in Angular when making an HTTP request

Dealing with Special Characters # and & in Angular's http.get() Request URL Take a look at my code first. Angular Service let versionsearch = "&"; let strweeksearch = "%23"; this.http.get(this.apiUrl + 'GetVersionInfo?vehicleVersion=' + v ...

Developing Derived Classes in Typescript

I am looking to enhance my service class by creating a subclass where I can define functions with the same name but different implementations. My desired structure is as follows: httpWrapper.get //default is observables. returns observable httpWrapper.pr ...

Experiencing difficulty with Angular's connectivity to API via HTTP due to the absence of the CORS header 'Access-Control-Allow-Origin'

My current challenge involves retrieving data from an API using http in Angular. When I trigger the function through ngOnInit, the data is successfully fetched: handleError(error: HttpErrorResponse) { return throwError(() => new Error('Somethin ...

The hidden pop-up window from a particular tool is currently not being displayed

I attempted to upload my code onto Stackblitz in search of assistance with my dynamic modal not displaying. I am working on a function that I intend to be able to call from any component to create a popup where I can dynamically modify the header, body, an ...

What is causing pre-defined variables to act unexpectedly in Cypress?

Encountering unexpected results while pre-defining element variables and using them later in Cypress 10 with Cucumber. Let's take a look at this login test: Given("I'm logged in as Default user", () => { cy.visit('/ ...

Angular ListView and GridView is a powerful feature that allows developers to display

I am looking to create Listview and Gridview components using Angular with the following features: Paging Sorting Filtering Searching In addition, the Gridview should also have support for Nested Gridview. What are currently the most commonly us ...

Is it achievable to validate image dimensions using angular forms? Or, how can one go about validating image dimensions through angular forms?

I have encountered an issue with using Angular forms, file input type, and custom validators. Whenever the form value changes, I only receive a fake path URL. This fake path URL does not contain any file metadata, so how can I validate image dimensions? ...

Issue with CSS files in Jest"errors"

I'm currently facing an issue while trying to pass my initial Jest Test in React with Typescript. The error message I am encountering is as follows: ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){.App ...

TypeScript mandates the inclusion of either one parameter or the other, without the possibility of having neither

Consider the following type: export interface Opts { paths?: string | Array<string>, path?: string | Array<string> } The requirement is that the user must provide either 'paths' or 'path', but it is not mandatory to pa ...

Backend communication functions seamlessly within the service scope, yet encounters obstacles beyond the service boundaries

I'm facing an issue with accessing data from my backend. Although the service successfully retrieves and logs the data, when I try to use that service in a different module, it either shows "undefined" or "Observable". Does anyone have any suggestions ...

Retrieving the return type of generic functions stored in a map

In this unique scenario, I have a dictionary with polymorphic functions that accept the same argument but return different results: const dict = { one: { foo<A>(a: A) { return [a] as const } }, two: { foo<A>(a: A) { ...

Trigger a click event on a nested Angular 13 component to remove a class from its grandparent component

In my Angular 13 project, I am working with 3 components: Child Component : Burger-menu Parent Component : Header Grand-Parent Component : app.component.html Within the burger-menu component, there is a close button. When this button is clicked, I want t ...

Currently in the process of modernizing an outdated method to a more up-to-date version in Jasmine, encountering issues related to

Currently working to update the method throwOnExpectationFailure to the newer one oneFailurePerSpec. According to the Jasmine documentation, this can be achieved by: Use the `oneFailurePerSpec` option with Env#configure After conducting research, I came ...

`ng-apexcharts` causing unit test failures

I've been working on integrating apexcharts and ng-apexcharts into my home component. While I was able to get it up and running smoothly, it seems to be causing issues with my unit tests. Despite researching possible solutions, I haven't been abl ...

Exploring the capabilities of using the injectGlobal API from styled-components in a TypeScript

I've been attempting to utilize the simple injectGlobal API, but I am encountering difficulties getting it to work with TypeScript. Here is my setup in theme.tsx: import * as styledComponents from "styled-components"; import { ThemedStyledComponentsM ...

The Angular project was functioning properly when tested locally, but encountered an error in the Quill Editor during the building process

I have encountered an issue when deploying my Angular 8 + Quill project. Everything works fine locally with 'ng serve', but upon deployment, I am facing the following error. Despite trying various solutions like updating Angular or deleting &apos ...

Is it possible for Angular components to retrieve information on the current route and parameters?

I am struggling to understand why it's so difficult... why we are not supposed to care about the route from outside the routed component.... HOWEVER: I am attempting to develop a service so that my header (and any other component) can have access to ...

Issue encountered while trying to run `npm install` on an angular-cli

I recently moved my angular-cli project with node modules to a new directory. Upon running npm install, I encountered the following warnings: npm WARN deprecated <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e2838c85978e8 ...

Troubleshooting connectivity issues between Entities in microORM and Next.js

While trying to run my Next.js application in typescript, I encountered the following error: Error - ReferenceError: Cannot access 'Member' before initialization After consulting the documentation at https://mikro-orm.io/docs/relationships#relat ...