• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# \<form> Development
2
3The **\<form>** component allows the content in [\<Input>](../reference/arkui-js/js-components-basic-input.md) components to be submitted and reset. For details, see [form](../reference/arkui-js/js-components-container-form.md).
4
5
6> **NOTE**
7>
8> The APIs of this module are supported since API version 6.
9
10
11## Creating a \<form> Component
12
13Create a **\<form>** component in the .hml file under **pages/index**.
14```html
15<!-- xxx.hml -->
16<div class="container">
17  <form style="width: 100%; height: 20%">
18    <input type="text" style="width:80%"></input>
19  </form>
20</div>
21```
22
23```css
24/* xxx.css */
25.container {
26  width:100%;
27  height:100%;
28  flex-direction: column;
29  justify-content: center;
30  align-items: center;
31  background-color: #F1F3F5;
32}
33```
34
35![en-us_image_0000001267887873](figures/en-us_image_0000001267887873.png)
36
37
38## Zooming In or Out on a Form
39
40To implement the zoom effect after a form is clicked, add the **click-effect** attribute to the **\<form>** component. For values of **click-effect**, see [Universal Attributes](../reference/arkui-js/js-components-common-attributes.md).
41```html
42<!-- xxx.hml -->
43<div class="container">
44  <form  id="formId" class="formClass" click-effect="spring-large">
45    <input type="text"></input>
46  </form>
47</div>
48```
49
50
51## Setting the Form Style
52
53
54Add the **background-color** and **border** attributes.
55
56
57```css
58/* xxx.css */
59.container {
60  width: 100%;
61  height: 100%;
62  flex-direction: column;
63  align-items: center;
64  justify-content: center;
65  background-color: #F1F3F5;
66}
67.formClass{
68  width: 80%;
69  height: 100px;
70  padding: 10px;
71  border: 1px solid #cccccc;
72}
73```
74
75
76![en-us_image_0000001267607913](figures/en-us_image_0000001267607913.gif)
77
78
79## Adding Response Events
80
81To submit or reset a form, add the **submit** and **reset** events.
82
83```html
84<!-- xxx.hml -->
85<div class="container">
86  <form onsubmit='onSubmit' onreset='onReset' class="form">
87    <div style="width: 100%;justify-content: center;">
88      <label>Option 1</label>
89      <input type='radio' name='radioGroup' value='radio1'></input>
90      <label>Option 2</label>
91      <input type='radio' name='radioGroup' value='radio2'></input>
92    </div>
93    <div style="width: 100%;justify-content: center; margin-top: 20px">
94      <input type="submit" value="Submit" style="width:120px; margin-right:20px;" >
95      </input>
96      <input type="reset" value="Reset" style="width:120px;"></input>
97    </div>
98  </form>
99</div>
100```
101
102```css
103/* index.css */
104.container{
105  width: 100%;
106  height: 100%;
107  flex-direction: column;
108  justify-items: center;
109  align-items: center;
110  background-color: #F1F3F5;
111}
112.form{
113  width: 100%;
114  height: 30%;
115  margin-top: 40%;
116  flex-direction: column;
117  justify-items: center;
118  align-items: center;
119}
120```
121
122```js
123// xxx.js
124import promptAction from '@ohos.promptAction';
125export default{
126  onSubmit(result) {
127    promptAction.showToast({
128      message: result.value.radioGroup
129    })
130  },
131  onReset() {
132    promptAction.showToast({
133      message: 'Reset All'
134    })
135  }
136}
137```
138
139
140![en-us_image_0000001267767885](figures/en-us_image_0000001267767885.gif)
141
142
143## Example Scenario
144
145Select an option and submit or reset the form data.
146
147Create two [\<Input>](../reference/arkui-js/js-components-basic-input.md) components, set their **type** attribute to **checkbox** and **radio**, and use the **onsubmit** and **onreset** events of the **\<form>** component to submit and reset the form data, respectively.
148
149```html
150<!-- xxx.hml -->
151<div class="container">
152   <form onsubmit="formSubmit" onreset="formReset">
153 <text style="font-size: 30px; margin-bottom: 20px; margin-top: 100px;">
154      <span > Form </span>
155  </text>
156    <div style="flex-direction: column;width: 90%;padding: 30px 0px;">
157     <text class="txt">Select 1 or more options</text>
158      <div style="width: 90%;height: 150px;align-items: center;justify-content: space-around;">
159        <label target="checkbox1">Option 1</label>
160        <input id="checkbox1" type="checkbox" name="checkbox1"></input>
161        <label target="checkbox2">Option 2</label>
162        <input id="checkbox2" type="checkbox" name="checkbox2"></input>
163       </div>
164       <divider style="margin: 20px 0px;color: pink;height: 5px;"></divider>
165       <text class="txt">Select 1 option</text>
166       <div style="width: 90%;height: 150px;align-items: center;justify-content: space-around;">
167         <label target="radio1">Option 1</label>
168         <input id="radio1" type="radio" name="myradio"></input>
169         <label target="radio2">Option 2</label>
170         <input id="radio2" type="radio" name="myradio"></input>
171       </div>
172       <divider style="margin: 20px 0px;color: pink;height: 5px;"></divider>
173       <text class="txt">Text box</text>
174       <input type="text" placeholder="Enter content." style="margin-top: 50px;"></input>
175       <div style="width: 90%;align-items: center;justify-content: space-between;margin: 40px;">
176         <input type="submit">Submit</input>
177         <input type="reset">Reset</input>
178       </div>
179    </div>
180  </form>
181</div>
182```
183
184```css
185/* index.css */
186.container {
187  width: 100%;
188  height: 100%;
189  flex-direction:column;
190  align-items:center;
191  background-color:#F1F3F5;
192}
193.txt {
194  font-size:33px;
195  font-weight:bold;
196  color:darkgray;
197}
198label{
199  font-size: 20px;
200}
201```
202
203```js
204// xxx.js
205import promptAction from '@ohos.promptAction';
206export default {
207  formSubmit() {
208    promptAction.showToast({
209      message: 'Submitted.'
210    })
211  },
212  formReset() {
213    promptAction.showToast({
214      message: 'Reset.'
215    })
216  }
217}
218```
219
220![en-us_image_0000001222967788](figures/en-us_image_0000001222967788.gif)
221