forked from muicss/mui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextfield.js
More file actions
139 lines (110 loc) · 3.69 KB
/
textfield.js
File metadata and controls
139 lines (110 loc) · 3.69 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* MUI CSS/JS form-control module
* @module forms/form-control
*/
'use strict';
var jqLite = require('./lib/jqLite'),
util = require('./lib/util'),
animationHelpers = require('./lib/animationHelpers'),
cssSelector = '.mui-textfield > input, .mui-textfield > textarea',
floatingLabelClass = 'mui-textfield--float-label';
var touchedClass = 'mui--is-touched', // hasn't lost focus yet
untouchedClass = 'mui--is-untouched',
pristineClass = 'mui--is-pristine', // user hasn't interacted yet
dirtyClass = 'mui--is-dirty',
emptyClass = 'mui--is-empty', // control is empty
notEmptyClass = 'mui--is-not-empty';
/**
* Initialize input element.
* @param {Element} inputEl - The input element.
*/
function initialize(inputEl) {
// check flag
if (inputEl._muiTextfield === true) return;
else inputEl._muiTextfield = true;
// add initial control state classes
if (inputEl.value.length) jqLite.addClass(inputEl, notEmptyClass);
else jqLite.addClass(inputEl, emptyClass);
jqLite.addClass(inputEl, untouchedClass + ' ' + pristineClass);
// replace `untouched` with `touched` when control loses focus
jqLite.on(inputEl, 'blur', function blurHandler () {
// ignore if event is a window blur
if (document.activeElement === inputEl) return;
// replace class and remove event handler
jqLite.removeClass(inputEl, untouchedClass);
jqLite.addClass(inputEl, touchedClass);
jqLite.off(inputEl, 'blur', blurHandler);
});
// replace `pristine` with `dirty` when user interacts with control
jqLite.one(inputEl, 'input change', function() {
jqLite.removeClass(inputEl, pristineClass);
jqLite.addClass(inputEl, dirtyClass);
});
// add change handler
jqLite.on(inputEl, 'input change', inputHandler);
}
/**
* Handle input events.
*/
function inputHandler() {
var inputEl = this;
if (inputEl.value.length) {
jqLite.removeClass(inputEl, emptyClass);
jqLite.addClass(inputEl, notEmptyClass);
} else {
jqLite.removeClass(inputEl, notEmptyClass);
jqLite.addClass(inputEl, emptyClass)
}
}
/**
* Handle autofill events.
*/
function autofillHandler(inputEl) {
// exit if not under css/js control
if (inputEl._muiTextfield !== true) return;
// execute inputHandler
inputHandler.call(inputEl);
}
/** Define module API */
module.exports = {
/** Initialize input elements */
initialize: initialize,
/** Initialize module listeners */
initListeners: function() {
var doc = document;
// markup elements available when method is called
var elList = doc.querySelectorAll(cssSelector),
i = elList.length;
while (i--) initialize(elList[i]);
// listen for new elements
animationHelpers.onAnimationStart('mui-textfield-inserted', function(ev) {
initialize(ev.target);
});
// add transition css for floating labels
setTimeout(function() {
var css = '.mui-textfield.mui-textfield--float-label > label {' + [
'-webkit-transition',
'-moz-transition',
'-o-transition',
'transition',
''
].join(':all .15s ease-out;') + '}';
util.loadStyle(css);
}, 150);
// listen for autofill events
animationHelpers.onAnimationStart('mui-textfield-autofill', function(ev) {
autofillHandler(ev.target);
});
// pointer-events shim for floating labels
if (util.supportsPointerEvents() === false) {
jqLite.on(doc, 'click', function(ev) {
var targetEl = ev.target;
if (targetEl.tagName === 'LABEL' &&
jqLite.hasClass(targetEl.parentNode, floatingLabelClass)) {
var inputEl = targetEl.previousElementSibling;
if (inputEl) inputEl.focus();
}
});
}
}
};