Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #40. #82

Merged
merged 1 commit into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,3 @@ export const validationAnchorMap = new WeakMap<IElementInternals, HTMLElement>()

/** Map DocumentFragments to their MutationObservers so we can disconnect once elements are removed */
export const documentFragmentMap = new WeakMap<DocumentFragment, MutationObserver>();

/** Save references to the form onsubmit if present */
export const onSubmitMap = new WeakMap<HTMLFormElement, Function>();
78 changes: 37 additions & 41 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hiddenInputMap, formsMap, formElementsMap, internalsMap, onSubmitMap } from './maps.js';
import { hiddenInputMap, formsMap, formElementsMap, internalsMap } from './maps.js';
import { ICustomElement, IElementInternals, LabelsList } from './types.js';

const observerConfig: MutationObserverInit = { attributes: true, attributeFilter: ['disabled'] };
Expand Down Expand Up @@ -131,46 +131,49 @@ export const formInputCallback = (event: Event) => {
/**
* The global form submit callback. We need to cancel any submission
* if a nested internals is invalid.
* @param {Event} - The form submit event
* @param {HTMLFormElement} - The form element
* @return {void}
*/
export const formSubmitCallback = (event: Event) => {
/** Get the Set of elements attached to this form */
const form = event.target as HTMLFormElement;
const elements = formElementsMap.get(form);
export const wireSubmitLogic = (form: HTMLFormElement) => {
const SUBMIT_BUTTON_SELECTOR = ':is(:is(button, input)[type=submit], button:not([type])):not([disabled])';
let submitButtonSelector = `${SUBMIT_BUTTON_SELECTOR}:not([form])`;

/**
* If this form does not validate then we're done
*/
if (form.noValidate) {
return;
if (form.id) {
submitButtonSelector += `,${SUBMIT_BUTTON_SELECTOR}[form='${form.id}']`;
}

/** If the Set has items, continue */
if (elements.size) {
const nodes = Array.from(elements);
/** Check the internals.checkValidity() of all nodes */
const validityList = nodes
.reverse()
.map(node => {
const internals = internalsMap.get(node);
return internals.reportValidity();
});
form.addEventListener('click', event => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nice and it does work for most use cases. But forms can be submitted with other events (like typing "Enter" in a field). So, we need to also keep in mind those use cases. Do you think opening an issue about this is worth it?

Copy link
Contributor Author

@sipris sipris Sep 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx, for the annotation. However, according to the spec, hitting enter in a field causes "the user agent to fire a click event at that default button."

EDIT:
If it is wanted to force this behavior on custom elements, then a new issue is certainly worth it.

There is an ongoing discussion regarding faCE and implicit submission WICG Issue 815. I would wait for a decision.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While pressing enter in an input field, this doesn't necessarily hold true for FACE elements, FWIW. I think this is fine as is. If you do happen upon any use cases, open an issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. This does works nicely!

const target = event.target as Element;
if (target.closest(submitButtonSelector)) {
// validate
const elements = formElementsMap.get(form);

/**
* If this form does not validate then we're done
*/
if (form.noValidate) {
return;
}

/** If the Set has items, continue */
if (elements.size) {
const nodes = Array.from(elements);
/** Check the internals.checkValidity() of all nodes */
const validityList = nodes
.reverse()
.map(node => {
const internals = internalsMap.get(node);
return internals.reportValidity();
});

/** If any node is false, stop the event */
if (validityList.includes(false)) {
event.stopImmediatePropagation();
event.stopPropagation();
event.preventDefault();
} else if (onSubmitMap.get(form)) {
const callback = onSubmitMap.get(form);
const canceled = callback.call(form, event);
if (canceled === false) {
event.preventDefault();
/** If any node is false, stop the event */
if (validityList.includes(false)) {
event.preventDefault();
}
}
}
}
};
});
}

/**
* The global form reset callback. This will loop over added
Expand Down Expand Up @@ -202,13 +205,6 @@ export const formResetCallback = (event: Event) => {
*/
export const initForm = (ref: ICustomElement, form: HTMLFormElement, internals: IElementInternals) => {
if (form) {
/** If the form has an onsubmit function, save it and remove it */
if (form.onsubmit) {
/** TODO: Find a way to parse arguments better */
onSubmitMap.set(form, form.onsubmit.bind(form));
form.onsubmit = null;
}

/** This will be a WeakMap<HTMLFormElement, Set<HTMLElement> */
const formElements = formElementsMap.get(form);

Expand All @@ -222,7 +218,7 @@ export const initForm = (ref: ICustomElement, form: HTMLFormElement, internals:
formElementsMap.set(form, initSet);

/** Add listeners to emulate validation and reset behavior */
form.addEventListener('submit', formSubmitCallback);
wireSubmitLogic(form);
form.addEventListener('reset', formResetCallback);
form.addEventListener('input', formInputCallback);
form.addEventListener('change', formChangeCallback);
Expand Down