-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclearable-input.html
More file actions
273 lines (250 loc) · 6.73 KB
/
clearable-input.html
File metadata and controls
273 lines (250 loc) · 6.73 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<!--
`clearable-input` is a Polymer element that encapsulates the idea of a text input that has a clickable icon inside the element to clear the input.
<clearable-input></clearable-input>
@author Brent Hosie
@element clearable-input
@homepage http://github.com/xrapiddavex/clearable-input/
-->
<link rel="import" href="../polymer/polymer.html"/>
<link rel="import" href="../iron-icons/iron-icons.html">
<dom-module id="clearable-input">
<link rel="import" type="css" href="./clearable-input.css"/>
<template>
<span class="wrapper">
<input type="text" id="textInput" placeholder$="{{placeholder}}" value$="{{value::input}}" on-click="fireClicked" on-input="_valueChanged"/>
<div id="iconWrapper" hidden$="{{_isClearHidden}}">
<iron-icon id="clearInput" icon="{{icon}}" on-click="_testClearInput"></iron-icon>
<div>
</span>
</template>
<script>
(function() {
"use strict";
Polymer({
is: "clearable-input",
properties: {
/**
* The placeholder of the text input
*
* @attribute placeholder
* @access public
* @type string
* @default ''
*/
placeholder: {
type: String,
value: "",
notify: true
},
/**
* The value of the text input
*
* @attribute value
* @access public
* @type string
* @default ''
*/
value: {
type: String,
value: "",
observer: "_valueChanged",
notify: true
},
/**
* If set on the element, makes the text input readonly. The text input is still clearable.
*
* @attribute isReadOnly
* @access public
* @type boolean
* @default undefined
*/
isReadOnly: {
type: Boolean,
value: void 0,
},
/**
* If set on the element, makes the text input disabled. The text input will not be clearable.
* isDisabled overrides isReadOnly.
*
* @attribute isDisabled
* @access public
* @type boolean
* @default undefined
*/
isDisabled: {
type: Boolean,
value: void 0,
},
/**
* The iron-icon that's used for clearing the input text
*
* @attribute icon
* @access public
* @type string
* @default 'clear'
*/
icon: {
type: String,
value: "clear",
notify: true
},
/**
* Whether or not to show the clear input
*
* attribute _isClearHidden
* access public
* type boolean
* default true
*/
_isClearHidden: false
},
/**
* On Polymer ready, set defaults
*/
ready: function() {
this._resolveDisabledUI();
this._resolveReadOnlyUI();
},
/**
* Tests whether or not clicking on the clear button should clear the value.
* Returns true if the isDisabled attribute is not set, false otherwise.
*
* function _testClearInput
* access private
* returns boolean
*/
_testClearInput: function() {
if(this.isDisabled === void 0) {
this.clearInput();
return true;
}
return false;
},
/**
* Programmatically clears the value of the text input
*
* @method clearInput
* @access public
* @returns boolean
*/
clearInput: function() {
this.value='';
/**
* Fires when the the value of the text input is cleared
*
* @event clear
*/
this.fire('clear');
return true;
},
/**
* Is called when the value of the text input changes
*
* method _valueChanged
* access private
* returns undefined
*/
_valueChanged: function() {
this._resolveClearHiddenUI();
/**
* Fires when the the value of the text input is changed
*
* @event change
*/
this.fire('change');
},
/**
* Sets the input as is-disabled (non-clearable)
*
* @method setAsDisabled
* @access public
* @returns undefined
*/
setAsDisabled: function() {
this.isDisabled = true;
this._resolveDisabledUI();
},
/**
* Sets the input as is-read-only (clearable, non-editable)
*
* @method setAsReadOnly
* @access public
* @returns undefined
*/
setAsReadOnly: function() {
this.isReadOnly = true;
this._resolveReadOnlyUI();
},
/**
* Fully enables the input, removing any restrictions
*
* @method enable
* @access public
* @returns undefined
*/
enable: function() {
this.isDisabled = void 0;
this.isReadOnly = void 0;
this._resolveDisabledUI();
this._resolveReadOnlyUI();
},
/**
* Adds/removes the disabled flag from the textInput based
* on the value of isDisabled
*
* method _resolveDisabledUI
* access private
* returns undefined
*/
_resolveDisabledUI: function() {
this.$.textInput.removeAttribute('disabled');
if (this.isDisabled !== void 0) {
this.$.textInput.setAttribute('disabled', true);
}
this._resolveClearHiddenUI();
},
/**
* Adds/removes the readonly flag from the textInput based
* on the value of isReadOnly
*
* method _resolveReadOnlyUI
* access private
* returns undefined
*/
_resolveReadOnlyUI: function() {
this.$.textInput.removeAttribute('readonly');
if (this.isReadOnly !== void 0) {
this.$.textInput.setAttribute('readonly', true);
}
},
/**
* Sets _isClearHidden to true if the textInput is disabled, or
* the textInput contains no value
*
* method _resolveClearHiddenUI
* access private
* returns undefined
*/
_resolveClearHiddenUI: function() {
this._isClearHidden = this.value.length === 0 || this.isDisabled !== void 0;
},
/**
* Is called when the text input is clicked on. This will allow people to tell when the input itself is clicked,
* not when the clearable button is clicked
*
* method valueChanged
* access public
* returns undefined
*/
fireClicked: function() {
/**
* Fires only when the input itself is clicked, not when the clearable button is clicked
*
* @event inputclicked
*/
this.fire('inputclicked');
}
});
})();
</script>
</dom-element>