ui-js-components-input.md 6.6 KB
Newer Older
G
ge-yafang 已提交
1
# <input> Development
Z
zengyawen 已提交
2 3


G
ge-yafang 已提交
4 5 6 7 8 9
The <input> component provides an interactive way to receive user input of various types, including date, checkbox, and button. For details, see [input](../reference/arkui-js/js-components-basic-input.md).


## Creating an <input> Component

Create an <input> component in the .hml file under pages/index.
Z
zengyawen 已提交
10 11 12 13 14


```
<!-- xxx.hml -->
<div class="container">       
G
ge-yafang 已提交
15
  <input type="text">             Please enter the content  </input>
Z
zengyawen 已提交
16 17 18
</div>
```

G
ge-yafang 已提交
19

Z
zengyawen 已提交
20 21 22 23 24 25 26 27 28 29
```
/* xxx.css */
.container {
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5;
}
```

G
ge-yafang 已提交
30 31 32 33
![en-us_image_0000001222807768](figures/en-us_image_0000001222807768.png)


## Setting the Input Type
Z
zengyawen 已提交
34

G
ge-yafang 已提交
35
Set the type attribute of the <input> component to button, date, or any of the supported values.
Z
zengyawen 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57


```
<!-- xxx.hml -->
<div class="container">
  <div class="div-button">
    <dialog class="dialogClass" id="dialogId">
      <div class="content">
        <text>this is a dialog</text>
      </div>
    </dialog>
    <input class="button" type="button" value="click" onclick="btnclick"></input>
  </div>
  <div class="content">
    <input onchange="checkboxOnChange" checked="true" type="checkbox"></input>
  </div>
  <div class="content">
    <input type="date" class="flex" placeholder="Enter data"></input>
  </div>
</div>
```

G
ge-yafang 已提交
58

Z
zengyawen 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
```
/* xxx.css */
.container {
  align-items: center;
  flex-direction: column;
  justify-content: center;
  background-color: #F1F3F5 ;
}
.div-button {
  flex-direction: column;
  align-items: center;
}
.dialogClass{
  width:80%;
  height: 200px;
}
.button {
  margin-top: 30px;
  width: 50%;
}
.content{
  width: 90%;
  height: 150px;
  align-items: center;
  justify-content: center;
}
.flex {
  width: 80%;
  margin-bottom:40px;
}
```

G
ge-yafang 已提交
91

Z
zengyawen 已提交
92 93 94 95 96 97 98 99 100 101
```
// xxx.js
export default {
  btnclick(){
    this.$element('dialogId').show()
  },
}
```


G
ge-yafang 已提交
102 103 104 105 106 107 108
![en-us_image_0000001223287672](figures/en-us_image_0000001223287672.gif)


> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**:
> - For wearables, the input type can only be button, radio, or checkbox.
> 
> - The settings of checked take effect only when the input type is set to checkbox or radio. The default value of checked is false.
Z
zengyawen 已提交
109 110


G
ge-yafang 已提交
111 112 113
## Event Binding

  Add the search and translate events to the <input> component.
Z
zengyawen 已提交
114 115 116 117 118 119 120 121 122 123 124 125

```
<!-- xxx.hml -->
<div class="content">
  <text style="margin-left: -7px;">
    <span>Enter text and then touch and hold what you've entered</span>
  </text>
  <input class="input" type="text" onsearch="search" placeholder="search"> </input>
  <input class="input" type="text" ontranslate="translate" placeholder="translate"> </input>
</div>
```

G
ge-yafang 已提交
126

Z
zengyawen 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
```
/* xxx.css */
.content {
  width: 100%;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: #F1F3F5;
}
.input {
  margin-top: 50px;
  width: 60%;
  placeholder-color: gray;
}
text{
  width:100%;
  font-size:25px;
  text-align:center;
}
```

G
ge-yafang 已提交
148

Z
zengyawen 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
```
// xxx.js
import prompt from '@system.prompt'
export default {
  search(e){
    prompt.showToast({
      message:  e.value,
      duration: 3000,
    });
  },
  translate(e){
    prompt.showToast({
      message:  e.value,
      duration: 3000,
    });
  }
}
```

G
ge-yafang 已提交
168
![en-us_image_0000001267647853](figures/en-us_image_0000001267647853.gif)
Z
zengyawen 已提交
169 170


G
ge-yafang 已提交
171 172 173 174
## Setting the Input Error Message

Add the showError method to the <input> component to display an error message in the event of incorrect input.

Z
zengyawen 已提交
175 176 177 178 179 180 181 182 183 184

```
<!-- xxx.hml -->
<div class="content">
  <input id="input" class="input" type="text"  maxlength="20" placeholder="Please input text" onchange="change">
  </input>
  <input class="button" type="button" value="Submit" onclick="buttonClick"></input>
</div>
```

G
ge-yafang 已提交
185

Z
zengyawen 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
```
/* xxx.css */
.content {
  width: 100%;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: #F1F3F5;
}
.input {
  width: 80%;
  placeholder-color: gray;
}
.button {
  width: 30%;
  margin-top: 50px;
}
```

G
ge-yafang 已提交
205

Z
zengyawen 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
```
// xxx.js
import prompt from '@system.prompt' 
 export default { 
   data:{ 
     value:'', 
   }, 
   change(e){ 
     this.value = e.value; 
     prompt.showToast({ 
     message: "value: " + this.value, 
       duration: 3000, 
      }); 
   }, 
   buttonClick(e){ 
     if(this.value.length > 6){ 
G
ge-yafang 已提交
222
       this.$element("input").showError({        error:  'Up to 6 characters are allowed.'       }); 
Z
zengyawen 已提交
223
      }else if(this.value.length == 0){ 
G
ge-yafang 已提交
224
        this.$element("input").showError({         error:this.value + 'This field cannot be left empty.'       }); 
Z
zengyawen 已提交
225 226 227 228 229 230 231 232 233
      }else{ 
        prompt.showToast({ 
          message: "success " 
        }); 
      } 
   }, 
 }
```

G
ge-yafang 已提交
234 235 236 237
![en-us_image_0000001223127708](figures/en-us_image_0000001223127708.gif)

> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**:
> - This method is available when the input type is set to text, email, date, time, number, or password.
Z
zengyawen 已提交
238 239


G
ge-yafang 已提交
240 241 242 243 244
## Example Scenario


Enter information by using the <input> component of the type that suits your needs.

Z
zengyawen 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274


```
<!-- xxx.hml -->
<div class="container">    
  <div class="label-item"> 
    <label>memorandum</label>   
  </div>    
  <div class="label-item">        
    <label class="lab" target="input1">content:</label>        
    <input class="flex" id="input1" placeholder="Enter content" />    
  </div>    
  <div class="label-item">        
    <label class="lab" target="input3">date:</label>        
    <input class="flex" id="input3" type="date" placeholder="Enter data" />    
  </div>    
  <div class="label-item">        
    <label class="lab" target="input4">time:</label>        
    <input class="flex" id="input4" type="time" placeholder="Enter time" />    
  </div>   
  <div class="label-item">        
    <label class="lab" target="checkbox1">Complete:</label>        
    <input class="flex" type="checkbox" id="checkbox1" style="width: 100px;height: 100px;" />    
  </div>    
  <div class="label-item">        
    <input class="flex" type="button" id="button" value="save" onclick="btnclick"/>    
  </div>
</div>
```

G
ge-yafang 已提交
275 276


Z
zengyawen 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
```
/* xxx.css */
.container { 
  flex-direction: column;
  background-color: #F1F3F5;
}
.label-item {   
  align-items: center;
  border-bottom-width: 1px;border-color: #dddddd;
}
.lab {    
  width: 400px;}
label {    
  padding: 30px;
  font-size: 30px;      
  width: 320px;
  font-family: serif;
  color: #9370d8;
  font-weight: bold;
}
.flex {    
  flex: 1;
}
.textareaPadding {    
  padding-left: 100px;
}
```

G
ge-yafang 已提交
305 306


Z
zengyawen 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
```
// xxx.js
import prompt from '@system.prompt';
export default {    
  data: {    
  },    
  onInit() { 
  },   
  btnclick(e) {        
    prompt.showToast({            
      message:'Saved successfully!'        
    })    
  }
}     
```


G
ge-yafang 已提交
324
![en-us_image_0000001222807760](figures/en-us_image_0000001222807760.gif)