From 21d4c42571cd7151be0689312956abc64702edb5 Mon Sep 17 00:00:00 2001 From: Vivian Hu Date: Tue, 31 Jul 2018 16:28:48 -0700 Subject: [PATCH 1/3] update dependency --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 1bf2d290c08a..14c82982ccdc 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,7 @@ node_modules # IDEs /.idea /.vscode +/*.iml # misc .DS_Store From 4c8448a39d975e44b0db0e6eaee9d30c211e18a3 Mon Sep 17 00:00:00 2001 From: Vivian Hu Date: Fri, 3 Aug 2018 15:43:11 -0700 Subject: [PATCH 2/3] feat(input): add native select to be form input. --- src/demo-app/select/select-demo.html | 161 ++++++++++++++++++ src/demo-app/select/select-demo.ts | 16 +- src/lib/form-field/form-field.md | 4 +- src/lib/input/input.scss | 27 +++ src/lib/input/input.spec.ts | 132 ++++++++++++++ src/lib/input/input.ts | 20 ++- src/lib/select/select.md | 25 ++- .../select-disabled-example.html | 30 +++- .../select-error-state-matcher-example.html | 14 ++ .../select-error-state-matcher-example.ts | 10 ++ .../select-form/select-form-example.html | 14 +- .../select-form/select-form-example.ts | 12 ++ .../select-hint-error-example.html | 17 ++ .../select-hint-error-example.ts | 1 + .../select-optgroup-example.html | 15 ++ .../select-overview-example.html | 11 ++ .../select-reset/select-reset-example.html | 13 ++ 17 files changed, 498 insertions(+), 24 deletions(-) diff --git a/src/demo-app/select/select-demo.html b/src/demo-app/select/select-demo.html index 2b2cf07d1e1c..6abd94afcbd9 100644 --- a/src/demo-app/select/select-demo.html +++ b/src/demo-app/select/select-demo.html @@ -1,3 +1,163 @@ + + + + Native Select + +
+

Basic

+ + Select your car + + +

Disabled and required

+ + Select your car (disabled) + + +

Floating label

+ + Float with value + + + + Not float when empty + + + + Float with no value, but with label + + + + Float with no value, but with html + + +

Looks

+ + Legacy + + + + Standard + + + + Fill + + + + Outline + + +

Option group

+ + + +

Place holder

+ + + +

Error message, hint, form sumbit

+ + Select your car (required) + + + This field is required + + You can pick up your favorite car here + + +

Error message with errorStateMatcher

+ + Select your car + + + This field is required + + You can pick up your favorite car here + + +
+
+
+ + +mat-select Space above cards:
@@ -240,3 +400,4 @@
This div is for testing scrolled selects.
+ diff --git a/src/demo-app/select/select-demo.ts b/src/demo-app/select/select-demo.ts index 56f0305af7c5..dabde38fca54 100644 --- a/src/demo-app/select/select-demo.ts +++ b/src/demo-app/select/select-demo.ts @@ -7,9 +7,18 @@ */ import {Component} from '@angular/core'; -import {FormControl} from '@angular/forms'; -import {MatSelectChange} from '@angular/material'; +import {FormControl, Validators} from '@angular/forms'; +import {ErrorStateMatcher, MatSelectChange} from '@angular/material'; +/** Error any time control is invalid */ +export class MyErrorStateMatcher implements ErrorStateMatcher { + isErrorState(control: FormControl | null): boolean { + if (control) { + return control.invalid; + } + return false; + } +} @Component({ moduleId: module.id, @@ -37,6 +46,7 @@ export class SelectDemo { drinksTheme = 'primary'; pokemonTheme = 'primary'; compareByValue = true; + selectFormControl = new FormControl('', Validators.required); foods = [ {value: null, viewValue: 'None'}, @@ -137,6 +147,8 @@ export class SelectDemo { return o1 === o2; } + matcher = new MyErrorStateMatcher(); + toggleSelected() { this.currentAppearanceValue = this.currentAppearanceValue ? null : this.digimon[0].value; } diff --git a/src/lib/form-field/form-field.md b/src/lib/form-field/form-field.md index e431374ed1ca..6a1d14396890 100644 --- a/src/lib/form-field/form-field.md +++ b/src/lib/form-field/form-field.md @@ -8,6 +8,7 @@ In this document, "form field" refers to the wrapper component ` The following Angular Material components are designed to work inside a ``: * [`` & ` +

diff --git a/src/demo-app/select/select-demo.html b/src/demo-app/select/select-demo.html index 6abd94afcbd9..b92727f38e8b 100644 --- a/src/demo-app/select/select-demo.html +++ b/src/demo-app/select/select-demo.html @@ -7,7 +7,7 @@

Basic

Select your car - @@ -18,7 +18,7 @@

Basic

Disabled and required

Select your car (disabled) - @@ -28,7 +28,7 @@

Disabled and required

Floating label

Float with value - @@ -37,7 +37,7 @@

Floating label

Not float when empty - @@ -46,7 +46,7 @@

Floating label

Float with no value, but with label - @@ -55,7 +55,7 @@

Floating label

Float with no value, but with html - @@ -65,7 +65,7 @@

Floating label

Looks

Legacy - @@ -74,7 +74,7 @@

Looks

Standard - @@ -83,7 +83,7 @@

Looks

Fill - @@ -92,7 +92,7 @@

Looks

Outline - @@ -101,7 +101,7 @@

Looks

Option group

- @@ -114,7 +114,7 @@

Option group

Place holder

- @@ -125,7 +125,7 @@

Place holder

Error message, hint, form sumbit

Select your car (required) - @@ -140,7 +140,7 @@

Error message, hint, form sumbit

Error message with errorStateMatcher

Select your car - diff --git a/src/lib/form-field/form-field.md b/src/lib/form-field/form-field.md index 6a1d14396890..a26fadb3ddc0 100644 --- a/src/lib/form-field/form-field.md +++ b/src/lib/form-field/form-field.md @@ -7,8 +7,8 @@ In this document, "form field" refers to the wrapper component ` (e.g. the input, textarea, select, etc.) The following Angular Material components are designed to work inside a ``: -* [`` & ` + ` }) class MatInputTextareaWithBindings { @@ -1807,7 +1808,7 @@ class AutosizeTextareaInAStep {} @Component({ template: ` - @@ -1823,7 +1824,7 @@ class MatInputSelect { @Component({ template: ` - @@ -1836,7 +1837,7 @@ class MatInputSelectWithNoLabelNoValue {} @Component({ template: ` - @@ -1849,7 +1850,7 @@ class MatInputSelectWithLabel {} @Component({ template: ` - diff --git a/src/lib/input/input.ts b/src/lib/input/input.ts index 3af05a323129..13419d519219 100644 --- a/src/lib/input/input.ts +++ b/src/lib/input/input.ts @@ -58,7 +58,8 @@ export const _MatInputMixinBase = mixinErrorState(MatInputBase); /** Directive that allows a native input to work inside a `MatFormField`. */ @Directive({ - selector: `input[matInput], textarea[matInput], select[matControl]`, + selector: `input[matInput], textarea[matInput], select[matNativeControl], + input[matNativeControl], textarea[matNativeControl]`, exportAs: 'matInput', host: { /** @@ -72,6 +73,7 @@ export const _MatInputMixinBase = mixinErrorState(MatInputBase); '[attr.placeholder]': 'placeholder', '[disabled]': 'disabled', '[required]': 'required', + '[attr.readonly]': 'readonly && !_isNativeSelect || null', '[attr.aria-describedby]': '_ariaDescribedby || null', '[attr.aria-invalid]': 'errorState', '[attr.aria-required]': 'required.toString()', @@ -361,12 +363,12 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl< */ get shouldLabelFloat(): boolean { if (this._isNativeSelect) { - // For multi select, float mat input label - // If single select has value or has a option label or html, float mat input label to avoid - // mat input label to overlap with the select content. - const selectElement = this._elementRef.nativeElement; - return selectElement.multiple || !this.empty || selectElement.options[0].label - || this.focused; + // For a single-selection ``, the label *always* floats to avoid + // overlapping the label with the options. + const selectElement = this._elementRef.nativeElement; + return selectElement.multiple || !this.empty || selectElement.options[0].label || + this.focused; } else { return this.focused || !this.empty; } diff --git a/src/lib/select/select.md b/src/lib/select/select.md index 4eeaed793a25..2fa6fc8f5f90 100644 --- a/src/lib/select/select.md +++ b/src/lib/select/select.md @@ -8,12 +8,13 @@ To add options to the select, add `` elements to the ``. has a `value` property that can be used to set the value that will be selected if the user chooses this option. The content of the `` is what will be shown to the user. -Angular Material also supports use of native `` element inside of ``. The native control has several performance, accessibility, and usability advantages. See [the documentation for form-field](https://material.angular.io/components/form-field) for more information. -To use native select inside ``, add `matInput` as an attribute to native html select. +To use a native select inside ``, add the `matNativeControl` attribute +to the `` or `` and the `