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

md-select doesn't reset placeholder effectively "clearing" selection #2634

Closed
skeize opened this issue Jan 13, 2017 · 4 comments · Fixed by #3141
Closed

md-select doesn't reset placeholder effectively "clearing" selection #2634

skeize opened this issue Jan 13, 2017 · 4 comments · Fixed by #3141
Labels
feature This issue represents a new feature or feature request rather than a bug or bug fix

Comments

@skeize
Copy link

skeize commented Jan 13, 2017

It's entirely possible I'm not doing things right, but... the placeholder within the md-select doesn't appear to be resetting from it's floating position after selecting an empty md-option.. effectively clearing the selection.

Here's what I've got:

    <md-select name="thing" placeholder="Thing" formControlName="thing">
        <md-option></md-option>
        <md-option *ngFor="let t of things" [value]="t.id">{{t.name}}</md-option>
    </md-select>

My form/controls are defined as follow:

    thing = new FormControl(null, Validators.required);

    this.editForm = new FormGroup({
        thing: this.thing,
        ...
    });

When initialized with a null value, the placeholder appears as it should, after choosing an option, then choosing the empty option (), the form control's value is set to null, however the placeholder doesn't leave the floating position.

I've also tried setting the empty option's value to null (<md-option [value]="null">) with no success.

I'm using Chrome on Windows 10.
Angular 2.4.3
Material 2.0.0-beta.1

Thanks!

@ig16022
Copy link

ig16022 commented Jan 13, 2017

I am seeing the same behavior. Does anyone have a workaround for this?

@codef0rmer
Copy link

codef0rmer commented Jan 14, 2017

@skeize @ig16022 When you select the first option without value and text, the thing property is set to undefined (or whatever value you set in the template) which matches with the value of the first option i.e no value meaning undefined, resulting in not clearing the placeholder. Even if you set the value to null i.e. <md-option value="null"></md-option>, it won't work because it has to be null not "null".

So to fix the issue, you have to watch for the model change and explicitly nullify it:

<md-select name="thing" placeholder="Thing" formControlName="thing" (ngModelChange)="thing = (thing == undefined ? null : thing)">
        <md-option></md-option>
        <md-option *ngFor="let t of things" [value]="t.id">{{t.name}}</md-option>
</md-select>

@kara
Copy link
Contributor

kara commented Jan 24, 2017

@skeize We haven't yet implemented a convenient "clearing" API by clicking an option like there was in Material 1. In the meantime, you can do something like this:

<md-select placeholder="Thing" formControlName="thing">
   <md-option (click)="thing.reset()"></md-option>
   <md-option *ngFor="let t of things" [value]="t.id">{{t.name}}</md-option>
</md-select>

I had a note for this on the md-select board, but I'll keep this issue open so people can track when it gets in.

@kara kara added the feature This issue represents a new feature or feature request rather than a bug or bug fix label Jan 24, 2017
crisbeto added a commit to crisbeto/material2 that referenced this issue Feb 16, 2017
…tion

Allows for options, with a value of `null` or `undefined`, to clear the select. This is similar to the way the native select works.

Fixes angular#3110.
Fixes angular#2634.
crisbeto added a commit to crisbeto/material2 that referenced this issue Feb 24, 2017
…tion

Allows for options, with a value of `null` or `undefined`, to clear the select. This is similar to the way the native select works.

Fixes angular#3110.
Fixes angular#2634.
crisbeto added a commit to crisbeto/material2 that referenced this issue May 9, 2017
…tion

Allows for options, with a value of `null` or `undefined`, to clear the select. This is similar to the way the native select works.

Fixes angular#3110.
Fixes angular#2634.
crisbeto added a commit to crisbeto/material2 that referenced this issue May 22, 2017
…tion

Allows for options, with a value of `null` or `undefined`, to clear the select. This is similar to the way the native select works.

Fixes angular#3110.
Fixes angular#2634.
tinayuangao pushed a commit that referenced this issue May 23, 2017
…tion (#3141)

* fix(select): allow option with undefined or null value to clear selection

Allows for options, with a value of `null` or `undefined`, to clear the select. This is similar to the way the native select works.

Fixes #3110.
Fixes #2634.

* fix: address feedback
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature This issue represents a new feature or feature request rather than a bug or bug fix
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants