Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 758ea49

Browse files
committed
fix(inputs): Fix floating label and char counter positions.
Recent updates to the `md-char-counter` class caused alignment issues when the input was side-by-side with another input. The underlying cause was that the other input uses `maring-top: auto` instead of `margin-top: 0` which made the inputs "bottom aligned" as the height of it's neighbors changed. This fixes the CSS on the char counter, inputs and select to better align and updates the inputs demo to show the select next to an input with the char counter. Also, I noticed a bug with the floating labels when using only the placeholder, so this adds some additional tests and fixes the bug. Fixes #4872.
1 parent b983c0d commit 758ea49

File tree

6 files changed

+68
-23
lines changed

6 files changed

+68
-23
lines changed

src/components/input/demoBasicUsage/index.html

+22-5
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
<div layout layout-sm="column">
1818
<md-input-container style="width:70%">
19-
<label>Company (Disabled)</label>
20-
<input ng-model="user.company" disabled>
21-
</md-input-container>
19+
<label>Company (Disabled)</label>
20+
<input ng-model="user.company" disabled>
21+
</md-input-container>
2222

2323
<md-datepicker ng-model="user.submissionDate" md-placeholder="Enter date"></md-datepicker>
2424
</div>
@@ -47,13 +47,30 @@
4747
<label>City</label>
4848
<input ng-model="user.city">
4949
</md-input-container>
50+
5051
<md-input-container flex>
5152
<label>State</label>
52-
<input ng-model="user.state">
53+
<md-select ng-model="user.state">
54+
<md-option ng-repeat="state in states" value="{{state.abbrev}}">
55+
{{state.abbrev}}
56+
</md-option>
57+
</md-select>
5358
</md-input-container>
59+
5460
<md-input-container flex>
5561
<label>Postal Code</label>
56-
<input ng-model="user.postalCode" placeholder="12345">
62+
<input name="postalCode" ng-model="user.postalCode" placeholder="12345"
63+
required ng-pattern="/^[0-9]{5}$/" md-maxlength="5">
64+
65+
<div ng-messages="userForm.postalCode.$error" role="alert" multiple>
66+
<div ng-message="required" class="my-message">You must supply a postal code.</div>
67+
<div ng-message="pattern" class="my-message">That doesn't look like a valid postal
68+
code.
69+
</div>
70+
<div ng-message="md-maxlength" class="my-message">
71+
Don't use the long version silly...we don't need to be that specific...
72+
</div>
73+
</div>
5774
</md-input-container>
5875
</div>
5976

src/components/input/demoBasicUsage/script.js

+15-9
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,27 @@ angular
55
title: 'Developer',
66
77
firstName: '',
8-
lastName: '' ,
9-
company: 'Google' ,
10-
address: '1600 Amphitheatre Pkwy' ,
11-
city: 'Mountain View' ,
12-
state: 'CA' ,
8+
lastName: '',
9+
company: 'Google',
10+
address: '1600 Amphitheatre Pkwy',
11+
city: 'Mountain View',
12+
state: 'CA',
1313
biography: 'Loves kittens, snowboarding, and can type at 130 WPM.\n\nAnd rumor has it she bouldered up Castle Craig!',
14-
postalCode : '94043'
14+
postalCode: '94043'
1515
};
16+
17+
$scope.states = ('AL AK AZ AR CA CO CT DE FL GA HI ID IL IN IA KS KY LA ME MD MA MI MN MS ' +
18+
'MO MT NE NV NH NJ NM NY NC ND OH OK OR PA RI SC SD TN TX UT VT VA WA WV WI ' +
19+
'WY').split(' ').map(function(state) {
20+
return {abbrev: state};
21+
})
1622
})
17-
.config( function($mdThemingProvider){
23+
.config(function($mdThemingProvider) {
1824

1925
// Configure a dark theme with primary foreground yellow
2026

2127
$mdThemingProvider.theme('docs-dark', 'default')
22-
.primaryPalette('yellow')
23-
.dark();
28+
.primaryPalette('yellow')
29+
.dark();
2430

2531
});

src/components/input/input.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -426,14 +426,15 @@ function placeholderDirective($log) {
426426
// If there is no input container, just return
427427
if (!inputContainer) return;
428428

429-
// Add a placeholder class so we can target it in the CSS
430-
inputContainer.setHasPlaceholder(true);
431-
432429
var label = inputContainer.element.find('label');
433430
var hasNoFloat = angular.isDefined(inputContainer.element.attr('md-no-float'));
434431

435432
// If we have a label, or they specify the md-no-float attribute, just return
436-
if ((label && label.length) || hasNoFloat) return;
433+
if ((label && label.length) || hasNoFloat) {
434+
// Add a placeholder class so we can target it in the CSS
435+
inputContainer.setHasPlaceholder(true);
436+
return;
437+
}
437438

438439
// Otherwise, grab/remove the placeholder
439440
var placeholderText = attr.placeholder;

src/components/input/input.scss

+5-4
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ md-input-container {
137137
.md-input {
138138
order: 2;
139139
display: block;
140-
margin-top: auto;
140+
margin-top: 0;
141141

142142
background: none;
143143
padding-top: $input-padding-top;
@@ -166,9 +166,9 @@ md-input-container {
166166
}
167167

168168
.md-char-counter {
169-
position: relative;
170-
text-align: right;
171-
order: 3;
169+
position: absolute;
170+
right: $input-container-padding;
171+
bottom: 7px;
172172
}
173173

174174
ng-messages, data-ng-messages, x-ng-messages,
@@ -181,6 +181,7 @@ md-input-container {
181181
position: absolute;
182182
top: 0;
183183
right: 0;
184+
bottom: auto;
184185
}
185186
}
186187

src/components/input/input.spec.js

+20
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,26 @@ describe('md-input-container directive', function() {
164164
expect(label.textContent).toEqual('some placeholder');
165165
}));
166166

167+
it('should not add the md-input-has-placeholder class when the placeholder is transformed into a label', inject(function($rootScope, $compile) {
168+
var el = $compile(
169+
'<md-input-container><input ng-model="foo" placeholder="some placeholder"></md-input-container>'
170+
)($rootScope);
171+
172+
expect(el.hasClass('md-input-has-placeholder')).toBe(false);
173+
}));
174+
175+
176+
it('should add the md-input-has-placeholder class when both the placeholder and label are provided', inject(function($rootScope, $compile) {
177+
var el = $compile(
178+
'<md-input-container>' +
179+
' <label>Hello</label>' +
180+
' <input ng-model="foo" placeholder="some placeholder" />' +
181+
'</md-input-container>'
182+
)($rootScope);
183+
184+
expect(el.hasClass('md-input-has-placeholder')).toBe(true);
185+
}));
186+
167187
it('should ignore placeholder when a label element is present', inject(function($rootScope, $compile) {
168188
var el = $compile(
169189
'<md-input-container>' +

src/components/select/select.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ $select-max-visible-options: 5;
4747
}
4848

4949
md-input-container > md-select {
50-
margin: auto 0 0 0;
50+
margin: 0;
5151
order: 2;
5252
}
5353

0 commit comments

Comments
 (0)