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

form: asynchronous ElementInternals form data synchronization #1197

Open
pdesoyres-cc opened this issue Oct 8, 2024 · 0 comments
Open

form: asynchronous ElementInternals form data synchronization #1197

pdesoyres-cc opened this issue Oct 8, 2024 · 0 comments

Comments

@pdesoyres-cc
Copy link
Contributor

Let say you have a <cc-select> inside a <form> and you want to submit the form immediately after the selection changes:

_onTypeChange(e) {
	this._filterFormRef.value.requestSubmit();
}

_onFormSubmit(e) {
	const formData = new FormData(e.target);
	console.log(formData.type);
}

render () {
	return html`
		<form ${ref(this._filterFormRef)} ${formSubmit(this._onFormSubmit)}>
			<cc-select name="type" @cc-select:input=${this._onTypeChange}></cc-select>
		</form>
    `;

In the above code, the formData.type logged to the console is not the one that has just been selected. It is the one prior the new selection.

This is because, the ElementInternals form data synchronization is done asynchronously: in the updated stage of the Lit component lifecycle. (see the implementation of the updated method of cc-form-control-element.abstract.js).

So, to make this work, we need to write:

async _onTypeChange(e) {
    await this.updateComplete;
	this._filterFormRef.value.requestSubmit();
}

And this is not very developer friendly!

  • Could we find a way to do the ElementInternals form data synchronization synchronously?
    • this implies that each form control component may need to change their internal value AND also ask for form data synchronization. (which is not very developer friendly neither)
  • Should we make sure we always wait for the next render before handling the form submission?
    • we could wait for next render inside the form-submit-handler.js (or maybe in the form-submit-directive.js).
    • At these points we do not have a reference to the component that we should wait for render, so we could,
      • try to find the first LitElement in the ascendants
      • just use a setTimeout(fn, 0).
  • If we cannot find a good solution, we should at least document this somewhere.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant