Skip to content

Commit 32fdbc2

Browse files
committed
feat(autocomplete): support static placeholders
Closes #3104
1 parent a02ed5a commit 32fdbc2

File tree

3 files changed

+63
-8
lines changed

3 files changed

+63
-8
lines changed

src/demo-app/autocomplete/autocomplete-demo.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<div>Reactive value: {{ stateCtrl.value | json }}</div>
77
<div>Reactive dirty: {{ stateCtrl.dirty }}</div>
88

9-
<md-input-container>
9+
<md-input-container floatPlaceholder="never">
1010
<input mdInput placeholder="State" [mdAutocomplete]="reactiveAuto" [formControl]="stateCtrl">
1111
</md-input-container>
1212

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {Subject} from 'rxjs/Subject';
2525
import 'rxjs/add/observable/merge';
2626
import 'rxjs/add/operator/startWith';
2727
import 'rxjs/add/operator/switchMap';
28-
import {MdInputContainer, FloatPlaceholderType} from '../input/input-container';
28+
import {MdInputContainer} from '../input/input-container';
2929

3030
/**
3131
* The following style constants are necessary to save here in order
@@ -81,6 +81,9 @@ export class MdAutocompleteTrigger implements AfterContentInit, ControlValueAcce
8181
/** Stream of blur events that should close the panel. */
8282
private _blurStream = new Subject<any>();
8383

84+
/** Whether or not the placeholder state is being overridden. */
85+
private _manuallyFloatingPlaceholder = false;
86+
8487
/** View -> model callback called when value changes */
8588
_onChange = (value: any) => {};
8689

@@ -134,7 +137,7 @@ export class MdAutocompleteTrigger implements AfterContentInit, ControlValueAcce
134137
}
135138

136139
this._panelOpen = true;
137-
this._floatPlaceholder('always');
140+
this._floatPlaceholder();
138141
}
139142

140143
/** Closes the autocomplete suggestion panel. */
@@ -144,7 +147,7 @@ export class MdAutocompleteTrigger implements AfterContentInit, ControlValueAcce
144147
}
145148

146149
this._panelOpen = false;
147-
this._floatPlaceholder('auto');
150+
this._resetPlaceholder();
148151
}
149152

150153
/**
@@ -232,9 +235,18 @@ export class MdAutocompleteTrigger implements AfterContentInit, ControlValueAcce
232235
* This causes the value to jump when selecting an option with the mouse.
233236
* This method manually floats the placeholder until the panel can be closed.
234237
*/
235-
private _floatPlaceholder(state: FloatPlaceholderType): void {
236-
if (this._inputContainer) {
237-
this._inputContainer.floatPlaceholder = state;
238+
private _floatPlaceholder(): void {
239+
if (this._inputContainer && this._inputContainer.floatPlaceholder === 'auto') {
240+
this._inputContainer.floatPlaceholder = 'always';
241+
this._manuallyFloatingPlaceholder = true;
242+
}
243+
}
244+
245+
/** If the placeholder has been manually elevated, return it to its normal state. */
246+
private _resetPlaceholder(): void {
247+
if (this._manuallyFloatingPlaceholder) {
248+
this._inputContainer.floatPlaceholder = 'auto';
249+
this._manuallyFloatingPlaceholder = false;
238250
}
239251
}
240252

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,48 @@ describe('MdAutocomplete', () => {
210210
});
211211
}));
212212

213+
it('should not mess with placeholder placement if set to never', async(() => {
214+
fixture.componentInstance.placeholder = 'never';
215+
fixture.detectChanges();
216+
217+
fixture.componentInstance.trigger.openPanel();
218+
expect(fixture.componentInstance.inputContainer.floatPlaceholder)
219+
.toEqual('never', 'Expected placeholder to stay static.');
220+
221+
fixture.whenStable().then(() => {
222+
fixture.detectChanges();
223+
224+
const options =
225+
overlayContainerElement.querySelectorAll('md-option') as NodeListOf<HTMLElement>;
226+
options[1].click();
227+
fixture.detectChanges();
228+
229+
expect(fixture.componentInstance.inputContainer.floatPlaceholder)
230+
.toEqual('never', 'Expected placeholder to stay in static state after close.');
231+
});
232+
}));
233+
234+
it('should not mess with placeholder placement if set to always', async(() => {
235+
fixture.componentInstance.placeholder = 'always';
236+
fixture.detectChanges();
237+
238+
fixture.componentInstance.trigger.openPanel();
239+
expect(fixture.componentInstance.inputContainer.floatPlaceholder)
240+
.toEqual('always', 'Expected placeholder to stay elevated on open.');
241+
242+
fixture.whenStable().then(() => {
243+
fixture.detectChanges();
244+
245+
const options =
246+
overlayContainerElement.querySelectorAll('md-option') as NodeListOf<HTMLElement>;
247+
options[1].click();
248+
fixture.detectChanges();
249+
250+
expect(fixture.componentInstance.inputContainer.floatPlaceholder)
251+
.toEqual('always', 'Expected placeholder to stay elevated after close.');
252+
});
253+
}));
254+
213255
});
214256

215257
it('should have the correct text direction in RTL', () => {
@@ -823,7 +865,7 @@ describe('MdAutocomplete', () => {
823865

824866
@Component({
825867
template: `
826-
<md-input-container>
868+
<md-input-container [floatPlaceholder]="placeholder">
827869
<input mdInput placeholder="State" [mdAutocomplete]="auto" [formControl]="stateCtrl">
828870
</md-input-container>
829871
@@ -838,6 +880,7 @@ class SimpleAutocomplete implements OnDestroy {
838880
stateCtrl = new FormControl();
839881
filteredStates: any[];
840882
valueSub: Subscription;
883+
placeholder = 'auto';
841884

842885
@ViewChild(MdAutocompleteTrigger) trigger: MdAutocompleteTrigger;
843886
@ViewChild(MdAutocomplete) panel: MdAutocomplete;

0 commit comments

Comments
 (0)