Vue 自定义MAC地址输入组件

实现效果

组件代码

<template>
  <div class="ip-input flex flex-space-between flex-center-cz">
    <input type="text" v-model="value1" maxlength="2" ref="ip1" :placeholder="placeholder" @input="changeToNext1(value1)"/>
    :
    <input type="text" v-model="value2" maxlength="2" ref="ip2" :placeholder="placeholder" @input="changeToNext2(value2)"/>
    :
    <input type="text" v-model="value3" maxlength="2" ref="ip3" :placeholder="placeholder" @input="changeToNext3(value3)"/>
    :
    <input type="text" v-model="value4" maxlength="2" ref="ip4" :placeholder="placeholder" @input="changeToNext4(value4)"/>
    :
    <input type="text" v-model="value5" maxlength="2" ref="ip5" :placeholder="placeholder" @input="changeToNext5(value5)"/>
    :
    <input type="text" v-model="value6" maxlength="2" ref="ip6" :placeholder="placeholder" @input="macAddress"/>
  </div>
</template>

<script>
export default {
  name:'MacInput',
  props:{
    placeholder:{},
    mac: {}
  },
  model: {
    prop: "mac",
    event: "change"
  },
  created(){
  },
  watch: {
    mac: {
      handler(newVal) {
        console.log("newVal", newVal)
        let arr = newVal.split(":")
        if ( arr.length === 6 ) {
          this.value1 = arr[0]
          this.value2 = arr[1]
          this.value3 = arr[2]
          this.value4 = arr[3]
          this.value5 = arr[4]
          this.value6 = arr[5]
        }
        if ( newVal === ":::::" ) {
          this.$emit("change", '')
        }
      },
      deep: true
    }
  },
  mounted() {
    if (this.mac == null) return;
    let arr = this.mac.split(":")
    if ( arr.length === 6 ) {
      this.value1 = arr[0]
      this.value2 = arr[1]
      this.value3 = arr[2]
      this.value4 = arr[3]
      this.value5 = arr[4]
      this.value6 = arr[5]
      this.$emit("change", this.value1 + ":" + this.value2 + ":" + this.value3 + ":" + this.value4 + ":" + this.value5 + ":" + this.value6)
    }
  },
  data() {
    return {
      value1: '',
      value2: '',
      value3: '',
      value4: '',
      value5: '',
      value6: '',
    }
  },
  methods:{
    macAddress() {
      this.$emit("change", this.value1 + ":" + this.value2 + ":" + this.value3 + ":" + this.value4 + ":" + this.value5 + ":" + this.value6);
    },
    changeToNext1(v) {
      if (v.toString().length === 2) {
        this.$nextTick(() => {
          this.$refs.ip2.focus();
        });
      }
      this.$emit("change", this.value1 + ":" + this.value2 + ":" + this.value3 + ":" + this.value4 + ":" + this.value5 + ":" + this.value6);
    },
    changeToNext2(v) {
      if (v.toString().length === 2) {
        this.$nextTick(() => {
          this.$refs.ip3.focus();
        });
      }
      this.$emit("change", this.value1 + ":" + this.value2 + ":" + this.value3 + ":" + this.value4 + ":" + this.value5 + ":" + this.value6);
    },
    changeToNext3(v) {
      if (v.toString().length === 2) {
        this.$nextTick(() => {
          this.$refs.ip4.focus();
        });
      }
      this.$emit("change", this.value1 + ":" + this.value2 + ":" + this.value3 + ":" + this.value4 + ":" + this.value5 + ":" + this.value6);
    },
    changeToNext4(v) {
      if (v.toString().length === 2) {
        this.$nextTick(() => {
          this.$refs.ip5.focus();
        });
      }
      this.$emit("change", this.value1 + ":" + this.value2 + ":" + this.value3 + ":" + this.value4 + ":" + this.value5 + ":" + this.value6);
    },
    changeToNext5(v) {
      if (v.toString().length === 2) {
        this.$nextTick(() => {
          this.$refs.ip6.focus();
        });
      }
      this.$emit("change", this.value1 + ":" + this.value2 + ":" + this.value3 + ":" + this.value4 + ":" + this.value5 + ":" + this.value6);
    },

  }
}
</script>

<style scoped>

.ip-input {
  box-sizing: border-box;
  border: 1px solid #E1DCDC;
  height: 28px;
  background-color: #FFFFFF;
}
.ip-input input {
  border: 0;
  width: 100%;
  text-align: center;
}

</style>

组件使用代码

<template>
  <div class="root flex flex-col border-box padding-l">
    <MacInput v-model="mac" style="width: 200px;"></MacInput>
  </div>
</template>

<script>
import MacInput from '@/components/input/MacInput.vue'

export default{
  name:'',
  created() {
  },
  components: {MacInput},
  data() {
    return {
      mac:null
    }
  },
  methods:{
  }
}
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是唐赢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值