然后写这三个类名的不同样式
//....k-input.k-input--medium {.k-input__inner {height: 36px;&::placeholder {font-size: 15px;}}}.k-input.k-input--small {.k-input__inner {height: 32px;&::placeholder {font-size: 14px;}}}.k-input.k-input--mini {.k-input__inner {height: 28px;&::placeholder {font-size: 13px;}}}
继承原生 input 属性原生的input
有type
,placeholder
等属性,这里可以使用 vue3 中的useAttrs
来实现props
穿透.子组件可以通过v-bind
将props
绑定
<template><div class="k-input" :class="styleClass"><inputclass="k-input__inner":value="https://www.huyubaike.com/biancheng/inputProps.modelValue"@input="changeInputVal":disabled="inputProps.disabled"v-bind="attrs"/></div></template><script lang="ts" setup>//...const attrs = useAttrs();</script>
可清空通过clearable
属性、Input
的值是否为空以及是否鼠标是否移入来判断是否需要显示可清空图标 。图标则使用组件库的Icon
组件
<template><divclass="k-input"@mouseenter="isEnter = true"@mouseleave="isEnter = false":class="styleClass"><inputclass="k-input__inner":disabled="inputProps.disabled"v-bind="attrs":value="https://www.huyubaike.com/biancheng/inputProps.modelValue"@input="changeInputVal"/><div@click="clearValue"v-if="inputProps.clearable && isClearAbled"v-show="isFoucs"class="k-input__suffix"><Icon name="error" /></div></div></template><script setup lang="ts">//...import Icon from "../icon/index";//...//双向数据绑定&接收属性type InputProps = {modelValue?: string | number;disabled?: boolean;size?: string;clearable?: boolean;};//...const isClearAbled = ref(false);const changeInputVal = (event: Event) => {//可清除clearable(event.target as HTMLInputElement).value? (isClearAbled.value = https://www.huyubaike.com/biancheng/true): (isClearAbled.value = false);inputEmits("update:modelValue", (event.target as HTMLInputElement).value);};//清除input valueconst isEnter = ref(true);const clearValue = https://www.huyubaike.com/biancheng/() => {inputEmits("update:modelValue", "");};</script>
清除图标部分 css 样式
.k-input__suffix {position: absolute;right: 10px;height: 100%;top: 0;display: flex;align-items: center;cursor: pointer;color: #c0c4cc;}
文章插图
密码框 show-password通过传入
show-password
属性可以得到一个可切换显示隐藏的密码框 。这里要注意的是如果传了clearable
则不会显示切换显示隐藏的图标<template><divclass="k-input"@mouseenter="isEnter = true"@mouseleave="isEnter = false":class="styleClass"><inputref="ipt"class="k-input__inner":disabled="inputProps.disabled"v-bind="attrs":value="https://www.huyubaike.com/biancheng/inputProps.modelValue"@input="changeInputVal"/><div class="k-input__suffix" v-show="isShowEye"><Icon @click="changeType" :name="eyeIcon" /></div></div></template><script setup lang="ts">//...const attrs = useAttrs();//...//显示隐藏密码框 showPasswordconst ipt = ref();Promise.resolve().then(() => {if (inputProps.showPassword) {ipt.value.type = "password";}});const eyeIcon = ref("browse");const isShowEye = computed(() => {return (inputProps.showPassword && inputProps.modelValue && !inputProps.clearable);});const changeType = () => {if (ipt.value.type === "password") {eyeIcon.value = "https://www.huyubaike.com/biancheng/eye-close";ipt.value.type = attrs.type || "text";return;}ipt.value.type = "password";eyeIcon.value = "https://www.huyubaike.com/biancheng/browse";};</script>
这里是通过获取input
元素,然后通过它的type
属性进行切换,其中browse
和eye-close
分别是Icon
组件中眼睛开与闭,效果如下
文章插图
带 Icon 的输入框通过
prefix-icon
和suffix-icon
属性可以为Input
组件添加首尾图标 。可以通过
计算属性
判断出是否显示首尾图标,防止和前面的clearable
和show-password
冲突.这里代码做了<template><div class="k-input"><inputref="ipt"class="k-input__inner":class="{ ['k-input--prefix']: isShowPrefixIcon }":disabled="inputProps.disabled"v-bind="attrs":value="https://www.huyubaike.com/biancheng/inputProps.modelValue"@input="changeInputVal"/><div class="k-input__prefix" v-if="isShowPrefixIcon"><Icon :name="inputProps.prefixIcon" /></div><div class="k-input__suffix no-cursor" v-if="isShowSuffixIcon"><Icon :name="inputProps.suffixIcon" /></div></div></template><script setup lang="ts">//...type InputProps = {prefixIcon?: string;suffixIcon?: string;};//...//带Icon输入框const isShowSuffixIcon = computed(() => {return (inputProps.suffixIcon && !inputProps.clearable && !inputProps.showPassword);});const isShowPrefixIcon = computed(() => {return inputProps.prefixIcon;});</script>
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Vue3实现动态导入Excel表格数据
- Vue3 企业级优雅实战 - 组件库框架 - 3 搭建组件库开发环境
- 带你从0到1开发AI图像分类应用
- 从0到1搭建redis6.0.7续更~
- 从0到1搭建redis6.0.7
- Windows操作系统搭建Lsky Pro
- 红米k40参数详细参数_红米k40参数和配置
- 如何破解压缩包的密码从网盘里面下载了一个压缩包,解压的时候需要输入密码,不知道密码是什么,该怎么
- 【深入浅出 Yarn 架构与实现】1-2 搭建 Hadoop 源码阅读环境
- 【炫丽】从0开始做一个WPF+Blazor对话小程序