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

Autocomplete should always handle display value and model separately #14086

Open
gempain opened this issue Nov 10, 2018 · 4 comments
Open

Autocomplete should always handle display value and model separately #14086

gempain opened this issue Nov 10, 2018 · 4 comments
Labels
area: material/autocomplete feature This issue represents a new feature or feature request rather than a bug or bug fix needs: discussion Further discussion with the team is needed before proceeding P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@gempain
Copy link

gempain commented Nov 10, 2018

Bug, feature request, or proposal:

Feature request / proposal

What is the expected behavior?

Assuming the following:

TS

options = [{name: 'One'}, {name: 'Two'}];
text: string;

HTML

<input type="text" matInput [(ngModel)]="text" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
    <mat-option *ngFor="let option of options" [value]="option">
      {{option.name}}
    </mat-option>
</mat-autocomplete>

I expect text to be of type string at any time. When selecting an option from the dropdown, I do not expect that value to be assigned to the input value. MatAutocomplete should provide a separate ngModel to be bound to. Sure, the selected option should be displayed in the input, but using a string. This is possible via the [displayWith] input of <mat-autocomplete>, but should not be an "opt-in" feature, it should be required.

What is the current behavior?

When you select an option, text is now of type {name: string}, because Angular Material autocomplete assigns the selected value to it.

What are the steps to reproduce?

What is the use-case or motivation for changing an existing behavior?

When you fetch search results from a backend, you're usually subscribing to valueChanges (if using FormControl) or ngModelChange (if using ngModel). You would expect such subscriptions to stream values of the same type at runtime.

In the docs, you have worked around this problem by doing the following:

map(value => typeof value === 'string' ? value : value.name),

Which works fine, but is not intuitive, and semantically awkward. The docs aren't clear about this specific semantic inconsistency which may cause users (like me) to waste some time trying to understand why typing is not preserved until realizing it's not a bug, but an intended behavior.

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Angular 7

Is there anything else we should know?

@josephperrott josephperrott added feature This issue represents a new feature or feature request rather than a bug or bug fix discussion labels Nov 26, 2018
@mmalerba mmalerba added needs: discussion Further discussion with the team is needed before proceeding and removed discussion labels Mar 3, 2020
@mmalerba mmalerba added the needs triage This issue needs to be triaged by the team label May 20, 2020
@crisbeto crisbeto added area: material/autocomplete P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent and removed needs triage This issue needs to be triaged by the team labels May 29, 2020
@angular-robot
Copy link
Contributor

angular-robot bot commented Feb 1, 2022

Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

Find more details about Angular's feature request process in our documentation.

@angular-robot
Copy link
Contributor

angular-robot bot commented Feb 22, 2022

Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage.

We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.

You can find more details about the feature request process in our documentation.

@to-ros
Copy link

to-ros commented Jul 27, 2022

Is there any update on this?
I recently implemented a mat-autocomplete in a component with reactive forms and it just felt messy.
It works but it doesn't feel like an optimal implementation, it would be really nice if an OnSearch event was fired or have a seperate form control that could be bound to.
This way I wouldn't have to expand my FormControl<T> type to include string and type check typeOf x === 'string' every time when reading values.
Also if the result value type actually needed to be a string it is impossible to differentiate between a search value and an actual result.

@morgan-germain
Copy link

exact ! Since FormControl are strict-typed objects, this issue becomes more important to consider

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: material/autocomplete feature This issue represents a new feature or feature request rather than a bug or bug fix needs: discussion Further discussion with the team is needed before proceeding P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
Development

No branches or pull requests

7 participants