-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtag-input.js
108 lines (92 loc) · 2.12 KB
/
tag-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
104
105
106
107
108
import { html, css, LitElement } from 'https://unpkg.com/lit-element?module';
class TagInput extends LitElement {
static styles = css`
:host {
display: block;
}
.tag-container {
display: flex;
flex-wrap: wrap;
align-items: center;
border: 1px solid #ccc;
padding: 8px;
min-height: 40px;
}
.tag {
display: flex;
align-items: center;
background-color: #f2f2f2;
border-radius: 4px;
padding: 4px 8px;
margin: 4px;
}
.tag-text {
margin-right: 4px;
}
.tag-remove {
cursor: pointer;
}
.input-container {
margin-top: 8px;
}
.tag-input {
width: 98%;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
}
`;
static properties = {
tags: { type: Array },
inputText: { type: String },
};
constructor() {
super();
this.tags = [];
this.inputText = '';
}
handleInputChange(event) {
this.inputText = event.target.value;
}
handleInputKeyDown(event) {
if (event.key === 'Enter' || event.key === ',') {
event.preventDefault();
this.addTag();
}
}
addTag() {
const trimmedText = this.inputText.trim();
if (trimmedText) {
this.tags = [...this.tags, trimmedText];
this.inputText = '';
}
}
removeTag(index) {
this.tags = this.tags.filter((_, i) => i !== index);
}
render() {
return html`
<div class="tag-container">
${this.tags.map(
(tag, index) => html`
<div class="tag">
<span class="tag-text">${tag}</span>
<span class="tag-remove" @click="${() => this.removeTag(index)}">x</span>
</div>
`
)}
</div>
<div class="input-container">
<input
type="text"
class="tag-input form-control"
placeholder="Type and press Enter or comma to add tags"
.value="${this.inputText}"
@input="${this.handleInputChange}"
@keydown="${this.handleInputKeyDown}"
/>
</div>
`;
}
}
customElements.define('tag-input', TagInput);