Skip to content

Textarea

多行文本输入控件。可以单独用,也可以放进 FormField。有 FormField 时会合并 id / aria-* / data-*。支持自动增高(autoSize)、字数统计、加载态与 v-model modifiers。

Installation

pnpm dlx @weme-ui/weme-ui add textarea

Usage

*
0 / 100
Vueexamples/usage.vueExpand codeHide code
<script lang="ts" setup>
import * as v from 'valibot'
import Button from '../../button/button.vue'
import FormField from '../../form-field/form-field.vue'
import Form from '../../form/form.vue'
import Textarea from '../textarea.vue'

const schema = v.object({
  bio: v.pipe(v.string('简介不能为空'), v.minLength(10, '至少写 10 个字')),
})

const defaultValues = {
  bio: '',
}

function onFormSubmit(values: { bio: string }) {
  // eslint-disable-next-line no-alert
  alert(JSON.stringify(values))
}
</script>

<template>
  <Form
    class="max-w-sm w-full"
    :default-values="defaultValues"
    :schema="schema"
    @form-submit="onFormSubmit"
  >
    <FormField v-slot="{ field }" name="bio" label="Bio" required>
      <Textarea
        :model-value="field.state.value"
        :max-length="100"
        placeholder="Tell us about yourself…"
        countable
        @blur="field.api.handleBlur"
        @update:model-value="field.api.handleChange"
      />
    </FormField>
    <Button type="submit"> Submit </Button>
  </Form>
</template>

Examples

Variants

Vueexamples/variants.vueExpand codeHide code
<script lang="ts" setup>
import Textarea from '../textarea.vue'
</script>

<template>
  <div class="flex flex-col gap-3 max-w-sm w-full">
    <Textarea variant="soft" placeholder="Soft" />

    <Textarea variant="outline" placeholder="Outline" />
  </div>
</template>

Sizes

Vueexamples/sizes.vueExpand codeHide code
<script lang="ts" setup>
import Textarea from '../textarea.vue'
</script>

<template>
  <div class="flex flex-col gap-3 max-w-sm w-full">
    <Textarea size="sm" placeholder="Small" />

    <Textarea size="md" placeholder="Medium" />

    <Textarea size="lg" placeholder="Large" />
  </div>
</template>

Auto Size

Vueexamples/auto-size.vueExpand codeHide code
<script lang="ts" setup>
import Textarea from '../textarea.vue'
</script>

<template>
  <div class="flex flex-col gap-3 max-w-sm w-full">
    <Textarea :auto-size="true" :rows="2" :max-rows="6" placeholder="Grows with content…" />

    <Textarea :auto-size="false" :rows="4" placeholder="Fixed height…" />
  </div>
</template>

Loading

Vueexamples/loading.vueExpand codeHide code
<script lang="ts" setup>
import Textarea from '../textarea.vue'
</script>

<template>
  <div class="max-w-sm w-full">
    <Textarea loading placeholder="Loading…" />
  </div>
</template>

Disabled

Vueexamples/disabled.vueExpand codeHide code
<script lang="ts" setup>
import Textarea from '../textarea.vue'
</script>

<template>
  <div class="max-w-sm w-full">
    <Textarea disabled placeholder="Disabled" />
  </div>
</template>

Invalid

Vueexamples/invalid.vueExpand codeHide code
<script lang="ts" setup>
import Textarea from '../textarea.vue'
</script>

<template>
  <div class="flex flex-col gap-3 max-w-sm w-full">
    <Textarea placeholder="Default" />
    <Textarea invalid placeholder="Invalid" />
  </div>
</template>

Unstyled

Vueexamples/unstyled.vueExpand codeHide code
<script lang="ts" setup>
import Textarea from '../textarea.vue'
</script>

<template>
  <div class="max-w-sm w-full">
    <Textarea
      unstyled
      placeholder="Fully custom…"
      class="text-sm py-1 outline-none border-b b-default w-full"
    />
  </div>
</template>

Props

PropTypeDefault
asany'div'
根节点渲染为的元素或组件(Reka UI Primitive)
any
'div'
modelValuestring | null | undefined-
受控值;可用 v-model
string | null | undefined
-
defaultValuestring | null | undefined-
非受控初始值
string | null | undefined
-
variant'soft' | 'outline''soft'
外观变体
'soft' | 'outline'
'soft'
size'sm' | 'md' | 'lg''md'
尺寸
'sm' | 'md' | 'lg'
'md'
placeholderstring-
占位文案
string
-
rowsnumber3
最小行数(自动增高的起始行数)
number
3
maxRowsnumber-
自动增高时的最大行数;未传则不限制
number
-
maxLengthnumber-
最大长度(countable 展示用);超出时计数器使用 error 色
number
-
autoSizeboolean-
是否按内容自动调整行数
boolean
-
countableboolean-
配合 maxLength 显示字数统计
boolean
-
loadingIconIconifyIconProps['icon']'ri:loader-line'
加载中图标(Iconify);loading 为真时显示
IconifyIconProps['icon']
'ri:loader-line'
unstyledboolean-
移除样式变体类,仅保留结构
boolean
-
idstring-
控件 id;未传时回退到 FormField Context
string
-
namestring-
控件 name;未传时回退到 FormField Context
string
-
loadingboolean-
是否加载中;会与 FormField Context 的 loading 合并
boolean
-
requiredboolean-
是否必填;会与 FormField Context 的 required 合并
boolean
-
disabledboolean-
是否禁用;会与 FormField Context 的 disabled 合并
boolean
-
invalidboolean-
是否无效;会与 FormField Context 的 invalid 合并
boolean
-
modelModifiersTextareaModelModifiers-
v-model modifiers(lazy / trim / nullable / optional)
TextareaModelModifiers
-
classany-
根节点额外 class
any
-
uiPartial<TextareaStyleSlots>-
覆盖 slots 样式(root / textarea / innerWrapper / counter / loader)
Partial<TextareaStyleSlots>
-

Events

EventType
update:modelValuevalue: string | null | undefined
值变更时触发(含 v-model);受 lazy / trim / nullable / optional modifiers 影响
value: string | null | undefined
changevalue: string | null | undefined
原生 change 时触发;参数为当前 textarea 字符串(非原生 Event)。lazy 时同时写入 model
value: string | null | undefined
focusevent: FocusEvent
获得焦点
event: FocusEvent
blurevent: FocusEvent
失去焦点
event: FocusEvent

Slots

SlotDescription
counter自定义字数统计;作用域参数 { count };未传时渲染 count / maxLength
自定义字数统计;作用域参数 { count };未传时渲染 count / maxLength
loader自定义加载指示;作用域参数 { loading };未传且 loading 为真时渲染 loadingIcon
自定义加载指示;作用域参数 { loading };未传且 loading 为真时渲染 loadingIcon

Accessibility

原生 <textarea> 控件。通过 useFormFieldControl 合并 FormField Context 的 id、name、disabled、required、aria-labelledby、aria-describedby、aria-invalid 与字段 data-*,并向 FormField 注册控件。根节点为 Reka UI Primitive,并设置 data-slot="textarea"。

Files

  • src/components/textarea/textarea.vue
  • src/components/textarea/textarea.props.ts
  • src/components/textarea/textarea.style.ts