Skip to content

docs(autocomplete): Adds option group example #10666

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 2 commits into from
Apr 18, 2018

Conversation

raygervais
Copy link
Contributor

fixes #10196,
StackBlitz example: https://stackblitz.com/edit/angular-smg2xm?file=app/app.component.ts

Adds a option group example which displays ideal logic and best practices for the material autocomplete component when using said groups.

Adds a option group example which displays ideal logic and best
practices for the material autocomplete component.
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Apr 2, 2018
</mat-optgroup>
</mat-autocomplete>
</mat-form-field>
</div>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary closing tag.

@@ -0,0 +1,13 @@
<form [formGroup]="animalForm" novalidate>
<mat-form-field>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation seems to be wrong here.

<input type="text" matInput placeholder="Species Group" formControlName="speciesGroup" required [matAutocomplete]="autoGroup"/>
<mat-autocomplete #autoGroup="matAutocomplete">
<mat-optgroup *ngFor="let group of speciesGroupeOptions | async" [label]="group.letter">
<mat-option *ngFor="let name of group.name" [value]="name">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space before the value binding.

</mat-autocomplete>
</mat-form-field>
</div>
</form>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs an extra line at the end of the file.

@@ -0,0 +1,63 @@
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spacing around the imports is inconsistent.

class SpeciesGroup {
letter:string;
name: string[];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a newline at the end of the file.

}

class SpeciesGroup {
letter:string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a space after letter:.


class SpeciesGroup {
letter:string;
name: string[];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is an array it should be called names.


buildForm() {
this.animalForm = this.fb.group({
species: ['', [Validators.required]],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that we should start putting validation on this. We try to keep the examples as simple as possible and in this case the validation doesn't add anything.

filterGroup(val: string): SpeciesGroup[] {
if (val) {
return this.speciesGroup
.map(group => ({ letter: group.letter, name: this._filter(group.name, val) }))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The map and the filter need some indentation.

@raygervais
Copy link
Contributor Author

Fixed @crisbeto, I also forked the original StackBlitz and made the updates here for visual testing / inspection. Thanks for pointing out the inconsistencies, it appears I need more coffee in my system before I touch a keyboard again ☕️

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Observable } from 'rxjs/Observable';
import { startWith } from 'rxjs/operators/startWith';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general we don't do spaces around the imports. E.g. these should be import {startWith} from 'rxjs/operators/startWith';


ngOnInit() {
this.stateGroupOptions = this.stateForm.get('stateGroup').valueChanges
.pipe(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .pipe should either be on the previous line or be indented.

stateGroupOptions: Observable<StateGroup[]>;

constructor(private fb: FormBuilder) {
this.stateForm = this.fb.group({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can initialize this one when you declare the property.

names: ['Vermont', 'Virginia']
}, {
letter: 'W',
names: ['Washingto n', 'West Virginia', 'Wisconsin', 'Wyoming']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space before the "n".

@raygervais raygervais force-pushed the autocomplete-documentation branch from 84b5503 to fcb8824 Compare April 4, 2018 00:12
@raygervais
Copy link
Contributor Author

Thanks for the reviews @crisbeto, I do apologize for the little items such as the spacing which really I should be catching as well. Will make note to improve and be diligent about that.

@raygervais
Copy link
Contributor Author

Just noticed the lint and e2e failures, will address today.

@raygervais raygervais force-pushed the autocomplete-documentation branch from fcb8824 to 337fcbc Compare April 5, 2018 00:56
@josephperrott josephperrott added the target: patch This PR is targeted for the next patch release label Apr 6, 2018
@raygervais
Copy link
Contributor Author

Sorry for the delay, I've been wrestling school obligations and apartment hunting prior to starting a new job. Does anyone have any idea how to fix the lint error here? I have not seen it before:

ERROR: /home/travis/build/angular/material2/src/material-examples/autocomplete-optgroup/autocomplete-optgroup-example.ts[2, 33]: 'Validators' is declared but its value is never read.

Here is the code that I'm trying to use:

stateForm: FormGroup = this.fb.group({
  stateGroup: '',
});

@rafaelss95
Copy link
Contributor

Just remove Validators from import.

@raygervais raygervais force-pushed the autocomplete-documentation branch from 337fcbc to e046c28 Compare April 9, 2018 16:39
@@ -0,0 +1,12 @@
<form [formGroup]="stateForm" novalidate>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

novalidate isn't necessary.

@raygervais raygervais force-pushed the autocomplete-documentation branch from e046c28 to 1589821 Compare April 9, 2018 16:52
}
}

class StateGroup {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could replace this by an Interface and move this to the top of the file (after imports);

@raygervais raygervais force-pushed the autocomplete-documentation branch 3 times, most recently from 32976e5 to 76a8680 Compare April 16, 2018 00:27
Changes code style and logic to expected logic and styleguide
@raygervais raygervais force-pushed the autocomplete-documentation branch from 76a8680 to 23f7333 Compare April 16, 2018 00:49
@raygervais
Copy link
Contributor Author

Thanks for the tips @rafaelss95, and sorry for the delay. I've fixed the Lint / E2E issues, and the failing test case relates to Material Table. If there is any other fix needed, I'd be happy to correct 👍

Copy link
Member

@crisbeto crisbeto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@crisbeto crisbeto added pr: lgtm action: merge The PR is ready for merge by the caretaker labels Apr 16, 2018
@mmalerba mmalerba merged commit 1cc0c7f into angular:master Apr 18, 2018
@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 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Example about mat-optgroup inside mat-autocomplete
6 participants