Skip to content

Textarea 文本域 v0.3.7+

19:21

文本域此组件满足了可能出现的表单信息补充,编辑等实际逻辑的功能,内置了字数校验等,和 u-input 的 textarea 基本一致。

平台差异说明

AppH5微信小程序支付宝小程序百度小程序头条小程序QQ小程序

基本使用

html
<template>
  <u-textarea v-model="value1" placeholder="请输入内容" />
</template>

<script setup lang="ts">
import { ref } from 'vue'
const value1 = ref<string>('')
</script>

字数统计

html
<template>
  <u-textarea v-model="value2" placeholder="请输入内容" count />
</template>

<script setup lang="ts">
import { ref } from 'vue'
const value2 = ref<string>('统计字数')
</script>

自动增高

html
<template>
  <u-textarea v-model="value3" placeholder="请输入内容" autoHeight />
</template>

<script setup lang="ts">
import { ref } from 'vue'
const value3 = ref<string>('')
</script>

禁用状态

设置disabled属性实现进行禁用,您也可以动态设置是否禁用

html
<template>
  <u-textarea v-model="value4" placeholder="文本域已被禁用" disabled count />
</template>

<script setup lang="ts">
import { ref } from 'vue'
const value4 = ref<string>('')
</script>

下划线模式

设置border="bottom"属性单独设置底部下划线

html
<template>
  <u-textarea v-model="value5" placeholder="请输入内容" border="bottom" />
</template>

<script setup lang="ts">
import { ref } from 'vue'
const value5 = ref<string>('')
</script>

格式化处理

如有需要,可以通过formatter参数编写自定义格式化规则。

html
<template>
	<u-textarea v-model="value" :formatter="formatter" ref="textarea" />
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
const value = ref<string>('')
const textarea = ref<any>(null)

function formatter(val: string) {
	// 让输入框只能输入数值,过滤其他字符
	return val.replace(/[^0-9]/ig, '')
}
</script>

API

Props

参数说明类型默认值可选值
value输入框的内容String | String--
placeholder输入框为空时占位符Number | String--
height输入框高度String | Number70-
confirmType设置键盘右下角按钮的文字,仅微信小程序、App-vue 和 H5 有效Stringdone-
disabled是否禁用Booleanfalsetrue
count是否显示统计字数Booleanfalsetrue
focus是否自动获取焦点,nvue 不支持,H5 取决于浏览器实现Booleanfalsetrue
autoHeight是否自动增加高度Booleanfalsetrue
ignoreCompositionEvent是否忽略组件内对文本合成系统事件的处理。为 false 时将触发 compositionstart、compositionend、compositionupdate 事件,且在文本合成期间会触发 input 事件Booleantruefalse
fixed如果 textarea 在一个 position:fixed 的区域,需要显示指定属性 fixed 为 trueBooleanfalsetrue
cursorSpacing指定光标与键盘的距离Number0-
cursor指定 focus 时的光标位置Number | String--
showConfirmBar是否显示键盘上方带有“完成”按钮的那一栏Booleantruefalse
selectionStart光标起始位置,自动聚焦时有效,需与 selectionEnd 搭配使用Number-1-
selectionEnd光标结束位置,自动聚焦时有效,需与 selectionStart 搭配使用Number-1-
adjustPosition键盘弹起时,是否自动上推页面Booleantruefalse
disableDefaultPadding是否去掉 iOS 下的默认内边距(仅微信小程序有效)Booleanfalsetrue
holdKeyboardfocus 时,点击页面不会收起键盘(仅微信小程序有效)Booleanfalsetrue
maxlength最大输入长度,设置为 -1 时不限制最大长度String | Number140-
border边框类型:surround-四周边框,none-无边框,bottom-底部边框Stringsurroundbottom
placeholderClass指定 placeholder 的样式类,注意当页面或组件使用 scoped 时需在类名前写 /deep/Stringtextarea-placeholder-
placeholderStyle指定 placeholder 的样式,字符串或对象形式,如 "color: red;"String | Objectcolor: #c0c4cc-
formatter输入过滤或格式化函数Functionnull-
ignoreCompositionEvent是否忽略组件内对文本合成系统事件的处理Booleantruefalse

Events

事件名说明回调参数
focus输入框聚焦时触发,event.detail = { value, height },height 为键盘高度event
blur输入框失去焦点时触发,event.detail = {value, cursor}event
linechange输入框行数变化时调用,event.detail = {height: 0, heightRpx: 0, lineCount: 0}event
input当键盘输入时,触发 input 事件event.detail.value
confirm点击完成时, 触发 confirm 事件event
keyboardheightchange键盘高度发生变化的时候触发此事件event