Vue select custom component

Keywords: PHP

Custom select component

I believe that many times you have such a demand. You can't use the Ui framework, but you have to write a select component by yourself. Of course, I have met it today. So I made a very simple select component by myself, but there is a problem with it. Let's talk about the problem later.

<template>
  <div class="xbsjselect">
    <div class="xbsjselect-selectdiv" @click="selectClick">
      <span class="xbsjselect-selectText">{{currentValue}}</span>
      <button class="xbsjselect-selectButton"></button>
    </div>
    <ul class="xbsjselect-selectOption" v-show="selectshow">
      <li v-for="(ct,index) in listdata" :key="index" @click="selectName(ct)">
        <span>{{ ct }}</span>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  props: {
    list: {
        type:Array,
        default:[]
    },
    value:''
  },
  data() {
    return {
      selectshow: false,
      currentValue:this.value
    };
  },
  computed: {
    listdata() {
      return this.list;
    }
  },
  methods: {
    selectClick() {
      this.selectshow = !this.selectshow;
    },
    selectName(value) {
      this.currentValue = value;
      this.selectshow = false;
    }
  },
  watch: {
    currentValue(val, old) {
        this.$emit("input",val);
    }
  }
};
</script>

<style scoped>
.xbsjselect {
  position: relative;
  width: 100%;
}
.xbsjselect-selectdiv {
  display: inline-block;
  width: 100%;
  background: rgba(0, 0, 0, 0.4);
  height: 28px;
  position: relative;
  text-align: left;
  line-height: 28px;
  cursor: pointer;
  padding-left: 13px;
  left: -2px;
  top: 2px;
  border-radius: 3px;
}
.xbsjselect-selectText {
  font-size: 12px;
}
.xbsjselect-selectButton {
  width: 12px;
  height: 10px;
  border: none;
  background: url(../../../images/titles-select.png) no-repeat;
  background-size: contain;
  cursor: pointer;
  float: right;
  margin-right: 20px;
  margin-top: 10px;
  outline: none;
}
.xbsjselect-selectOption {
  width: 100%;
  height: 80px;
  background: rgba(138, 138, 138, 1);
  border-radius: 0px 0px 4px 4px;
  list-style: none;
  overflow: auto;
  z-index: 1;
  position: absolute;
}

.xbsjselect-selectOption li {
  width: 100%;
  height: 20px;
  font-size: 14px;
  color: rgba(221, 221, 221, 1);
  line-height: 20px;
  cursor: pointer;
}
.xbsjselect-selectOption li span {
  display: inline-block;
  height: 20px;
  position: relative;
  left: 11px;
}
.xbsjselect-selectOption li:hover {
  background: rgba(107, 107, 107, 1);
}
</style>

Here's a picture. It is a small triangle, which can be replaced by itself.

Posted by gazalec on Fri, 01 Nov 2019 16:07:06 -0700