cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              
<template id="tagsInput-template">
	<div class="tags">
		<span class="asInput" v-for="(tag, index) in tags1" :class="{ deleting: confirmWaiting && position==index+1 }">{{tag}}</span>
		<input v-model="newTag" placeholder="Add" @keyup.enter="add()" @keyup.backspace="checkIfRemove()" @keyup.left="left()" @keyup.right="right()" :readonly="readonly"/>
		<span class="asInput" v-for="(tag, index) in tags2">{{tag}}</span>
	</div>
</template>

<div id="app">
	<!--<div class="my-control"
         v-for="input in inputs"
         :class="{active: input.isActive}">
    <div class="form-label" 
         :class="{active: input.isActive || input.value}">
			{{input.for}}
		</div>
	</div>-->
	
	<h1>Very simple tags-input with VueJS 2</h1>
	
	<tags-input :tags="tags" :readonly="readonly"></tags-input>
	
	<label>
		<input type="checkbox" v-model="readonly" /> Readonly
	</label>
	
	<ul>
		<li>Write and press enter to add a new tag.</li>
		<li>Press left (it needs to be at position 0 in the input value) or right key (it needs to be at last position in the input value) in order to change position of the input in the list.</li>
		<li>Press backspace twice (within a second and a half) aimed at deleting previous tag.</li>
	</ul>
</div>
            
          
!
            
              
.tags {
	
	display: flex;
	flex-direction: row;
	flex-shrink: 0;
	
	.asInput {
		width: auto;
		padding-right: 1rem;
		background: rgba($cyan, 0.2);
		margin: 0 .2rem;
		
		&.deleting {
			background: $red;
		}
	}
	
	input {
		margin: 0 .2rem;
		max-width: 15rem;
		opacity: 1;
		transition: all .3s ease-in-out;
	}
	
	input[readonly] {
		max-width: 0;
		padding-left: 0;
		padding-right: 0;
		opacity: 0;
		margin-left: 0;
		margin-right: 0;
		pointer-events: none;
	}
}

label {
	margin: 1rem 0;
}

#app {
	padding: 2rem;
	h1 {
		margin-bottom: 1rem;
	}
}
            
          
!
            
              Vue.config.keyCodes.backspace = 8

var tagsInput = Vue.component('tagsInput', Vue.extend({
	template: '#tagsInput-template',
	props: ['tags', 'readonly'],
	data(){
    return{
			newTag: '',
			confirmWaiting: false,
			position: 1
    }
  },
	computed: {
		tags1: function() {
			return this.tags.slice(0, this.position)
		},
		tags2: function() {
			return this.tags.slice(this.position, this.tags.length)
		}
	},
	methods: {
		add() {
			this.tags.splice(this.position, 0, this.newTag)
			this.position += 1
			this.newTag = ''
		},
		checkIfRemove() {
			if(this.confirmWaiting) {
				if(this.newTag.length===0) {
					this.tags.splice(this.position-1, 1)
					this.position -= 1
					if(this.confirmWaiting)
						clearTimeout(this.confirmWaiting)
					this.confirmWaiting = false
				}
			} else {
				//this.confirmWaiting = true
				var _this = this
				this.confirmWaiting = setTimeout(function(){
					_this.confirmWaiting = false;
				}, 1500)
			}
		},
		left() {
			var pos = event.target.selectionStart;
			if(pos===0) {
				this.position = Math.max(0, this.position-1);
			}
			var evt = event
			setTimeout(function() {
				console.log(evt.target)
				evt.target.focus()
			}, 1300)
		},
		right() {
			var pos = event.target.selectionStart;
			if(pos===this.newTag.length) {
				this.position = Math.min(this.tags.length, this.position+1);
			}
			var evt = event
			setTimeout(function() {
				evt.target.focus()
			}, 200)
		}
	}
}))

var app = new Vue({
  el:'#app',
  data(){
    return{
			tags: ['Aurora', 'Hannah', 'John', 'Simone'],
			readonly: false
    }
  }
})
            
          
!
999px
Loading ..................

Console