Skip to content

feat(material/list): support two-data binding on list option selected #23125

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

Merged
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
11 changes: 11 additions & 0 deletions src/material-experimental/mdc-list/list-option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ import {
Component,
ContentChildren,
ElementRef,
EventEmitter,
Inject,
InjectionToken,
Input,
NgZone,
OnDestroy,
OnInit,
Optional,
Output,
QueryList,
ViewChild,
ViewEncapsulation
Expand Down Expand Up @@ -96,6 +98,14 @@ export class MatListOption extends MatListItemBase implements ListOption, OnInit
*/
private _inputsInitialized = false;

/**
* Emits when the selected state of the option has changed.
* Use to facilitate two-data binding to the `selected` property.
* @docs-private
*/
@Output()
readonly selectedChange: EventEmitter<boolean> = new EventEmitter<boolean>();

@ViewChild('text') _itemText: ElementRef<HTMLElement>;

@ContentChildren(MatLine, {read: ElementRef, descendants: true}) lines:
Expand Down Expand Up @@ -241,6 +251,7 @@ export class MatListOption extends MatListItemBase implements ListOption, OnInit
this._selectionList.selectedOptions.deselect(this);
}

this.selectedChange.emit(selected);
this._changeDetectorRef.markForCheck();
return true;
}
Expand Down
48 changes: 48 additions & 0 deletions src/material-experimental/mdc-list/selection-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,44 @@ describe('MDC-based MatSelectionList without forms', () => {
});

});

describe('with single selection', () => {
let fixture: ComponentFixture<ListOptionWithTwoWayBinding>;
let optionElement: HTMLElement;
let option: MatListOption;

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [MatListModule],
declarations: [ListOptionWithTwoWayBinding],
}).compileComponents();

fixture = TestBed.createComponent(ListOptionWithTwoWayBinding);
fixture.detectChanges();
const optionDebug = fixture.debugElement.query(By.directive(MatListOption));
option = optionDebug.componentInstance;
optionElement = optionDebug.nativeElement;
}));

it('should sync the value from the view to the option', () => {
expect(option.selected).toBe(false);

fixture.componentInstance.selected = true;
fixture.detectChanges();

expect(option.selected).toBe(true);
});

it('should sync the value from the option to the view', () => {
expect(fixture.componentInstance.selected).toBe(false);

optionElement.click();
fixture.detectChanges();

expect(fixture.componentInstance.selected).toBe(true);
});
});

});

describe('MDC-based MatSelectionList with forms', () => {
Expand Down Expand Up @@ -1651,3 +1689,13 @@ class SelectionListWithIndirectChildOptions {
})
class SelectionListWithIndirectDescendantLines {
}


@Component({template: `
<mat-selection-list>
<mat-list-option [(selected)]="selected">Item</mat-list-option>
</mat-selection-list>
`})
class ListOptionWithTwoWayBinding {
selected = false;
}
47 changes: 47 additions & 0 deletions src/material/list/selection-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,43 @@ describe('MatSelectionList without forms', () => {
});

});

describe('with single selection', () => {
let fixture: ComponentFixture<ListOptionWithTwoWayBinding>;
let optionElement: HTMLElement;
let option: MatListOption;

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [MatListModule],
declarations: [ListOptionWithTwoWayBinding],
}).compileComponents();

fixture = TestBed.createComponent(ListOptionWithTwoWayBinding);
fixture.detectChanges();
const optionDebug = fixture.debugElement.query(By.directive(MatListOption));
option = optionDebug.componentInstance;
optionElement = optionDebug.nativeElement;
}));

it('should sync the value from the view to the option', () => {
expect(option.selected).toBe(false);

fixture.componentInstance.selected = true;
fixture.detectChanges();

expect(option.selected).toBe(true);
});

it('should sync the value from the option to the view', () => {
expect(fixture.componentInstance.selected).toBe(false);

optionElement.click();
fixture.detectChanges();

expect(fixture.componentInstance.selected).toBe(true);
});
});
});

describe('MatSelectionList with forms', () => {
Expand Down Expand Up @@ -1828,3 +1865,13 @@ class SelectionListWithIndirectChildOptions {
})
class SelectionListWithIndirectDescendantLines {
}


@Component({template: `
<mat-selection-list>
<mat-list-option [(selected)]="selected">Item</mat-list-option>
</mat-selection-list>
`})
class ListOptionWithTwoWayBinding {
selected = false;
}
9 changes: 9 additions & 0 deletions src/material/list/selection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ export class MatListOption extends _MatListOptionBase implements AfterContentIni
@ContentChild(MatListIconCssMatStyler) _icon: MatListIconCssMatStyler;
@ContentChildren(MatLine, {descendants: true}) _lines: QueryList<MatLine>;

/**
* Emits when the selected state of the option has changed.
* Use to facilitate two-data binding to the `selected` property.
* @docs-private
*/
@Output()
readonly selectedChange: EventEmitter<boolean> = new EventEmitter<boolean>();

/** DOM element containing the item's text. */
@ViewChild('text') _text: ElementRef;

Expand Down Expand Up @@ -300,6 +308,7 @@ export class MatListOption extends _MatListOptionBase implements AfterContentIni
this.selectionList.selectedOptions.deselect(this);
}

this.selectedChange.emit(selected);
this._changeDetector.markForCheck();
return true;
}
Expand Down
3 changes: 2 additions & 1 deletion tools/public_api_guard/material/list.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export declare class MatListOption extends _MatListOptionBase implements AfterCo
set disabled(value: any);
get selected(): boolean;
set selected(value: boolean);
readonly selectedChange: EventEmitter<boolean>;
selectionList: MatSelectionList;
get value(): any;
set value(newValue: any);
Expand All @@ -82,7 +83,7 @@ export declare class MatListOption extends _MatListOptionBase implements AfterCo
static ngAcceptInputType_disableRipple: BooleanInput;
static ngAcceptInputType_disabled: BooleanInput;
static ngAcceptInputType_selected: BooleanInput;
static ɵcmp: i0.ɵɵComponentDeclaration<MatListOption, "mat-list-option", ["matListOption"], { "disableRipple": "disableRipple"; "checkboxPosition": "checkboxPosition"; "color": "color"; "value": "value"; "disabled": "disabled"; "selected": "selected"; }, {}, ["_avatar", "_icon", "_lines"], ["*", "[mat-list-avatar], [mat-list-icon], [matListAvatar], [matListIcon]"]>;
static ɵcmp: i0.ɵɵComponentDeclaration<MatListOption, "mat-list-option", ["matListOption"], { "disableRipple": "disableRipple"; "checkboxPosition": "checkboxPosition"; "color": "color"; "value": "value"; "disabled": "disabled"; "selected": "selected"; }, { "selectedChange": "selectedChange"; }, ["_avatar", "_icon", "_lines"], ["*", "[mat-list-avatar], [mat-list-icon], [matListAvatar], [matListIcon]"]>;
static ɵfac: i0.ɵɵFactoryDeclaration<MatListOption, never>;
}

Expand Down