Skip to content

Commit bc1345f

Browse files
feat(remove hidden input): work to remove hidden input
1 parent ab58266 commit bc1345f

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

Diff for: src/element-internals.js

+24-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import {
44
internalsMap,
55
hiddenInputMap,
66
validationMessageMap,
7-
shadowHostsMap
7+
shadowHostsMap,
8+
formElementsMap,
9+
refValueMap
810
} from './maps.js';
911
import { getHostRoot, initRef, initLabels, initForm, findParentForm } from './utils.js';
1012
import { ValidityState, reconcileValidty, setValid } from './ValidityState.js';
@@ -63,8 +65,10 @@ class ElementInternals {
6365
if (!this.form) {
6466
return undefined;
6567
}
66-
const hiddenInput = hiddenInputMap.get(this);
67-
hiddenInput.value = value;
68+
// const hiddenInput = hiddenInputMap.get(this);
69+
const ref = refMap.get(this);
70+
refValueMap.set(ref, value);
71+
// hiddenInput.value = value;
6872
}
6973

7074
setValidity(validityChanges, validationMessage) {
@@ -134,4 +138,21 @@ if (!window.ElementInternals) {
134138

135139
const documentObserver = new MutationObserver(observerCallback);
136140
documentObserver.observe(document.body, observerConfig);
141+
142+
class FormData extends window.FormData {
143+
constructor(form) {
144+
super(form);
145+
if (form && formElementsMap.has(form)) {
146+
const refs = formElementsMap.get(form);
147+
refs.forEach(ref => {
148+
if (ref.getAttribute('name')) {
149+
const value = refValueMap.get(ref);
150+
this.set(ref.getAttribute('name'), value);
151+
}
152+
});
153+
}
154+
}
155+
}
156+
157+
window.FormData = FormData;
137158
}

Diff for: src/maps.js

+4
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ export const validationMessageMap = new WeakMap();
66
export const formsMap = new WeakMap();
77
export const observedShadowRootsMap = new WeakMap();
88
export const shadowHostsMap = new WeakMap();
9+
/** @type WeakMap<HTMLFormElement, Set<HTMLElement>> */
10+
export const formElementsMap = new WeakMap();
11+
/** @type WeakMap<HTMLElement, any> */
12+
export const refValueMap = new WeakMap();

Diff for: src/mutation-observers.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { internalsMap, shadowHostsMap } from './maps.js';
2+
import { initForm } from './utils.js';
23

34
export function observerCallback(mutationList) {
45
mutationList.forEach(mutationRecord => {
@@ -8,9 +9,7 @@ export function observerCallback(mutationList) {
89
const internals = internalsMap.get(node);
910
const { form } = internals;
1011

11-
if (form && node.formAssociatedCallback) {
12-
node.formAssociatedCallback.apply(node, [form]);
13-
}
12+
initForm(node, form, internals);
1413
}
1514
});
1615

Diff for: src/utils.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { hiddenInputMap, formsMap } from './maps.js';
1+
import { hiddenInputMap, formsMap, formElementsMap } from './maps.js';
22

33
const observerConfig = { attributes: true };
44

@@ -46,6 +46,7 @@ export const initLabels = (ref, labels) => {
4646

4747
export const initForm = (ref, form, internals) => {
4848
if (form) {
49+
// TODO: check all form elements instead of just this
4950
form.addEventListener('submit', event => {
5051
if (internals.checkValidity() === false) {
5152
event.stopImmediatePropagation();
@@ -60,8 +61,16 @@ export const initForm = (ref, form, internals) => {
6061
}
6162
});
6263

64+
const formElements = formElementsMap.get(form);
6365
formsMap.set(form, { ref, internals });
6466

67+
if (formElements) {
68+
formElements.add(ref);
69+
} else {
70+
const initSet = new Set();
71+
initSet.add(ref);
72+
formElementsMap.set(form, initSet);
73+
}
6574
if (ref.formAssociatedCallback) {
6675
ref.formAssociatedCallback.apply(ref, [form]);
6776
}

0 commit comments

Comments
 (0)