SearchBox.vue 2.31 KB
<template>
  <form :class="['search-box','flex',{'max':!showDropdown}]" action="">
    <van-search
      v-model="searchValue"
      :placeholder="placeholder"
      @search="onEmit($event,'search')"
      @clear="onEmit('','search')"
      @focus="onEmit('','focus')"
      :maxlength="maxlength"
    />
    <template v-if="showDropdown">
      <van-dropdown-menu active-color="#3975C6">
        <van-dropdown-item
          :options="option"
          v-model="value"
          @change="onEmit($event,'change')"
        >
          <template #title>
            <div class="menu-title flex">
              {{option[value].text}}
              <div class="sprite-icon icon-down"></div>
            </div>
          </template>
        </van-dropdown-item>
      </van-dropdown-menu>
    </template>
    <template v-if="showMenu">
      <dropdown-menu
        @confirm="onEmit($event,'menuSelect')"
        :single="single"
        :title="title"
        :data="option"
      ></dropdown-menu>
    </template>
  </form>
</template>

<script type='text/ecmascript-6'>
export default {
  name: 'SearchBox',
  props: {
    keyword: String,
    index: {
      type: Number,
      default: 0
    },
    updata: {
      type: Number,
      default: new Date().getTime()
    },
    showDropdown: {
      type: Boolean,
      default: true
    },
    placeholder: String,
    maxlength: {
      type: Number,
      default: 30
    },
    showMenu: Boolean,
    option: {
      type: Array,
      default: () => []
    },
    single: Boolean,
    title: String

  },
  data () {
    return {
      value: 0,
      searchValue: ''
    }
  },
  watch: {
    'updata' (n) {
      this.value = this.index
    },
    'keyword' (n) {
      this.searchValue = n
    }
  },
  methods: {
    onEmit (e, name) {
      this.$emit(name, e)
    }
  }
}
</script>

<style  lang='scss'>
.search-box {
  height: 50px;
  background: #fff;
  box-shadow: 0px 1px 0px 0px #e9e9e9;
  padding-right: 16px;
  position: relative;
  margin-bottom: 0;
  .van-search {
    width: 263px;
    padding: 4.5px 8px;
    height: 100%;
    background: #fff;
    .van-search__content {
      background-color: #fff;
      border: 1px solid #cdd1d4;
      border-radius: 16px;
    }
  }
}
.max {
  padding-right: 0;
  .van-search {
    width: 100%;
  }
}
.van-icon-search:before {
  color: #cdd1d4;
}
</style>