Skip to content

docs(input, select): update docs and create more examples #7673

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 6 commits into from
Oct 12, 2017

Conversation

mmalerba
Copy link
Contributor

@mmalerba mmalerba commented Oct 9, 2017

No description provided.

@mmalerba mmalerba added docs This issue is related to documentation pr: merge safe labels Oct 9, 2017
@mmalerba mmalerba requested review from jelbourn and kara October 9, 2017 23:40
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Oct 9, 2017
@mmalerba mmalerba force-pushed the ff-docs branch 2 times, most recently from 84c55ba to b110cdd Compare October 9, 2017 23:56
Validators.email,
]);

matcher = new MyErrorStateMatcher();
Copy link
Contributor

Choose a reason for hiding this comment

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

In my experience, the custom matchers are very practical for showing errors when a parent FormGroup is invalid. As in,

  • Password/confirm must be equal
  • end date must be after begin date.

This requires either checking control.parent.valid or using a class property in the method. Maybe consider expanding the example? Or I'd be happy to give it a shot in a followup.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that sounds like a good thing to add. I'd like to keep this example around too since it's a little less complex and we don't have to worry about also making a custom validator

using any of the form directives from the core `FormsModule` or `ReactiveFormsModule`: `ngModel`,
`formControl`, etc. As with native `<select>`, `<mat-select>` also supports a `compareWith`
function. (Additional information about using a custom `compareWith` function can be found in the
[angular forms documentation](https://angular.io/api/forms/SelectControlValueAccessor#caveat-option-selection)).
Copy link
Contributor

Choose a reason for hiding this comment

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

Capitalize 'Angular'?

either the user has interacted with (touched) the element or the parent form has been submitted. If
you wish to override this behavior (e.g. to show the error as soon as the invalid control is dirty
or when a parent form group is invalid), you can use the `errorStateMatcher` property of the
`matInput`. The property takes an instance of an `ErrorStateMatcher` object. An `ErrorStateMatcher`
Copy link
Member

Choose a reason for hiding this comment

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

I think the more common use-case for customizing the ErrorStateMatcher is overriding the global one, rather than doing it per-control. Maybe we should have an example for that case as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately our examples aren't that flexible atm. We don't get to mess with the module. That's why I left an inline code snippet showing how to do it globally.

`mat-option` tag. Note that you can disable items by adding the `disabled` boolean attribute or
binding to it.
The select component is set up as a custom value accessor, so you can manipulate the select's value
using any of the form directives from the core `FormsModule` or `ReactiveFormsModule`: `ngModel`,
Copy link
Member

Choose a reason for hiding this comment

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

The select also supports setting the value via the value input which doesn't require forms.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done and added example


### Changing when error messages are shown

The `<mat-form-field>` allows you to
Copy link
Member

Choose a reason for hiding this comment

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

A few of these sections are going to be the same between the select and input. Maybe we should consider (not necessarily now) having a common page for them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I'd also like to consolidate the code itself so we're not duplicating it in input and select, just need to find a good way to make it work with change detection

@@ -461,6 +464,12 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
}
}

ngOnChanges(changes: SimpleChanges) {
if (changes.disabled) {
Copy link
Member

Choose a reason for hiding this comment

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

Did these changes make it through as a part of another branch?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was fixing a bug I noticed when making one of the demos

Copy link
Member

Choose a reason for hiding this comment

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

Add a comment here?

</mat-select>
<mat-hint>Errors appear instantly!</mat-hint>
<mat-error *ngIf="selected.hasError('required')">You must make a selection</mat-error>
<mat-error *ngIf="selected.hasError('pattern') && !selected.hasError('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'm not sure how common of a use case it is to have pattern validation on a select since the developer provides the possible values beforehand.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah its not the most practical example, but I just wanted to show a couple different error states

Copy link
Contributor

@kara kara left a comment

Choose a reason for hiding this comment

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

Mostly missing CSS links and comma nits. (We should either remove the file itself or add the link to it in the TS)

/** @title Select with reset option */
@Component({
selector: 'select-reset-example',
templateUrl: 'select-reset-example.html',
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing the link to the example CSS file.

/** @title Select with option groups */
@Component({
selector: 'select-optgroup-example',
templateUrl: 'select-optgroup-example.html',
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing link to CSS file

/** @title Select with cno option ripple */
@Component({
selector: 'select-no-ripple-example',
templateUrl: 'select-no-ripple-example.html',
Copy link
Contributor

Choose a reason for hiding this comment

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

Link to CSS file

@@ -0,0 +1,8 @@
import {Component} from '@angular/core';

/** @title Select with cno option ripple */
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo: cno -> no

/** @title Select with multiple selection */
@Component({
selector: 'select-multiple-example',
templateUrl: 'select-multiple-example.html',
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing CSS link here


This error occurs if you attempt to assign something other than a function to the `compareWith`
property. For more information on proper usage of `compareWith` see the
[angular forms documentation](https://angular.io/api/forms/SelectControlValueAccessor#caveat-option-selection)).
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: angular -> Angular


Global default placeholder options can be specified by setting the `MAT_PLACEHOLDER_GLOBAL_OPTIONS` provider. This setting will apply to all components that support the floating placeholder.
By default when a user clicks on a `<mat-option>` a ripple animation is shown. This can be disabled
Copy link
Contributor

Choose a reason for hiding this comment

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

By default, when a user clicks on a mat-option, a ripple animation is shown.


### Selecting multiple options

By default the `<mat-select>` only supports selecting a single option at a time. However you can use
Copy link
Contributor

Choose a reason for hiding this comment

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

By default, the mat-select


### Selecting multiple options

By default the `<mat-select>` only supports selecting a single option at a time. However you can use
Copy link
Contributor

Choose a reason for hiding this comment

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

However, you can use

/** @title Select with 2-way value binding */
@Component({
selector: 'select-value-binding-example',
templateUrl: 'select-value-binding-example.html',
Copy link
Member

Choose a reason for hiding this comment

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

Missing link to the CSS file.

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

@mmalerba
Copy link
Contributor Author

comments addressed. @kara we can't remove the empty CSS files because the example generator will barf. I added the links to the no-op files though

Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

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

Looked at the

the `MatFormFieldControl` interface, see its
[definition](https://github.com/angular/material2/blob/master/src/lib/form-field/form-field-control.ts).
the `MatFormFieldControl` interface, see the
[form field API documentation](https://material.angular.io/components/form-field/api).
(Unfortunately generated API docs are not available yet, but we'll go through the methods and
properties in this guide.)
Copy link
Member

Choose a reason for hiding this comment

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

Remove the Unfortunately... bit? I'd rather just get into the content than draw attention to it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually meant to delete this since it's not true anymore


<!-- example(input-prefix-suffix) -->
The `<mat-form-field>` allows you to
Copy link
Member

Choose a reason for hiding this comment

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

Should this be moved to the mat-form-field docs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ideally yes, in the future once the functionality is extracted into the form field, but currently this is up to the individual FormFieldControl to add support for custom error state matchers (and the attribute goes on the <input> not the <mat-form-field>).

In your template, create an `mat-select` element. For each option you'd like in your select, add an
`mat-option` tag. Note that you can disable items by adding the `disabled` boolean attribute or
binding to it.
The select component is set up as a custom value accessor, so you can manipulate the select's value
Copy link
Member

Choose a reason for hiding this comment

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

"is set up as..." -> "supports all directives from @angular/forms"?


### Getting and setting the select value
`<mat-select>` also supports 2-way binding to the `value` property without the need for Angular
Copy link
Member

Choose a reason for hiding this comment

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

Swap the order of using value and forms?

be specified either via a `placeholder` attribute on the `<mat-select>` or a `<mat-placeholder>`
element in the same form field as the `<mat-select>`. The `<mat-form-field>` also has additional
options for changing the behavior of the placeholder. For more information see the
[form field placeholder documentation](https://material.angular.io/components/form-field/overview#floating-placeholder).
Copy link
Member

Choose a reason for hiding this comment

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

Could this section be folded into the "Form field features" section?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd like to keep it here too since placeholder is an attribute that you place on the <mat-select> (in the future when we split the placeholder and label into separate concepts it can be completely handled by the form field).


### Selecting multiple options

By default, the `<mat-select>` only supports selecting a single option at a time. However, you can
Copy link
Member

Choose a reason for hiding this comment

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

"By default, the <mat-select> only supports selecting a single option at a time. "
->
"<mat-select> defaults to single-selection mode, but can be configured..."


#### Error: Cannot change `multiple` mode of select after initialization

This error is thrown if you attempt to bind the `multiple` property on `<mat-select>` to a dynamic
Copy link
Member

Choose a reason for hiding this comment

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

Why can't we change this at run-time?

Copy link
Member

@crisbeto crisbeto Oct 11, 2017

Choose a reason for hiding this comment

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

I put that in place when I initially implemented multiple selection. It's there because there's some ambiguity about what to do with the selected values when you're switching modes. Also it's unlikely that you'll need to toggle it after init anyway (we haven't had any issues about it yet).

</mat-select>
```

#### Error: Cannot assign truthy non-array value to select in `multiple` mode
Copy link
Member

Choose a reason for hiding this comment

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

Rename this error "Value must be an array in multiple-selection mode" ?

a `<mat-select multiple>`. For example, something like `mySelect.value = 'option1'`. What you likely
meant to do was `mySelect.value = ['option1']`.

#### Error: Cannot assign a non-function value to `compareWith`
Copy link
Member

Choose a reason for hiding this comment

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

Rename this error "compareWith must be a function"?

@@ -461,6 +464,12 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
}
}

ngOnChanges(changes: SimpleChanges) {
if (changes.disabled) {
Copy link
Member

Choose a reason for hiding this comment

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

Add a comment here?

Copy link
Contributor Author

@mmalerba mmalerba left a comment

Choose a reason for hiding this comment

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

comments addressed


<!-- example(input-prefix-suffix) -->
The `<mat-form-field>` allows you to
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ideally yes, in the future once the functionality is extracted into the form field, but currently this is up to the individual FormFieldControl to add support for custom error state matchers (and the attribute goes on the <input> not the <mat-form-field>).

be specified either via a `placeholder` attribute on the `<mat-select>` or a `<mat-placeholder>`
element in the same form field as the `<mat-select>`. The `<mat-form-field>` also has additional
options for changing the behavior of the placeholder. For more information see the
[form field placeholder documentation](https://material.angular.io/components/form-field/overview#floating-placeholder).
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd like to keep it here too since placeholder is an attribute that you place on the <mat-select> (in the future when we split the placeholder and label into separate concepts it can be completely handled by the form field).

Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

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

LGTM

@jelbourn jelbourn added pr: lgtm action: merge The PR is ready for merge by the caretaker and removed pr: needs review labels Oct 11, 2017
@andrewseguin andrewseguin merged commit e129e3d into angular:master Oct 12, 2017
@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 7, 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 docs This issue is related to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants