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