Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(itemFloatingLabel): Added support for floating labels #1611

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions js/angular/directive/itemFloatingLabel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

IonicModule
.directive('itemFloatingLabel', function() {
return {
restrict: 'C',
link: function(scope, element) {
var el = element[0];
var input = el.querySelector('input, textarea');
var inputLabel = el.querySelector('.input-label');

if ( !input || !inputLabel ) return;

var onInput = function() {
var hasInput = inputLabel.classList.contains('has-input');
if ( input.value !== '' && !hasInput ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if there are only spaces? We probably want the label to stay visible in that case.

var value = (input.value || '').trim()
if ( value && !hasInput ) {
  inputLabel.classList.add('has-input').;
} else if ( !value && hasInput ) {
  inputLabel.classList.remove(hasInput);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

..also, we need to change the label when an angular model changes (if ngModel is set)

var ngModelCtrl = angular.element(input).controller('ngModel');
if (ngModelCtrl) {
  ngModelCtrl.$render = onKeyUp;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The placeholder text? Because the placeholder usually disappears even for spaces, I'm not sure I understand why the trim() is necessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I was thinking wrong.

inputLabel.classList.add('has-input');
}
else if ( input.value == '' && hasInput ) {
inputLabel.classList.remove('has-input');
};
}

input.addEventListener('input', onInput);

var ngModelCtrl = angular.element(input).controller('ngModel');
if ( ngModelCtrl ) {
ngModelCtrl.$render = function() {
if ( ngModelCtrl.$viewValue ) input.value = ngModelCtrl.$viewValue;
else input.value = '';
onInput();
}
}

scope.$on('$destroy', function() {
input.removeEventListener('input', onInput);
});
}
}
})
20 changes: 20 additions & 0 deletions scss/_form.scss
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,26 @@ textarea {
height: $line-height-computed + $font-size-base + 12px;
}

.item-floating-label {
display: block;
background-color: transparent;
box-shadow: none;

.input-label {
position: relative;
padding: 5px 0 0 0;
opacity: 0;
top: 10px;
@include transition(opacity .2s ease-in, top .2s linear);

&.has-input {
opacity: 1;
top: 0;
@include transition(opacity .2s ease-in, top .2s linear);
}
}
}


// Form Controls
// -------------------------------
Expand Down