open-wc / form-participation

Helper, mixins, tools, and more for supporting your custom elements in participating in an ancestor form.
MIT License
53 stars 7 forks source link

Form submission element is absent from `event.submitter` #55

Open stuffmattdoes opened 3 months ago

stuffmattdoes commented 3 months ago

When I submit a <form/>, I'd like to know which element submitted the form. In native HTML, I have access to the element that submitted the form via event.submitter

Read more about the event.submitter spec here

Example:

// JS
document
    .querySelector("form")
    .addEventListener("submit", (event) => {
        let submitter = event.submitter;
    });

// HTML
<form>
    <button name='FirstButton' value='FirstValue'>First</button>
    <button name='SecondButton' value='SecondValue'>Second</button>
</form>

Expected: event.submitter should be a reference to the

stuffmattdoes commented 3 months ago

I've also noticed that the submit button/element does not appear in formData object on submission events. If a button is used to submit a form and also includes a name/value pair, it should be in FormData.

Read more here

Example

// JS
document
    .querySelector("form")
    .addEventListener("submit", (event) => {
        let formData = new FormData(event.currentTarget);
        formData.get('FirstButton') // should be reference to <button/> but is undefined
    });

// HTML
<form>
    <button name='FirstButton' value='FirstValue'>First</button> // Click
    <button name='SecondButton' value='SecondValue'>Second</button>
</form>
michaelwarren1106 commented 3 months ago

I think this is a good feature request. I think that the submit() function could be updated to accept an optional submitter param, and also be updated to conditionally use form.requestSubmit(submitter) if it is available?

Regarding the request about FormData, the submitter is the second arg to new FormData(form, submitter) so when creating a FormData, you can add the submitter.

However, I think maybe the formValues() helper could also be updated to accept an optional submitter param so that param can be passed to the new FormData() construction that happens in formValues()