Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id='app'>
    <div class='container'>
        <h1>Vue <strong>&lt;select-search&gt;</strong> component</h1>
        <p>Use arrow keys (up and down) on the input field to switch between previous and next item</p>
        <p>Hit enter on the input field to select, or use your mouse to manually select from select box</p>
        <select-search v-model='firstExample' :items='ppl' :shows='"name"'></select-search>
        <p>Result: {{ firstExample }}</p>
        <p>First option doesn't necessarily have to be disabled</p>
        <select-search v-model='secondExample' :items='ppl' :shows='"name"' :first-disabled='false' :first-label='"ALL"'></select-search>
        <p>Result: {{ secondExample }}</p>
        <p>Button at the end to make it feel like an inline form</p>
        <select-search v-model='thirdExample' :items='ppl' :shows='"name"' :add-btn='true' @selected='thirdExampleSelected'></select-search>
        <p>Smashing enter on the input or clicking the button emits an 'selected' event, so you can listen for it on the parent</p>
        <p>Customize which item key is returned as a result (default: id)</p>
        <select-search v-model='fourthExample' :items='ppl' :shows='"name"' :returns='"name"'></select-search>
        <p>Result: {{ fourthExample }}</p>
    </div>
</div>

<template id='my-template'>
    <div class='input-group col' style='padding:0'>
        <div class='input-group-prepend'>
            <span class='input-group-text'><i class='fa fa-search'></i>&nbsp;{{ getCount() }}</span>
        </div>
        <input type='text' class='form-control'
            v-model='searchQuery'
            placeholder='Search'
            @keyup.enter='addItem'
            @keyup.up='prevItem'
            @keyup.down='nextItem'
            @keyup.esc='close'
        >
        <select class='form-control d-inline' v-model='selectedItem' @change='manualChange'>
            <option :disabled='firstDisabled' value=0>
                <template v-if='addedItem == 0 && !firstDisabled'>=></template> {{ firstLabel }}
            </option>
            <option v-for='item in searchedItems' :key='item.id' :value='item[returns]'>
                <template v-if='addedItem == item[returns]'>=></template> {{ formOptionText(item) }}
            </option>
        </select>
        <div class='input-group-append' v-if='addBtn'>
            <button class='btn btn-primary' @click='addItem'><i class='fa fa-chevron-right'></i></button>
        </div>
    </div>
</template>
              
            
!

CSS

              
                *
  outline: none
  box-sizing: border-box

.container
    padding-top: 20px
              
            
!

JS

              
                Vue.component('select-search', {
    template: '#my-template',
    props: {
        value: {
            required: true
        },
        items: {
            type: Array,
            required: true
        },
        returns: {
            type: String,
            default: 'id'
        },
        shows: {
            type: String,
            required: true
        },
        firstLabel: {
            type: String,
            default: 'Please select'
        },
        firstDisabled: {
            type: Boolean,
            default: true
        },
        search: {
            type: String,
            default: ''
        },
        addBtn: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            searchQuery: '',
            addedItem: this.value || 0,
            selectedItem: this.value || 0
        }
    },
    computed: {
        columns() {
            if(!this.items.length) return []
            return Object.keys(this.items[0])
        },
        searchColumns() {
            if(!this.search.length) return this.columns
            return this.search.split('|')
        },
        searchedItems() {
            if(!this.searchQuery) {
                this.selectedItem = 0
                if(this.addedItem !== 0 && this.checkItemExists(this.addedItem)) this.selectedItem = this.addedItem
                else this.addedItem = 0
                return this.items
            }

            let items = this.items.filter(item => {
                return this.searchColumns.some((column) => {
                    return String(item[column]).toLowerCase().indexOf(this.searchQuery.toLowerCase()) > -1
                })
            })

            if(items.length) this.selectedItem = items[0][this.returns]
            else this.selectedItem = 0

            return items
        }
    },
    methods: {
        checkItemExists(item) {
            return _.map(this.items, this.returns).indexOf(item) > -1
        },
        addItem() {
            //if(this.selectedItem == 0) return
            this.addedItem = this.selectedItem

            if(this.addedItem == 0 && this.firstDisabled) return

            this.$emit('input', this.addedItem)
            this.$emit('selected')
        },
        prevItem() {
            if(!this.items.length) return

            let i = this.searchedItems.map(itm => itm[this.returns]).indexOf(this.selectedItem)

            if(i == 0) {
                if(this.firstDisabled) this.selectedItem = this.searchedItems[this.searchedItems.length - 1][this.returns]
                else this.selectedItem = 0
            } else if(this.selectedItem == 0) this.selectedItem = this.searchedItems[this.searchedItems.length - 1][this.returns]
            else this.selectedItem = this.searchedItems[i - 1][this.returns]
        },
        nextItem() {
            if(!this.items.length) return

            let i = this.searchedItems.map(itm => itm[this.returns]).indexOf(this.selectedItem)

            if(i == this.searchedItems.length - 1) {
                if(this.firstDisabled) this.selectedItem = this.searchedItems[0][this.returns]
                else this.selectedItem = 0
            }
            else this.selectedItem = this.searchedItems[i + 1][this.returns]
        },
        close() {
            this.$emit('close ')
        },
        formOptionText(item) {
            let j = []
            let s = this.shows.split('|')
            s.forEach(x => j.push(item[x]))

            return j.join(' | ')
        },
        getCount() {
            return this.firstDisabled ? this.searchedItems.length : this.searchedItems.length + 1
        },
        manualChange() {
            this.addedItem = this.selectedItem
            this.$emit('input', this.addedItem)
        }
    }
});

const ppl = [
    {
        id: 1,
        name: 'John Doe'
    },
    {
        id: 2,
        name: 'John Cena'
    },
    {
        id: 3,
        name: 'Sylvester Stallone'
    }
]

new Vue({
    el: '#app',
    data: {
        ppl: ppl,
        firstExample: 0,
        secondExample: 0,
        thirdExample: 0,
        fourthExample: 0
    },
    methods: {
        thirdExampleSelected() {
            alert('Result: ' + this.thirdExample)
        }
    }
});
              
            
!
999px

Console