|
| 1 | +# Auto submit |
| 2 | + |
| 3 | +## Description |
| 4 | +Auto submit will submit what the user fills out on a form without the need for the user to press the submit button. It can be used to incrementally send form input data to a server. |
| 5 | + |
| 6 | +For instance to create a live search pattern where the search results update on every keypress. |
| 7 | + |
| 8 | +## Documentation |
| 9 | +The autosubmit pattern submits a form if a form element changes. It is |
| 10 | +enabled by class "pat-autosubmit" for individual form elements, on |
| 11 | +grouping elements for all elements in the group, and on forms for all |
| 12 | +elements of the form. |
| 13 | + |
| 14 | +Only one element: |
| 15 | + |
| 16 | + <form> |
| 17 | + <input class="pat-autosubmit" type="text" name="q" placeholder="Search query"/> |
| 18 | + </form> |
| 19 | + |
| 20 | +On a fieldset: |
| 21 | + |
| 22 | + <form> |
| 23 | + <fieldset class="pat-autosubmit"> |
| 24 | + <input type="text" name="q" placeholder="Search query"/> |
| 25 | + <label><input type="checkbox" name="local"/> Only search in this section</label> |
| 26 | + </fieldset> |
| 27 | + </form> |
| 28 | + |
| 29 | +And on the form: |
| 30 | + |
| 31 | + <form class="pat-autosubmit"> |
| 32 | + <fieldset> |
| 33 | + <input type="text" name="q" placeholder="Search query"/> |
| 34 | + <label><input type="checkbox" name="local"/> Only search in this section</label> |
| 35 | + </fieldset> |
| 36 | + </form> |
| 37 | + |
| 38 | +### What constitutes a change |
| 39 | + |
| 40 | +While for a checkbox a change is clearly defined as uncheck/check, for |
| 41 | +input fields there are more options. |
| 42 | + |
| 43 | +Without configuration, a change will be detected 400ms after the last |
| 44 | +keypress in an input field. Naturally, if two keypresses are more than |
| 45 | +400ms apart, two submits will be triggered. |
| 46 | + |
| 47 | +You can configure this behaviour with the delay option. |
| 48 | + |
| 49 | +no delay: |
| 50 | + |
| 51 | + <form> |
| 52 | + <input name="q" class="pat-autosubmit" data-pat-autosubmit="delay: 0ms"/> |
| 53 | + </form> |
| 54 | + |
| 55 | +longer delay: |
| 56 | + |
| 57 | + <form> |
| 58 | + <input name="q" class="pat-autosubmit" data-pat-autosubmit="delay: 1000ms"/> |
| 59 | + </form> |
| 60 | + |
| 61 | +delay until element loses focus: |
| 62 | + |
| 63 | + <form> |
| 64 | + <input name="q" class="pat-autosubmit" data-pat-autosubmit="delay: defocus"/> |
| 65 | + </form> |
| 66 | + |
| 67 | +### Combining with injection |
| 68 | + |
| 69 | +Autosubmit is most useful when combined with injection. This makes it |
| 70 | +trivial to create a form that automatically loads content and displays |
| 71 | +it on the page. Here is a minimal search page: |
| 72 | + |
| 73 | + <form class="pat-inject pat-autosubmit" action="/search" data-pat-inject="target: #results"> |
| 74 | + <input type="text" name="q" placeholder="Search query"/> |
| 75 | + <label><input type="checkbox" name="local"/> Only search in this section</label> |
| 76 | + </form> |
| 77 | + |
| 78 | + <section id="results"> |
| 79 | + ... present search results here |
| 80 | + </section> |
| 81 | + |
| 82 | +As soon as the user starts entering text in the search field or changes |
| 83 | +the local-search toggle search requests will send to the server and the |
| 84 | +results will be inserted into the existing page by replacing the content |
| 85 | +of the *results* section. |
0 commit comments