diy-input

公共组件input,开箱即用

<!-- 使用 -->
<DiyInput
    v-model="state.searchInfo.decisionNo"
    placeholder="请输入施工道路、申请部门或施工许可号"
    clearable 
    @clear="confirmSearch"
></DiyInput>
<!-- DiyInput.vue -->
<template>
    <div class="diy-input" @blur="blur">
        <input
            class="diy-input__container"
            :value="props.modelValue"
            :placeholder="props.placeholder"
            @input="input"
            @change="change"
            @clear="clear"
            @focus="focus"
        />
        <el-icon v-if="showClear" class="diy-input__clear" @click="clear"
            ><CircleClose
        /></el-icon>
    </div>
</template>

<script setup>
import { computed, reactive } from "vue";
const emits = defineEmits(["update:modelValue", "input", "change", "clear"]);
const props = defineProps({
    modelValue: {
        //绑定值
        type: String,
        default: "",
    },
    placeholder: {
        type: String,
        default: "请输入",
    },
    clearable: {
        type: Boolean,
        default: false,
    },
});
const state = reactive({
    isFocus: false,
});
let showClear = computed(() => {
    if (props.clearable) {
        return props.modelValue && props.modelValue.length > 0 && state.isFocus;
    } else {
        return false;
    }
});
function input(e) {
    emits("input", e.target.value);
    emits("update:modelValue", e.target.value);
}
function change(e) {
    emits("change", e.target.value);
    emits("update:modelValue", e.target.value);
}
function clear() {
    emits("clear", null);
    emits("update:modelValue", null);
}
function focus() {
    state.isFocus = true;
}
function blur() {
    state.isFocus = false;
}
</script>

<style lang="less" scoped>
.diy-input {
    position: relative;
    width: 100%;
    height: 100%;
    .diy-input__container {
        box-sizing: border-box;
        width: 100%;
        height: 100%;
        font-size: 14px;
        background: rgba(49, 92, 155, 0.2);
        border-radius: 4px 4px 4px 4px;
        border: 1px solid rgba(255, 255, 255, 0.4);
        padding-right: 24px;
    }
    .diy-input__container::-webkit-input-placeholder {
        color: rgba(255, 255, 255, 0.6);
    }
    .diy-input__clear {
        cursor: pointer;
        position: absolute;
        top: 50%;
        right: 8px;
        transform: translate(0, -50%);
    }
}
</style>

预览

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值