|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 6 | + <title>Input</title> |
| 7 | + <link rel="stylesheet" href="../../common.css" /> |
| 8 | + <script src="../../common.js"></script> |
| 9 | + <script type="module" src="https://cdn.jsdelivr.net/npm/@ionic/core@8/dist/ionic/ionic.esm.js"></script> |
| 10 | + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@ionic/core@8/css/ionic.bundle.css" /> |
| 11 | + </head> |
| 12 | + |
| 13 | + <body> |
| 14 | + <div class="container"> |
| 15 | + <form id="my-form"> |
| 16 | + <ion-select |
| 17 | + label="Favorite fruit" |
| 18 | + placeholder="Select fruit" |
| 19 | + helper-text="Select your favorite fruit" |
| 20 | + error-text="This field is required" |
| 21 | + > |
| 22 | + <ion-select-option value="apple">Apple</ion-select-option> |
| 23 | + <ion-select-option value="banana">Banana</ion-select-option> |
| 24 | + <ion-select-option value="orange">Orange</ion-select-option> |
| 25 | + </ion-select> |
| 26 | + |
| 27 | + <br /> |
| 28 | + |
| 29 | + <ion-button type="submit" size="small">Submit</ion-button> |
| 30 | + </form> |
| 31 | + </div> |
| 32 | + |
| 33 | + <script> |
| 34 | + const form = document.getElementById('my-form'); |
| 35 | + const favFruit = form.querySelector('ion-select'); |
| 36 | + |
| 37 | + form.addEventListener('submit', (event) => submit(event)); |
| 38 | + favFruit.addEventListener('ionChange', (event) => validateSelect(event)); |
| 39 | + favFruit.addEventListener('ionBlur', () => onIonBlur({ detail: { value: favFruit.value } })); |
| 40 | + |
| 41 | + const validateSelect = (event) => { |
| 42 | + if (!event.detail.value) { |
| 43 | + favFruit.classList.add('ion-invalid'); |
| 44 | + favFruit.classList.remove('ion-valid'); |
| 45 | + } else { |
| 46 | + favFruit.classList.remove('ion-invalid'); |
| 47 | + favFruit.classList.add('ion-valid'); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + const markTouched = () => { |
| 52 | + favFruit.classList.add('ion-touched'); |
| 53 | + }; |
| 54 | + |
| 55 | + const onIonBlur = (event) => { |
| 56 | + markTouched(); |
| 57 | + validateSelect(event); |
| 58 | + }; |
| 59 | + |
| 60 | + const submit = (event) => { |
| 61 | + event.preventDefault(); |
| 62 | + |
| 63 | + markTouched(); |
| 64 | + validateSelect({ detail: { value: favFruit.value } }); |
| 65 | + }; |
| 66 | + </script> |
| 67 | + </body> |
| 68 | +</html> |
0 commit comments