Vue3文本域(Textarea)

article/2025/9/24 20:54:24

APIs

参数说明类型默认值必传
width文本域宽度string | number‘100%’false
allowClear可以点击清除图标删除内容booleanfalsefalse
autoSize自适应内容高度boolean | {minRows?: number, maxRows?: number}falsefalse
disabled是否禁用booleanfalsefalse
maxlength最大长度numberundefinedfalse
showCount是否展示字数booleanfalsefalse
value(v-model)文本域内容string‘’false

Events

事件名称说明参数
change文本域内容变化时的回调(e: Event) => void
enter按下回车的回调(e: Event) => void

效果如下图:在线预览

在这里插入图片描述
在这里插入图片描述

创建文本域组件Textarea.vue

<script lang="ts">
/*一个根节点时,禁用组件根节点自动继承 attribute,必须使用这种写法!然后在要继承 attribute 的节点上绑定 v-bind="$attrs" 即可多个根节点时,只需在要继承 attribute 的节点上绑定 v-bind="$attrs" 即可
*/
export default {inheritAttrs: false
}
</script>
<script setup lang="ts">
import { ref, computed, watch, onMounted, nextTick } from 'vue'
interface Props {width?: string|number // 文本域宽度allowClear?: boolean // 可以点击清除图标删除内容autoSize?: boolean|{minRows?:number, maxRows?:number} // 自适应内容高度disabled?: boolean // 是否禁用maxlength?: number // 最大长度showCount?: boolean // 是否展示字数value?: string // 文本域内容(v-model)valueModifiers?: object // 用于访问组件的v-model上添加的修饰符
}
const props = withDefaults(defineProps<Props>(), {width: '100%',allowClear: false,autoSize: false,disabled: false,maxlength: undefined,showCount: false,value: '',valueModifiers: () => ({})
})
const areaWidth = computed(() => {if (typeof props.width === 'number') {return props.width + 'px'}return props.width
})
const autoSizeProperty = computed(() => {if (typeof props.autoSize === 'object') {const style: {'min-height'?: string, 'max-height'?: string, [propName: string]: any} = {resize: 'none'}if ('minRows' in props.autoSize) {style['min-height'] = (props.autoSize.minRows as number) * 22 + 10 + 'px'}if ('maxRows' in props.autoSize) {style['max-height'] = (props.autoSize.maxRows as number) * 22 + 10 + 'px'}return style}if (typeof props.autoSize === 'boolean') {if (props.autoSize) {return {'max-height': '9000000000000000px',resize: 'none'}}return {}}
})
const showCountNum = computed(() => {if (props.maxlength) {return props.value.length + ' / ' + props.maxlength}return props.value.length
})
watch(() => props.value,() => {if (JSON.stringify(autoSizeProperty.value) !== '{}') {areaHeight.value = 32nextTick(() => {getAreaHeight()})}}
)
const textarea = ref()
const areaHeight = ref(32)
onMounted(() => {getAreaHeight()
})
function getAreaHeight () {areaHeight.value = textarea.value.scrollHeight + 2
}
const emits = defineEmits(['update:value', 'change', 'enter'])
function onInput (e: any) {if (!('lazy' in props.valueModifiers)) {emits('update:value', e.target.value)emits('change', e)}
}
function onChange (e: any) {if ('lazy' in props.valueModifiers) {emits('update:value', e.target.value)emits('change', e)}
}
function onKeyboard (e: any) {if (e.key === 'Enter') {emits('enter', e)}
}
function onClear () {emits('update:value', '')textarea.value.focus()
}
</script>
<template><divclass="m-textarea":class="{'show-count': showCount}":style="`width: ${areaWidth};`":data-count="showCountNum"><textarearef="textarea"class="u-textarea":class="{disabled: disabled}":style="[`height: ${autoSize ? areaHeight : ''}px`, autoSizeProperty]":value="value":maxlength="maxlength":disabled="disabled"@input="onInput"@change="onChange"@keydown="onKeyboard"v-bind="$attrs" /><span class="m-clear" v-if="!disabled&&allowClear&&value" @click="onClear"><svg focusable="false" class="u-clear" data-icon="close-circle" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="64 64 896 896"><path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64zm165.4 618.2l-66-.3L512 563.4l-99.3 118.4-66.1.3c-4.4 0-8-3.5-8-8 0-1.9.7-3.7 1.9-5.2l130.1-155L340.5 359a8.32 8.32 0 01-1.9-5.2c0-4.4 3.6-8 8-8l66.1.3L512 464.6l99.3-118.4 66-.3c4.4 0 8 3.5 8 8 0 1.9-.7 3.7-1.9 5.2L553.5 514l130 155c1.2 1.5 1.9 3.3 1.9 5.2 0 4.4-3.6 8-8 8z"></path></svg></span></div>
</template>
<style lang="less" scoped>
.m-textarea {position: relative;display: inline-block;.u-textarea {width: 100%;min-width: 0;min-height: 32px;max-width: 100%;height: auto;padding: 4px 11px;color: rgba(0, 0, 0, 0.88);font-size: 14px;line-height: 1.5714285714285714;list-style: none;transition: all 0.3s, height 0s;resize: vertical;position: relative;display: inline-block;vertical-align: bottom;text-overflow: ellipsis;background-color: #ffffff;border: 1px solid #d9d9d9;border-radius: 6px;outline: none;&:hover {border-color: #4096ff;border-inline-end-width: 1px;z-index: 1;}&:focus-within {border-color: #4096ff;box-shadow: 0 0 0 2px rgba(5, 145, 255, 0.1);border-inline-end-width: 1px;outline: 0;}}textarea:disabled {color: rgba(0, 0, 0, 0.25);}textarea::-webkit-input-placeholder {color: rgba(0, 0, 0, 0.25)}textarea:-moz-placeholder {color: rgba(0, 0, 0, 0.25)}textarea::-moz-placeholder {color: rgba(0, 0, 0, 0.25)}textarea:-ms-input-placeholder {color: rgba(0, 0, 0, 0.25)}.m-clear {position: absolute;inset-block-start: 8px;inset-inline-end: 8px;z-index: 1;display: inline-block;line-height: 0;.u-clear {display: inline-block;color: rgba(0, 0, 0, 0.25);font-size: 12px;line-height: 1;vertical-align: -1px;cursor: pointer;transition: color 0.3s;transition: fill 0.3s;&:hover {fill: rgba(0, 0, 0, 0.45);}}}.disabled {color: rgba(0, 0, 0, 0.25);background-color: rgba(0, 0, 0, 0.04);cursor: not-allowed;&:hover {border-color: #d9d9d9;}&:focus-within {border-color: #d9d9d9;box-shadow: none}}
}
.show-count {&::after {color: rgba(0, 0, 0, 0.45);white-space: nowrap;content: attr(data-count);pointer-events: none;float: right;}
}
</style>

在要使用的页面引入

其中引入使用了 Vue3间距(Space)

<script setup lang="ts">
import Textarea from './Textarea.vue'
import { ref, watchEffect } from 'vue'
const value = ref('')
const lazyValue = ref('')
watchEffect(() => {console.log('value:', value.value)
})
watchEffect(() => {console.log('lazyValue:', lazyValue.value)
})
function onChange (e: Event) {console.log('change e:', e)
}
function onEnter (e: KeyboardEvent) {console.log('enter e:', e)
}
</script>
<template><div><h1>Textarea 文本域</h1><h2 class="mt30 mb10">基本使用</h2><Space direction="vertical"><Textareav-model:value="value"placeholder="Basic usage rows 2":rows="2"@change="onChange"@enter="onEnter" />.lazy: 默认情况下,v-model 会在每次 input 事件后更新数据 (IME 拼字阶段的状态例外)<br/>你可以添加 lazy 修饰符来改为在每次 change 事件后更新数据:<Textareav-model:value.lazy="lazyValue"placeholder="Lazy usage rows 2":rows="2"@change="onChange" /></Space><h2 class="mt30 mb10">适应文本高度的文本域</h2><Space direction="vertical" :width="300"><Textareav-model:value="value"placeholder="Autosize height based on content lines"auto-size/></Space><h2 class="mt30 mb10">设置行数</h2><Space direction="vertical" :width="300"><Textareav-model:value="value"placeholder="Autosize height with minimum and maximum number of lines":auto-size="{ minRows: 2, maxRows: 5 }"/></Space><h2 class="mt30 mb10">带移除图标</h2><Space direction="vertical" :width="300"><Textarea v-model:value="value" placeholder="textarea with clear icon" allow-clear /></Space><h2 class="mt30 mb10">带数字提示</h2><Space direction="vertical" :width="300"><Textarea v-model:value="value" show-count :maxlength="10" /></Space><h2 class="mt30 mb10">禁用</h2><Space direction="vertical" :width="300"><Textarea v-model:value="value" disabled /></Space></div>
</template>

http://chatgpt.dhexx.cn/article/1FtPyLDU.shtml

相关文章

HTML 文本框输入框 列表文本框和文本域 等等

初识表单和get提交 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>登录注册</title> </head> <body><!--表单form action: 表单提交的位置&#xff0c;可以是网站&#xff0c;也…

LayUI富文本域使用案例

LayUI富文本域是一款轻量级的富文本编辑器&#xff0c;页面设计比较简约。之前写项目时因为用的是LayUI框架&#xff0c;所以富文本域当时就用的LayUI富文本编辑器&#xff0c;这里整理一下。 先来看一下效果图&#xff0c;页面效果图。 访问效果图&#xff1a; 首先需要引入L…

文本域(可输入多行文本)

1&#xff09;和文本、密码输入框一样&#xff0c;文本域的代码也是在<form></form>标签内部的。 语法如下&#xff1a; <textarea rows"行数" cols"列数">文本</textarea>如&#xff1a; <!DOCTYPE HTML> <html> <…

文本域、标签、密码域、文本区、滚动窗格——文本输入

文本域 将文本域添加到窗口的常用办法就是将它添加到面板或者其他的容器中&#xff1a; JPanel panel new JPanel(); JTextField textField new JTextField("Default input", 20); panel.add(textField);上面的代码添加一个文本域&#xff0c;同时传递“Default …

第十三篇、文本框、密码框和文本域。

文章目录 前言一、文本框、密码框和文本域之间的对比二、代码示例1.文本框2.密码框3.文本域 总结 前言 上一篇我们共同学习了下拉框和列表框&#xff0c;本篇我们将学习文本框、密码框和回顾我们之前学习的文本域。 一、文本框、密码框和文本域之间的对比 文本框&#xff1a;…

java:文本域的简单使用

文本域的简单使用 一、关键代码二、简单说明三、流程图四、源码&#xff08;一&#xff09;、源码A&#xff08;二&#xff09;、源码A的运行效果 五、结语六、定位日期 一、关键代码 常见的创建类方式&#xff0c;创建文本域。需要注意添加相关的包类&#xff0c;具体可看源码…

HTML|下拉框和文本域、文件域

HTML|下拉框和文本域、文件域 1.下拉框 在平时我们填问卷或者冲浪的时候做筛选的时候都会遇到下拉框&#xff0c;html写一个下拉框的方式是使用select标签&#xff0c;name和id是默认属性 <select name"1" id""></select>在select标签内部可…

20、HTML <textarea>标签(文本域)

在使用表单时&#xff0c;例如姓名、年龄字段我们可以使用单行文本框&#xff0c;但是当涉及到描述信息&#xff0c;内容比较多时&#xff0c;单行文本框很有可能放不下所有的内容&#xff0c;这时就需要用到多行文本框。 在 HTML 中&#xff0c;使用 <textarea> 标签来…

概率论公式整理

1、排列与组合公式 2、加法和乘法原理 3、随机试验和随机事件 4、基本事件、样本空间和事件  5、事件的关系与运算 6、概率的公理化定义 7、古典概型  8、几何概型 9、加法公式 10、减法公式 11、条件概率 12、乘法公式 13、独立性 14、全概率公式 15、贝叶斯…

古典概率,条件概率,全概率

文章目录 概率论的基本概念排列数组合数例题 古典概率例题例题 联合概率条件概率例题 条件概率的推广例题 全概率公式例题 贝叶斯公式例题 概率论的基本概念 排列数 组合数 例题 古典概率 例题 例题 联合概率 条件概率 例题 条件概率的推广 例题 全概率公式 例题 贝叶斯公式 例…

《高数叔》概率论与数理统计期末总复习笔记(持续更新中)

文章目录 一、随机时间与概率---day11.随机事件与样本空间的概念2.事件的关系&#xff08;集合之间的关系&#xff09;3.事件的运算律---交换律-结合律-分配律-德摩根律4.概率的概念和性质5.古典概型6.条件概率7.乘法定理8.全概率公式9.贝叶斯公式全概率&贝叶斯举例 10.事件…

概率论发展史上的几个经典问题

1.巴拿赫的火柴盒问题 巴拿赫 Stefan Banach 是 20 世纪初最重要的数学家之一——如果你对流行数学感兴趣&#xff0c;你就会听说过 Banach-Tarski 悖论&#xff1b;如果你做过任何严肃的线性代数&#xff0c;你就会知道巴拿赫空间&#xff1b;如果你读过《破解数学》&#xf…

古典概型,条件概率,贝叶斯公式

概率的定义&#xff0c;性质 定义 设 E E E 是随机试验&#xff0c; S S S 是它的样本空间。    对于 E E E 的每一个事件 A A A 赋予一个实数&#xff0c;记为 P ( A ) P(A) P(A)&#xff0c;称为事件 A A A 的概率。    如果集合函数 P ( ⋅ ) P(\, \boldsymbo…

古典概率基础

1. 抛硬币问题&#xff1a;(二项分布) 一般地&#xff0c;抛硬币n次&#xff0c;其中正面出现k次的情况是, 所以正面出现k次的概率为 练习&#xff1a;使用数值或者解析方法&#xff0c;分析n->无穷的情况&#xff0c;得出结论。 2. 掷骰子 1.用列举法枚举骰子的情况 2.…

古典概率习题

对于至少有一个人中奖&#xff0c;我们都要将其转换为没有人中奖&#xff0c;然后再拿1减去这个概率

统计-4 概率、古典概率

概率 描述事件发生可能性的指标&#xff1b; 假设4个人要出去玩&#xff0c;要决定是否带伞&#xff0c;因此对事件 A “明天会下雨”估计&#xff0c;甲说100%可能下雨&#xff0c;乙说70%&#xff0c;丙说30%&#xff0c;丁说0%肯定不下雨&#xff1b;这些数字代表了每个人对…

C3: 古典概率/几何概率/概率定义及性质/条件概率

》》点赞&#xff0c;收藏关注&#xff0c;理财&技术不迷路《《 目录&#xff1a; 3. 古典概率Classical Probability 频率概率&#xff1a; 古典概型&#xff1a; 这个例子&#xff0c;n的区别就是指定和没有指定。 后面365*364******* 意思是每个人的生日都不一样&…

古典概率,排列组合和贝叶斯定理(学习笔记)

第一次用CSDN写博客&#xff0c;其实主要目的是用来自己做统计学笔记归纳。 我现在是在外国就读统计与数据分析本科。其实本人以前在国内是个数学白痴&#xff0c;只是出国了突然就成了数学好..而且也是听朋友说读统计数据分析以后找工作不愁&#xff0c;所以就误打误撞近了这…

古典概率,先验概率,后验概率,贝叶斯分类器

古典概率&#xff1a;随机现象所能发生的事件是有限的、互不相容的,而且每个基本事件发生的可能性相等。 两个特点&#xff1a; 一是试验的样本空间有限&#xff0c;如掷硬币有正反两种结果&#xff0c;掷骰子有6种结果等&#xff1b; 二是试验中每个结果出现的可能性相同&am…