Skip to content

bug(mat-chip-option): mat-chip-option doesn't listen for [selected] input #25939

Closed
@e-oz

Description

@e-oz

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

Simple code:

<ng-container *ngIf="vm$|async as $">
  <mat-chip-listbox
      aria-label="Roles selection"
      [multiple]="true"
  >
    <mat-chip-option
        *ngFor="let role of $.allRoles" [value]="role"
        [selected]="collection.hasItemIn(role, $.roles)"
        (click)="collection.hasItemIn(role, $.roles) ? removeRole(role) : addRole(role)"
    >{{role.name}}</mat-chip-option>
  </mat-chip-listbox>
</ng-container>

Whenever I click the chip, visually its "selected" state is being toggled, but it absolutely ignores what [selected] is sending.
I know that there is selectionChange output, but the point is: I want to control the "selected" state, not just toggle it automatically.

Simplified code from StackBlitz:

<mat-chip-listbox aria-label="Roles selection" [multiple]="true">
  <mat-chip-option
    *ngFor="let role of allRoles"
    [value]="role"
    [selected]="selectedRoles.has(role)"
    (click)="selectedRoles.has(role) ? removeRole(role) : addRole(role)"
    >{{role.name}}</mat-chip-option
  >
</mat-chip-listbox>

Reproduction

Code is here, but I can't make it run with Angular 15 on StackBlitz:
https://stackblitz.com/edit/components-issue-w1yexo?file=src/app/example-component.ts

Expected Behavior

[selected] should not be overriden by the internal state. Quite the contrary, [selected] should define the state.

Actual Behavior

[selected] is being ignored

Environment

  • Angular: 15.0.0-rc2
  • CDK/Material: 15.0.0-rc1
  • Browser(s): Chrome
  • Operating System (e.g. Windows, macOS, Ubuntu): macOS 13

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageThis issue needs to be triaged by the team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions