Skip to content

Commit 050b4f2

Browse files
tlancinaajoslin
authored andcommitted
feat(itemFloatingLabel): add floating labels: 'item-floating-label' class
Closes #1611
1 parent 1a7c1f1 commit 050b4f2

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
IonicModule
3+
.directive('itemFloatingLabel', function() {
4+
return {
5+
restrict: 'C',
6+
link: function(scope, element) {
7+
var el = element[0];
8+
var input = el.querySelector('input, textarea');
9+
var inputLabel = el.querySelector('.input-label');
10+
11+
if ( !input || !inputLabel ) return;
12+
13+
var onInput = function() {
14+
var hasInput = inputLabel.classList.contains('has-input');
15+
if ( input.value && !hasInput ) {
16+
inputLabel.classList.add('has-input');
17+
}
18+
else if ( !input.value && hasInput ) {
19+
inputLabel.classList.remove('has-input');
20+
}
21+
};
22+
23+
input.addEventListener('input', onInput);
24+
25+
var ngModelCtrl = angular.element(input).controller('ngModel');
26+
if ( ngModelCtrl ) {
27+
ngModelCtrl.$render = function() {
28+
input.value = ngModelCtrl.$viewValue || '';
29+
if ( ngModelCtrl.$viewValue ) input.value = ngModelCtrl.$viewValue;
30+
else input.value = '';
31+
onInput();
32+
};
33+
}
34+
35+
scope.$on('$destroy', function() {
36+
input.removeEventListener('input', onInput);
37+
});
38+
}
39+
};
40+
});

scss/_form.scss

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,26 @@ textarea {
155155
height: $line-height-computed + $font-size-base + 12px;
156156
}
157157

158+
.item-floating-label {
159+
display: block;
160+
background-color: transparent;
161+
box-shadow: none;
162+
163+
.input-label {
164+
position: relative;
165+
padding: 5px 0 0 0;
166+
opacity: 0;
167+
top: 10px;
168+
@include transition(opacity .2s ease-in, top .2s linear);
169+
170+
&.has-input {
171+
opacity: 1;
172+
top: 0;
173+
@include transition(opacity .2s ease-in, top .2s linear);
174+
}
175+
}
176+
}
177+
158178

159179
// Form Controls
160180
// -------------------------------

0 commit comments

Comments
 (0)