数码资讯
08 - Vue3 UI Framework - Input 组件
选购提示
关注价格、性能、续航、售后和真实使用场景,理性比较后再下单。
接下来再做一个常用的组件 -
input组件返回阅读列表点击 这里
需求分析
开始之前我们先做一个简单的需求分析
input组件有两种类型,即input和textarea类型- 当类型为
textarea时,可以自定义行高,但是当类型为input时,行高恒为1 - 可以自定义外边框的颜色
- 可以设置为禁用
那么可以得到如下参数表格
骨架
实际上我们这里是根据 theme 值判断,然后去渲染原生的 input 或者 textarea 组件,所以可以很容易得到骨架,代码如下:
<template>
<input
v-if="theme === 'input'"
class="jeremy-input-input"
:style="{ '--color': color }"
v-model="text"
@input="change"
/>
<textarea
v-else
class="jeremy-input-textarea"
:rows="rows"
:style="{ '--color': color }"
v-model="text"
@input="change"
/>
</template>
功能
首先,我们再在 Typescript 中声明一些需求分析中的属性:
declare const props: {
value: string;
theme?: "input" | "textarea";
rows?: number;
color?: string;
};
declare const context: SetupContext;
然后,再在 export default 中写入声明的参数:
export default {
install: function (Vue) {
Vue.component(this.name, this);
},
name: "JeremyInput",
props: {
value: {
type: String,
required: true,
},
theme: {
type: String,
default: "input",
},
rows: {
type: Number,
default: 5,
},
color: {
type: String,
default: "#8c6fef",
},
},
};
最后再补全 setup :
setup(props, context) {
const text = ref(props.value);
const change = () => {
context.emit("update:value", text.value);
};
return { text, change };
},
样式表
补全层叠样式表
<style lang="scss">
$theme-color: var(--color);
[class^="jeremy-input"] {
resize: none;
padding: 8px;
border-radius: 4px;
border: none;
box-shadow: 0px 0px 3px 0px $theme-color;
outline: none;
width: 100%;
&[disabled] {
box-shadow: 0px 0px 3px 0px gray;
}
}
</style>
测试
创建一个测试页,导入 JeremyInput 组件,看一下效果:
项目地址 ?
GitHub: https://github.com/JeremyWu917/jeremy-ui
官网地址 ?
JeremyUI: https://ui.jeremywu.top
感谢阅读 ☕
声明:本文内容用于数码产品信息整理与选购参考,具体价格、库存、售后政策以官方渠道和电商页面实时信息为准。