How to Integrate the FormValidityObserver
into lit
#4437
Replies: 5 comments 18 replies
-
1) How to Integrate
|
Beta Was this translation helpful? Give feedback.
-
2) Is Automated Setup/Teardown Possible? [Resolved]As I mentioned in the I thought I could accomplish something similar with a Ref Callback in Lit. But the following seems to do nothing: // Somewhere in Lit's `createFormValidityObserver` ...
observer.autoObserve = function autoObserve(novalidate = true) {
/** @type {HTMLFormElement | undefined} */
let form;
/** @param {typeof form} litRef @returns {void} */
return (litRef) => {
if (litRef) {
form = litRef;
observer.observe(form);
if (novalidate) form.setAttribute("novalidate", "");
return;
}
observer.unobserve(/** @type {HTMLFormElement} */ (form));
form = undefined;
};
}; export class TestForm extends LitElement {
#observer = createFormValidityObserver("input");
#formRef = this.#observer.autoObserve() as RefOrCallback<Element>;
render() {
return html`<form ${ref(this.#formRef)}> Other Markup...</form>`;
}
} So I was hoping for insight on how to proceed. |
Beta Was this translation helpful? Give feedback.
-
3) Can
|
Beta Was this translation helpful? Give feedback.
-
4) Any Recommendations for Event Delegation and the Shadow DOM in Lit? [Resolved]When I first wrote the The problem with this is that Lit tends to create elements in the ShadowDOM by default. This causes Some potential options (without having explored them in greater detail) could be to:
If there are any thoughts on this, that would be appreciated. But there's another great problem: If I understand correctly The ShadowDOM disrupts forms. I don't consider this a bug in the Web Standard. Since the ShadowDOM is intended to hide a set of elements from the outside, then it only makes since that these hidden elements would also be hidden from The reason this can be problematic is that sometimes developers want to split their forms into multiple sub components. One component is one section of the form, another component is another section, etc. If each of these components are created with Lit (using the default settings), then that means that their fields will be invisible to the "owning" Has Lit considered how to deal with this dilemma? Is there a standardized way to resolve this? |
Beta Was this translation helpful? Give feedback.
-
First Draft of Usage with Lit@augustjk Not sure if you saw my latest comment on the last remaining open question, but I decided to go through with the idea since it would work with SSR. (I can add a client-only solution if it would be beneficial.) If Lit ever supports dynamic attributes in the future, the code would be a little less redundant. The example Lit usage can be found here. The main action is happening in The |
Beta Was this translation helpful? Give feedback.
-
I'm opening this discussion after talking briefly in lit/rfcs#8 with the hope of learning how to properly integrate the
FormValidityObserver
intoLit
. For those who don't know, theFormValidityObserver
(source code) is a tool in the@form-obser/core
NPM package that enables devs to validate their form fields as users interact with them (and to display accessible error messages for those fields when they become invalid). It also exposes some helpers for manual error handling.For those who are interested or who will find it helpful for the discussion, I'll give some
Background
andGoals
for the Lit Integration. And, of course, I'll addQuestions
.Background
The
FormValidityObserver
is a little unique in that it doesn't try to reinvent the browser's form features in a framework-specific way. Instead, it seeks to enhance the areas where the browser falls short, and it encourages the use of the existing browser features that already seem to work well. The enhancements include things like:Because of the utility's approach, most of the heavy lifting is actually done in the
Core
, and much of the logic is easily portable to any JS Framework. So far, each of the JS framework integrations have only needed to do 3 things:autoObserve
) that can (optionally) be used to automatically setup/cleanup theFormValidityObserver
.FormValidityObserver.configure
method so that it can provide the right attributes to form controls during SSR in addition to doing what the rawconfigure
method does. (More on that in a later question.)FormValidityObserver
's methods as destructure-able properties.So my questions will primarily be related to items 1 and 2. But I'm open to any unrelated feedback that could make the
FormValidityObserver
better (and therefore improve DX for Lit users).Goals
While building out this integration, these are the goals that I would like to achieve if possible:
required
,minlength
, etc.).So far, I've been able to achieve these goals with Svelte, Vue (source), Solid (source), and React (source). So I'm hoping that I can achieve them with Lit as well.
Questions
The OP felt really long when I included the full questions. So I've placed the original questions here, and I've put their details in separate comments.
configure
for Lit Apps?RefCallback
Be Tied to a SpecificHTMLElement
?Beta Was this translation helpful? Give feedback.
All reactions