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

Button: submit event is never called inside a form #3752

Closed
Lukas742 opened this issue Aug 30, 2021 · 1 comment · Fixed by #4942
Closed

Button: submit event is never called inside a form #3752

Lukas742 opened this issue Aug 30, 2021 · 1 comment · Fixed by #4942
Assignees
Labels
bug This issue is a bug in the code High Prio TOPIC Core

Comments

@Lukas742
Copy link
Collaborator

Lukas742 commented Aug 30, 2021

Bug Description

When clicking a Button to submit a form, the form is submitted, but it's not possible to listen to the submit event.

Expected Behavior

A clear and concise description of what you expect to happen.

Steps to Reproduce

  1. Go to codeSandbox and open the dev tools
  2. Click on the HTML button --> onSubmit is called and default behavior is prevented
  3. Click on UI5 WC button --> the form submits without going into the callback

Isolated Example

https://codesandbox.io/s/ui5-webcomponents-forked-77tf2?file=/index.html

Context

  • UI5 Web Components version: 1.0.0.rc-15

Possible Solution

This is happening because of the submit() called inside InputElementsFormSupport. I tried to fix it but came along some problems, which is why I opened an issue instead of a PR.

My solution was to replace submit with dispatchEvent(new Event('submit', { bubbles: true })), unfortunately this does not validate the form, so required on one of the inputs would be ignored. (Since there is an open issue regarding the required attribute of web-components, I tested it with HTML inputs.) All that can be solved by using requestSubmit() which is basically acting like a button has been clicked and therefore is submitting the form and firing the submit callback after validating. This is not available for IE and Safari, though.

In summary, I couldn't find a way to validate the form when clicking on a button in IE or Safari, but was able to make it call the listener in all browsers. Maybe you have an idea how also the validation is working consistently.

My approach:
Affected file: https://github.com/SAP/ui5-webcomponents/blob/master/packages/main/src/features/InputElementsFormSupport.js

  static triggerFormSubmit(element) {
    if (!element.submits) {
      return;
    }
    let currentElement = element.parentElement;
    while (currentElement && currentElement.tagName.toLowerCase() !== 'form') {
      currentElement = currentElement.parentElement;
    }
    if (currentElement) {
      if (isIE() || isSafari()) {
        currentElement.dispatchEvent(new Event('submit', { bubbles: true })); // validation is not working
      } else {
        currentElement.requestSubmit(); // not available with IE and Safari
      }
    } else {
      console.error(`${element} is not within a form. Please add it in a form.`); // eslint-disable-line
    }
  }
@Shtilianova
Copy link

Hello @SAP/ui5-webcomponents-topic-core colleagues,
This ticket looks related to this one #3755.
Kind regards, Diana

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug in the code High Prio TOPIC Core
Projects
Status: Completed
Development

Successfully merging a pull request may close this issue.

3 participants