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

fix(material/chips): chip set overwriting disabled state #29795

Merged
merged 1 commit into from
Sep 30, 2024
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
23 changes: 23 additions & 0 deletions src/material/chips/chip-listbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ describe('MatChipListbox', () => {
expect(chipListboxNativeElement.hasAttribute('role')).toBe(false);
expect(chipListboxNativeElement.hasAttribute('aria-required')).toBe(false);
});

it('should toggle the chips disabled state based on whether it is disabled', fakeAsync(() => {
fixture.destroy();
TestBed.resetTestingModule();
const disabledFixture = createComponent(IndividuallyDisabledChipInsideForm);
disabledFixture.detectChanges();
flush();
expect(disabledFixture.componentInstance.chip.disabled).toBe(true);
}));
});

describe('with selected chips', () => {
Expand Down Expand Up @@ -1043,3 +1052,17 @@ class FalsyBasicChipListbox {
@ViewChild(MatChipListbox) chipListbox: MatChipListbox;
@ViewChildren(MatChipOption) chips: QueryList<MatChipOption>;
}

// Based on #29783.
@Component({
template: `
<form>
<mat-chip-listbox name="test" [ngModel]="null">
<mat-chip-option value="1" disabled>Hello</mat-chip-option>
</mat-chip-listbox>
</form>
`,
})
class IndividuallyDisabledChipInsideForm {
@ViewChild(MatChipOption) chip: MatChipOption;
}
10 changes: 4 additions & 6 deletions src/material/chips/chip-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,10 @@ export class MatChipSet implements AfterViewInit, OnDestroy {

/** Syncs the chip-set's state with the individual chips. */
protected _syncChipsState() {
if (this._chips) {
this._chips.forEach(chip => {
chip.disabled = this._disabled;
chip._changeDetectorRef.markForCheck();
});
}
this._chips?.forEach(chip => {
chip._chipListDisabled = this._disabled;
chip._changeDetectorRef.markForCheck();
});
}

/** Dummy method for subclasses to override. Base chip set cannot be focused. */
Expand Down
11 changes: 10 additions & 1 deletion src/material/chips/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ export class MatChip implements OnInit, AfterViewInit, AfterContentInit, DoCheck
/** Id of a span that contains this chip's aria description. */
_ariaDescriptionId = `${this.id}-aria-description`;

/** Whether the chip list is disabled. */
_chipListDisabled: boolean = false;

private _textElement!: HTMLElement;

/**
Expand Down Expand Up @@ -201,7 +204,13 @@ export class MatChip implements OnInit, AfterViewInit, AfterContentInit, DoCheck

/** Whether the chip is disabled. */
@Input({transform: booleanAttribute})
disabled: boolean = false;
get disabled(): boolean {
return this._disabled || this._chipListDisabled;
}
set disabled(value: boolean) {
this._disabled = value;
}
private _disabled = false;

/** Emitted when a chip is to be removed. */
@Output() readonly removed: EventEmitter<MatChipEvent> = new EventEmitter<MatChipEvent>();
Expand Down
4 changes: 3 additions & 1 deletion tools/public_api_guard/material/chips.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ export class MatChip implements OnInit, AfterViewInit, AfterContentInit, DoCheck
protected basicChipAttrName: string;
// (undocumented)
_changeDetectorRef: ChangeDetectorRef;
_chipListDisabled: boolean;
color?: string | null;
readonly destroyed: EventEmitter<MatChipEvent>;
disabled: boolean;
get disabled(): boolean;
set disabled(value: boolean);
disableRipple: boolean;
// (undocumented)
protected _document: Document;
Expand Down
Loading