ts-basic-components-textarea.md 5.0 KB
Newer Older
Z
zengyawen 已提交
1
# TextArea
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
The **\<TextArea>** component provides multi-line text input and automatically wraps text so that each line does not have more than the width of the component.
Z
zengyawen 已提交
4

E
esterzhou 已提交
5
>  **NOTE**
E
ester.zhou 已提交
6
>
E
ester.zhou 已提交
7
>  This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9

E
esterzhou 已提交
10
## Child Components
Z
zengyawen 已提交
11

E
ester.zhou 已提交
12
Not supported
Z
zengyawen 已提交
13

Z
zengyawen 已提交
14 15 16

## APIs

E
esterzhou 已提交
17
TextArea(value?:{placeholder?: ResourceStr, text?: ResourceStr, controller?: TextAreaController})
Z
zengyawen 已提交
18

E
ester.zhou 已提交
19
**Parameters**
Z
zengyawen 已提交
20

E
ester.zhou 已提交
21 22
| Name                    | Type                                    | Mandatory  | Description          |
| ----------------------- | ---------------------------------------- | ---- | -------------- |
E
ester.zhou 已提交
23
| placeholder      | [ResourceStr](ts-types.md#resourcestr)  | No   | Placeholder text displayed when there is no input.    |
E
ester.zhou 已提交
24 25
| text             | [ResourceStr](ts-types.md#resourcestr)  | No   | Current text input.    |
| controller<sup>8+</sup> | [TextAreaController](#textareacontroller8) | No   | Text area controller.|
Z
zengyawen 已提交
26 27


E
ester.zhou 已提交
28
## Attributes
Z
zengyawen 已提交
29

E
ester.zhou 已提交
30
In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.
Z
zengyawen 已提交
31

E
ester.zhou 已提交
32 33 34 35 36 37
| Name                      | Type                                    | Description                                      |
| ------------------------ | ---------------------------------------- | ---------------------------------------- |
| placeholderColor         | [ResourceColor](ts-types.md#resourcecolor) | Placeholder text color.                      |
| placeholderFont          | [Font](ts-types.md#font) | Placeholder text style.                                   |
| textAlign                | [TextAlign](ts-appendix-enums.md#textalign) | Horizontal alignment of the text.<br>Default value: **TextAlign.Start**|
| caretColor               | [ResourceColor](ts-types.md#resourcecolor) | Color of the caret in the text box.                              |
E
ester.zhou 已提交
38 39 40
| inputFilter<sup>8+</sup> | {<br>value: [ResourceStr](ts-types.md#resourcestr),<br>error?: (value: string) => void<br>} | Regular expression for input filtering. Only inputs that comply with the regular expression can be displayed. Other inputs are filtered out. The specified regular expression can match single characters, but not strings.<br>- **value**: regular expression to set.<br>- **error**: filtered-out content to return when regular expression matching fails.|
| copyOption<sup>9+</sup>  | [CopyOptions](ts-appendix-enums.md#copyoptions9) | Whether copy and paste is allowed.|

Z
zengyawen 已提交
41 42 43

## Events

E
ester.zhou 已提交
44 45 46 47
In addition to the [universal events](ts-universal-events-click.md), the following events are supported.

| Name                                                        | Description                                                    |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
E
ester.zhou 已提交
48
| onChange(callback: (value: string) =&gt; void) | Triggered when the input in the text box changes.<br>- **value**: text entered.   |
E
ester.zhou 已提交
49 50 51
| onCopy<sup>8+</sup>(callback:(value: string) =&gt; void) | Triggered when the copy button on the pasteboard, which displays when the text box is long pressed, is clicked.<br>- **value**: text to be copied.|
| onCut<sup>8+</sup>(callback:(value: string) =&gt; void) | Triggered when the cut button on the pasteboard, which displays when the text box is long pressed, is clicked.<br>- **value**: text to be cut.|
| onPaste<sup>8+</sup>(callback:(value: string) =&gt; void) | Triggered when the paste button on the pasteboard, which displays when the text box is long pressed, is clicked.<br>- **value**: text to be pasted.|
Z
zengyawen 已提交
52

E
ester.zhou 已提交
53 54
## TextAreaController<sup>8+</sup>

E
ester.zhou 已提交
55
Defines the controller for controlling the **\<TextArea>** component. Currently, the controller can be used to control the caret position.
E
ester.zhou 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68

### Objects to Import

```
controller: TextAreaController = new TextAreaController()
```

### caretPosition<sup>8+</sup>

caretPosition(value: number): void

Sets the position of the caret.

E
ester.zhou 已提交
69 70 71 72 73
**Parameters**

| Name| Type| Mandatory| Description                              |
| ------ | -------- | ---- | -------------------------------------- |
| value  | number   | Yes  | Length from the start of the string to the position where the caret is located.|
E
ester.zhou 已提交
74

Z
zengyawen 已提交
75 76 77

## Example

E
ester.zhou 已提交
78 79
```ts
// xxx.ets
Z
zengyawen 已提交
80 81
@Entry
@Component
E
ester.zhou 已提交
82
struct TextAreaExample {
Z
zengyawen 已提交
83
  @State text: string = ''
E
ester.zhou 已提交
84 85
  controller: TextAreaController = new TextAreaController()

Z
zengyawen 已提交
86 87
  build() {
    Column() {
E
ester.zhou 已提交
88 89 90
      TextArea({ placeholder: 'The text area can hold an unlimited amount of text. input your word', controller: this.controller })
        .placeholderFont({ size: 14, weight: 400 })
        .width(400)
Z
zengyawen 已提交
91
        .height(50)
E
ester.zhou 已提交
92 93
        .margin(20)
        .fontSize(14)
Z
zengyawen 已提交
94 95 96
        .onChange((value: string) => {
          this.text = value
        })
E
ester.zhou 已提交
97 98 99 100 101 102 103 104
      Text(this.text)
      Button('Set caretPosition 1')
        .margin(15)
        .onClick(() => {
          // Move the caret to after the first entered character.
          this.controller.caretPosition(1)
        })
    }.width('100%')
Z
zengyawen 已提交
105 106 107 108
  }
}
```

E
ester.zhou 已提交
109
![textArea](figures/textArea.gif)