Pen Settings

HTML

CSS

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.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

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">
  <h3>form auto complete</h3>
  <div class="d-flex">
    <div class="d-flex-item">
      <h4>Single Select</h4>
      <form-autocomplete
        v-model="countryA"
        :options="countries"
        placeholder="please select country"
      ></form-autocomplete>
      <p>
        Selected country : <br/>
        uid: {{countryA.uid}}  <br/>
        title: {{countryA.title}}
      </p>
    </div>
    <div class="d-flex-item">
      <h4>Multi Select</h4>
      <form-autocomplete
        is-multiple
        v-model="multiCountryA"
        :options="countries"
        placeholder="please select country"
      ></form-autocomplete>
      <div>
        Selected country : <br/>
        <p v-for="(v, k) in multiCountryA" :key="k">
          uid: {{v.uid}} - title: {{v.title}}
        </p>
      </div>
    </div>
    <!-- With Scope Slot -->
    <div class="d-flex-item">
      <h4>Single Select - with Scope Slot</h4>
      <form-autocomplete
        v-model="countryB"
        :options="countries"
        placeholder="please select country"
      >
        <template slot-scope="row"><span :class="`flag-icon flag-icon-${toLowerCase(row.item.uid)}`"></span> {{row.item.title}}</template>
        <template slot="selected" slot-scope="row"><span :class="`flag-icon flag-icon-${toLowerCase(row.item.uid)}`"></span> {{row.item.title}}</template>
      </form-autocomplete>
      <p>
        Selected country : <br/>
        uid: {{countryB.uid}}  <br/>
        title: {{countryB.title}}
      </p>
    </div>
    <div class="d-flex-item">
      <h4>Multi Select - with Scope Slot</h4>
      <form-autocomplete
        is-multiple
        v-model="multiCountryB"
        :options="countries"
        placeholder="please select country"
      >
        <template slot-scope="row"><span :class="`flag-icon flag-icon-${toLowerCase(row.item.uid)}`"></span> {{row.item.title}}</template>
        <template slot="selected" slot-scope="row"><span :class="`flag-icon flag-icon-${toLowerCase(row.item.uid)}`"></span> {{row.item.title}}</template>
      </form-autocomplete>
      <div>
        Selected country : <br/>
        <p v-for="(v, k) in multiCountryB" :key="k">
          uid: {{v.uid}} - title: {{v.title}}
        </p>
      </div>
    </div>
    <!-- Default value -->
    <div class="d-flex-item">
      <h4>Single Select - Default value</h4>
      <form-autocomplete
        v-model="countryC"
        :options="countries"
        placeholder="please select country"
      >
        <template slot-scope="row"><span :class="`flag-icon flag-icon-${toLowerCase(row.item.uid)}`"></span> {{row.item.title}}</template>
        <template slot="selected" slot-scope="row"><span :class="`flag-icon flag-icon-${toLowerCase(row.item.uid)}`"></span> {{row.item.title}}</template>
      </form-autocomplete>
      <p>
        Selected country : <br/>
        uid: {{countryC.uid}}  <br/>
        title: {{countryC.title}}
      </p>
    </div>
    <div class="d-flex-item">
      <h4>Multi Select - Default Value</h4>
      <form-autocomplete
        is-multiple
        v-model="multiCountryC"
        :options="countries"
        placeholder="please select country"
      >
        <template slot-scope="row"><span :class="`flag-icon flag-icon-${toLowerCase(row.item.uid)}`"></span> {{row.item.title}}</template>
        <template slot="selected" slot-scope="row"><span :class="`flag-icon flag-icon-${toLowerCase(row.item.uid)}`"></span> {{row.item.title}}</template>
      </form-autocomplete>
      <div>
        Selected country : <br/>
        <p v-for="(v, k) in multiCountryC" :key="k">
          uid: {{v.uid}} - title: {{v.title}}
        </p>
      </div>
    </div>
  </div>
</div>
              
            
!

CSS

              
                .d-flex {
  display: flex;
  flex-wrap: wrap;
}
.d-flex-item {
  width: 300px;
  padding: 10px;
  border: 1px solid #eaeaea;
  margin: 0 20px 20px 0;
}
.form-autocomplete {
  padding: 10px;
  background: #eaeaea;
}

ul {
  width: 200px;
  padding-left: 0;
}
ul > li {
  cursor: pointer;
  list-style: none;
  padding: 5px;
  margin-bottom: 5px;
  border: 1px solid white;
  background: #dedede;
}
ul > li:hover {
  background: #313131;
  color: white;
}
ul > li.disabled,
ul > li.disabled:hover {
  background: #eaeaea;
  color: #a7a7a7;
  cursor: inherit;
}
ul > li.disabled .flag-icon {
  opacity: 0.5;
}

.items-selected {
  margin-top: 20px;
}
.item-selected {
  border-radius: 5px;
  display: inline-block;
  padding: 5px;
  background: #7b8fdc;
  color: white;
  margin: 0 5px 10px 0;
}
              
            
!

JS

              
                const countries = [
  { uid: 'AU', title: 'Australia' },
  { uid: 'ID', title: 'Indonesia' },
  { uid: 'KR', title: 'South Korea' },
  { uid: 'NZ', title: 'New Zealand' },
];

Vue.component('form-autocomplete', {
  template: 
    `<div class="form-autocomplete">
      <div v-if="!isMultiple">
        <input v-if="!value.uid" v-model="textSearch" type="text" :placeholder="placeholder" @focus="showOptions"/>
        <div class="items-selected">
          <div class="item-selected" v-if="value.uid">
            <slot name="selected" :item="value">{{value.uid}} - {{value.title}}</slot> <button @click="clearItem">remove</button>
          </div>
        </div>
        <ul class="options" v-show="isShow">
          <li v-for="item in filteredOptions" @click="selectItem(item)">
            <slot :item="item">{{item.title}}</slot>
          </li>
          <li v-if="filteredOptions.length===0">Item not found</li>
        </ul>
      </div>
      <div v-if="isMultiple">
        <input v-model="textSearch" type="text" :placeholder="placeholder" @focus="showOptions"/>
        <div class="items-selected">
          <div class="item-selected" v-for="(v, k) in value">
            <slot name="selected" :item="v">{{v.uid}} - {{v.title}}</slot> <button @click="removeItem(k)">remove</button>
          </div>
        </div>
        <ul class="options" v-show="isShow">
          <li v-for="item in filteredOptions" :class="{'disabled': inSelectedItems(item.uid)}" @click="addItem(item)">
            <slot :item="item">{{item.title}}</slot>
          </li>
          <li v-if="filteredOptions.length===0">Item not found</li>
        </ul>
      </div>
    </div>`
  ,
  props: {
    isMultiple: {
      type: Boolean,
      default: false,
    },
    placeholder: {
      type: String,
      default: '',
    },
    options: {
      type: Array,
      default() {
        return [];
      },
    },
    value: {
      type: [Object, Array],
      default() {
        return {
          uid: undefined,
          title: undefined,
        };
      },
    },
  },
  data() {
    return {
      textSearch: '',
      isShow: false,
    };
  },
  computed: {
    filteredOptions() {
      return this.options.filter(val => val.title.toLowerCase().includes(this.textSearch.toLowerCase()));
    },
    selectedItems() {
      if (this.isMultiple) {
        return this.value.map(v => v.uid);
      }
      return [];
    },
  },
  methods: {
    inSelectedItems(uid) {
      return this.selectedItems.includes(uid);
    },
    showOptions() {
      this.isShow = true;
    },
    selectItem(item) {
      this.textSearch = '';
      this.isShow = false;
      // this.$emit('update:item', item);
      this.$emit('input', item);
      this.$emit('onSelectItem', item);
    },
    clearItem() {
      this.textSearch = '';
      // this.$emit('update:item', {
      //   id: undefined,
      //   title: undefined,
      // });
      this.$emit('input', {
        id: undefined,
        title: undefined,
      });
      this.$emit('onClearItem');
    },
    addItem(item) {
      if (!this.inSelectedItems(item.uid)) {
        this.textSearch = '';
        this.isShow = false;
        const items = JSON.parse(JSON.stringify(this.value));
        items.push(item);
        // this.$emit('update:item', items);
        this.$emit('input', items);
        this.$emit('onAddItem', items);
      }
    },
    removeItem(index) {
      this.textSearch = '';
      const items = JSON.parse(JSON.stringify(this.value));
      items.splice(index, 1);
      // this.$emit('update:item', items);
      this.$emit('input', items);
      this.$emit('onRemoveItem', items);
    },
  },
});
new Vue({
  el: '#app',
  data() {
    return {
      countryA: {
        uid: undefined,
        title: undefined,
      },
      countryB: {
        uid: undefined,
        title: undefined,
      },
      countryC: {
        uid: 'ID',
        title: 'Indonesia',
      },
      multiCountryA: [],
      multiCountryB: [],
      multiCountryC: [
        { uid: 'ID', title: 'Indonesia' },
        { uid: 'AU', title: 'Australia' },
      ],
    };
  },
  methods: {
    toLowerCase(text) {
      return text.toLowerCase();
    },
  },
});
              
            
!
999px

Console