Skip to content

Commit e007c27

Browse files
crisbetojelbourn
authored andcommitted
fix(chips): dynamic chip input placeholder changes not being propagated to form field (#12422)
Fixes changes to the placeholder of a chip input not being propagated to its parent form field. Also un-deprecates the input for `placeholder`, because we need to know when it has changed. Fixes #11861.
1 parent bcf4c9f commit e007c27

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {createKeyboardEvent} from '@angular/cdk/testing';
55
import {Component, DebugElement} from '@angular/core';
66
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
77
import {By} from '@angular/platform-browser';
8+
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
9+
import {MatFormFieldModule} from '@angular/material/form-field';
810
import {MatChipInput, MatChipInputEvent} from './chip-input';
911
import {MatChipsModule} from './index';
1012
import {MAT_CHIPS_DEFAULT_OPTIONS, MatChipsDefaultOptions} from './chip-default-options';
@@ -20,7 +22,7 @@ describe('MatChipInput', () => {
2022

2123
beforeEach(async(() => {
2224
TestBed.configureTestingModule({
23-
imports: [MatChipsModule, PlatformModule],
25+
imports: [PlatformModule, MatChipsModule, MatFormFieldModule, NoopAnimationsModule],
2426
declarations: [TestChipInput],
2527
providers: [{
2628
provide: Directionality, useFactory: () => {
@@ -64,6 +66,22 @@ describe('MatChipInput', () => {
6466

6567
expect(inputNativeElement.getAttribute('placeholder')).toBe('bound placeholder');
6668
});
69+
70+
it('should propagate the dynamic `placeholder` value to the form field', () => {
71+
fixture.componentInstance.placeholder = 'add a chip';
72+
fixture.detectChanges();
73+
74+
const label: HTMLElement = fixture.nativeElement.querySelector('.mat-form-field-label');
75+
76+
expect(label).toBeTruthy();
77+
expect(label.textContent).toContain('add a chip');
78+
79+
fixture.componentInstance.placeholder = 'or don\'t';
80+
fixture.detectChanges();
81+
82+
expect(label.textContent).toContain('or don\'t');
83+
});
84+
6785
});
6886

6987
describe('[addOnBlur]', () => {
@@ -117,7 +135,7 @@ describe('MatChipInput', () => {
117135
TestBed
118136
.resetTestingModule()
119137
.configureTestingModule({
120-
imports: [MatChipsModule, PlatformModule],
138+
imports: [MatChipsModule, MatFormFieldModule, PlatformModule, NoopAnimationsModule],
121139
declarations: [TestChipInput],
122140
providers: [{
123141
provide: MAT_CHIPS_DEFAULT_OPTIONS,
@@ -146,12 +164,14 @@ describe('MatChipInput', () => {
146164

147165
@Component({
148166
template: `
149-
<mat-chip-list #chipList>
150-
</mat-chip-list>
151-
<input matInput [matChipInputFor]="chipList"
152-
[matChipInputAddOnBlur]="addOnBlur"
153-
(matChipInputTokenEnd)="add($event)"
154-
[placeholder]="placeholder" />
167+
<mat-form-field>
168+
<mat-chip-list #chipList>
169+
</mat-chip-list>
170+
<input matInput [matChipInputFor]="chipList"
171+
[matChipInputAddOnBlur]="addOnBlur"
172+
(matChipInputTokenEnd)="add($event)"
173+
[placeholder]="placeholder" />
174+
</mat-form-field>
155175
`
156176
})
157177
class TestChipInput {

src/lib/chips/chip-input.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {coerceBooleanProperty} from '@angular/cdk/coercion';
10-
import {Directive, ElementRef, EventEmitter, Input, Output, Inject} from '@angular/core';
10+
import {Directive, ElementRef, EventEmitter, Input, Output, Inject, OnChanges} from '@angular/core';
1111
import {MatChipList} from './chip-list';
1212
import {MAT_CHIPS_DEFAULT_OPTIONS, MatChipsDefaultOptions} from './chip-default-options';
1313

@@ -41,7 +41,7 @@ let nextUniqueId = 0;
4141
'[attr.placeholder]': 'placeholder || null',
4242
}
4343
})
44-
export class MatChipInput {
44+
export class MatChipInput implements OnChanges {
4545
/** Whether the control is focused. */
4646
focused: boolean = false;
4747
_chipList: MatChipList;
@@ -76,11 +76,7 @@ export class MatChipInput {
7676
@Output('matChipInputTokenEnd')
7777
chipEnd: EventEmitter<MatChipInputEvent> = new EventEmitter<MatChipInputEvent>();
7878

79-
/**
80-
* The input's placeholder text.
81-
* @deprecated Bind to the `placeholder` attribute directly.
82-
* @breaking-change 7.0.0
83-
*/
79+
/** The input's placeholder text. */
8480
@Input() placeholder: string = '';
8581

8682
/** Unique id for the input. */
@@ -98,6 +94,10 @@ export class MatChipInput {
9894
this._inputElement = this._elementRef.nativeElement as HTMLInputElement;
9995
}
10096

97+
ngOnChanges() {
98+
this._chipList.stateChanges.next();
99+
}
100+
101101
/** Utility method to make host definition/tests more clear. */
102102
_keydown(event?: KeyboardEvent) {
103103
this._emitChipEnd(event);

0 commit comments

Comments
 (0)