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 (forms): clear selected options when model is not an array #12519
Conversation
8eae1a4
to
6f05c93
Compare
I fixed a lint error in my code. 2 of the travis-ci build steps are still failing though: |
this._optionMap.forEach((opt, o) => { opt._setSelected(ids.indexOf(o.toString()) > -1); }); | ||
let optionSelectedStateSetter: (opt: NgSelectMultipleOption, o: any) => void; | ||
if (Array.isArray(value)) { | ||
let values: Array<any> = <Array<any>>value; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const values: any[] = <any[]>values;
but using value
directly should also work because of Array.isArray(value)
@@ -701,6 +701,66 @@ export function main() { | |||
})); | |||
}); | |||
|
|||
describe('select multiple controls', () => { | |||
var fixture: ComponentFixture<NgModelSelectMultipleForm>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var -> let (or const where applicable)
6f05c93
to
0cab245
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor comments, but looks good
` | ||
}) | ||
class NgModelSelectMultipleForm { | ||
@Input() selectedCities: any[]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The @input decorator here isn't necessary - can you remove?
comp.cities = [{'name': 'SF'}, {'name': 'NYC'}, {'name': 'Buffalo'}]; | ||
}); | ||
|
||
const setSelectedCitiesDetectChangesAndTick = (selectedCities: any): void => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you rename to setSelectedCities
? The name is a bit of a handful to read.
const assertOptionElementSelectedState = (selectedStates: boolean[]): void => { | ||
const options = fixture.debugElement.queryAll(By.css('option')); | ||
if (options.length !== selectedStates.length) { | ||
throw `there are ${options.length} option elements but ${selectedStates.length}` + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd wrap the whole error message in backticks so you don't have to concat the string. Or if you're trying to avoid the line break, I'd just make it shorter.
0cab245
to
e17bf59
Compare
When an invalid model value (eg empty string) was preset ngModel on select[multiple] would throw an error, which is inconsistent with how it works on other user input elements. Setting the model value to null or undefined would also have no effect on what was already selected in the UI. Fix this by clearing selected options when model set to null, undefined or a type other than Array. Closes angular#11926
e17bf59
to
a24ae95
Compare
PR updated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
When an invalid model value (eg empty string) was preset ngModel on select[multiple] would throw an error, which is inconsistent with how it works on other user input elements. Setting the model value to null or undefined would also have no effect on what was already selected in the UI. Fix this by clearing selected options when model set to null, undefined or a type other than Array. Closes #11926
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Please check if the PR fulfills these requirements
What kind of change does this PR introduce? (check one with "x")
What is the current behavior? (You can also link to an open issue here)
The repro in Issue SelectMultipleControlValueAccessor is a string and fails with values.map does not exist #11926 includes the use of ngModel on a select element, where ngModel is not explicitly bound. This result in an initial model value for the select of an empty string. (I'm going to raise a separate issue about this ngModel behavior, which I suspect is related to one time string initialization.)
What is the new behavior?
Does this PR introduce a breaking change? (check one with "x")