-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathampersand-floatinglabel-input-view.js
62 lines (53 loc) · 1.73 KB
/
ampersand-floatinglabel-input-view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var AmpersandInputView = require( 'ampersand-input-view' ),
extend = require( 'amp-extend' );
module.exports = AmpersandInputView.extend({
bindings: extend( AmpersandInputView.prototype.bindings, {
validityClass: [
{
type: 'class',
selector: 'input, textarea'
},
{
type: 'class',
hook: 'label'
}
]
}),
initialize: function( options ) {
'use strict';
options = options || {};
if ( options.labelClass ) {
this.labelClass = options.labelClass.split( ' ' );
} else {
this.labelClass = [ 'floating' ];
}
AmpersandInputView.prototype.initialize.apply( this, arguments );
},
render: function() {
'use strict';
AmpersandInputView.prototype.render.apply( this, arguments );
this.on( 'change:value', this.checkLabel );
this.labelEl = this.queryByHook( 'label' );
this.labelContainer = this.queryByHook( 'label-container' );
this.checkLabel();
},
checkLabel: function() {
'use strict';
var self = this,
action;
// Float the label up if there is input text, or, if the warning message
// must be displayed (adjacent to label)
if ( this.input.value || ( this.shouldValidate && this.changed ) ) {
action = 'add';
} else {
action = 'remove';
}
this.labelClass.forEach(function( cls ) {
if ( self.labelContainer ) {
self.labelContainer.classList[action]( cls );
} else {
self.labelEl.classList[action]( cls );
}
});
}
});