不多说直接上代码:
<template>
<div class="ip-box">
<el-input v-model="ip1" name="ip" maxlength="3" onkeyup="value=value.replace(/[^\d]/g,'')" @keyup.native="keyupEvent(1,$event)" @blur="submitIp" @input="(val) => checkVal(val,'ip1')" ref="ip1"></el-input>
<div class="ip-dot"></div>
<el-input v-model="ip2" name="ip" maxlength="3" onkeyup="value=value.replace(/[^\d]/g,'')" @keyup.native="keyupEvent(2,$event)" @blur="submitIp" @input="(val) => checkVal(val,'ip2')" ref="ip2"></el-input>
<div class="ip-dot"></div>
<el-input v-model="ip3" name="ip" maxlength="3" onkeyup="value=value.replace(/[^\d]/g,'')" @keyup.native="keyupEvent(3,$event)" @blur="submitIp" @input="(val) => checkVal(val,'ip3')" ref="ip3"></el-input>
<div class="ip-dot"></div>
<el-input v-model="ip4" name="ip" maxlength="3" onkeyup="value=value.replace(/[^\d]/g,'')" @keyup.native="keyupEvent(4,$event)" @blur="submitIp" @input="(val) => checkVal(val,'ip4')" ref="ip4"></el-input>
<i class="el-icon-circle-close" @click="clearValue"></i>
</div>
<script>
export default {
name: 'ipInput',
data () {
return {
ip1: null,
ip2: null,
ip3: null,
ip4: null
}
},
props: ['defaultIp'],
created () {
if(this.defaultIp) {
let ipList = this.defaultIp.split('.');
this.ip1 = ipList[0];
this.ip2 = ipList[1];
this.ip3 = ipList[2];
this.ip4 = ipList[3];
}
},
methods: {
clearValue () {
this.ip1 = null;
this.ip2 = null;
this.ip3 = null;
this.ip4 = null;
},
submitIp () {
if(this.ip1 && this.ip2 && this.ip3 && this.ip4) {
let ipVal = this.ip1 + '.' + this.ip2 + '.' + this.ip3 + '.' + this.ip4;
this.$emit('getIpVal',ipVal);
this.$parent.$emit("el.form.change");
}else {
return;
}
},
checkVal (val,key) {
if(parseInt(val) > 255) {
switch(key) {
case "ip1":
this.ip1 = 255;
this.focusInput(2);
break;
case "ip2":
this.ip2 = 255;
this.focusInput(3);
break;
case "ip3":
this.ip3 = 255;
this.focusInput(4);
break;
case "ip4":
this.ip4 = 255;
break;
default:
break;
}
}else if(parseInt(val) <= 255 && parseInt(val) >= 100){
switch(key) {
case "ip1":
this.focusInput(2);
break;
case "ip2":
this.focusInput(3);
break;
case "ip3":
this.focusInput(4);
break;
default:
break;
}
}
},
focusInput (index) {
switch(key) {
case "ip2":
this.$refs.ip2.focus();
break;
case "ip3":
this.$refs.ip3.focus();
break;
case "ip4":
this.$refs.ip4.focus();
break;
default:
break;
}
},
keyupEvent (index,e) {
if(e.keyCode === 110) {
if(index == 1) {
this.$refs.ip2.focus();
}else if(index == 2){
this.$refs.ip3.focus();
}else if(index ==3) {
this.$refs.ip4.focus();
}
}
}
}
}
</script>
<style lang='less' scoped>
.ip-box {
width: 240px;
border:1px solid #576372;
height: 32px;
display: flex;
align-items: cneter;
position: relative;
/deep/ .el-input__inner {
border: 0 !important;
}
/deep/ .el-input {
width: 60px;
}
.el-icon-circle-close {
color: #fff;
display: none;
font-size: 12px;
cursor: pointer;
position: absolute;
right: 2px;
}
&:hover {
.el-icon-circle-close{
display: inline-block;
}
}
}
.ip-dot {
display: inline-block;
width: 3px;
height: 3px;
border-radius: 50%;
background-color: #fff;
}
</style>
</template>
|