Skip to content

fix(cdk/listbox): incorrectly validating preselected value #25893

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
36 changes: 36 additions & 0 deletions src/cdk/listbox/listbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,18 @@ describe('CdkOption and CdkListbox', () => {
fixture.detectChanges();
}).toThrowError('Listbox has selected values that do not match any of its options.');
});

it('should not throw on init with a preselected form control and a dynamic set of options', () => {
expect(() => {
setupComponent(ListboxWithPreselectedFormControl, [ReactiveFormsModule]);
}).not.toThrow();
});

it('should throw on init if the preselected value is invalid', () => {
expect(() => {
setupComponent(ListboxWithInvalidPreselectedFormControl, [ReactiveFormsModule]);
}).toThrowError('Listbox has selected values that do not match any of its options.');
});
});
});

Expand Down Expand Up @@ -955,6 +967,30 @@ class ListboxWithFormControl {
isActiveDescendant = false;
}

@Component({
template: `
<div cdkListbox [formControl]="formControl">
<div *ngFor="let option of options" [cdkOption]="option">{{option}}</div>
</div>
`,
})
class ListboxWithPreselectedFormControl {
options = ['a', 'b', 'c'];
formControl = new FormControl('c');
}

@Component({
template: `
<div cdkListbox [formControl]="formControl">
<div *ngFor="let option of options" [cdkOption]="option">{{option}}</div>
</div>
`,
})
class ListboxWithInvalidPreselectedFormControl {
options = ['a', 'b', 'c'];
formControl = new FormControl('d');
}

@Component({
template: `
<ul cdkListbox>
Expand Down
31 changes: 18 additions & 13 deletions src/cdk/listbox/listbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ export class CdkListbox<T = unknown> implements AfterContentInit, OnDestroy, Con
ngAfterContentInit() {
if (typeof ngDevMode === 'undefined' || ngDevMode) {
this._verifyNoOptionValueCollisions();
this._verifyOptionValues();
}

this._initKeyManager();
Expand Down Expand Up @@ -561,19 +562,7 @@ export class CdkListbox<T = unknown> implements AfterContentInit, OnDestroy, Con
*/
writeValue(value: readonly T[]): void {
this._setSelection(value);

if (typeof ngDevMode === 'undefined' || ngDevMode) {
const selected = this.selectionModel.selected;
const invalidValues = this._getInvalidOptionValues(selected);

if (!this.multiple && selected.length > 1) {
throw Error('Listbox cannot have more than one selected value in multi-selection mode.');
}

if (invalidValues.length) {
throw Error('Listbox has selected values that do not match any of its options.');
}
}
this._verifyOptionValues();
}

/**
Expand Down Expand Up @@ -924,6 +913,22 @@ export class CdkListbox<T = unknown> implements AfterContentInit, OnDestroy, Con
});
}

/** Verifies that the option values are valid. */
private _verifyOptionValues() {
if (this.options && (typeof ngDevMode === 'undefined' || ngDevMode)) {
const selected = this.selectionModel.selected;
const invalidValues = this._getInvalidOptionValues(selected);

if (!this.multiple && selected.length > 1) {
throw Error('Listbox cannot have more than one selected value in multi-selection mode.');
}

if (invalidValues.length) {
throw Error('Listbox has selected values that do not match any of its options.');
}
}
}

/**
* Coerces a value into an array representing a listbox selection.
* @param value The value to coerce
Expand Down