Skip to content

Commit 784abb7

Browse files
committed
make placeholder float when select is opened, and prevent it from overlapping the trigger arrow
1 parent e043074 commit 784abb7

File tree

4 files changed

+23
-18
lines changed

4 files changed

+23
-18
lines changed

src/demo-app/baseline/baseline-demo.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</md-form-field>
1616
| Text 5 |
1717
<md-form-field>
18-
<md-select placeholder="Select">
18+
<md-select placeholder="This placeholder is really really really long">
1919
<md-option value="option">Option</md-option>
2020
<md-option value="long">This option is really really really long</md-option>
2121
</md-select>
@@ -46,7 +46,7 @@ <h1>
4646
</md-form-field>
4747
| Text 5 |
4848
<md-form-field>
49-
<md-select placeholder="Select">
49+
<md-select placeholder="This placeholder is really really really long">
5050
<md-option value="option">Option</md-option>
5151
<md-option value="long">This option is really really really long</md-option>
5252
</md-select>

src/lib/select/select.scss

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ $mat-select-arrow-margin: 4px !default;
99
$mat-select-panel-max-height: 256px !default;
1010
$mat-select-item-height: 3em !default;
1111

12+
$mat-select-placeholder-arrow-space: 2 * ($mat-select-arrow-size + $mat-select-arrow-margin);
13+
1214
.mat-select {
1315
//display: inline-block;
1416
outline: none;
@@ -82,6 +84,12 @@ $mat-select-item-height: 3em !default;
8284
}
8385
}
8486

85-
.mat-form-field-type-mat-select .mat-form-field-flex {
86-
cursor: pointer;
87+
.mat-form-field-type-mat-select {
88+
.mat-form-field-flex {
89+
cursor: pointer;
90+
}
91+
92+
.mat-form-field-placeholder {
93+
width: calc(100% - #{$mat-select-placeholder-arrow-space});
94+
}
8795
}

src/lib/select/select.spec.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,8 @@ describe('MdSelect', () => {
590590

591591
const options =
592592
overlayContainerElement.querySelectorAll('md-option') as NodeListOf<HTMLElement>;
593-
expect(options[1].classList)
594-
.toContain('mat-selected', `Expected option with the control's new value to be selected.`);
593+
expect(options[1].classList).toContain('mat-selected',
594+
`Expected option with the control's new value to be selected.`);
595595
});
596596

597597
it('should update the form value when the view changes', () => {
@@ -974,7 +974,7 @@ describe('MdSelect', () => {
974974
trigger = fixture.debugElement.query(By.css('.mat-select-trigger')).nativeElement;
975975
formField = fixture.debugElement.query(By.css('.mat-form-field')).nativeElement;
976976
}));
977-
/* TODO(mmalerba): Verify this is correct behavior
977+
978978
it('should float the placeholder when the panel is open and unselected', () => {
979979
expect(formField.classList.contains('mat-form-field-should-float'))
980980
.toBe(false, 'Expected placeholder to initially have a normal position.');
@@ -992,7 +992,7 @@ describe('MdSelect', () => {
992992
expect(formField.classList.contains('mat-form-field-should-float'))
993993
.toBe(false, 'Expected placeholder to animate back down to normal position.');
994994
});
995-
*/
995+
996996
it('should add a class to the panel when the menu is done animating', fakeAsync(() => {
997997
trigger.click();
998998
fixture.detectChanges();
@@ -1037,7 +1037,7 @@ describe('MdSelect', () => {
10371037
const optionTop = options[index].getBoundingClientRect().top;
10381038
const triggerFontSize = parseInt(window.getComputedStyle(trigger)['font-size']);
10391039

1040-
// TODO(mmalerba): not really sure where the "+1" is coming from here...
1040+
/** TODO(mmalerba): not really sure where the "+1" is coming from here... */
10411041
expect(Math.floor(optionTop)).toBe(Math.floor(triggerTop - triggerFontSize + 1),
10421042
`Expected trigger to align with option ${index}.`);
10431043

@@ -1151,8 +1151,8 @@ describe('MdSelect', () => {
11511151
fixture.whenStable().then(() => {
11521152
// The selected option should be scrolled to the center of the panel.
11531153
// This will be its original offset from the scrollTop - half the panel height + half the
1154-
// option height. 10 (option index + 3 group labels before it) * 48 (option height) = 480px.
1155-
// 480 (offset from scrollTop) - 256/2 + 48/2 = 376px
1154+
// option height. 10 (option index + 3 group labels before it) * 48 (option height) =
1155+
// 480px. 480 (offset from scrollTop) - 256/2 + 48/2 = 376px
11561156
expect(Math.floor(scrollContainer.scrollTop))
11571157
.toBe(376, `Expected overlay panel to be scrolled to center the selected option.`);
11581158

@@ -3153,7 +3153,6 @@ class BasicSelectWithTheming {
31533153
<md-option *ngFor="let food of foods" [value]="food.value">
31543154
{{ food.viewValue }}
31553155
</md-option>
3156-
31573156
<md-option>None</md-option>
31583157
</md-select>
31593158
</md-form-field>
@@ -3199,12 +3198,10 @@ class FalsyValueSelect {
31993198
<md-select placeholder="Pokemon" [formControl]="control">
32003199
<md-optgroup *ngFor="let group of pokemonTypes" [label]="group.name"
32013200
[disabled]="group.disabled">
3202-
32033201
<md-option *ngFor="let pokemon of group.pokemon" [value]="pokemon.value">
32043202
{{ pokemon.viewValue }}
32053203
</md-option>
32063204
</md-optgroup>
3207-
32083205
<md-option value="mime-11">Mr. Mime</md-option>
32093206
</md-select>
32103207
</md-form-field>
@@ -3331,7 +3328,7 @@ class BasicSelectWithoutFormsPreselected {
33313328
}
33323329

33333330
@Component({
3334-
template: `
3331+
template: `
33353332
<md-form-field>
33363333
<md-select placeholder="Food" [(value)]="selectedFoods" multiple>
33373334
<md-option *ngFor="let food of foods" [value]="food.value">
@@ -3355,8 +3352,8 @@ class BasicSelectWithoutFormsMultiple {
33553352

33563353
@Component({
33573354
selector: 'select-with-custom-trigger',
3358-
template: `
3359-
<md-form-field>
3355+
template: `
3356+
<md-form-field>
33603357
<md-select placeholder="Food" [formControl]="control" #select="mdSelect">
33613358
<md-select-trigger>
33623359
{{ select.selected?.viewValue.split('').reverse().join('') }}

src/lib/select/select.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1191,7 +1191,7 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
11911191
}
11921192

11931193
// Implemented as part of MdFormFieldControl.
1194-
get shouldPlaceholderFloat() { return !this.empty; }
1194+
get shouldPlaceholderFloat() { return this._panelOpen || !this.empty; }
11951195
}
11961196

11971197
/** Clamps a value n between min and max values. */

0 commit comments

Comments
 (0)