|
1 |
| -import { getElements, scrollTo } from '../util'; |
| 1 | +import { getElements } from '../util'; |
2 | 2 |
|
3 |
| -function handleFormSubmit(event: Event, form: HTMLFormElement): void { |
| 3 | +function handleFormSubmit(): void { |
4 | 4 | // Automatically select all options in any <select> with the "select-all" class. This is useful for
|
5 | 5 | // multi-select fields that are used to add/remove choices.
|
6 | 6 | for (const element of getElements<HTMLOptionElement>('select.select-all option')) {
|
7 | 7 | element.selected = true;
|
8 | 8 | }
|
9 |
| - |
10 |
| - // Track the names of each invalid field. |
11 |
| - const invalids = new Set<string>(); |
12 |
| - |
13 |
| - for (const element of form.querySelectorAll<FormControls>('*[name]')) { |
14 |
| - if (!element.validity.valid) { |
15 |
| - invalids.add(element.name); |
16 |
| - // If the field is invalid, but doesn't contain the .is-invalid class, add it. |
17 |
| - if (!element.classList.contains('is-invalid')) { |
18 |
| - element.classList.add('is-invalid'); |
19 |
| - } |
20 |
| - } else { |
21 |
| - // If the field is valid, but contains the .is-invalid class, remove it. |
22 |
| - if (element.classList.contains('is-invalid')) { |
23 |
| - element.classList.remove('is-invalid'); |
24 |
| - } |
25 |
| - } |
26 |
| - } |
27 |
| - |
28 |
| - if (invalids.size !== 0) { |
29 |
| - // If there are invalid fields, pick the first field and scroll to it. |
30 |
| - const firstInvalid = form.elements.namedItem(Array.from(invalids)[0]) as Element; |
31 |
| - scrollTo(firstInvalid); |
32 |
| - |
33 |
| - // If the form has invalid fields, don't submit it. |
34 |
| - event.preventDefault(); |
35 |
| - } |
36 | 9 | }
|
37 | 10 |
|
38 | 11 | /**
|
39 |
| - * Attach an event listener to each form's submitter (button[type=submit]). When called, the |
40 |
| - * callback checks the validity of each form field and adds the appropriate Bootstrap CSS class |
41 |
| - * based on the field's validity. |
| 12 | + * Attach event listeners to each form's submit/reset buttons. |
42 | 13 | */
|
43 | 14 | export function initFormElements(): void {
|
44 | 15 | for (const form of getElements('form')) {
|
45 |
| - // Find each of the form's submitters. Most object edit forms have a "Create" and |
46 |
| - // a "Create & Add", so we need to add a listener to both. |
| 16 | + // Find each of the form's submit buttons. |
47 | 17 | const submitters = form.querySelectorAll<HTMLButtonElement>('button[type=submit]');
|
48 | 18 | for (const submitter of submitters) {
|
49 | 19 | // Add the event listener to each submitter.
|
50 |
| - submitter.addEventListener('click', (event: Event) => handleFormSubmit(event, form)); |
| 20 | + submitter.addEventListener('click', () => handleFormSubmit()); |
51 | 21 | }
|
52 | 22 |
|
53 | 23 | // Initialize any reset buttons so that when clicked, the page is reloaded without query parameters.
|
|
0 commit comments