SearchBox.vue 2.38 KB
<template>
  <div :class="['search-box','flex',{'max':!showDropdown}]">
    <van-search
      v-model="searchValue"
      :placeholder="placeholder"
      @search="onEmit($event,'search')"
      @clear="onEmit('','search')"
    />
    <template v-if="showDropdown">
      <van-dropdown-menu active-color="#3975C6">
        <van-dropdown-item
          v-model="value"
          :options="option"
          @change="onEmit($event,'change')"
        />
        <div class="arrow y-center">
          <div class="sprite-icon icon-down"></div>
        </div>
      </van-dropdown-menu>
    </template>
  </div>
</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
  },
  data () {
    return {
      value: 0,
      option: [
        // v2.3.0,优惠券和门店服务整合到一起
        { text: '优惠券', value: 0 },
        { text: '精品礼券', value: 1 },
        { text: '商品订单', value: 2 }
        // { text: '门店服务', value: 3 }
      ],
      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;
  .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;
    }
  }
  .van-dropdown-menu {
    margin-left: auto;
    .arrow {
      right: 8px;
      z-index: 998;
    }
    .van-dropdown-menu__bar--opened ~ .arrow .sprite-icon {
      transform: rotate(-180deg);
      transition: all 0.25s;
    }
    .van-dropdown-menu__bar {
      background-color: #fff;
      box-shadow: none;
      .van-dropdown-menu__title {
        color: #545454 !important;
        font-size: 16px;

        &::after {
          display: none;
        }
      }
    }
  }
}
.max {
  padding-right: 0;
  .van-search {
    width: 100%;
  }
}
</style>