> For AI agents: the complete documentation index is available at /next/zh/llms.txt, the full documentation bundle is available at /next/zh/llms-full.txt.

# `<textarea>`

APISummary
`<textarea>` 用于创建交互式输入控件，允许用户输入和编辑多行文本。

:::info

此功能需要客户端添加额外依赖才能被使用，集成方式请请参考 [More Elements](/next/zh/guide/start/integrate-with-existing-apps.md).

:::

## 使用指南

### 基本用法

下面是一个基本的 `<textarea>` 组件用法示例：

**下面是一个示例: textarea**

**Bundle:** `dist/base.lynx.bundle` | Web: `dist/base.web.bundle`

```tsx {13-21}
// Copyright 2024 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.

import { root, useState } from "@lynx-js/react";

const App = () => {
  const [inputContent, setInputContent] = useState("");

  return (
    <view style={{ linearOrientation: "vertical", width: "100%", height: "100%", padding: "20px" }}>
      <view style={{ width: "100%", padding: "10px", backgroundColor: "#87654321", borderRadius: "10px" }}>
        <textarea
          style={{ width: "100%", color: "blue", fontSize: "50px" }}
          placeholder="Title"
          maxlines={2}
          bindinput={(res: any) => {
            console.log(res.detail.value);
            setInputContent(res.detail.value);
          }}
        />
      </view>

      <view
        style={{
          width: "100%",
          padding: "10px",
          marginTop: "20px",
          backgroundColor: "#12345678",
          borderRadius: "10px",
        }}
      >
        {
          <textarea
            style={{ width: "100%", height: "300px", fontSize: "30px" }}
            placeholder="Content"
            bindinput={(res: any) => {
              console.log(res.detail.value);
              setInputContent(res.detail.value);
            }}
          />
        }
      </view>
    </view>
  );
};

root.render(<App />);

if (import.meta.webpackHot) {
  import.meta.webpackHot.accept();
}

```

v0.7.0



### 自定义占位符样式

`<textarea>` 可以通过设置 `::placeholder` 伪元素来定制其样式：

**下面是一个示例: textarea**

**Bundle:** `dist/base.lynx.bundle` | Web: `dist/base.web.bundle`

```tsx {13-21}
// Copyright 2024 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.

import { root, useState } from "@lynx-js/react";
import "./index.scss";

const App = () => {
  const [inputContent, setInputContent] = useState("");

  return (
    <view style={{ linearOrientation: "vertical", width: "100%", height: "100%", padding: "20px" }}>
      <text style={{ fontSize: "15px" }}>Customized Placeholder And Caret Color</text>
      <view
        style={{
          width: "100%",
          padding: "10px",
          backgroundColor: "#87654321",
          borderRadius: "10px",
          marginTop: "20px",
        }}
      >
        <textarea
          style={{ width: "100%" }}
          placeholder=".textarea::placeholder {
                              color: oragne
                            }"
          class="textarea"
          bindinput={(res: any) => {
            console.log(res.detail.value);
            setInputContent(res.detail.value);
          }}
        />
      </view>
    </view>
  );
};

root.render(<App />);

if (import.meta.webpackHot) {
  import.meta.webpackHot.accept();
}

```

v0.7.0



## 属性

属性名和属性值用于描述元件的行为和外观。

### `placeholder`

```ts
// DefaultValue: undefined
placeholder?: string
```

占位文字

### `confirm-type`

```ts
// DefaultValue: undefined
'confirm-type'?: 'send' | 'search' | 'go' | 'done' | 'next';
```

指定输入法回车键的表现形式

### `maxlength`

```ts
// DefaultValue: 140
maxlength?: number;
```

输入框最大字符数量限制

### `maxlines`

```ts
// DefaultValue: 140
maxlines?: number;
```

输入框最大输入行数量限制

### `readonly`

```ts
// DefaultValue: false
readonly?: boolean;
```

是否允许输入框可交互，不影响对其进行组件方法调用

### `show-soft-input-on-focus`

```ts
// DefaultValue: true
'show-soft-input-on-focus'?: boolean;
```

聚焦时是否允许拉起系统软键盘

### `type`

```ts
// DefaultValue: 'text'
type?: 'text' | 'number' | 'digit' | 'tel' | 'email';
```

指定键盘的类型

- text\tel\email: 全符号
- number: 0-9 +-.
- digit: 0-9 .

::: info
不同的输入法可能会有不同的视觉表现
:::

### `input-filter`

```ts
// DefaultValue: undefined
'input-filter'?: string;
```

指定单个字符的过滤条件，用正则表达式描述

## 事件

前端可以在元件上设置[事件处理器属性](/next/zh/guide/interaction/event-handling/event-propagation.md#事件处理器属性)来监听元件的运行时行为。

### `bindfocus`

```ts
export interface TextareaFocusEvent {
  value: string;
}
 bindfocus?: (e: BaseEvent<'bindfocus', TextareaFocusEvent>) => void;
```

输入框聚焦时的回调

### `bindblur`

```ts
export interface TextareaBlurEvent {
  value: string;
}
bindblur?: (e: BaseEvent<'bindblur', TextareaBlurEvent>) => void;
```

输入框失去焦点时的回调

### `bindconfirm`

```ts
export interface TextareaConfirmEvent {
  value: string;
}
bindconfirm?: (e: BaseEvent<'bindconfirm', TextareaConfirmEvent>) => void;
```

点击软键盘回车时的回调

### `bindinput`

```ts
export interface TextareaTextareaEvent {
  value: string;
  selectionStart: number;
  selectionEnd: number;
  isComposing?: boolean;
}
bindinput?: (e: BaseEvent<'bindinput', TextareaTextareaEvent>) => void;
```

输入框内容变化的回调

### `bindselection`

```ts
export interface TextareaSelectionEvent {
  selectionStart: number;
  selectionEnd: number;
}
bindselection?: (e: BaseEvent<'bindselection', TextareaSelectionEvent>) => void;
```

输入框光标变化的回调

## 方法

前端可以通过 [SelectorQuery](/next/zh/api/lynx-api/nodes-ref/nodes-ref-invoke.md) API 执行元件的方法。

### `focus`

```ts
export interface TextareaFocusMethod extends BaseMethod {
  method: 'focus';
}
```

控制输入框主动聚焦

### `blur`

```ts
export interface TextareaBlurMethod extends BaseMethod {
  method: 'blur';
}
```

控制输入框主动取消聚焦

### `getValue`

```ts
export interface TextareaGetValueMethod extends BaseMethod {
  method: 'getValue';
  success?: Callback<{
    value: string;
    selectionStart: number;
    selectionEnd: number;
    isComposing: boolean;
  }>;
}
```

获取输入框的内容

### `setValue`

```ts
export interface TextareaSetValueMethod extends BaseMethod {
  method: 'setValue';
  params: {
    value: string;
  };
}
```

主动设置输入框的内容

### `setSelectionRange`

```ts
export interface TextareaSetSelectionRangeMethod extends BaseMethod {
  method: 'setSelectionRange';
  params: {
    selectionStart: number;
    selectionEnd: number;
  };
}
```

主动设置输入框的光标

## 兼容性

**Compatibility Table**

**Query:** `elements.textarea`

[Source](https://github.com/lynx-family/lynx-website/tree/main/packages/lynx-compat-data/elements/textarea.json)

| Feature | Android | HarmonyOS | iOS | macOS | Windows | Web |
| --- | --- | --- | --- | --- | --- | --- |
| textarea | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes |
| attributes | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Partial |
| attributes.placeholder | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes |
| attributes.confirm-type | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes |
| attributes.maxlength | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes |
| attributes.readonly | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes |
| attributes.show-soft-input-on-focus | Yes (3.4) | Yes (3.4) | Yes (3.4) | No | No | No |
| attributes.type | Yes (3.4) | Yes (3.4) | Yes (3.4) | No | No | No |
| attributes.input-filter | Yes (3.4) | Yes (3.4) | Yes (3.4) | No | No | Yes |
| attributes.maxlines | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | No |
| attributes.android-fullscreen-mode | Yes (3.4) | No | No | No | No | No |
| attributes.bounces | No | No | Yes (3.4) | No | No | No |
| attributes.default-value | Yes (4.0) | Yes (4.0) | Yes (3.4) | No | No | No |
| attributes.disabled | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes |
| attributes.enable-scroll-bar | Yes (3.6) | No | Yes (3.5) | No | No | No |
| attributes.ios-auto-correct | No | No | Yes (3.4) | No | No | No |
| attributes.ios-spell-check | No | No | Yes (3.4) | No | No | Yes |
| attributes.line-spacing | Yes (3.4) | Yes (3.4) | Yes (3.4) | No | No | No |
| rests | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes |
| events.bindblur | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (4.0) | Yes |
| events.bindconfirm | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (4.0) | Yes (4.0) | Yes |
| events.bindfocus | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (4.0) | Yes (3.4) | Yes |
| events.bindinput | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes |
| events.bindselection | Yes (3.4) | Yes (3.4) | Yes (3.4) | No | No | Yes |
| methods.blur | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (4.0) | Yes (4.0) | Yes |
| methods.focus | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (4.0) | Yes (3.4) | Yes |
| methods.getValue | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes |
| methods.setSelectionRange | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes |
| methods.setValue | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes |
| methods.boundingClientRect | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (4.0) | Yes (4.0) | Yes |
| methods.requestAccessibilityFocus | Yes (3.4) | No | Yes (4.1) | No | No | No |
| methods.scrollIntoView | Yes (3.4) | Yes (3.4) | Yes (3.4) | Yes (4.0) | Yes (4.0) | Yes |
| methods.setFocus | No | No | No | Yes (4.0) | Yes (3.4) | No |
| methods.takeScreenshot | Yes (4.1) | Yes (4.1) | Yes (4.1) | Yes (4.0) | Yes (4.0) | No |

**`textarea`**

textarea

**`attributes`**

attributes

- **Web — Supported (version unknown):** Partial support; Web Lynx implements only part of this grouped feature.

**`attributes.placeholder`**

<code>placeholder</code>

**`attributes.confirm-type`**

<code>confirm-type</code>

**`attributes.maxlength`**

<code>maxlength</code>

**`attributes.readonly`**

<code>readonly</code>

**`attributes.show-soft-input-on-focus`**

<code>show-soft-input-on-focus</code>

**`attributes.type`**

<code>type</code>

- **Web — Not supported:** The Web textarea does not implement this attribute.

**`attributes.input-filter`**

<code>input-filter</code>

**`attributes.maxlines`**

<code>maxlines</code>

**`attributes.android-fullscreen-mode`**

<code>android-fullscreen-mode</code>

**`attributes.bounces`**

<code>bounces</code>

**`attributes.default-value`**

<code>default-value</code>

**`attributes.disabled`**

<code>disabled</code>

**`attributes.enable-scroll-bar`**

<code>enable-scroll-bar</code>

**`attributes.ios-auto-correct`**

<code>ios-auto-correct</code>

**`attributes.ios-spell-check`**

<code>ios-spell-check</code>

**`attributes.line-spacing`**

<code>line-spacing</code>

**`rests`**

All the rest

**`events.bindblur`**

<code>bindblur</code>

**`events.bindconfirm`**

<code>bindconfirm</code>

**`events.bindfocus`**

<code>bindfocus</code>

**`events.bindinput`**

<code>bindinput</code>

**`events.bindselection`**

<code>bindselection</code>

**`methods.blur`**

<code>blur</code>

**`methods.focus`**

<code>focus</code>

**`methods.getValue`**

<code>getValue</code>

**`methods.setSelectionRange`**

<code>setSelectionRange</code>

**`methods.setValue`**

<code>setValue</code>

**`methods.boundingClientRect`**

<code>boundingClientRect</code>

**`methods.requestAccessibilityFocus`**

<code>requestAccessibilityFocus</code>

**`methods.scrollIntoView`**

<code>scrollIntoView</code>

**`methods.setFocus`**

<code>setFocus</code>

**`methods.takeScreenshot`**

<code>takeScreenshot</code>

