Skip to content

Instantly share code, notes, and snippets.

Created February 25, 2018 01:26
Show Gist options
  • Select an option

  • Save anonymous/a5025d2e4e8ab93fee8478708f7825e1 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/a5025d2e4e8ab93fee8478708f7825e1 to your computer and use it in GitHub Desktop.
Svelte component
<div>
{{#each tags as tag}}<span class="tag">{{tag}}<i on:click="remove(tag)"/></span> {{/each}}
<input type="text" on:keyup="update(event.key)" ref:input placeholder="Tags (separate with commas)">
</div>
<script>
export default {
methods: {
remove(tag) {
this.set({
tags: this.get('tags').filter(t => t !== tag)
});
},
update(key) {
if(key !== ',') return;
let value = this.refs.input.value.trim();
let tags = this.get('tags');
value.split(',').map(x => tags.includes(x) || x && tags.push(x));
this.set({'tags': tags});
this.refs.input.value = '';
},
addTag() {
let value = this.refs.input.value.trim();
if(!value) return;
let tags = this.get('tags');
tags.includes(value) || tags.push(value);
this.set({'tags': tags});
this.refs.input.value = '';
}
}
}
</script>
<style>
div {
display: flex;
flex-wrap: wrap;
line-height: 1.5;
vertical-align: baseline;
}
input {
min-width: 6em;
flex: 1 1 0;
margin: 0.2em 0.2em;
}
.tag {
background-color: #efefef;
border: 1px solid #cdcfd3;
margin: 0.2em 0.2em;
padding: 0.2em 0.2em;
border-radius: 1em;
white-space: nowrap;
}
.tag i:hover {
font-weight: bold;
cursor: pointer;
}
.tag i:before {
content: ' \24e7';
font-style: normal;
font-size: larger;
color:red;
}
</style>
{
"tags": [
"fun",
"stuff",
"is",
"super",
"holy",
"cow",
"this",
"is . ",
"varrry",
"fast"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment