Skip to content
Snippets Groups Projects
SearchFeature.vue 2.86 KiB
Newer Older
Florent's avatar
Florent committed
<template>
  <div>
    <Multiselect
      v-model="selection"
      :options="results"
      :options-limit="10"
      :allow-empty="true"
      track-by="feature_id"
      label="title"
      :reset-after="false"
      select-label=""
      selected-label=""
      deselect-label=""
      :searchable="true"
      :placeholder="placeholder"
Florent's avatar
Florent committed
      :show-no-results="true"
      :loading="loading"
      :clear-on-select="false"
      :preserve-search="true"
      @search-change="search"
      @select="select"
      @close="close"
    >
      <template slot="clear">
        <div
          v-if="selection"
          class="multiselect__clear"
          @click.prevent.stop="selection = null"
        >
Florent Lavelle's avatar
Florent Lavelle committed
          <i
            class="close icon"
            aria-hidden="true"
          />
Florent's avatar
Florent committed
        </div>
      </template>
      <span slot="noResult">
        Aucun résultat.
      </span>
      <span slot="noOptions">
        Saisissez les premiers caractères ...
      </span>
    </Multiselect>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions } from 'vuex';

import Multiselect from 'vue-multiselect';

export default {
  name: 'SearchFeature',

  components: {
    Multiselect
  },

  props: {
    currentSelection : {
      type: Object,
      default: null,
    }
  },

Florent's avatar
Florent committed
  data() {
    return {
      loading: false,
      selection: null,
      text: null,
      results: [],
      placeholder: 'Rechercher un signalement ...'
Florent's avatar
Florent committed
  },

  computed: {
    ...mapState('feature', [
      'features'
    ])
  },

  watch: {
    text: function(newValue) {
      this.loading = true;
      this.GET_PROJECT_FEATURES({
        project_slug: this.$route.params.slug,
        feature_type__slug: this.$route.params.slug_type_signal,
        search: newValue,
        limit: '10'
      })
        .then(() => {
          if (newValue) {
            this.results = this.features;
          } else {
            this.results.splice(0);
          }
          this.loading = false;
        })
        .catch((err) => {
          console.error(err);
          this.loading = false;
Florent's avatar
Florent committed
        });
    }
  },

  created() {
    this.RESET_CANCELLABLE_SEARCH_REQUEST();
  },

    if (this.currentSelection && this.currentSelection.feature_to) {
      this.placeholder = this.currentSelection.feature_to.title;
    }
  },

Florent's avatar
Florent committed
  methods: {
    ...mapMutations(['RESET_CANCELLABLE_SEARCH_REQUEST']),
    ...mapActions('feature', [
      'GET_PROJECT_FEATURES'
    ]),
    search(text) {
      this.text = text;
    },
    select(e) {
      this.$emit('select', e);
    },
    close() {
      this.$emit('close', this.selection);
    }
  }
Florent's avatar
Florent committed
</script>

<style>
.multiselect input {
  line-height: 1em !important;
  padding: 0 !important;
}
.multiselect__placeholder {
  margin: 0;
  padding: 0;
}

.multiselect__input {
  min-height: auto !important;
  line-height: 1em !important;
}
Florent's avatar
Florent committed
</style>