Skip to content

Commit eb74659

Browse files
committed
feat(chip-list): Implement FormFieldControl
1 parent 13fb5b4 commit eb74659

File tree

11 files changed

+1223
-201
lines changed

11 files changed

+1223
-201
lines changed

src/demo-app/chips/chips-demo.html

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@ <h4>Form Field</h4>
4646
</p>
4747

4848

49-
<md-form-field>
50-
<md-chip-list mdPrefix #chipList1>
49+
<md-form-field class="has-chip-list">
50+
<md-chip-list #chipList1 [(ngModel)]="selectedPeople" required>
5151
<md-chip *ngFor="let person of people" [color]="color" [selectable]="selectable"
5252
[removable]="removable" (remove)="remove(person)">
5353
{{person.name}}
5454
<md-icon mdChipRemove *ngIf="removable">cancel</md-icon>
5555
</md-chip>
56+
<input placeholder="New Contributor..."
57+
[mdChipInputFor]="chipList1"
58+
[mdChipInputSeparatorKeyCodes]="separatorKeysCodes"
59+
[mdChipInputAddOnBlur]="false"
60+
(mdChipInputTokenEnd)="add($event)" />
5661
</md-chip-list>
57-
<input mdInput placeholder="New Contributor..."
58-
[mdChipInputFor]="chipList1"
59-
[mdChipInputSeparatorKeyCodes]="separatorKeysCodes"
60-
[mdChipInputAddOnBlur]="addOnBlur"
61-
(mdChipInputTokenEnd)="add($event)" />
6262
</md-form-field>
6363

6464

@@ -68,21 +68,37 @@ <h4>Form Field</h4>
6868
With <code>mdChipInput</code> the input work with chip-list
6969
</p>
7070

71-
<md-chip-list #chipList2>
72-
<md-chip *ngFor="let person of people" [color]="color" [selectable]="selectable"
73-
[removable]="removable" (remove)="remove(person)">
74-
{{person.name}}
75-
<md-icon mdChipRemove *ngIf="removable">cancel</md-icon>
76-
</md-chip>
77-
</md-chip-list>
7871
<md-form-field>
79-
<input mdInput placeholder="New Contributor..."
72+
<md-chip-list #chipList2>
73+
<md-chip *ngFor="let person of people" [color]="color" [selectable]="selectable"
74+
[removable]="removable" (remove)="remove(person)">
75+
{{person.name}}
76+
<md-icon mdChipRemove *ngIf="removable">cancel</md-icon>
77+
</md-chip>
78+
</md-chip-list>
79+
<input placeholder="New Contributor..."
8080
[mdChipInputFor]="chipList2"
8181
[mdChipInputSeparatorKeyCodes]="separatorKeysCodes"
8282
[mdChipInputAddOnBlur]="addOnBlur"
8383
(mdChipInputTokenEnd)="add($event)" />
8484
</md-form-field>
8585

86+
<p>
87+
Chips list without input
88+
</p>
89+
90+
91+
<md-form-field class="has-chip-list">
92+
<md-chip-list #chipList3>
93+
<md-chip *ngFor="let person of people" [color]="color" [selectable]="selectable"
94+
[removable]="removable" (remove)="remove(person)">
95+
{{person.name}}
96+
<md-icon mdChipRemove *ngIf="removable">cancel</md-icon>
97+
</md-chip>
98+
</md-chip-list>
99+
</md-form-field>
100+
101+
86102
<p>
87103
The example above has overridden the <code>[separatorKeys]</code> input to allow for
88104
<code>ENTER</code>, <code>COMMA</code> and <code>SEMI COLON</code> keys.
@@ -108,6 +124,17 @@ <h4>Stacked Chips</h4>
108124
{{aColor.name}}
109125
</md-chip>
110126
</md-chip-list>
127+
128+
<h4>NgModel with chip list</h4>
129+
<md-chip-list [(ngModel)]="selectedColors">
130+
<md-chip *ngFor="let aColor of availableColors" color="{{aColor.color}}"
131+
[value]="aColor.name" (remove)="removeColor(aColor)">
132+
{{aColor.name}}
133+
<md-icon mdChipRemove>cancel</md-icon>
134+
</md-chip>
135+
</md-chip-list>
136+
137+
The selected colors are <span *ngFor="let color of selectedColors"> {{color}}</span>.
111138
</md-card-content>
112139
</md-card>
113140
</div>

src/demo-app/chips/chips-demo.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@
2525
width: 150px;
2626
}
2727
}
28+
29+
.has-chip-list {
30+
width: 100%;
31+
}

src/demo-app/chips/chips-demo.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export class ChipsDemo {
2929
// Enter, comma, semi-colon
3030
separatorKeysCodes = [ENTER, COMMA, 186];
3131

32+
selectedPeople = null;
33+
3234
people: Person[] = [
3335
{ name: 'Kara' },
3436
{ name: 'Jeremy' },
@@ -72,7 +74,22 @@ export class ChipsDemo {
7274
}
7375
}
7476

77+
removeColor(color: DemoColor) {
78+
let index = this.availableColors.indexOf(color);
79+
80+
if (index >= 0) {
81+
this.availableColors.splice(index, 1);
82+
}
83+
84+
index = this.selectedColors.indexOf(color.name);
85+
86+
if (index >= 0) {
87+
this.selectedColors.splice(index, 1);
88+
}
89+
}
90+
7591
toggleVisible(): void {
7692
this.visible = false;
7793
}
94+
selectedColors: any[] = ["Primary", "Warn"];
7895
}

src/lib/chips/chip-input.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {async, TestBed, ComponentFixture} from '@angular/core/testing';
22
import {MdChipsModule} from './index';
33
import {Component, DebugElement} from '@angular/core';
4+
import {PlatformModule} from '../core/platform/index';
45
import {MdChipInput, MdChipInputEvent} from './chip-input';
56
import {By} from '@angular/platform-browser';
67
import {Directionality} from '../core';
@@ -21,7 +22,7 @@ describe('MdChipInput', () => {
2122

2223
beforeEach(async(() => {
2324
TestBed.configureTestingModule({
24-
imports: [MdChipsModule],
25+
imports: [MdChipsModule, PlatformModule],
2526
declarations: [TestChipInput],
2627
providers: [{
2728
provide: Directionality, useFactory: () => {

src/lib/chips/chip-input.ts

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,29 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Directive, Output, EventEmitter, ElementRef, Input} from '@angular/core';
9+
import {
10+
Directive,
11+
ElementRef,
12+
Output,
13+
EventEmitter,
14+
Inject,
15+
Input,
16+
Optional,
17+
Renderer2,
18+
Self,
19+
} from '@angular/core';
20+
import {FormControl, FormGroupDirective, NgControl, NgForm} from '@angular/forms';
21+
import {Platform, getSupportedInputTypes} from '@angular/cdk/platform';
1022
import {coerceBooleanProperty} from '@angular/cdk/coercion';
1123
import {ENTER} from '../core/keyboard/keycodes';
1224
import {MdChipList} from './chip-list';
25+
import {MdInput} from '../input/input';
26+
import {
27+
defaultErrorStateMatcher,
28+
ErrorOptions,
29+
ErrorStateMatcher,
30+
MD_ERROR_GLOBAL_OPTIONS
31+
} from '../core/error/error-options';
1332

1433
export interface MdChipInputEvent {
1534
input: HTMLInputElement;
@@ -19,12 +38,13 @@ export interface MdChipInputEvent {
1938
@Directive({
2039
selector: 'input[mdChipInputFor], input[matChipInputFor]',
2140
host: {
22-
'class': 'mat-chip-input',
41+
'class': 'mat-chip-input mat-input-element',
2342
'(keydown)': '_keydown($event)',
24-
'(blur)': '_blur()'
43+
'(blur)': '_blur()',
44+
'(focus)': '_focus()',
2545
}
2646
})
27-
export class MdChipInput {
47+
export class MdChipInput extends MdInput {
2848

2949
_chipList: MdChipList;
3050

@@ -33,7 +53,7 @@ export class MdChipInput {
3353
set chipList(value: MdChipList) {
3454
if (value) {
3555
this._chipList = value;
36-
this._chipList.registerInput(this._inputElement);
56+
this._chipList.registerInput(this);
3757
}
3858
}
3959

@@ -71,7 +91,14 @@ export class MdChipInput {
7191
/** The native input element to which this directive is attached. */
7292
protected _inputElement: HTMLInputElement;
7393

74-
constructor(protected _elementRef: ElementRef) {
94+
constructor(protected _elementRef: ElementRef,
95+
protected _renderer: Renderer2,
96+
protected _platform: Platform,
97+
@Optional() @Self() public ngControl: NgControl,
98+
@Optional() protected _parentForm: NgForm,
99+
@Optional() protected _parentFormGroup: FormGroupDirective,
100+
@Optional() @Inject(MD_ERROR_GLOBAL_OPTIONS) errorOptions: ErrorOptions) {
101+
super(_elementRef, _renderer, _platform, ngControl, _parentForm, _parentFormGroup, errorOptions);
75102
this._inputElement = this._elementRef.nativeElement as HTMLInputElement;
76103
}
77104

@@ -85,6 +112,13 @@ export class MdChipInput {
85112
if (this.addOnBlur) {
86113
this._emitChipEnd();
87114
}
115+
super._focusChanged(false);
116+
this._chipList.stateChanges.next();
117+
}
118+
119+
_focus() {
120+
super._focusChanged(true);
121+
this._chipList.stateChanges.next();
88122
}
89123

90124
/** Checks to see if the (chipEnd) event needs to be emitted. */

0 commit comments

Comments
 (0)