forked from material-components/material-components-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
210 lines (196 loc) · 6.36 KB
/
index.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
* @license
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import MDCComponent from '@material/base/component';
import {MDCRipple} from '@material/ripple';
import {cssClasses, strings} from './constants';
import {MDCTextFieldAdapter} from './adapter';
import MDCTextFieldFoundation from './foundation';
import {MDCTextFieldBottomLine} from './bottom-line';
import {MDCTextFieldHelperText} from './helper-text';
import {MDCTextFieldInput} from './input';
/**
* @extends {MDCComponent<!MDCTextFieldFoundation>}
* @final
*/
class MDCTextField extends MDCComponent {
/**
* @param {...?} args
*/
constructor(...args) {
super(...args);
/** @private {!MDCTextFieldInput} */
this.input_;
/** @private {?Element} */
this.label_;
/** @type {?MDCRipple} */
this.ripple;
/** @private {?MDCTextFieldBottomLine} */
this.bottomLine_;
/** @private {?MDCTextFieldHelperText} */
this.helperText_;
/** @private {?Element} */
this.icon_;
}
/**
* @param {!Element} root
* @return {!MDCTextField}
*/
static attachTo(root) {
return new MDCTextField(root);
}
/**
* @param {(function(!Element): !MDCRipple)=} rippleFactory A function which
* creates a new MDCRipple.
* @param {(function(!Element): !MDCTextFieldBottomLine)=} bottomLineFactory A function which
* creates a new MDCTextFieldBottomLine.
* @param {(function(!Element): !MDCTextFieldInput)=} inputFactory A function which
* creates a new MDCTextFieldInput.
*/
initialize(
rippleFactory = (el) => new MDCRipple(el),
bottomLineFactory = (el) => new MDCTextFieldBottomLine(el),
inputFactory = (el) => new MDCTextFieldInput(el)) {
const inputElement = this.root_.querySelector(strings.INPUT_SELECTOR);
this.input_ = inputFactory(inputElement);
this.label_ = this.root_.querySelector(strings.LABEL_SELECTOR);
this.ripple = null;
if (this.root_.classList.contains(cssClasses.BOX)) {
this.ripple = rippleFactory(this.root_);
};
if (!this.root_.classList.contains(cssClasses.TEXTAREA)) {
const bottomLineElement = this.root_.querySelector(strings.BOTTOM_LINE_SELECTOR);
if (bottomLineElement) {
this.bottomLine_ = bottomLineFactory(bottomLineElement);
}
};
if (inputElement.hasAttribute(strings.ARIA_CONTROLS)) {
const helperTextElement = document.getElementById(inputElement.getAttribute(strings.ARIA_CONTROLS));
if (helperTextElement) {
this.helperText_ = new MDCTextFieldHelperText(helperTextElement);
}
}
if (!this.root_.classList.contains(cssClasses.TEXT_FIELD_ICON)) {
this.icon_ = this.root_.querySelector(strings.ICON_SELECTOR);
};
}
destroy() {
if (this.ripple) {
this.ripple.destroy();
}
if (this.bottomLine_) {
this.bottomLine_.destroy();
}
if (this.helperText_) {
this.helperText_.destroy();
}
this.input_.destroy();
super.destroy();
}
/**
* @return {boolean} True if the Text Field is disabled.
*/
get disabled() {
return this.foundation_.isDisabled();
}
/**
* @param {boolean} disabled Sets the Text Field disabled or enabled.
*/
set disabled(disabled) {
this.foundation_.setDisabled(disabled);
}
/**
* @param {boolean} valid Sets the Text Field valid or invalid.
*/
set valid(valid) {
this.foundation_.setValid(valid);
}
/**
* Sets the helper text element content.
* @param {string} content
*/
set helperTextContent(content) {
this.foundation_.setHelperTextContent(content);
}
/**
* @return {!MDCTextFieldFoundation}
*/
getDefaultFoundation() {
return new MDCTextFieldFoundation(/** @type {!MDCTextFieldAdapter} */ (Object.assign({
addClass: (className) => this.root_.classList.add(className),
removeClass: (className) => this.root_.classList.remove(className),
addClassToLabel: (className) => {
const label = this.label_;
if (label) {
label.classList.add(className);
}
},
removeClassFromLabel: (className) => {
const label = this.label_;
if (label) {
label.classList.remove(className);
}
},
eventTargetHasClass: (target, className) => target.classList.contains(className),
registerTextFieldInteractionHandler: (evtType, handler) => this.root_.addEventListener(evtType, handler),
deregisterTextFieldInteractionHandler: (evtType, handler) => this.root_.removeEventListener(evtType, handler),
notifyIconAction: () => this.emit(MDCTextFieldFoundation.strings.ICON_EVENT, {}),
registerInputEventHandler: (evtType, handler) => this.input_.listen(evtType, handler),
deregisterInputEventHandler: (evtType, handler) => this.input_.unlisten(evtType, handler),
registerBottomLineEventHandler: (evtType, handler) => {
if (this.bottomLine_) {
this.bottomLine_.listen(evtType, handler);
}
},
deregisterBottomLineEventHandler: (evtType, handler) => {
if (this.bottomLine_) {
this.bottomLine_.unlisten(evtType, handler);
}
},
getBottomLineFoundation: () => {
if (this.bottomLine_) {
return this.bottomLine_.foundation;
}
return undefined;
},
getHelperTextFoundation: () => {
if (this.helperText_) {
return this.helperText_.foundation;
}
return undefined;
},
getInputFoundation: () => {
return this.input_.foundation;
},
},
this.getIconAdapterMethods_())));
}
/**
* @return {!{
* setIconAttr: function(string, string): undefined,
* }}
*/
getIconAdapterMethods_() {
return {
setIconAttr: (name, value) => {
if (this.icon_) {
this.icon_.setAttribute(name, value);
}
},
};
}
}
export {MDCTextField, MDCTextFieldFoundation};