• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# TextInput
2
3The **\<TextInput>** component provides single-line text input.
4
5>  **NOTE**
6>
7>  This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
8
9
10## Child Components
11
12Not supported
13
14
15## APIs
16
17TextInput(value?:{placeholder?: ResourceStr, text?: ResourceStr, controller?: TextInputController})
18
19**Parameters**
20
21| Name                    | Type                                    | Mandatory  | Description           |
22| ----------------------- | ---------------------------------------- | ---- | --------------- |
23| placeholder   | [ResourceStr](ts-types.md#resourcestr)       | No   | Placeholder text displayed when there is no input.     |
24| text          | [ResourceStr](ts-types.md#resourcestr)       | No   | Current text input.    |
25| controller<sup>8+</sup> | [TextInputController](#textinputcontroller8) | No   | Text input controller.|
26
27
28## Attributes
29
30In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.
31
32| Name                      | Type                                    | Description                                      |
33| ------------------------ | ---------------------------------------- | ---------------------------------------- |
34| type                     | InputType                                | Input box type.<br>Default value: **InputType.Normal**       |
35| placeholderColor         | [ResourceColor](ts-types.md#resourcecolor)     | Placeholder text color.|
36| placeholderFont          | [Font](ts-types.md#font) | Placeholder text font.|
37| enterKeyType             | EnterKeyType                             | Type of the Enter key. Currently, only the default value is supported.<br>Default value: **EnterKeyType.Done**|
38| caretColor               | [ResourceColor](ts-types.md#resourcecolor)    | Color of the caret in the text box.                              |
39| maxLength                | number                                   | Maximum number of characters in the text input.                           |
40| inputFilter<sup>8+</sup> | {<br>value: [ResourceStr](ts-types.md#resourcestr),<br>error?: (value: string) =&gt; 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.|
41| copyOption<sup>9+</sup>  | [CopyOptions](ts-appendix-enums.md#copyoptions9) | Whether copy and paste is allowed.<br>If this attribute is set to **CopyOptions.None**, the paste operation is allowed, but not the copy or cut operation.|
42| showPasswordIcon<sup>9+</sup> | boolean | Whether to display the show password icon at the end of the password text box.<br>Default value: **true**|
43| style<sup>9+</sup> | TextInputStyle | Text input style.<br>Default value: **TextInputStyle.Default**|
44| textAlign<sup>9+</sup>   | [TextAlign](ts-appendix-enums.md#textalign) | Alignment mode of the text in the text box.<br>Default value: **TextAlign.Start** |
45
46## EnterKeyType
47
48| Name                 | Description       |
49| ------------------- | --------- |
50| Go     | The Enter key is labeled "Go."  |
51| Search | The Enter key is labeled "Search." |
52| Send   | The Enter key is labeled "Send." |
53| Next   | The Enter key is labeled "Next."|
54| Done   | The Enter key is labeled "Done."    |
55
56## InputType
57
58| Name                | Description           |
59| ------------------ | ------------- |
60| Normal   | Normal input mode.<br>The value can contain digits, letters, underscores (_), spaces, and special characters.|
61| Password | Password input mode.      |
62| Email    | Email address input mode.|
63| Number   | Digit input mode.     |
64| PhoneNumber<sup>9+</sup> | Phone number input mode.<br>The value can contain digits, plus signs (+), hyphens (-), asterisks (*), and number signs (#). The length is not limited.|
65
66## TextInputStyle<sup>9+</sup>
67
68| Name                | Description           |
69| ------------------ | ------------- |
70| Default   | Default style. The caret width is fixed at 1.5 vp, and the caret height is subject to the background height and font size of the selected text.  |
71| Inline    | Inline input style. The background height of the selected text is the same as the height of the text box.     |
72
73## Events
74
75In addition to the [universal events](ts-universal-events-click.md), the following events are supported.
76
77| Name                                      | Description                                    |
78| ---------------------------------------- | ---------------------------------------- |
79| onChange(callback: (value: string) =&gt; void) | Triggered when the input changes.             |
80| onSubmit(callback: (enterKey: EnterKeyType) =&gt; void) | Triggered when the Enter key on the keyboard is pressed. The return value is the current type of the Enter key.         |
81| onEditChanged(callback: (isEditing: boolean) =&gt; void)<sup>(deprecated)</sup> | Triggered when the input status changes. Sicne API version 8, **onEditChange** is recommended.         |
82| onEditChange(callback: (isEditing: boolean) =&gt; void)<sup>8+</sup> | Triggered when the input status changes. If the value of **isEditing** is **true**, text input is in progress.       |
83| onCopy(callback:(value: string) =&gt; void)<sup>8+</sup> | 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.|
84| onCut(callback:(value: string) =&gt; void)<sup>8+</sup> | 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.|
85| onPaste(callback:(value: string) =&gt; void)<sup>8+</sup> | 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.|
86
87## TextInputController<sup>8+</sup>
88
89Implements the controller of the **\<TextInput>** component.
90
91### Objects to Import
92```
93controller: TextInputController = new TextInputController()
94```
95### caretPosition
96
97caretPosition(value: number): void
98
99Sets the position of the caret.
100
101**Parameters**
102
103| Name| Type| Mandatory| Description                              |
104| ------ | -------- | ---- | -------------------------------------- |
105| value  | number   | Yes  | Length from the start of the string to the position where the caret is located.|
106
107
108## Example
109
110```ts
111// xxx.ets
112@Entry
113@Component
114struct TextInputExample {
115  @State text: string = ''
116  controller: TextInputController = new TextInputController()
117
118  build() {
119    Column() {
120      TextInput({ placeholder: 'input your word...', controller: this.controller })
121        .placeholderColor(Color.Grey)
122        .placeholderFont({ size: 14, weight: 400 })
123        .caretColor(Color.Blue)
124        .width(400)
125        .height(40)
126        .margin(20)
127        .fontSize(14)
128        .fontColor(Color.Black)
129        .onChange((value: string) => {
130          this.text = value
131        })
132      Text(this.text)
133      Button('Set caretPosition 1')
134        .margin(15)
135        .onClick(() => {
136          // Move the caret to after the first entered character.
137          this.controller.caretPosition(1)
138        })
139      // Password text box.
140      TextInput({ placeholder: 'input your password...' })
141        .width(400)
142        .height(40)
143        .margin(20)
144        .type(InputType.Password)
145        .maxLength(9)
146        .showPasswordIcon(true)
147      // Inline-style text box.
148      TextInput({ placeholder: 'inline style' })
149        .width(400)
150        .height(50)
151        .margin(20)
152        .borderRadius(0)
153        .style(TextInputStyle.Inline)
154    }.width('100%')
155  }
156}
157```
158
159![textInput](figures/textInput.gif)
160