Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
feat(input): make input placeholder attr work
Browse files Browse the repository at this point in the history
Closes #1279.
  • Loading branch information
ajoslin committed Jan 24, 2015
1 parent 12febb1 commit f1d7f83
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/components/input/demoBasicUsage/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<div layout layout-sm="column">
<md-input-container flex>
<label>First Name</label>
<input ng-model="user.firstName">
<input ng-model="user.firstName" placeholder="Placeholder text">
</md-input-container>
<md-input-container flex>
<label>Last Name</label>
Expand Down
3 changes: 2 additions & 1 deletion src/components/input/input-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ md-input-container.md-THEME_NAME-theme {
text-shadow: '{{foreground-shadow}}';
}

label {
label,
.md-placeholder {
text-shadow: '{{foreground-shadow}}';
color: '{{foreground-3}}';
}
Expand Down
38 changes: 29 additions & 9 deletions src/components/input/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ angular.module('material.components.input', [
.directive('label', labelDirective)
.directive('input', inputTextareaDirective)
.directive('textarea', inputTextareaDirective)
.directive('mdMaxlength', mdMaxlengthDirective);
.directive('mdMaxlength', mdMaxlengthDirective)
.directive('placeholder', placeholderDirective);

/**
* @ngdoc directive
Expand Down Expand Up @@ -42,7 +43,7 @@ angular.module('material.components.input', [
*
* </hljs>
*/
function mdInputContainerDirective($mdTheming) {
function mdInputContainerDirective($mdTheming, $animate, $mdUtil) {
return {
restrict: 'E',
link: postLink,
Expand All @@ -52,18 +53,18 @@ function mdInputContainerDirective($mdTheming) {
function postLink(scope, element, attr) {
$mdTheming(element);
}
function ContainerCtrl($scope, $element, $mdUtil) {
function ContainerCtrl($scope, $element) {
var self = this;

self.element = $element;
self.setFocused = function(isFocused) {
$element.toggleClass('md-input-focused', !!isFocused);
$animate[isFocused ? 'addClass' : 'removeClass']($element, 'md-input-focused');
};
self.setHasValue = function(hasValue) {
$element.toggleClass('md-input-has-value', !!hasValue);
$animate[hasValue ? 'addClass' : 'removeClass']($element, 'md-input-has-value');
};
self.setInvalid = function(isInvalid) {
$element.toggleClass('md-input-invalid', !!isInvalid);
$animate[isInvalid ? 'addClass' : 'removeClass']($element, 'md-input-invalid');
};

$scope.$watch(function() {
Expand Down Expand Up @@ -207,11 +208,15 @@ function inputTextareaDirective($mdUtil, $window, $compile, $animate) {
element
.on('input', inputCheckValue)
.on('focus', function(ev) {
containerCtrl.setFocused(true);
scope.$evalAsync(function() {
containerCtrl.setFocused(true);
});
})
.on('blur', function(ev) {
containerCtrl.setFocused(false);
inputCheckValue();
scope.$evalAsync(function() {
containerCtrl.setFocused(false);
inputCheckValue();
});
});

scope.$on('$destroy', function() {
Expand Down Expand Up @@ -313,4 +318,19 @@ function mdMaxlengthDirective($animate) {
}
}

function placeholderDirective() {
return {
restrict: 'A',
require: '^mdInputContainer',

This comment has been minimized.

Copy link
@gkalpak

gkalpak Jan 25, 2015

Member

This would better be ^^mdInputContainer, I think.

This comment has been minimized.

Copy link
@ajoslin

ajoslin Jan 25, 2015

Author Contributor

Good call.

link: postLink
};

function postLink(scope, element, attr, inputContainer) {
var placeholderText = attr.placeholder;
element.removeAttr('placeholder');

inputContainer.element.append('<div class="md-placeholder">' + placeholderText + '</div>');

This comment has been minimized.

Copy link
@gkalpak

gkalpak Jan 25, 2015

Member

A couple of thoughts:

  1. It would probable make sense to only append a new element iff there is indeed s placeholder specified.
  2. It would be nice to also support interpolated dynamically changing placeholders (ot at least document that they are not supported).

This comment has been minimized.

Copy link
@ajoslin

ajoslin Jan 25, 2015

Author Contributor
  1. The directive won't run unless the placeholder attribute is provided.
  2. I considered this, but thought native angular didn't support dynamic input placeholders. Am I wrong?

This comment has been minimized.

Copy link
@ajoslin

ajoslin Jan 25, 2015

Author Contributor

So native angular doesn't have a placeholder directive, but HTML5 does support dynamic placeholders: http://jsbin.com/cebicafenu/6/edit

I'll fix it.

This comment has been minimized.

Copy link
@gkalpak

gkalpak Jan 25, 2015

Member

The directive won't run unless the placeholder attribute is provided.

LOL, I overlooked this simple fact (been overthinking things every now and then 😄)

}
}

})();
26 changes: 25 additions & 1 deletion src/components/input/input.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ $input-label-default-scale: 1.0 !default;
$input-label-float-offset: 4px !default;
$input-label-float-scale: 0.75 !default;

$input-placeholder-offset: $input-label-default-offset !default;

$input-border-width-default: 1px !default;
$input-border-width-focused: 2px !default;
$input-line-height: 26px !default;
Expand Down Expand Up @@ -44,7 +46,8 @@ md-input-container {
overflow: hidden;
}

label {
label,
.md-placeholder {
order: 1;
pointer-events: none;
-webkit-font-smoothing: antialiased;
Expand All @@ -54,6 +57,24 @@ md-input-container {
transform-origin: left top;
transition: transform $swift-ease-out-timing-function 0.25s;
}
.md-placeholder {
position: absolute;
top: 0;
opacity: 0;
transition-property: opacity, transform;
transform: translate3d(0, $input-placeholder-offset + $baseline-grid, 0);
}
&.md-input-focused:not(.md-input-has-value) {
.md-placeholder {
opacity: 1;
transform: translate3d(0, $input-placeholder-offset, 0);
}
}
&.md-input-has-value-remove-active,
&.md-input-focused {
transition: none;
transition-delay: none;
}

/*
* The .md-input class is added to the input/textarea
Expand Down Expand Up @@ -117,6 +138,9 @@ md-input-container {
transform: translate3d(0,$input-label-float-offset,0) scale($input-label-float-scale);
}
}
&.md-input-has-value .md-placeholder {
transition: none;
}

// Use wide border in error state or in focused state
&.md-input-focused .md-input,
Expand Down

0 comments on commit f1d7f83

Please sign in to comment.