-
Notifications
You must be signed in to change notification settings - Fork 27
/
paper-chip-input-autocomplete.js
406 lines (355 loc) · 11.6 KB
/
paper-chip-input-autocomplete.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/**
Material design: [Chips](https://material.io/guidelines/components/chips.html)
`paper-chip-input-autocomplete`
An element with a lightweight completion suggester. The selected item will be
displayed as an paper-chip.
### Styling
The following custom properties and mixins are available for styling:
Custom property | Description | Default
----------------|-------------|----------
`--paper-chip-autocomplete-focus-color` | Label and underline color when the
input is focused | `--primary-color`
`--paper-chip-input-autocomplete-font-size` | Fontsize applied to input field |
`13px`
`--paper-chip-input-autocomplete-label-font-size` | Fontsize applied to input
label | `13px`
`--paper-chip-autocomplete-width` | Width of the suggestion field | `100%`
`--paper-chip-autocomplete-suggestion-background` | Background color of the
autocomplete suggestion field | `--primary-background-color`
`--paper-chip-autocomplete-suggestion-text-color` | Text color of the
autocomplete suggestion field | `--primary-text-color`
`--paper-chip-autocomplete-item-font-size` | Fontsize of the autocomplete
suggestion field | `13px`
`--paper-chip-autocomplete-item-height` | Minimum height of the item | `48px`
@element paper-chip-input-autocomplete
@demo demo/index.html
*/
import '@polymer/iron-a11y-keys/iron-a11y-keys.js';
import '@polymer/paper-input/paper-input.js';
import '@polymer/paper-item/paper-item.js';
import '@polymer/paper-listbox/paper-listbox.js';
import '@polymer/paper-material/paper-material.js';
import '@polymer/paper-ripple/paper-ripple.js';
import '@polymer/paper-styles/default-theme.js';
import './paper-chip-input.js';
import {html, PolymerElement} from '@polymer/polymer/polymer-element.js';
class PaperChipInputAutocomplete extends PolymerElement {
static get template() {
return html`
<style>
:host {
display: block;
position: relative;
}
paper-item:hover {
background: #eee;
color: #333;
cursor: pointer;
}
paper-item.active,
:host ::content paper-item.active {
background: #eee;
color: #333;
}
paper-item {
height: 0px;
--paper-item-selected-weight: normal;
--paper-item-min-height: var(--paper-chip-autocomplete-item-height, 48px);
--paper-item: {
font-family: 'Roboto', sans-serif;
-webkit-font-smoothing: antialiased;
font-size: var(--paper-chip-autocomplete-item-font-size, 13px);
}
}
.hide {
display: none;
}
.autocomplete {
width: var(--paper-chip-autocomplete-width, 100%);
position: absolute;
z-index: 1;
}
paper-listbox {
--paper-listbox-background-color: var(--paper-chip-autocomplete-suggestion-background, var(--primary-background-color));
--paper-listbox-color: var(--paper-chip-autocomplete-suggestion-text-color, var(--primary-text-color));
}
paper-input {
--paper-input-container-focus-color: var(--paper-chip-autocomplete-focus-color, var(--primary-color));
--paper-input-container-input: {
font-family: 'Roboto', sans-serif;
-webkit-font-smoothing: antialiased;
font-size: var(--paper-chip-input-autocomplete-font-size, 13px);
}
--paper-input-container-label: {
font-family: 'Roboto', sans-serif;
-webkit-font-smoothing: antialiased;
font-size: var(--paper-chip-input-autocomplete-label-font-size, 13px);
}
}
paper-chip {
@apply --paper-chip-close-label;
}
</style>
<iron-a11y-keys target="[[_paperInputElement]]" keys="enter" on-keys-pressed="_onKeyEnter"></iron-a11y-keys>
<iron-a11y-keys target="[[_paperInputElement]]" keys="backspace" on-keys-pressed="_onKeyBackspace"></iron-a11y-keys>
<iron-a11y-keys target="[[_paperInputElement]]" keys="esc" on-keys-pressed="_onKeyEsc"></iron-a11y-keys>
<iron-a11y-keys target="[[_paperInputElement]]" keys="down" on-keys-pressed="_onKeyUpOrDown"></iron-a11y-keys>
<iron-a11y-keys target="[[_paperInputElement]]" keys="up" on-keys-pressed="_onKeyUpOrDown"></iron-a11y-keys>
<div>
<paper-input id="paperInput"
disabled$="[[disabled]]"
readonly$="[[readonly]]"
label="[[label]]"
on-keyup="_findItems"
value="{{_inputValue}}"
autofocus="{{autofocus}}"
allowed-pattern="[[allowedPattern]]"
pattern="[[pattern]]"
required$="[[required]]"
auto-validate$="[[autoValidate]]"
error-message="[[errorMessage]]">
<div id="slot2" slot="prefix">
<dom-repeat items="[[items]]">
<template>
[[item.name]]
<paper-chip id="paper-chip-[[item]]-[[index]]"
label="[[item]]"
closable$="[[closable]]"
on-chip-removed="_removeChip">
</paper-chip>
</template>
</dom-repeat>
</div>
</paper-input>
</div>
<dom-if if="[[_isEmpty(_filteredSource)]]">
<template>
<paper-material class$="[[_autocompleteClass]]" elevation="1">
<paper-listbox id="listbox">
<dom-repeat items="[[_filteredSource]]">
<template>
<paper-item id="paperItemAutocomplete" on-tap="_selectPaperItem" closable$="[[closable]]">[[item.text]]
<paper-ripple></paper-ripple>
</paper-item>
</template>
</dom-repeat>
</paper-listbox>
</paper-material>
</template>
</dom-if>
`;
}
static get is() {
return 'paper-chip-input-autocomplete';
}
static get properties() {
return {
/**
* The minimum lengt to trigger the autocomplete.
*/
minSearchStringLength: {type: Number, value: 2},
/**
* An array containing the items.
*/
source: {
type: Array,
value: function() {
return [];
}
},
/**
* If true, the paper-chips can be closed.
*/
closable: {type: Boolean, value: false},
/**
* The label for this paper-chip-input-autocomplete.
*/
label: {type: String, value: ''},
/**
* If true, the paper-chip-input-autocomplete is focused.
*/
autofocus: {type: Boolean, value: false},
/**
* The list of items, which will be set as paper-chips.
*/
items: {
type: Array,
value: function() {
return [];
}
},
/**
* Set to true to disable this input.
*/
disabled: {
type: Boolean,
value: false,
},
/**
* True, to add additional items.
*/
additionalItems: {type: Boolean, value: false},
/**
* True, when duplicates are allowed in combination with additionalItems.
*/
allowDuplicates: {type: Boolean, value: false},
/**
* Set to true to mark the input as required.
*/
required: {type: Boolean, value: false},
/**
* Set to true to auto-validate the input value when it changes.
*/
autoValidate: {type: Boolean, value: false},
/**
* The error message to display when the input is invalid.
*/
errorMessage: {type: String},
/**
* A pattern to validate the input with.
*/
pattern: {type: String},
/**
* Set this to specify the pattern.
*/
allowedPattern: {type: String},
/**
* Set to true to prevent to change the input value.
*/
readonly: {type: Boolean, value: false},
_autoValidate: {type: Boolean, value: false},
_required: {type: Boolean, value: false},
_selectedSuggestionId: {type: Object, observer: '_selectPaperItem'},
_filteredSource: {
type: Array,
value: function() {
return [];
}
},
_inputValue: {type: String, value: ''},
_autocompleteClass: {type: String, value: 'hide'},
_paperInputWidth: {type: Number},
_paperInputElement: {type: Object}
};
}
connectedCallback() {
super.connectedCallback();
this._autoValidate = this.autoValidate;
this._required = this.required;
this._paperInputElement = this.$.paperInput;
}
_isEmpty(item) {
return item.length > 0;
}
_onKeyEnter() {
if (this.additionalItems &&
(this._inputValue != '' && this._inputValue != undefined)) {
this._saveTag(this._inputValue);
this.required = false;
this.autoValidate = false;
this._inputValue = '';
}
}
_onKeyBackspace(event) {
if (this.items.length != 0 &&
(this._inputValue == '' || this._inputValue == undefined)) {
this._removeLastItem();
}
if (this.items.length == 0) {
this.autoValidate = this._autoValidate;
this.required = this._required;
}
}
_onKeyEsc(event) {
this._inputValue = '';
this._hideAutocomplete();
this.$.paperInput.focus;
}
_onKeyUpOrDown(event) {
let lb = this.shadowRoot.querySelector('#listbox');
if (lb) {
lb.focus();
}
}
_removeLastItem() {
if (this.items.length != 0) {
let lastItemIndex = this.items.length - 1;
let lastItemname = this.items[lastItemIndex];
this.splice('items', -1, 1);
this._throwChipRemovedEvent(lastItemname);
}
}
_findItems(event) {
if (this._inputValue.length == 0) {
this._autocompleteClass = 'hide';
}
if (this._inputValue.length >= this.minSearchStringLength) {
this._updatePaperListboxWidth();
this._filterItems();
this._autocompleteClass = 'autocomplete';
} else {
this._autocompleteClass = 'hide';
}
}
_updatePaperListboxWidth() {
this.updateStyles({
'--paper-chip-input-autocomplete-width':
this.$.paperInput.offsetWidth + 'px',
});
}
_filterItems() {
let filteredByString =
this.source.filter(element => this._filterBySearchString(element));
if (filteredByString.length > 0 && this.items.length) {
this._filteredSource = filteredByString.filter(
element => this._filterByExistingPaperChip(element));
} else {
this._filteredSource = filteredByString;
}
}
_filterByExistingPaperChip(element) {
return this.items.indexOf(element.text) == -1;
}
_filterBySearchString(element) {
return element.text.toLowerCase().indexOf(this._inputValue.toLowerCase()) >=
0;
}
_selectPaperItem(event) {
this._saveTag(event.model.item.text);
this.required = false;
this.autoValidate = false;
this._inputValue = '';
this._autocompleteClass = 'hide';
this.$.paperInput.focus();
}
_saveTag(name) {
if (this.allowDuplicates) {
this.push('items', name);
this._throwChipCreatedEvent(name);
} else if (this.items.indexOf(name) == -1) {
this.push('items', name);
this._throwChipCreatedEvent(name);
}
}
_throwChipCreatedEvent(name) {
this.dispatchEvent(new CustomEvent(
'chip-created',
{detail: {'chipLabel': name}, composed: true, bubbles: true}));
}
_hideAutocomplete() {
this._autocompleteClass = 'hide';
}
_removeChip(event) {
const index = this.items.indexOf(event.detail.chipLabel);
if (index != -1) {
this.splice('items', index, 1);
}
}
_throwChipRemovedEvent(chipLabel) {
this.dispatchEvent(new CustomEvent(
'chip-removed',
{detail: {'chipLabel': chipLabel}, composed: true, bubbles: true}));
}
}
window.customElements.define(
PaperChipInputAutocomplete.is, PaperChipInputAutocomplete);