OLD | NEW |
(Empty) | |
| 1 <!doctype html> |
| 2 <!-- |
| 3 @license |
| 4 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. |
| 5 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
| 6 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 7 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
| 8 Code distributed by Google as part of the polymer project is also |
| 9 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
| 10 --> |
| 11 <html> |
| 12 <head> |
| 13 <title>iron-form demo</title> |
| 14 |
| 15 <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-
scale=1, user-scalable=yes"> |
| 16 <meta name="mobile-web-app-capable" content="yes"> |
| 17 <meta name="apple-mobile-web-app-capable" content="yes"> |
| 18 |
| 19 <script src="../../webcomponentsjs/webcomponents-lite.js"></script> |
| 20 <link rel="import" href="../../iron-demo-helpers/demo-snippet.html"> |
| 21 <link rel="import" href="../../iron-demo-helpers/demo-pages-shared-styles.html
"> |
| 22 <link rel="import" href="../../paper-button/paper-button.html"> |
| 23 <link rel="import" href="../../paper-checkbox/paper-checkbox.html"> |
| 24 <link rel="import" href="../../paper-dropdown-menu/paper-dropdown-menu.html"> |
| 25 <link rel="import" href="../../paper-input/paper-input.html"> |
| 26 <link rel="import" href="../../paper-item/paper-item.html"> |
| 27 <link rel="import" href="../../paper-menu/paper-menu.html"> |
| 28 <link rel="import" href="../../paper-spinner/paper-spinner.html"> |
| 29 <link rel="import" href="../../paper-styles/color.html"> |
| 30 <link rel="import" href="../../paper-styles/typography.html"> |
| 31 <link rel="import" href="../iron-form.html"> |
| 32 <link rel="import" href="simple-element.html"> |
| 33 |
| 34 <style is="custom-style" include="demo-pages-shared-styles"> |
| 35 .output { |
| 36 margin-top: 20px; |
| 37 word-wrap: break-word; |
| 38 @apply(--paper-font-common-code); |
| 39 } |
| 40 form > paper-button { |
| 41 width: 45%; |
| 42 margin-top: 20px; |
| 43 } |
| 44 form > paper-button:not([disabled]) { |
| 45 background: var(--paper-light-blue-500); |
| 46 color: white; |
| 47 } |
| 48 button { |
| 49 margin-top: 20px; |
| 50 } |
| 51 paper-spinner { |
| 52 width: 14px; |
| 53 height: 14px; |
| 54 margin-right: 20px; |
| 55 } |
| 56 paper-dropdown-menu { |
| 57 display: block; |
| 58 } |
| 59 demo-snippet { |
| 60 --demo-snippet-code: { |
| 61 max-height: 300px; |
| 62 }; |
| 63 } |
| 64 </style> |
| 65 </head> |
| 66 <body unresolved> |
| 67 |
| 68 <div class="vertical-section-container centered"> |
| 69 <h3>An <code>iron-form</code> can submit both custom and native elements.</h
3> |
| 70 <demo-snippet> |
| 71 <template> |
| 72 <form is="iron-form" method="get" action="/" id="basic"> |
| 73 <paper-input name="name" label="Name" required></paper-input> |
| 74 <br> |
| 75 <input type="checkbox" name="food" value="donuts" checked> I like donu
ts<br> |
| 76 <input type="checkbox" name="food" value="pizza" required> I like pizz
a<br> |
| 77 <paper-checkbox name="food" value="cheese" required>I like cheese</pap
er-checkbox><br> |
| 78 <paper-dropdown-menu label="Cars" name="cars" required> |
| 79 <paper-menu class="dropdown-content"> |
| 80 <paper-item value="volvo">Volvo</paper-item> |
| 81 <paper-item value="saab">Saab</paper-item> |
| 82 <paper-item value="fiat">Fiat</paper-item> |
| 83 <paper-item value="audi">Audi</paper-item> |
| 84 </paper-menu> |
| 85 </paper-dropdown-menu> |
| 86 <paper-button raised onclick="_submit(event)">Submit</paper-button> |
| 87 <paper-button raised onclick="_reset(event)">Reset</paper-button> |
| 88 <div class="output"></div> |
| 89 </form> |
| 90 <script> |
| 91 function _submit(event) { |
| 92 Polymer.dom(event).localTarget.parentElement.submit(); |
| 93 } |
| 94 function _reset(event) { |
| 95 var form = Polymer.dom(event).localTarget.parentElement |
| 96 form.reset(); |
| 97 form.querySelector('.output').innerHTML = ''; |
| 98 } |
| 99 basic.addEventListener('iron-form-submit', function(event) { |
| 100 this.querySelector('.output').innerHTML = JSON.stringify(event.detai
l); |
| 101 }); |
| 102 </script> |
| 103 </template> |
| 104 </demo-snippet> |
| 105 |
| 106 <h3>You can submit a form in many different ways: by manually calling submit
(), |
| 107 using a native button, or by wrapping a paper-button in a native button:</h3
> |
| 108 <demo-snippet> |
| 109 <template> |
| 110 <style> |
| 111 button.no-style { |
| 112 -webkit-appearance: none; |
| 113 border: 0; |
| 114 padding: 0; |
| 115 background: transparent; |
| 116 } |
| 117 </style> |
| 118 <p>You can also submit this form by pressing "Enter" in the input</p> |
| 119 <form is="iron-form" method="get" action="/" id="submitOptions"> |
| 120 <paper-input name="name" label="Name" value="Batman" required></paper-
input> |
| 121 <button type="submit">Submit</button> |
| 122 <button type="submit" class="no-style"><paper-button raised>Submit</pa
per-button></button> |
| 123 <br> |
| 124 <paper-button raised onclick="_submit(event)">Submit</paper-button> |
| 125 <paper-button raised onclick="_reset(event)">Reset</paper-button> |
| 126 |
| 127 <div class="output"></div> |
| 128 </form> |
| 129 <script> |
| 130 function _submit(event) { |
| 131 Polymer.dom(event).localTarget.parentElement.submit(); |
| 132 } |
| 133 function _reset(event) { |
| 134 var form = Polymer.dom(event).localTarget.parentElement |
| 135 form.reset(); |
| 136 form.querySelector('.output').innerHTML = ''; |
| 137 } |
| 138 submitOptions.addEventListener('iron-form-submit', function(event) { |
| 139 this.querySelector('.output').innerHTML = JSON.stringify(event.detai
l); |
| 140 }); |
| 141 </script> |
| 142 </template> |
| 143 </demo-snippet> |
| 144 |
| 145 <h3>To customize the request sent to the server, you can listen to the `iron
-form-presubmit` event</h3> |
| 146 <demo-snippet> |
| 147 <template> |
| 148 <form is="iron-form" method="get" action="/" id="presubmit"> |
| 149 <paper-input name="name" label="Name" value="Batman" required></paper-
input> |
| 150 <paper-button raised onclick="_submit(event)">Submit</paper-button> |
| 151 <paper-button raised onclick="_reset(event)">Reset</paper-button> |
| 152 <div class="output"></div> |
| 153 </form> |
| 154 <script> |
| 155 function _submit(event) { |
| 156 Polymer.dom(event).localTarget.parentElement.submit(); |
| 157 } |
| 158 function _reset(event) { |
| 159 var form = Polymer.dom(event).localTarget.parentElement |
| 160 form.reset(); |
| 161 form.querySelector('.output').innerHTML = ''; |
| 162 } |
| 163 presubmit.addEventListener('iron-form-presubmit', function(event) { |
| 164 this.request.params['sidekick'] = 'Robin'; |
| 165 }); |
| 166 presubmit.addEventListener('iron-form-submit', function(event) { |
| 167 this.querySelector('.output').innerHTML = JSON.stringify(event.detai
l); |
| 168 }); |
| 169 </script> |
| 170 </template> |
| 171 </demo-snippet> |
| 172 |
| 173 <h3>Example of an <code>iron-form</code> reacting to state changes.</h3> |
| 174 <p>This form will have the "Submit" button disabled until all fields |
| 175 are valid, and then show a spinner during submission.</p> |
| 176 |
| 177 <demo-snippet> |
| 178 <template> |
| 179 |
| 180 <form is="iron-form" method="get" action="/" id="eventsDemo"> |
| 181 <paper-input name="name" label="Name" required auto-validate></paper-i
nput> |
| 182 <paper-input name="password" label="Password" type="password" required
auto-validate></paper-input> |
| 183 <paper-checkbox name="read" required>You must check this box</paper-ch
eckbox><br> |
| 184 <paper-button raised onclick="_delayedSubmit(event)" disabled id="even
tsDemoSubmit"> |
| 185 <paper-spinner id="spinner" hidden></paper-spinner>Submit</paper-but
ton> |
| 186 <paper-button raised onclick="_reset(event)">Reset</paper-button> |
| 187 <div class="output"></div> |
| 188 </form> |
| 189 <script> |
| 190 eventsDemo.addEventListener('change', function(event) { |
| 191 // Validate the entire form to see if we should enable the `Submit`
button. |
| 192 eventsDemoSubmit.disabled = !eventsDemo.validate(); |
| 193 }); |
| 194 function _delayedSubmit(event) { |
| 195 spinner.active = true; |
| 196 spinner.hidden = false; |
| 197 eventsDemoSubmit.disabled = true; |
| 198 // Simulate a slow server response. |
| 199 setTimeout(function() { |
| 200 Polymer.dom(event).localTarget.parentElement.submit(); |
| 201 }, 1000); |
| 202 } |
| 203 function _reset(event) { |
| 204 var form = Polymer.dom(event).localTarget.parentElement |
| 205 form.reset(); |
| 206 form.querySelector('.output').innerHTML = ''; |
| 207 } |
| 208 document.getElementById('eventsDemo').addEventListener('iron-form-subm
it', function(event) { |
| 209 spinner.active = false; |
| 210 spinner.hidden = true; |
| 211 eventsDemoSubmit.disabled = false; |
| 212 this.querySelector('.output').innerHTML = JSON.stringify(event.detai
l); |
| 213 }); |
| 214 </script> |
| 215 </template> |
| 216 </demo-snippet> |
| 217 |
| 218 <h3>Example of using a native <code>form</code> to do a page redirect.</h3> |
| 219 |
| 220 <p>Since an <code>iron-form</code> submits its elements with an XHR, |
| 221 it cannot do a page redirect after submission. If you need one, you |
| 222 can use a native <code>form</code> element and submit all the custom |
| 223 elements using <code>hidden</code> inputs.</p> |
| 224 <demo-snippet> |
| 225 <template> |
| 226 <!-- This form is only used to display the elements --> |
| 227 <form is="iron-form" id="redirectDemo"> |
| 228 <paper-input name="name" label="Name" required></paper-input> |
| 229 <paper-input name="password" label="Password" type="password" required
></paper-input> |
| 230 <paper-checkbox name="read" required>You must check this box</paper-ch
eckbox><br> |
| 231 <paper-button raised onclick="_nativeSubmit(event)">Submit</paper-butt
on> |
| 232 </form> |
| 233 |
| 234 <!-- This form is used to submit the elements. This is where you would |
| 235 set your method, action, etc. --> |
| 236 <form method="get" action="redirect" id="nativeForm"> |
| 237 </form> |
| 238 <script> |
| 239 function _nativeSubmit(event) { |
| 240 if (redirectDemo.validate()) { |
| 241 // For each element the iron-form wants to submit, create a hidden |
| 242 // input in the submission form. |
| 243 var serializedItems = redirectDemo.serialize(); |
| 244 for (var name in serializedItems) { |
| 245 var input = document.createElement('input'); |
| 246 input.hidden = true; |
| 247 input.name = name; |
| 248 input.value = serializedItems[name]; |
| 249 nativeForm.appendChild(input); |
| 250 } |
| 251 nativeForm.submit(); |
| 252 } |
| 253 } |
| 254 </script> |
| 255 </template> |
| 256 </demo-snippet> |
| 257 |
| 258 </div> |
| 259 </body> |
| 260 |
| 261 </html> |
OLD | NEW |