|
14 | 14 | <script type="module" src="../../../../../dist/ionic/ionic.esm.js"></script> |
15 | 15 |
|
16 | 16 | <style> |
| 17 | + .grid { |
| 18 | + display: grid; |
| 19 | + grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); |
| 20 | + gap: 6px; |
| 21 | + } |
| 22 | + |
17 | 23 | .required-label { |
18 | 24 | display: none; |
19 | 25 | } |
20 | 26 |
|
21 | 27 | .textarea-required .required-label { |
22 | 28 | display: inline; |
23 | | - color: red; |
| 29 | + color: #b80000; |
| 30 | + } |
| 31 | + |
| 32 | + ion-textarea { |
| 33 | + margin-bottom: 16px; |
| 34 | + } |
| 35 | + |
| 36 | + ion-content button.reset-button { |
| 37 | + background: #960101; |
| 38 | + border-color: #650000; |
24 | 39 | } |
25 | 40 | </style> |
26 | 41 | </head> |
|
37 | 52 | <form onsubmit="return onSubmit(event)"> |
38 | 53 | <ion-textarea |
39 | 54 | fill="outline" |
| 55 | + label="Textarea 1" |
40 | 56 | label-placement="stacked" |
41 | | - placeholder="Placeholder" |
| 57 | + placeholder="Textarea 1" |
42 | 58 | helper-text="Helper message" |
43 | 59 | counter="true" |
44 | 60 | maxlength="999" |
| 61 | + name="textarea1" |
45 | 62 | > |
46 | | - <div slot="label">Label <span class="required-label">(Required) *</span></div> |
47 | 63 | <ion-icon slot="end" name="square-outline"></ion-icon> |
48 | 64 | </ion-textarea> |
49 | 65 |
|
| 66 | + <ion-textarea |
| 67 | + fill="outline" |
| 68 | + label-placement="stacked" |
| 69 | + placeholder="Textarea 2" |
| 70 | + helper-text="Helper message" |
| 71 | + counter="true" |
| 72 | + maxlength="999" |
| 73 | + name="textarea2" |
| 74 | + > |
| 75 | + <div slot="label">Textarea 2 <span class="required-label">(Required) *</span></div> |
| 76 | + <ion-icon slot="end" name="square-outline"></ion-icon> |
| 77 | + </ion-textarea> |
| 78 | + |
| 79 | + <div class="grid"> |
| 80 | + <button type="button" onClick="toggleRequired()">Toggle Required (Textarea 2)</button> |
| 81 | + </div> |
| 82 | + |
50 | 83 | <p> |
51 | | - Click the first button below to toggle the required prop and then click the submit button to attempt to |
52 | | - submit the form. It should show a popup warning to fill out the field when textarea is required and empty. |
53 | | - When the textarea is not required, or the field is filled, the form should submit by sending a console log. |
| 84 | + Click the button above to toggle the required prop on the |
| 85 | + 2nd textarea and then click the submit button to attempt to submit |
| 86 | + the form. It should show a popup warning on the 2nd textarea to |
| 87 | + fill out the field when the textarea is required and empty. When |
| 88 | + the textarea is not required, or the field is filled, the form |
| 89 | + should submit by sending a console log. Click the reset button to |
| 90 | + reset the form and clear the textareas. |
54 | 91 | </p> |
55 | | - <button type="button" onClick="toggleRequired()">Toggle Required</button> |
56 | | - <button type="submit">Submit</button> |
| 92 | + |
| 93 | + <div class="grid"> |
| 94 | + <button type="submit">Submit</button> |
| 95 | + <button type="reset" class="reset-button">Reset</button> |
| 96 | + </div> |
57 | 97 | </form> |
58 | 98 | </ion-content> |
59 | 99 | </ion-app> |
60 | 100 |
|
61 | 101 | <script> |
| 102 | + const form = document.querySelector('form'); |
| 103 | + |
62 | 104 | function onSubmit(event) { |
63 | 105 | event.preventDefault(); |
64 | | - const textarea = document.querySelector('ion-textarea'); |
65 | | - console.log('Form submitted with value:', textarea.value); |
| 106 | + console.log('Form submitted with values:', getFormData(form)); |
66 | 107 | return true; |
67 | 108 | } |
68 | 109 |
|
69 | 110 | function toggleRequired() { |
70 | | - const textarea = document.querySelector('ion-textarea'); |
| 111 | + const textarea = document.querySelector('[name="textarea2"]'); |
71 | 112 | textarea.required = textarea.required ? false : true; |
72 | 113 | textarea.classList.toggle('textarea-required', textarea.required); |
73 | 114 | console.log('Textarea required set to:', textarea.required); |
74 | 115 | } |
| 116 | + |
| 117 | + // Helper function to get the form data as a plain object |
| 118 | + function getFormData(form) { |
| 119 | + const formData = new FormData(form); |
| 120 | + // Convert FormData to a plain object for easier inspection |
| 121 | + const formDataEntries = {}; |
| 122 | + for (const [key, value] of formData.entries()) { |
| 123 | + formDataEntries[key] = value; |
| 124 | + } |
| 125 | + return formDataEntries; |
| 126 | + } |
75 | 127 | </script> |
76 | 128 | </body> |
77 | 129 | </html> |
0 commit comments