This repository has been archived by the owner on Jan 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
paper-tags-input.js
103 lines (96 loc) · 2.93 KB
/
paper-tags-input.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
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
import '@polymer/iron-icon/iron-icon.js';
import '@polymer/iron-icons/iron-icons.js';
import '@polymer/paper-input/paper-input.js';
import '@polymer/polymer/lib/elements/dom-repeat.js';
import '@cwmr/paper-chip/paper-chip.js';
/**
* `paper-tags-input`
* Material Design input control for a collection of tags
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class PaperTagsInput extends PolymerElement {
static get template() {
return html`
<style>
:host {
display: block;
}
:host[hidden] {
display: none !important;
}
input {
text-transform: lowercase;
height: 36px;
width: auto !important;
padding-left: 0.5em;
}
paper-chip {
margin: 2px;
padding-right: 6px;
cursor: pointer;
}
iron-icon {
--iron-icon-height: 20px;
--iron-icon-width: 20px;
color: var(--disabled-text-color);
}
</style>
<paper-input label="[[label]]" placeholder="+tag" on-keydown="_onInputKeydown">
<div slot="prefix">
<template is="dom-repeat" items="[[tags]]">
<paper-chip selectable="">[[item]] <iron-icon icon="icons:cancel" on-click="_onTagRemoveClicked"></iron-icon></paper-chip>
</template>
</div>
</paper-input>
`;
}
static get is() { return 'paper-tags-input'; }
static get properties() {
return {
label: {
type: String,
value: 'Tags'
},
tags: {
type: Array,
notify: true,
value: function() { return []; }
}
};
}
addTag(tag) {
if (this.tags === null) {
this.tags = [];
}
var trimmedTag = tag.replace(/^\s+/, '').replace(/\s+$/, '');
if (trimmedTag !== '') {
var tagIndex = this.tags.indexOf(trimmedTag);
if (tagIndex === -1) {
this.push('tags', trimmedTag);
}
}
}
removeTag(tag) {
if (this.tags === null) {
return;
}
var tagIndex = this.tags.indexOf(tag);
if (tagIndex > -1) {
this.splice('tags', tagIndex, 1);
}
}
_onTagRemoveClicked(e) {
this.removeTag(e.model.item);
}
_onInputKeydown(e) {
if (e.keyCode === 13) {
this.addTag(e.target.value.toLowerCase());
e.target.value = '';
}
}
}
window.customElements.define(PaperTagsInput.is, PaperTagsInput);