|
| 1 | +class FormSupport { |
| 2 | + /** |
| 3 | + * |
| 4 | + * @param element - the WebComponent that needs form support |
| 5 | + * @param nativeInputUpdateCallback - determines how the native input's disabled and value properties are calculated |
| 6 | + */ |
| 7 | + static syncNativeHiddenInput(element, nativeInputUpdateCallback) { |
| 8 | + const needsNativeInput = !!element.name; |
| 9 | + let nativeInput = element.querySelector("input[type=hidden][data-ui5-webcomponents-form-support]"); |
| 10 | + if (needsNativeInput && !nativeInput) { |
| 11 | + nativeInput = document.createElement("input"); |
| 12 | + nativeInput.type = "hidden"; |
| 13 | + nativeInput.setAttribute("data-ui5-webcomponents-form-support", ""); |
| 14 | + nativeInput.slot = "formSupport"; // Needed for IE - otherwise input elements are not part of the real DOM tree and are not detected by forms |
| 15 | + element.appendChild(nativeInput); |
| 16 | + } |
| 17 | + if (!needsNativeInput && nativeInput) { |
| 18 | + element.removeChild(nativeInput); |
| 19 | + } |
| 20 | + |
| 21 | + if (needsNativeInput) { |
| 22 | + nativeInput.name = element.name; |
| 23 | + (nativeInputUpdateCallback || copyDefaultProperties)(element, nativeInput); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + static triggerFormSubmit(element) { |
| 28 | + if (!element.submits) { |
| 29 | + return; |
| 30 | + } |
| 31 | + let parentElement; |
| 32 | + do { |
| 33 | + parentElement = element.parentElement; |
| 34 | + } while (parentElement && parentElement.tagName.toLowerCase() !== "form"); |
| 35 | + if (parentElement) { |
| 36 | + parentElement.submit(); |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +const copyDefaultProperties = (element, nativeInput) => { |
| 43 | + nativeInput.disabled = element.disabled; |
| 44 | + nativeInput.value = element.value; |
| 45 | +}; |
| 46 | + |
| 47 | +export default FormSupport; |
0 commit comments