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

Add @lit-labs/forms and FormAssociated RFC #45

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

justinfagnani
Copy link
Contributor

RFC for a new @lit-labs/forms package based on the work I prototyped in lit/lit#4773

Copy link

@KonnorRogers KonnorRogers left a comment

Choose a reason for hiding this comment

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

overall i appreciate the minimal impact on public APIs and the design of these APIs. It feels well thought out and has had quite a bit of time put into it. Overall I don't see any major blockers beyond attachInternals() needing to be managed by Lit with no escape hatches for if it was already defined.

attachInternals has to be managed by Lit. I don't know if there is a way around this, but many people call attachInternals() on a base class so they can leverage CSS custom states on any element, not just form associated elements. Would there be room in the mixin for providing a custom function for grabbing an existing ElementInternals state and if the function is provided, this helper should not attempt to register via attachInternals()

Initial idea is accepting an optional function argument which will get evaluated when calling getInternals(el). Something like the following:

class MyEl extends FormAssociated(BaseElement, { elementInternals: (element) => getInternals(element) })

// "internals" is a "public" field on the base element. The author using Lit's form associated may not have control over this.
class MyEl extends FormAssociated(BaseElement, { elementInternals: (element) => element.internals })

// Some libraries like Material UI use symbols to "protect" element internals.

export const elementInternalsSymbol = new Symbol("elementInternals")
import { elementInternalsSymbol } from "./internal/element-internals.js"
class MyEl extends FormAssociated(BaseElement, { elementInternals: (element) => element[elementInternalsSymbol] })


#### ElementInternals

The `FormAssociated()` mixin needs access to `ElementInternals`, so it calls `this.attachInternals()` in the constructor.

Choose a reason for hiding this comment

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

What happens if a user has already called attachInternals() in another base class? Is there any escape hatch to provide it with an existing instance of ElementInternals?

Comment on lines +205 to +207
The `FormAssociated()` mixin implements `formDisabledCallback()` to set the
`ariaDisabled` state and request a reactive update in case the element renders
differently when disabled.
Copy link

@KonnorRogers KonnorRogers Nov 8, 2024

Choose a reason for hiding this comment

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

Does this do so via ElementInternals.ariaDisabled?

I know in my tests screen readers still didn't have support for this ~2months ago, but sprouting aria-disabled on the host feels equally bad :/

I'm assuming we could just document this and hopefully it eventually gets fixed, and if needed a consumer could override, call super.formDisabledCallback() and then add their own aria-disabled attribute if needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does this do so via ElementInternals.ariaDisabled?

Yes

I know in my tests screen readers still didn't have support for this ~2months ago, but sprouting aria-disabled on the host feels equally bad :/

I'm assuming we could just document this and hopefully it eventually gets fixed, and if needed a consumer could override, call super.formDisabledCallback() and then add their own aria-disabled attribute if needed.

I think documenting it is good. You could potentially implement a small polyfill that implements ElementInternals.ariaDisabled by sprouting an attribute, and remove that over time.

@jpzwarte
Copy link

I like the simplicity of this API, but this is only usable for native <form> integration. I understand you have to start somewhere, but often you want more rich forms where you can have nested objects and/or non-string values. I wonder if both scenarios could be supported at the same time.

Comment on lines +96 to +103
// Setting this field calls internals.setFormValue()
@formValue()
@property({reflect: true})
accessor value: string = '';

// When the form resets, the value is set to this field
@formDefaultValue()
accessor defaultValue: string = '';

Choose a reason for hiding this comment

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

Is this representative of how normal form controls operate? In my (limited) testing, value is usually not reflected, and the current value for the value attribute is used as default, so I expected:

  // Setting this field calls internals.setFormValue()
  @formValue()
  @property({reflect: false, attribute: false})
  accessor value: string = '';

  // When the form resets, the value is set to this field
  @formDefaultValue()
  @property({attribute: 'value'})
  accessor defaultValue: string = '';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The point here is that you can use the decorators to construct the public API you want, including as you point out, one that matches the oddities of <input>. Yes, you do not have to reflect any of these decorated fields.

Copy link

Choose a reason for hiding this comment

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

Fwiw, I'd still change the example: it's looks a lot like <input> and doesn't require much change to behave like <input>. Either that or explicitly mention that this behaves differently from <input> so nobody ever copies this blindly.

Suggested change
// Setting this field calls internals.setFormValue()
@formValue()
@property({reflect: true})
accessor value: string = '';
// When the form resets, the value is set to this field
@formDefaultValue()
accessor defaultValue: string = '';
// Setting this field calls internals.setFormValue()
@formValue()
@property({attribute: false})
accessor value: string = '';
// When the form resets, the value is set to this field
@formDefaultValue()
@property({attribute: 'value', reflect: true})
accessor defaultValue: string = '';

whenever the value is changed. This is implemented by the `@formValue()`
decorator, which wrapps an accesor's setter to call `setValue()`.

A form value must be of type `string | File | FormData | null`. To facilitiate
Copy link

@lennartbuit lennartbuit Nov 13, 2024

Choose a reason for hiding this comment

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

So I take that you can mimic <select multiple> by having a FormData value containing multiple values for the same name, right? Is that something to spend a few words on?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but this sentence is merely reflecting the platform API constraints. This library won't dictate or interfere with how form data is represented, so however you would do this manually is how you would do it here.

Choose a reason for hiding this comment

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

Not in the scope of this PR, but as a webcomponent author, a section in some docs about both points I mentioned would be very helpful. Very finicky to get right :).

Comment on lines +182 to +185
throw new DOMException(
`Failed to execute 'attachInternals' on 'HTMLElement': ` +
`ElementInternals for the specified element was already attached.`,
'NotSupportedError');
Copy link

Choose a reason for hiding this comment

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

nit: how about just calling super.attachInternals() here to let the native implementation throw?

@justinfagnani justinfagnani changed the title Add FormAssociated RFC Add @lit-labs/forms and FormAssociated RFC Nov 13, 2024
Comment on lines +96 to +103
// Setting this field calls internals.setFormValue()
@formValue()
@property({reflect: true})
accessor value: string = '';

// When the form resets, the value is set to this field
@formDefaultValue()
accessor defaultValue: string = '';
Copy link

Choose a reason for hiding this comment

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

Fwiw, I'd still change the example: it's looks a lot like <input> and doesn't require much change to behave like <input>. Either that or explicitly mention that this behaves differently from <input> so nobody ever copies this blindly.

Suggested change
// Setting this field calls internals.setFormValue()
@formValue()
@property({reflect: true})
accessor value: string = '';
// When the form resets, the value is set to this field
@formDefaultValue()
accessor defaultValue: string = '';
// Setting this field calls internals.setFormValue()
@formValue()
@property({attribute: false})
accessor value: string = '';
// When the form resets, the value is set to this field
@formDefaultValue()
@property({attribute: 'value', reflect: true})
accessor defaultValue: string = '';

accessor defaultValue: string = '';

render() {
return html`<input .value=${this.value}> @input=${this.#onInput}`;
Copy link

Choose a reason for hiding this comment

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

Typo

Suggested change
return html`<input .value=${this.value}> @input=${this.#onInput}`;
return html`<input .value=${this.value} @input=${this.#onInput}>`;


The default value and state can be designated by decorators, in one of two ways:

- Decorators: The `@defaultValue()` and `@defaultState()` decorators let the
Copy link

Choose a reason for hiding this comment

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

Suggested change
- Decorators: The `@defaultValue()` and `@defaultState()` decorators let the
- Decorators: The `@formDefaultValue()` and `@formDefaultState()` decorators let the

- A set of decorators for designating fields as the form value and state
- `@formValue()` for the public form value field
- `@formState()` for the fields that compose part of the form state
- `@formDefaultValue()` for the public form default value field
Copy link

Choose a reason for hiding this comment

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

Is lib-labs/form handling whether the form is "touched" in some form? Because native <input> elements will have their value and checked change whenever defaultValue and defaultChecked are changed as long as the input hasn't been touched: https://html.spec.whatwg.org/multipage/input.html#the-input-element:concept-fe-dirty-2

If it's a non-goal then maybe be explicit about it (or is it already and I missed it?)

@kyubisation
Copy link

Would it be an option to extract the ElementInternals management into its own mixin? I feel like e.g. the assignment of the role via static property would be useful in other scenarios.

I also liked the suggestion in lit/lit#4773 (comment), which also seems like a clean separation between implementers and consumers.

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

Successfully merging this pull request may close these issues.

6 participants