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

Commit f1d7f83

Browse files
committed
feat(input): make input placeholder attr work
Closes #1279.
1 parent 12febb1 commit f1d7f83

File tree

4 files changed

+57
-12
lines changed

4 files changed

+57
-12
lines changed

src/components/input/demoBasicUsage/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<div layout layout-sm="column">
2323
<md-input-container flex>
2424
<label>First Name</label>
25-
<input ng-model="user.firstName">
25+
<input ng-model="user.firstName" placeholder="Placeholder text">
2626
</md-input-container>
2727
<md-input-container flex>
2828
<label>Last Name</label>

src/components/input/input-theme.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ md-input-container.md-THEME_NAME-theme {
66
text-shadow: '{{foreground-shadow}}';
77
}
88

9-
label {
9+
label,
10+
.md-placeholder {
1011
text-shadow: '{{foreground-shadow}}';
1112
color: '{{foreground-3}}';
1213
}

src/components/input/input.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ angular.module('material.components.input', [
1212
.directive('label', labelDirective)
1313
.directive('input', inputTextareaDirective)
1414
.directive('textarea', inputTextareaDirective)
15-
.directive('mdMaxlength', mdMaxlengthDirective);
15+
.directive('mdMaxlength', mdMaxlengthDirective)
16+
.directive('placeholder', placeholderDirective);
1617

1718
/**
1819
* @ngdoc directive
@@ -42,7 +43,7 @@ angular.module('material.components.input', [
4243
*
4344
* </hljs>
4445
*/
45-
function mdInputContainerDirective($mdTheming) {
46+
function mdInputContainerDirective($mdTheming, $animate, $mdUtil) {
4647
return {
4748
restrict: 'E',
4849
link: postLink,
@@ -52,18 +53,18 @@ function mdInputContainerDirective($mdTheming) {
5253
function postLink(scope, element, attr) {
5354
$mdTheming(element);
5455
}
55-
function ContainerCtrl($scope, $element, $mdUtil) {
56+
function ContainerCtrl($scope, $element) {
5657
var self = this;
5758

5859
self.element = $element;
5960
self.setFocused = function(isFocused) {
60-
$element.toggleClass('md-input-focused', !!isFocused);
61+
$animate[isFocused ? 'addClass' : 'removeClass']($element, 'md-input-focused');
6162
};
6263
self.setHasValue = function(hasValue) {
63-
$element.toggleClass('md-input-has-value', !!hasValue);
64+
$animate[hasValue ? 'addClass' : 'removeClass']($element, 'md-input-has-value');
6465
};
6566
self.setInvalid = function(isInvalid) {
66-
$element.toggleClass('md-input-invalid', !!isInvalid);
67+
$animate[isInvalid ? 'addClass' : 'removeClass']($element, 'md-input-invalid');
6768
};
6869

6970
$scope.$watch(function() {
@@ -207,11 +208,15 @@ function inputTextareaDirective($mdUtil, $window, $compile, $animate) {
207208
element
208209
.on('input', inputCheckValue)
209210
.on('focus', function(ev) {
210-
containerCtrl.setFocused(true);
211+
scope.$evalAsync(function() {
212+
containerCtrl.setFocused(true);
213+
});
211214
})
212215
.on('blur', function(ev) {
213-
containerCtrl.setFocused(false);
214-
inputCheckValue();
216+
scope.$evalAsync(function() {
217+
containerCtrl.setFocused(false);
218+
inputCheckValue();
219+
});
215220
});
216221

217222
scope.$on('$destroy', function() {
@@ -313,4 +318,19 @@ function mdMaxlengthDirective($animate) {
313318
}
314319
}
315320

321+
function placeholderDirective() {
322+
return {
323+
restrict: 'A',
324+
require: '^mdInputContainer',
325+
link: postLink
326+
};
327+
328+
function postLink(scope, element, attr, inputContainer) {
329+
var placeholderText = attr.placeholder;
330+
element.removeAttr('placeholder');
331+
332+
inputContainer.element.append('<div class="md-placeholder">' + placeholderText + '</div>');
333+
}
334+
}
335+
316336
})();

src/components/input/input.scss

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ $input-label-default-scale: 1.0 !default;
55
$input-label-float-offset: 4px !default;
66
$input-label-float-scale: 0.75 !default;
77

8+
$input-placeholder-offset: $input-label-default-offset !default;
9+
810
$input-border-width-default: 1px !default;
911
$input-border-width-focused: 2px !default;
1012
$input-line-height: 26px !default;
@@ -44,7 +46,8 @@ md-input-container {
4446
overflow: hidden;
4547
}
4648

47-
label {
49+
label,
50+
.md-placeholder {
4851
order: 1;
4952
pointer-events: none;
5053
-webkit-font-smoothing: antialiased;
@@ -54,6 +57,24 @@ md-input-container {
5457
transform-origin: left top;
5558
transition: transform $swift-ease-out-timing-function 0.25s;
5659
}
60+
.md-placeholder {
61+
position: absolute;
62+
top: 0;
63+
opacity: 0;
64+
transition-property: opacity, transform;
65+
transform: translate3d(0, $input-placeholder-offset + $baseline-grid, 0);
66+
}
67+
&.md-input-focused:not(.md-input-has-value) {
68+
.md-placeholder {
69+
opacity: 1;
70+
transform: translate3d(0, $input-placeholder-offset, 0);
71+
}
72+
}
73+
&.md-input-has-value-remove-active,
74+
&.md-input-focused {
75+
transition: none;
76+
transition-delay: none;
77+
}
5778

5879
/*
5980
* The .md-input class is added to the input/textarea
@@ -117,6 +138,9 @@ md-input-container {
117138
transform: translate3d(0,$input-label-float-offset,0) scale($input-label-float-scale);
118139
}
119140
}
141+
&.md-input-has-value .md-placeholder {
142+
transition: none;
143+
}
120144

121145
// Use wide border in error state or in focused state
122146
&.md-input-focused .md-input,

0 commit comments

Comments
 (0)